As the example here https://www.b4x.com/android/forum/threads/connecting-two-esp8266-boards.89726/ how can I send the variables, since it throws me an error of "Out of bounds error Array length = 8, Index = 65535 "? This is my code with the hc-sr04 sensor Client.
B4X:
Sub Process_Globals
Public Serial1 As Serial
Private wifi As ESP8266WiFi
Private socket As WiFiSocket
Private astream As AsyncStreams
Private Timer1 As Timer
Private convertir As B4RSerializator
Private trigPin, echoPin As Pin
Private Timer1 As Timer
Private pulsduration As ULong 'ignore
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("Cargando...")
Timer1.Initialize("Timer1_Tick",2000)
RunNative("SetSTA", Null)
Log(wifi.Connect("ServidorSp32"))
Log(wifi.LocalIp)
trigPin.Initialize(18, trigPin.MODE_OUTPUT)
echoPin.Initialize(19, echoPin.MODE_INPUT)
Conectado(0)
astream.Write("hello!!!")
End Sub
Sub Timer1_Tick
trigPin.DigitalWrite(False)
DelayMicroseconds(2)
trigPin.DigitalWrite(True)
DelayMicroseconds(10)
trigPin.DigitalWrite(False)
RunNative("pulseins", echoPin.PinNumber)
Dim duration As Long = pulsduration
Dim distance As Int = duration / 58.2
Log(distance,"Cm")
astream.Write(convertir.ConvertArrayToBytes(Array("Distancia: ",distance)))
End Sub
Sub Conectado(u As Byte)
Log("Tratando de conectar")
If socket.ConnectIP(Array As Byte(192, 168, 4, 1), 51042) Then
Log("conectado")
astream.InitializePrefix(socket.Stream, False, "astream_NewData", "astream_Error")
astream.Write("Hola!!!")
Timer1.Enabled = True
Else
CallSubPlus("Conectado", 1000, 0)
End If
End Sub
#if C
void SetSTA(B4R::Object* o) {
WiFi.mode(WIFI_STA);
}
#end if
Sub astream_NewData (Buffer() As Byte)
Log("new data: ", Buffer)
End Sub
Sub astream_Error
Log("error")
Timer1.Enabled = False
End Sub
#if C
void pulseins (B4R::Object* o) {
b4r_main::_pulsduration = pulseIn(o->toULong(),HIGH);
}
#End if