B4R Question Out of bounds error

AndroidMadhu

Active Member
Licensed User
Hello,
I am sending the data from Arduino to Esp8266.
I am able to send data from Arduino to ESP successfully as per the suggestion from @Erel, which is saved my time & Life :)

But after 1 minute of running I am facing error as "Out of bounds error. Array length = 2, Index = 65535"
Here is the error
B4X:
Wifi Connected Successfully!!!
Humid from Ardiono :33.39
Temp from Arduino :77.99
Humid from Ardiono :33.39
Temp from Arduino :77.99
Humid from Ardiono :33.39
Temp from Arduino :77.99
Humid from Ardiono :33.39
Temp from Arduino :77.99
Humid from Ardiono :33.39
Temp from Arduino :77.99
Humid from Ardiono :33.39
Temp from Arduino :77.99
Out of bounds error. Array length = 2, Index = 65535
Code at Arduino Side:
B4X:
Public Serial1 As Serial
    Public softserial As SoftwareSerial
    Public Timer1 As Timer
    Public Astrem As AsyncStreams
    Public bc As ByteConverter
    'Private humid(),temp() As Byte
    Private ser As B4RSerializator
Private Sub Timer1_Tick
    Dim humid(),temp() As Byte
    humid="33.39"
    temp="77.99"
    Astrem.Write(ser.ConvertArrayToBytes(Array(humid,temp))) 
    Log("Send to Esp8266 Successfully!!")
End Sub
Code at ESP Side:
B4X:
Public Serial1 As Serial
    Public softser As SoftwareSerial
    Public bc As ByteConverter
    Public wifi As ESP8266WiFi
    'Private timer1 As Timer
    Public Astream As AsyncStreams
    Private ser As B4RSerializator
    Private BE(2) As Object
Private Sub Arduino_Received_Data(buffer() As Byte)
ser.ConvertBytesToArray(buffer,BE)
Log("Humid from Ardiono :",BE(0))
Log("Temp from Arduino :",BE(1))
End Sub
 

AndroidMadhu

Active Member
Licensed User
Are you using prefix mode (AStream.InitializePrefix) ?
Yes.. I am using AStream.InitializePrefix at Both side Arduino and Esp8266
B4X:
Astream.InitializePrefix(softser.Stream,False,"Arduino_Received_Data",Null)
    Astream.MaxBufferSize=500
    Astream.WaitForMoreDataDelay=300
Check the length of the buffer array. If it is too small discard it.
B4X:
Humid from Ardiono :33.39
Temp from Arduino :77.99
Length of Buffer Array : 15
Out of bounds error. Array length = 5, Index = 65535
 

AndroidMadhu

Active Member
Licensed User
I hv made it 20. But the problem is still there

B4X:
Private BE(20) As Object
B4X:
Out of bounds error. Array length = 20, Index = 65535
 

AndroidMadhu

