B4R Question Wifi Status Check (NodeMCU+Arduino)

AndroidMadhu

Active Member
Licensed User
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
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
 

AndroidMadhu

Active Member
Licensed User
Check B4RSerializator if you want to send more complex data: https://www.b4x.com/android/forum/threads/72404/#content
@Erel,
Thanks for your advice... I am trying to incorporate B4RSerializator into my code now.
Meantime I found bug in my above code......
When I stop my router, still nodeMCU is receiving the message and Arduino also sending the message. After shutting down the router, I am able to see the message at the serial monitor of nodeMCU.

Am I doing any mistake in logic?
 

Erel

Administrator
Staff member
Licensed User
I'm not sure that I understand your code.

1. Send the byte value 0 or 1 as a representation of the wifi status.
B4X:
AStream.Write(Array As byte(0)) 'or 1
2. Why do you call Timer1_Tick:
B4X:
 If bc.IndexOf(buffer,"C") > -1 Then  'ESP8266 will provide an acknowkedgement "C" then 80 will be provided by Arduino
        Timer1_Tick
How do you know to distinguish between this call and the standard tick event?
 

AndroidMadhu

Active Member
Licensed User
Why do you call Timer1_Tick:
This is because when ESP provide acknowledgement, then Timer1_Tick will fire and send data to ESP8266.

What I want is, "when the acknowledgement will send by ESP then arduino will send the data to ESP8266."
 
Top