B4R Question When connecting two ESP8266 boards, how can I send values from the Hc-sr04 sensor?

Johan Hormaza

Well-Known Member
Licensed User
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
 

Johan Hormaza

Well-Known Member
Licensed User
@Erel this is the Server code

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private server As WiFiServerSocket
    Private astream As AsyncStreams
    Private convertidor As B4RSerializator
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Log(wifi.StartAccessPoint("ServidorSp32"))
    Log(wifi.AccessPointIp)
    RunNative("SetAP", Null)
    Log(wifi.AccessPointIp)
    Log(wifi.LocalIp)
    server.Initialize(51042, "server_NewConnection")
    server.Listen
End Sub



#if C
void SetAP(B4R::Object* o) {
   WiFi.mode(WIFI_AP);
}
#end if

Sub Server_NewConnection (NewSocket As WiFiSocket)
    astream.InitializePrefix(NewSocket.Stream, False, "astream_NewData", "astream_Error")
    Log("new connection")
End Sub

Sub astream_NewData (Buffer() As Byte)
    Log("new data: ", Buffer)
    Dim be(200) As Object 'used as a storage buffer.
    Dim objects() As Object = convertidor.ConvertBytesToArray(Buffer, be)
    For Each o As Object In objects
        Log(o)
    Next
    
    'astream.Write(ser.ConvertArrayToBytes(Array("Sent from ESP", Millis, pin.AnalogRead, pin.DigitalRead)))
    server.Socket.Close 'disconnect to allow new connections
End Sub


Sub astream_Error
    Log("error")
    server.Listen
End Sub
 
Upvote 0
Top