A couple of handy snippets of code for debugging | Arduino | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
A couple of handy snippets of code for debugging
Avatar
HydroponicsMAC

Silver
Forum Posts: 8
sp_UserOfflineSmall Offline
1
August 29, 2016 - 1:28 pm
sp_Permalink sp_Print sp_EditHistory

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 codingSmile

Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
2
August 30, 2016 - 1:23 am
sp_Permalink sp_Print

Does your setup work properly if the power goes off and both the RPi and Arduino reboot?   I tried a similar setup a few years ago (see my stackexchange question) but found the arduino wasnt recognised after the RPi was power cycled.

That was using an original RPi, so perhaps the later models behave better.

Avatar
HydroponicsMAC

Silver
Forum Posts: 8
sp_UserOfflineSmall Offline
3
August 30, 2016 - 10:59 am
sp_Permalink sp_Print

I haven't experience the same non-recognition problem with the Raspberry Pi 3 and Arduino Mega that I'm using.  I'm powering the Arduino via a 9v power adaptor so its USB port is just used for serial output to the Pi.

At at guess when the power goes off/on the Arduino will be up and running before the Pi, then when Pi starts logging the Arduino's serial output the Arduino will reboot again (which can be disabled with some hardware mods)

http://stackoverflow.com/questions/16224816/preventing-reset-on-serial-monitor-connect 

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online: cstapels
22 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

yeseniara11

RomanFug

JamesPes

RobertBak

ScottSmiva

Amourhotte

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee