I have a nodemcu that detects the change of state of a pin.
I have based my code in the code example @Filippo.
https://www.b4x.com/android/forum/t...on-problems-with-udpsocket.72239/#post-460935
In my code when the change of state of one pin is detected, a counter is incremented and is sent via wifi.
I've done various tests and always have error. I did not notice some changes state.
This happens to me with different pins.
But if not sent, and I do just at the end of the test, the reading is correct. All state changes are detected.
This leads me to think that the problem is sending. But I made a log of how long it takes to send and this occurs in micros 150/200, is fast.
My code:
I have based my code in the code example @Filippo.
https://www.b4x.com/android/forum/t...on-problems-with-udpsocket.72239/#post-460935
In my code when the change of state of one pin is detected, a counter is incremented and is sent via wifi.
I've done various tests and always have error. I did not notice some changes state.
This happens to me with different pins.
But if not sent, and I do just at the end of the test, the reading is correct. All state changes are detected.
This leads me to think that the problem is sending. But I made a log of how long it takes to send and this occurs in micros 150/200, is fast.
My code:
B4X:
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
Sub Process_Globals
Public Serial1 As Serial
Public WiFi As ESP8266WiFi
Private bc As ByteConverter
Dim MacArray(6) As Byte
Private server(2) As WiFiServerSocket
Private astream(2) As AsyncStreams
Private pinSensor As Pin
Private pin1 As D1Pins
Private contador As Long
Private codigo As Int
End Sub
Private Sub AppStart
Log("AppStart")
Serial1.Initialize(115200)
Dim Passwd As String = bc.HexFromBytes(bc.SubString2(MacAddress, 0, 4))
Log("StartAP: ", WiFi.StartAccessPoint2("fgWifi", Passwd))
Log("My AP ip: ", WiFi.AccessPointIp)
Log(Passwd)
IntiServer
End Sub
#Region "server-Subs"
Public Sub IntiServer
server(0).Initialize(51041, "Server1_NewConnection")
server(0).Listen
server(1).Initialize(51042, "Server2_NewConnection")
server(1).Listen
pinSensor.Initialize(pin1.D1, pinSensor.MODE_INPUT_PULLUP)
pinSensor.AddListener("pinSensor_StateChanged")
End Sub
Sub Server1_NewConnection (NewSocket As WiFiSocket)
Log("Server1_NewConnection=" , NewSocket.Connected)
If server(0).Socket.Connected Then
astream(0).Initialize(NewSocket.Stream, "astream_NewData", "astream1_Error")
astream(0).Write(bc.StringToBytes("OK"))
End If
End Sub
Sub Server2_NewConnection (NewSocket As WiFiSocket)
Log("Server2_NewConnection=" , NewSocket.Connected)
If server(1).Socket.Connected Then
astream(1).Initialize(NewSocket.Stream, "astream_NewData", "astream2_Error")
astream(1).Write(bc.StringToBytes("OK"))
End If
End Sub
Sub astream_NewData (Buffer() As Byte)
Dim cod As String = bc.StringFromBytes(Buffer)
codigo=cod
SendValue
End Sub
Sub astream1_Error
Log("astream1_Error")
If Not(server(0).Socket.Connected) Then
Log("server(0).Listen=", 0)
server(0).Listen
End If
End Sub
Sub astream2_Error
Log("astream2_Error")
If Not(server(1).Socket.Connected) Then
Log("server(1).Listen=", 1)
server(1).Listen
End If
End Sub
#End Region
Sub pinSensor_StateChanged (State As Boolean)
If State Then
contador=contador+1
SendValue
End If
End Sub
Sub SendValue
Dim key As String
key=NumberFormat(contador*100+codigo,0,0)
If server(0).Socket.Connected Then
astream(0).Write(key).Write(",")
End If
If server(1).Socket.Connected Then
astream(1).Write(key).Write(",")
End If
End Sub
Sub MacAddress() As Byte()
RunNative("getMac", Null)
Return MacArray
End Sub
#if C
#include <ESP8266WiFi.h>
void getMac(B4R::Object* u) {
WiFi.macAddress((Byte*)b4r_main::_macarray->data);
}
#end if