B4R Question Strange reboot when running HttpJob.Post 7 times

Gerardo Tenreiro

Active Member
Licensed User
Hello,
I'm running this code on an ESP32 and curiously it always restarts when executing the POST call on the seventh execution.
If I remove the header sending it doesn't restart but the correct data doesn't reach the server.

I'll leave the code here, you have to put the wifi and the password back to be able to test:

TEST POST:
#Region Project Attributes
    #AutoFlushLogs        : True
    #CheckArrayBounds    : True
    #StackBufferSize    : 1024
#End Region

Sub Process_Globals
    Public  Serial1         As Serial
    Private wifi             As ESP8266WiFi
    
    Public    Ts                As Timer
    Public  Segundo         As Byte                = 0
    Public  Minuto             As Byte                = 0
    Public  Hora             As Byte                = 0
    
    Public  bc                As ByteConverter
    
    
    Public  Maniobras        As UInt                = 0
    Public  Horas_Uso        As UInt                = 0
    
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("****************************")
    Log("Inicia Aplicacion")
    
    
    Ts.Initialize("Ts_Tick",1000)
    Ts.Enabled = True
    
    Conecta_Wifi(0)

End Sub


Sub Ts_Tick
    Segundo = Segundo + 1
    Horas_Uso = Horas_Uso + 1
    If Segundo > 59 Then
        Segundo = 0
        Minuto = Minuto + 1
        If Minuto > 59 Then
            Minuto = 0
            Hora = Hora + 1
            If Hora > 23 Then
                Hora = 0
            End If
        End If
    End If
End Sub



Sub Conecta_Wifi(Nada As Byte)
    Log("Intentando conectar a la red WIFI")
    wifi.Disconnect
    wifi.ConnectAsync("XXX", "XXX", 0, Null, "wifi_Connected")
End Sub

Sub WiFi_Connected (Success As Boolean)
    If Success = True Then
        Log("WIFI Conectada !!!")
        Log("La IP es: ",wifi.LocalIp)
    Else
        Log("No conectado la WIFI, se reintenta")
        CallSubPlus("Conecta_Wifi",5000,0)
    End If
    
    If Success = True Then
        HttpJob.Initialize("Paso_000")               
        Envia_Estado(0)
        
    End If
End Sub
'
'                 Envia Estados de la Tarjeta de Control.
'    Envia Datos al servidor
'    Numero de Serie        ->    5 Digitos        ->    00000
'     Fecha en el ESP32    ->    8 Caracteres    ->     dd/mm/aa
'   Hora en el Servidor    ->  8 Caracteres    ->     hh:mm:ss
'   Version Hardware    ->                  ->     4.12.00
'   Version Software    ->                  ->     4.12.26a
'   Año de Fabricacion    ->    4 Digitos        ->     2024
'     Mes de Fabricacion    ->    2 Digitos        ->    01
'    Numero de Maniobras    ->    10 Digitos        ->     0000000000
Sub Envia_Estado(Nada As Byte)
    Dim Ti                As ULong    = 0   
    Dim Amo                As Int        = 2024
    Dim Mes                As Int        = 1
    Dim Numero_Serie     As Int        = 20   
    
    Log("*******************************")
    Log("Inicia Envio ... Buffer Utilizado = ",StackBufferUsage, " ... Ram = ",AvailableRAM)
    
    Dim P0() As Byte    = "&".GetBytes
    Dim P1() As Byte      = JoinBytes(Array ("Amo=".GetBytes,NumberFormat(Amo,4,0).GetBytes))   
    Dim P2() As Byte      = JoinBytes(Array ("Mes=".GetBytes,NumberFormat(Mes,2,0).GetBytes))
    Dim P3() As Byte      = JoinBytes(Array ("Numero_de_Serie=".GetBytes,NumberFormat(Numero_Serie,5,0).GetBytes))
    Dim P4() As Byte    = "Fecha=01/01/2024".GetBytes
    Dim P5() As Byte      = JoinBytes(Array ("Hora=".GetBytes,NumberFormat(Hora,2,0).GetBytes,":".GetBytes,NumberFormat(Minuto,2,0).GetBytes,":".GetBytes,NumberFormat(Segundo,2,0).GetBytes))
    Dim P6() As Byte     = "Version_H=4.12.00".GetBytes
    Dim P7() As Byte     = "Version_S=4.12.26a".GetBytes
    
    Maniobras = Maniobras + 1
    Dim P8() As Byte     = JoinBytes(Array ("Maniobras=".GetBytes,NumberFormat(Maniobras,8,0).GetBytes))
    Dim P9() As Byte    = JoinBytes(Array ("Horas_Uso=".GetBytes,NumberFormat(Horas_Uso,8,0).GetBytes))
    
    Dim P10() As Byte     = JoinBytes(Array (P1,P0,P2,P0,P3,P0,P4,P0,P5,P0,P6,P0,P7,P0,P8,P0,P9))
    
    'Log("Envia ->",bc.StringFromBytes(P9))
    
    Ti = Micros
    
    Envia_Post(P10)
    
    Ti = Micros - Ti
    Log("Tiempo Invertido = ",Ti / 1000, " Maniobra = ",Maniobras)
    
