B4R Question How to Reset a Program Automatically

AndroidMadhu

Active Member
Licensed User
Hello,
I am running a program which is providing temp, humidity over the GSM and it is running successfully.
The problem is while running for one day continuously the data is not coming to cloud server.
We have to manually RESET the board[Arduino UNO] and then again the program runs as usual.
Everyday in the evening we have to RESET the program for the next day.


Is there is any way we can RESET the program automatically? Can it be possible to implement watchdog at b4r?

Please suggest.
 

emexes

Expert
Licensed User
The problem is while running for one day continuously the data is not coming to cloud server.
We have to manually RESET the board[Arduino UNO] and then again the program runs as usual.
A program resetting itself before it locks up will fix that problem only. If you're only going to fix the one problem, you'd be better to first try fix the actual problem, rather than mask it.

From the receiving logs at the cloud server, can you confirm that the sensor device runs for a specific amount of time before stopping? Like, it could be 86400 seconds (full day) but it could be some other nearby interval like 65536 seconds or 32 hours or... whatever.

It does sound a bit like there is time wraparound from eg 86399 to 0 seconds or 23.999 to 0.000 hours or "23:59:59" to "00:00:00", and that a timeout is implemented like:
B4X:
WaitUntil = CurrentTime + HowLongToWait
Do Until CurrentTime > WaitUntil
    'wait up to HowLongToWait for something to happen
Loop
Is there anything like that in your code?
 
Upvote 0

emexes

Expert
Licensed User
Another method I inadvertently found last week is by stack overflow ;-)

https://www.b4x.com/android/forum/t...ing-buffer-overwrite-or-out-of-bounds.108584/

although this was an ESP32 not Arduino, and we'd still be better off fixing the actual problem, but... hey, always good to have a Plan B ;-) eg from log:

AppStart
Packet #1
Packet #2
...
Packet #19
Packet #X�
Packet #21
Packet #22Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x40087f5f PS : 0x00060533 A0 : 0x80086e51 A1 : 0x3ffb1dd0
A2 : 0x0000001d A3 : 0x3ffb8058 A4 : 0x00000001 A5 : 0x00000001
A6 : 0x00060523 A7 : 0x00000000 A8 : 0x00000000 A9 : 0x3ffb1db0
A10 : 0x00000001 A11 : 0x3ffb8058 A12 : 0x00000001 A13 : 0x00000001
A14 : 0x00060323 A15 : 0x00000000 SAR : 0x0000001c EXCCAUSE: 0x0000001c
EXCVADDR: 0x0000002d LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xffffffff
Backtrace: 0x40087f5f:0x3ffb1dd0 0x40086e4e:0x3ffb1df0 0x4008541d:0x3ffb1e10 0x400d1cfd:0x3ffb1e50 0x400d1385:0x3ffb1e70 0x400d155e:0x3ffb1e90 0x400d1585:0x3ffb1eb0 0x400d10d4:0x3ffb1ed0 0x400d103a:0x3ffb1ef0 0x400d1268:0x3ffb1f50 0x400d12ce:0x3ffb1f90 0x400d20db:0x3ffb1fb0 0x40087911:0x3ffb1fd0
Rebooting...
 
Upvote 0

janderkan

Well-Known Member
Licensed User
Longtime User
B4R has a little flaw, if you have a timer event and you do not call Log() in the event, the program will stop eventually. (between 1 and 25 hours)
B4X:
Log(" ")
See this , post 4
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
B4R has a little flaw, if you have a timer event and you do not call Log() in the event, the program will stop eventually. (between 5 and 25 hours)
If this is true (and I'll bet it took a while to chase down that issue...) and you cannot modify the hardware, then Plan A Version 1.1 might be to combine the second method from Erel's link:

https://www.theengineeringprojects.com/2015/11/reset-arduino-programmatically.html

and implement resetFunc() inline within your B4R code per (yet another) Erel post:

https://www.b4x.com/android/forum/threads/inline-c-c.65714/#content

You might need to add the expected parameter B4R::Object* o to resetFunc to keep the B4R compiler happy.
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
Upvote 0

AndroidMadhu

Active Member
Licensed User
I have changed the code like below

B4X:
Private currentsate As Boolean
Private newResetState As Boolean
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    pin5.Initialize(5,pin5.MODE_OUTPUT)
    pin4.Initialize(4,pin4.MODE_OUTPUT) 'This is for RESET
    currentsate=pin4.DigitalRead
    newResetState=Not(currentsate)
    If currentsate Then
        pin4.DigitalWrite(newResetState)
    End If
End Sub
This is not working.... Please suggest...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code seems to work:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private Timer1 As Timer
   Private ResetPin As Pin
   Private Counter As Int
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   Timer1.Initialize("Timer1_Tick", 1000)
   Timer1.Enabled = True
End Sub

Sub Timer1_Tick
   Counter = Counter + 1
   Log("Counter = ", Counter)
   If Counter = 5 Then
       ResetPin.Initialize(4, ResetPin.MODE_OUTPUT)
       ResetPin.DigitalWrite(False)
   End If
End Sub
 
Upvote 0

Hanspeter

New Member
Licensed User
Longtime User

Hi Erel,

You should NOT reset the board from software by connecting an output pin to reset pin and driving it low.
The datasheet specifically cautions against this.

Had a pice of code that froze and the reset pin was of no help, not even pressing hardware button worked.

Hanspeter.
 
Upvote 0

janderkan

Well-Known Member
Licensed User
Longtime User
If your processor has stopped, no code is running.
First check that you dont acces value(3) in an array with only 3 members or other error#40
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…