Active Member
Licensed User
Not able to get any clue....:(
I want to transfer the object value of BE(0) and BE(1) to string. But no luck.I have used byteconverter, but not able to do that.
Also still facing out of bound error....
Not sure.. where I am wrong....
This is working..
B4X:
ser.ConvertBytesToArray(buffer,BE)
Log("Humid from Ardiono :",BE(0))
Details from ByteConverter.HexFromBytes:
B4X:
ser.ConvertBytesToArray(buffer,BE)
Log("Humid from Ardiono :",BE(0))
Log("Temp from Arduino :",BE(1))
'Log("Length of Buffer Array : ", BE.Length)
Dim myarray() As Byte=bc.HexFromBytes(buffer)
Log(myarray)
End Sub
Result from ByteConverter.HexFromBytes:
B4X:
Wifi Connected Successfully!!!
Humid from Ardiono :33.39
Temp from Arduino :77.99
7E024533332E33394537372E39397F
Humid from Ardiono :3s.39
Temp from Arduino :77.99
7E024533732E33394537372E39397F
Humid from Ardiono :3s.39
Temp from Arduino :-51401
7E024533732E33390537372E39397F
Humid from Ardiono :3s.39
Temp from Arduino :77.9y
7E024533732E33394537372E39797F
Humid from Ardiono :33.39
Temp from Arduino :77.99
7E024533332E33394537372E39397F
Humid from Ardiono :33.39
Temp from Arduino :77.99
7E024533332E33394537372E39397F
Humid from Ardiono :33.39
Temp from Arduino :77.99
7E024533332E33394537372E39397F
 

AndroidMadhu

Active Member
Licensed User
You should also post the logs when it fails.
The below is the logs I have...
B4X:
    1853ee638ca92b9f 3135e0f8dbdd387e | .S.c..+.15....8~
    238e6bc1e1c538fc 240e97e3b57eff95 | #.k...8.$....~..
    31fae6f9bf0094d3 751dc0           | 1.......u..
TRACE +0.641 Read 1 bytes: c0
TRACE +0.000 Read 3 bytes: 011102
TRACE +0.001 Read 1 bytes: 00
TRACE +0.000 Read 4 bytes: 00000000
TRACE +0.001 Read 1 bytes: 00
TRACE +0.000 Read 2 bytes: 00c0
TRACE +0.000 Received full packet: 01110200000000000000
Wrote 279200 bytes (203020 compressed) at 0x00000000 in 20.4 seconds (effective 109.5 kbit/s)...
TRACE +0.011 command op=0x13 data len=16 wait_response=1 timeout=3.000 data=00000000a04204000000000000000000
TRACE +0.000 Write 26 bytes:
    c000131000000000 0000000000a04204 | ..............B.
    0000000000000000 00c0             | ..........
TRACE +0.035 Read 1 bytes: c0
TRACE +0.001 Read 1 bytes: 01
TRACE +0.000 Read 4 bytes: 13120000
TRACE +0.001 Read 1 bytes: 00
TRACE +0.000 Read 2 bytes: 0000
TRACE +0.327 Read 1 bytes: 67
TRACE +0.001 Read 1 bytes: 8d
TRACE +0.000 Read 9 bytes: a8f608dbdc1c1a5c09
TRACE +0.001 Read 1 bytes: bf
TRACE +0.000 Read 8 bytes: a835a8a3840000c0
TRACE +0.000 Received full packet:
    0113120000000000 678da8f608c01c1a | ........g.......
    5c09bfa835a8a384 0000             | \...5.....
Hash of data verified.
Leaving...
TRACE +0.010 command op=0x02 data len=16 wait_response=1 timeout=3.000 data=00000000000000000040000000000000
TRACE +0.000 Write 26 bytes:
    c000021000000000 0000000000000000 | ................
    0000400000000000 00c0             | ..@.......
TRACE +0.003 Read 1 bytes: c0
TRACE +0.001 Read 1 bytes: 01
TRACE +0.000 Read 7 bytes: 02020000000000
TRACE +0.001 Read 1 bytes: 00
TRACE +0.000 Read 2 bytes: 00c0
TRACE +0.000 Received full packet: 01020200000000000000
TRACE +0.010 command op=0x12 data len=4 wait_response=1 timeout=3.000 data=01000000
TRACE +0.000 Write 14 bytes: c0001204000000000001000000c0
TRACE +0.002 Read 1 bytes: c0
TRACE +0.001 Read 1 bytes: 01
TRACE +0.000 Read 7 bytes: 12020000000000
TRACE +0.001 Read 1 bytes: 00
TRACE +0.000 Read 2 bytes: 00c0
TRACE +0.000 Received full packet: 01120200000000000000
Hard resetting via RTS pin...
********************* PROGRAM STARTING ****************
AppStart
Wifi Connected Successfully!!!
Humid from Ardiono :3�.39
Temp from Arduino :77.99
Humid from Ardiono :33n39
Temp from Arduino :77.99
Humid from Ardiono :33.39
Temp from Arduino :77.99
Humid from Ardiono :33.39
Temp from Arduino :77.99
Humid from Ardiono :33.39
Temp from Arduino :77.99
Humid from Ardiono :33.39
Temp from Arduino :77.99
Humid from Ardiono :33.39
Temp from Arduino :77.99
Humid from Ardiono :33.39
Temp from Arduino :77.99
Humid from Ardiono :33.39
Temp from Arduino :77n99
Out of bounds error. Array length = 20, Index = 65535
Is there is any other log location available for more troubleshooting?
 

AndroidMadhu

Active Member
Licensed User
@Erel
I have made the object as below
B4X:
Private BE(200) As Object
After that it was running for about 15 minutes. It seems a memory leak is happening... like below
B4X:
Exception (9):
epc1=0x40202012 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000063 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffd30 end: 3fffffc0 offset: 01a0
3ffffed0:  00000000 0000000d 00000028 00000004 
3ffffee0:  00000004 0000000f 40203dbc 3fffefa0 
3ffffef0:  40105358 061f3a86 3ffef110 00000017 
3fffff00:  3ffee2f0 3ffee2dc 3ffeee74 4020218a 
3fffff10:  0000000f 00003638 3ffeedec 40202b1e 
3fffff20:  4020437e 3ffefdd4 3ffeedac 0000000f 
3fffff30:  0000000f 3ffefdd4 3ffeedac 40201246 
3fffff40:  000f0000 000503f8 4010054c 000501a0 
3fffff50:  00000000 00000004 3ffeedac 00000000 
3fffff60:  05c9e1d1 3fffff78 3ffeedac 3ffef0b4 
3fffff70:  3fffdad0 3ffefdc4 3ffee2fc 40201cd8 
3fffff80:  3fffdad0 3ffee304 3fff0bbc 40201d3f 
3fffff90:  3fffdad0 00000000 3ffef084 402024cf 
3fffffa0:  feefeffe feefeffe feefeffe 40203e6c 
3fffffb0:  feefeffe feefeffe 3ffe850c 40100bcd 
<<<stack<<<
ets Jan  8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v8b899c12
~ld
AppStart
And my Esp is Restarting.

this is my code I am receiving from Arduino
B4X:
Private Sub Arduino_Received_Data(buffer() As Byte)
ser.ConvertBytesToArray(buffer,BE)
GlobalStore.Put(0,BE(0))    'humid.
GlobalStore.Put(1,BE(1))    'Temperature
Log(GlobalStore.Slot0)
 

MathiasM

Member
Licensed User
Is BE() a global variable? Just make it local in the sub you actually need it (the Arduino_received_data) and the variable will be cleared after every received data.
 

AndroidMadhu

Active Member
Licensed User
yes... BE() a global variable. I make it Local. But no luck..:(
B4X:
Private Sub Arduino_Received_Data(buffer() As Byte)
Private BE(200) As Object
ser.ConvertBytesToArray(buffer,BE)
GlobalStore.Put(0,BE(0))    'humid.
GlobalStore.Put(1,BE(1))    'Temperature
Log(GlobalStore.Slot0)
End Sub
B4X:
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
33.39
Out of bounds error. Array length = 200, Index = 65535
 

AndroidMadhu

Active Member
Licensed User
A strange thing is when I am running it for first time it is running for 15 minutes.... but if I change any code at ESP side and hard reset manually the esp and again compile the code... then the "out of bound error" comes after 2 minutes of running....
 

AndroidMadhu

Active Member
Licensed User
Here you go....

At ESP8266 Side
B4X:
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
    Private ser As B4RSerializator
    Public humidity(),temperature() As Byte
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    softser.Initialize(115200,5,4) '5 as for RX(D1) and 4 is for TX(D2)
    Astream.InitializePrefix(softser.Stream,False,"Arduino_Received_Data",Null)
    Astream.MaxBufferSize=500
    Astream.WaitForMoreDataDelay=300
    timer1.Initialize("timer1_Tick",2000)
    timer1.Enabled=True   
    Log("AppStart")
    'Connect the Router
    wifi.Connect2("MADHU","xxxx")
    Log("Wifi Connected Successfully!!!")
End Sub

Private Sub timer1_Tick
'    checkConStatus
'RunNative("read",Null)
End Sub

Private Sub Arduino_Received_Data(buffer() As Byte)
Private BE(200) As Object
ser.ConvertBytesToArray(buffer,BE)
GlobalStore.Put(0,BE(0))    'humid.
GlobalStore.Put(1,BE(1))    'Temperature
Log("Humid :",GlobalStore.Slot0)
End Sub
At Arduino Side[Sending data to Esp8266]

B4X:
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
    'Private humid(),temp() As Byte
    Private ser As B4RSerializator
    'Private data(2) As Object
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    softserial.Initialize(115200,10,11)  'RX as Pin10 and TX as Pin11
    Astrem.InitializePrefix(softserial.Stream,False,"astrem_Newdata",Null)
    Astrem.MaxBufferSize=500
    Astrem.WaitForMoreDataDelay=300
    Timer1.Initialize("Timer1_Tick",2000)
    Timer1.Enabled=True
    Log("AppStart")
End Sub

Private Sub Timer1_Tick
    'Dim FixedValue As String=80
    Dim humid(),temp() As Byte
    humid="33.39"
    temp="77.99"
    Astrem.Write(ser.ConvertArrayToBytes(Array(humid,temp)))
    Log("Send to Esp8266 Successfully!!")
End Sub
 

rodmcm

Active Member
Licensed User
I have also had this problem using the same methodology with Bluetooth and Wifi ESp32 to B4A. For some reason if i keep my phone (B4a) away from the ESp32 the problem went away. or at least decreased dramatically in frequency.
I have also had this problem with serial connection. The only way I solved it was to send straight bytes and deal with them at the receive end directly, not using B4Rserialization
 

AndroidMadhu

Active Member
Licensed User
The only way I solved it was to send straight bytes and deal with them at the receive end directly, not using B4Rserialization
I have checked your suggestion and it works partially.

At the Arduino Side below is the modified code:

B4X:
Dim humid(),temp() As Byte
    humid="33.39"
    temp="77.99"
    Astrem.Write(humid).Write(temp)
Log("Send to Esp8266 Successfully!!")
End Sub
At the ESP8266 end :

B4X:
Private Sub Arduino_Received_Data(buffer() As Byte)
Log(buffer)
End Sub
Here I am not facing any error. I ran it almost 3 hours and still No error so far.
But the issue is how Do I split the buffer cache... I am receiving the data as below

B4X:
Wifi Connected Successfully!!!
33.39
77.99
33.39
77.99
33.39
33.39
77.99
33.39
�7.99
33.39
77.99
33.39
77.99
33.39
33.39
77.99
33.39
77.99
33.39
�7.99
33.39
77.99
33.39
77.99
 

rbghongade

Active Member
Licensed User
If you want to receive the data finally using B4A, why not send a concatenated string of temperature and humidity with a comma seperator from Arduino side, then using mqtt you can send this string as payload, finally using regex.split at the Android side. I used the same successfully.
Better option is to interface sensors directly to ESP8266 and send the data to consumer app.
 
Top