End Sub
'
'
Sub Envia_Post(E() As Byte)
    HttpJob.AddHeader("Content-Type", "application/x-www-form-urlencoded")
    HttpJob.Post("https://ota-puerta.000webhostapp.com/?key1=value1",E)
End Sub

Sub JobDone (Job As JobResult)
    
    Log("Nombre del Paso : ", Job.JobName)
    Log("*******************************")
    
    If Job.Success Then
        
        'Log("Response: ", bc.SubString2(Job.Response, 0, Min(20, Job.Response.Length))) ' Limita a 20 Caracteres

        If Job.JobName = "Paso_000" Then
            CallSubPlus("Envia_Estado",10000,0)
            'HttpJob.Initialize("Paso_001")    '
            'Log("Cambio a Paso_001")
        End If
    Else
        CallSubPlus("Envia_Estado",5000,0)
        Log("++++++ ERROR +++++")
        Log("ErrorMessage: ", Job.ErrorMessage)
        Log("Status: ", Job.Status)
        'Log(Job.Response)
    End If
End Sub



Thank you so much
 

Daestrum

Expert
Licensed User
Longtime User
Shouldn't there be a Job.Release in there somewhere?
 
Upvote 0

Gerardo Tenreiro

Active Member
Licensed User
Hi, I don't know that instruction, in fact the editor doesn't recognize it.
Can you give me more information about how to use it and where to put it?

1725978941010.png


Thank you very much
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
(not used it) But I think it goes in the last line of your JobDone sub as Job.Release
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
No Job.Release in B4R.

My guess is that the problem is here:
B4X:
Envia_Post(P10)
At some point the stack allocated array becomes invalid.

Use GlobalStore to save P10 and change to:
B4X:
Sub Envia_Post(E() As Byte)
    HttpJob.AddHeader("Content-Type", "application/x-www-form-urlencoded")
    HttpJob.Post("https://ota-puerta.000webhostapp.com/?key1=value1",GlobalStore.slot1) 'something like that
End Sub
 
Upvote 0

Gerardo Tenreiro

Active Member
Licensed User
Hello,
I modified the code to use GLOBALSTORE, I cut the length of the telegram a little because I had a length error, but even so the problem persists, instead of sending the 7th message it goes to the 8th or ninth message.
I leave the modified code to see if a solution to the problem can be found

I also check that nothing is lost in the variable that is passed

B4X:
#Region Project Attributes
    #AutoFlushLogs        : True
    #CheckArrayBounds    : True
    #StackBufferSize    : 2000
#End Region

Sub Process_Globals
    Public  Serial1         As Serial
    Private wifi             As ESP8266WiFi
 
    Public    Ts                As Timer
    Public  Segundo         As Byte                = 0
    Public  Minuto             As Byte                = 0
    Public  Hora             As Byte                = 0
 
    Public  bc                As ByteConverter
 
 
    Public  Maniobras        As UInt                = 0
    Public  Horas_Uso        As UInt                = 0
 
 
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("****************************")
    Log("Inicia Aplicacion")
 
 
    Ts.Initialize("Ts_Tick",1000)
    Ts.Enabled = True
 
    Conecta_Wifi(0)

End Sub


Sub Ts_Tick
    Segundo = Segundo + 1
    Horas_Uso = Horas_Uso + 1
    If Segundo > 59 Then
        Segundo = 0
        Minuto = Minuto + 1
        If Minuto > 59 Then
            Minuto = 0
            Hora = Hora + 1
            If Hora > 23 Then
                Hora = 0
            End If
        End If
    End If
End Sub



Sub Conecta_Wifi(Nada As Byte)
    Log("Intentando conectar a la red WIFI")
    wifi.Disconnect
    wifi.ConnectAsync("xxx", "xxx", 0, Null, "wifi_Connected")
End Sub

Sub WiFi_Connected (Success As Boolean)
    If Success = True Then
        Log("WIFI Conectada !!!")
        Log("La IP es: ",wifi.LocalIp)
    Else
        Log("No conectado la WIFI, se reintenta")
        CallSubPlus("Conecta_Wifi",5000,0)
    End If
 
    If Success = True Then
        HttpJob.Initialize("Paso_000")           
        Envia_Estado(0)
    
    End If
