Hello,
In this small project nodemcu will check the wifi status and send the acknowledgement to Arduino.
Upon receive of acknowledgement from NodeMCU, Arduino will send value to NodeMCu.
Code at Arduino End
Code at ESP8266 End
Please suggest for improvements
In this small project nodemcu will check the wifi status and send the acknowledgement to Arduino.
Upon receive of acknowledgement from NodeMCU, Arduino will send value to NodeMCu.
Code at Arduino End
B4X:
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Public softserial As SoftwareSerial
Public Timer1 As Timer
Public Astrem As AsyncStreams
Public bc As ByteConverter
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
softserial.Initialize(115200,10,11) 'RX as Pin10 and TX as Pin11
Astrem.Initialize(softserial.Stream,"astrem_Newdata",Null)
Timer1.Initialize("Timer1_Tick",2000)
Timer1.Enabled=True
Log("AppStart")
End Sub
Private Sub Timer1_Tick
Dim FixedValue As String="80"
Astrem.Write(FixedValue.GetBytes)
Log("Send to Esp8266 Successfully!!")
End Sub
Private Sub astrem_Newdata(buffer() As Byte)
'Log("NewData from ESP:",buffer)
If bc.IndexOf(buffer,"C") > -1 Then 'ESP8266 will provide an acknowkedgement "C" then 80 will be provided by Arduino
Timer1_Tick
else if bc.IndexOf(buffer,"NC") > -1 Then 'Received from NodeMCU if No connection
'Do nothing
End If
End Sub
Code at ESP8266 End
B4X:
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Public softser As SoftwareSerial
Public bc As ByteConverter
Public wifi As ESP8266WiFi
Private timer1 As Timer
Public Astream As AsyncStreams
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
softser.Initialize(115200,5,4) '5 as for RX and 4 is for TX 'GPIO5 D1 and GPIO4 D2 in NODEMCU
Astream.Initialize(softser.Stream,"Arduino_Received_Data",Null)
timer1.Initialize("timer1_Tick",2000)
timer1.Enabled=True
Log("AppStart")
'Check the available network
End Sub
Private Sub timer1_Tick
checkConStatus
End Sub
Private Sub Arduino_Received_Data(buffer() As Byte)
If bc.IndexOf(buffer,"80") > -1 Then
Log("Received from Arduino : ",buffer)
'timer1_Tick
End If
End Sub
Private Sub checkConStatus
wifi.Connect2("MADHU","xxxxxxxx")
If wifi.IsConnected=True Then
'Send Ackknowledment to Arduino
Dim str As String="C" 'C for successfull Connection
Astream.Write(str.GetBytes)
Else if wifi.IsConnected=False Then
Dim str1 As String="NC" 'NC for Not Connected
Astream.Write(str1.GetBytes)
End If
End Sub
Please suggest for improvements