Hello All,
I thought I would share a couple of handy code fragments that I've been using while developing Arduino apps with ThingSpeak.
My use case is to have the Arduino out in the field running 24x7 which makes debugging difficult, so I have opted for a Raspberry Pi connected to the serial port (USB) of the Arduino to collect the serial output and debug what's going on. My solutions means I can terminal into the PI from anywhere in the world and debug / reprogram it.
So on to the handy Lunix command...connect the Arduio to the PI via a USB port then run the following command to capture its Serial.print output into a log-file on the PI with each entry prefixed with a datestamp:
cat /dev/ttyUSB0 | while IFS= read -r line; do echo "$(date) $line"; done >> logfile.txt
//That's /dev/ttyUSB0 ZERO not oh
//Additionally - if you want this to run continually just like a service simple add a & symbol after ... $line"; done >> logfile.txt &
The second tip is to automatically reboot the Arduino should it stop running, again this is handy if your use case is in the field and you can't spend all day monitoring it.
As the Arduino is running you can call on its 'WatchDog' capability which as the name suggests monitors the Arduino and if it stops responding automatically reboots it. Essentially what the below code does is turn on the WatchDog with wdt_enable(WDTO_8S); and tell it to wait 8 seconds before rebooting.
You now have 8 seconds to tell the WatchDog within your code that all is well and RESET the 8 second timer using wdt_reset();
If after 8 seconds you don't reset the timer the Arduino will automatically reboot. For more information see this...
#include <avr/wdt.h> // You can just add this line as the library is included with every Arduino IDE
void loop() {
wdt_enable(WDTO_8S); // Start the WatchDog
// Do something which must finish before 8 seconds is up
wdt_reset(); // Reset the timer, all is well
}
Happy coding