End Sub
'
'                 Envia Estados de la Tarjeta de Control.
'    Envia Datos al servidor
'    Numero de Serie        ->    5 Digitos        ->    00000
'     Fecha en el ESP32    ->    8 Caracteres    ->     dd/mm/aa
'   Hora en el Servidor    ->  8 Caracteres    ->     hh:mm:ss
'   Version Hardware    ->                  ->     4.12.00
'   Version Software    ->                  ->     4.12.26a
'   Año de Fabricacion    ->    4 Digitos        ->     2024
'     Mes de Fabricacion    ->    2 Digitos        ->    01
'    Numero de Maniobras    ->    10 Digitos        ->     0000000000
Sub Envia_Estado(Nada As Byte)
    Dim Ti                As ULong    = 0
    Dim Amo                As Int        = 2024
    Dim Mes                As Int        = 4
    Dim Numero_Serie     As Int        = 22
 
    Log("*******************************")
    Log("Inicia Envio ... Buffer Utilizado = ",StackBufferUsage, " ... Ram = ",AvailableRAM)
 
    Dim P0() As Byte    = "&".GetBytes
    Dim P1() As Byte      = JoinBytes(Array ("Amo=".GetBytes,NumberFormat(Amo,4,0).GetBytes))
    Dim P2() As Byte      = JoinBytes(Array ("Mes=".GetBytes,NumberFormat(Mes,2,0).GetBytes))
    Dim P3() As Byte      = JoinBytes(Array ("Numero_de_Serie=".GetBytes,NumberFormat(Numero_Serie,5,0).GetBytes))
    Dim P4() As Byte    = "Fecha=01/01/2024".GetBytes
    Dim P5() As Byte      = JoinBytes(Array ("Hora=".GetBytes,NumberFormat(Hora,2,0).GetBytes,":".GetBytes,NumberFormat(Minuto,2,0).GetBytes,":".GetBytes,NumberFormat(Segundo,2,0).GetBytes))
    Dim P6() As Byte     = "Version_H=4.12.00".GetBytes
    Dim P7() As Byte     = "Version_S=4.12.26a".GetBytes
 
    Maniobras = Maniobras + 1
    Dim P8() As Byte     = JoinBytes(Array ("Maniobras=".GetBytes,NumberFormat(Maniobras,8,0).GetBytes))
    Dim P9() As Byte    = JoinBytes(Array ("Horas_Uso=".GetBytes,NumberFormat(Horas_Uso,8,0).GetBytes))
 
    Dim P10() As Byte     = JoinBytes(Array (P1,P0,P2,P0,P3,P0,P4)) ',P0,P5,P0,P6,P0,P7,P0,P8,P0,P9))
 
    GlobalStore.Put(1,P10)
 
 
 
    'Log("Envia ->",bc.StringFromBytes(P9))
    Log("Largo P10=",P10.Length)
    Ti = Micros
 
    Envia_Post(P10)
 
    Ti = Micros - Ti
    Log("Tiempo Invertido = ",Ti / 1000, " Maniobra = ",Maniobras)
 
End Sub
'
'
Sub Envia_Post(E() As Byte)
    Log("Largo = ",E.Length)
    HttpJob.AddHeader("Content-Type", "application/x-www-form-urlencoded")
    HttpJob.Post("https://ota-puerta.000webhostapp.com/",GlobalStore.Slot1)
End Sub
'
'
Sub JobDone (Job As JobResult)
    If Job.Success Then
        'Log("Response: ", bc.SubString2(Job.Response, 0, Min(20, Job.Response.Length))) ' Limita a 20 Caracteres
        Log("Nombre del Paso : ", Job.JobName)   
        Log("*******PASO CORRECTO***********")

        If Job.JobName = "Paso_000" Then
            CallSubPlus("Envia_Estado",10000,0)
            'HttpJob.Initialize("Paso_001")    '
            'Log("Cambio a Paso_001")
        End If
    Else
        CallSubPlus("Envia_Estado",5000,0)
    
        Log("ErrorMessage: ", Job.ErrorMessage)
        Log("Status: ", Job.Status)
        'Log(Job.Response)
        Log("********ERROR EN EL PASO*******")
    End If
End Sub
Thank you very much
 
Upvote 0

Gerardo Tenreiro

Active Member
Licensed User
Thank you very much Erel,
I'm testing and so far the ESP32 has not restarted.
Now I'm going to check that I don't have any other problems and I'll implement it soon in the final project

Great job, yours.
 
Upvote 0
Top