emonBase Watchdog

The OEM sketch http://bit.ly/xjNfb2 includes a nifty watchdog routine, and I can work out that it uses a variable 'ethernet_requests' to count the number of times that a server response 200 is not received and if it exceeds 10, then it introduces a 10 second delay which triggers the watchdog reset. That bit is fine.

But... I can't work out how you have determined if a '200' OK code has been received!!

Pachube's server is playing up at the moment and playing havoc with my Nanode, so I would like to use a similar watchdog in my sketch (I'm using ethercard too) if that's OK.

Are you able to tell me what sections I would need as a minimum to add to achieve this (I don't need time & date and can configure the watchdog increment routine OK).

 

Many Thanks

Paul

Paul Reed's picture

Re: emonBase Watchdog

 By way of update...

I have added....

 

//-----------------------------------------------------------------------------------
// Ethernet callback
// recieve reply and decode
//-----------------------------------------------------------------------------------
int request_attempt = 0;
char line_buf[50];

static void my_callback (byte status, word off, word len) {
get_header_line(2,off);      // Get the date and time from the header
Serial.println(line_buf);    // Print out the date and time

get_reply_data(off);
if (strcmp(line_buf,"ok")) {Serial.println("ok recieved"); request_attempt = 0;} 
//-----------------------------------------------------------------------------------

...just before 'void setup ()', and added the decode_reply library, and 'request_attempt ++;' in the send loop but the request_attempt variable increments with every send, despite them being successful. i.e. the code above does not serial print "ok received" nor returns the request_attempt back to zero. 

The code above appears to contained in the Global Variables section of the sketch, but there is no call to it in the loop....

I can't get my head around this Glyn!!
 

 

TrystanLea's picture

Re: emonBase Watchdog

Hello Paul, the function static void my_callback (byte status, word off, word len) {

is a callback and is called here:

ether.browseUrl(PSTR("/emoncms3/api/post.json?apikey=2d177d7311daf401d054948ce29efe74&json="),str.buf, website, my_callback);

Its not actually looking for the 200 OK status code but for an ok sent by the server in the message body, this is given by emoncms. So maybe that would be why its not working with pachube.

Do you know what the in body responce is from pachube?

 

 

TrystanLea's picture

Re: emonBase Watchdog

Try adding the lines at the top of the function:

get_header_line(1,off);

Serial.print(line_buff);

Then it might be possible too do something like this:

if (strcmp(line_buf,"HTTP/1.1 200 OK")) {ethernet_requests = 0; ethernet_error = 0;}

 

Paul Reed's picture

Re: emonBase Watchdog

 Hi Trystan.

The reply body from Pachube is 'HTTP/1.1 200 OK'  (you were correct).
I have tried this code in the Global section;

//-----------------------------------------------------------------------------------
// Ethernet callback
// recieve reply and decode
//-----------------------------------------------------------------------------------
int request_attempt = 0;
char line_buf[50];
static void my_callback (byte status, word off, word len) {
get_header_line(1,off);      // Get the date and time from the header
Serial.println(line_buf);    // Print out the date and time
get_reply_data(off);
if (strcmp(line_buf,"HTTP/1.1 200 OK")) {Serial.println("ok recieved"); request_attempt = 0;}
}
//-----------------------------------------------------------------------------------

and this in the loop;

    // generate the header with payload - note that the stash size is used,
    // and that a "stash descriptor" is passed in as argument using "$H"
    Stash::prepare(PSTR("PUT http://$F/v2/feeds/$F.csv HTTP/1.0" "\r\n"
                        "Host: $F" "\r\n"
                        "X-PachubeApiKey: $F" "\r\n"
                        "Content-Length: $D" "\r\n"
                        "\r\n"
                        "$H"),
            website, PSTR(FEED), website, PSTR(APIKEY), stash.size(), sd, my_callback);
 
But I'm afraid it still doesnt acknowledge the server reply.

TrystanLea's picture

Re: emonBase Watchdog

Hello Paul, does the Serial.println(line_buf);  print out: "HTTP/1.1 200 OK" to the serial window?

Paul Reed's picture

Re: emonBase Watchdog

 No it doesn't, and it doesnt reset the request_attempt either.

Paul Reed's picture

Re: emonBase Watchdog

 ...also, just found out that the Nanode 5 doesn't support the avr.wdt.h library anyway, so perhaps I need a different approach to this problem.

glyn.hudson's picture

Re: emonBase Watchdog

It's not the Nanode5 that doesn't support the watchdog its the arduino duemilanove bootloader.  It's not too difficult to re-flash the ATmegs with the Uno (optiboot) bootloader. The isp_repair sketch included in the JeeLib library work well for this. See here for wiring instructions:

http://jeelabs.org/2011/05/29/summary-of-isp-options/

 

Paul Reed's picture

Re: emonBase Watchdog

 Thanks Glyn, I have just re-flashed the Nanode as per above, and all went well. It's now running the Uno optiboot bootloader, and the avr/wdt.h library now loads and runs.  :-)

Have you any thoughts pls as to why I can't parse the HTTP reply from Pachube using your library? Trystan made a suggestion above but that didn't seem to work either.  Watching the serial monitor, I get no reponse at all from the Serial.println(line_buf); section.

I have found though that the existing ethercard library does serial print the HTTP reponse, so it is being called from somewhere in the ethercard library. I havn't managed to find where, or to capture the response though.

Paul.

smithgor's picture

Re: emonBase Watchdog

Thanks for raising this problem and trying to find a solution, guys.

I'm also suffering with the problem of a Nanode initially linking to Pachube then for no reason ceasing to get the 200 code after hours or days. I know its a work-around, but It would be great to get a way to detect this dropout and do a reset.

I'm not knowledgeable enough to develop / modify the code myself so hoping you will get a solution.

Gordon

igordutra's picture

Re: emonBase Watchdog

I'll try to make the callback function works this week. I'm not a real coder but will give it a try and share once I manage to get it working. Any help would be appreciated :) 

Baz Crosen's picture

Re: emonBase Watchdog

Thats good news Igor. I hope that youre successful. I have to keep restarting mine because Pachubes server keeps going down, and like smithgor above, Im new to this and unable to code it myself.

Thank you for trying.

glyn.hudson's picture

Re: emonBase Watchdog

There have been many discussions lately regarding emonBase Nanode/NanodeRF instability.

To constructively try and move forward, lets create a knowledge base of setups that are stable Vs. setups which are not. Hopefully this will help us to debug this issue and give us a framework in which to discuss problems.

See forum thread: http://openenergymonitor.org/emon/emonbase/stability

Paul Reed's picture

Re: emonBase Watchdog

 The main problem (from my perpective) is that Pachube's servers over the past few weeks have become problematic.

Pachube are aware of this problem, and are in the process of moving to a dedicated server which should provide a more stable service, but no timeline has been given.

Whenever the Pachube servers stop responding, Arduinos and Nanodes across the globe lose their connection, and do not reconnect when the server is restored. If I compare when my Nanode has frozen with a feed search for 'frozen feeds' via Pachube's search engine, I can find hundreds of devices which have frozen at the exact time as mine.  ( https://pachube.com/feeds )

This suggests that once disconnected, the ethercard library struggles to reconnect, and hence why a watchdog is desirable.

My current sketch (thanks re the bootloader Glyn!) now restarts every hour due to a watchdog timer, but although it's only been in use for 2 days, it's already recovered from one such server outage - about 2.15am 30/01/2012.

igordutra's picture

Re: emonBase Watchdog

I haven't had time to work on the proper fix for the pachube sketch yet however I've done something very simple that so far is keeping my Nanode (duemilanove) sending data for over a day. Basically I reboot the board every 200 cycles (about every 20-30 minutes) using the following line:

if (request_attempt > 200) asm volatile ("  jmp 0");  

instead of
 
if (request_attempt > 100) delay(10000);
 
at the end of the sketch.
 
My other Nanode (UNO) is posting data to emoncms for almost 2 days without a single crash using the latest emoncms sketch.
 
My idea is to implement something similar to ArduWind (https://code.google.com/p/arduwind/source/browse/trunk/ArduWind.ino) to the pachube sketch that checks pachube response and also reboots the nanode at specific intervals. 
 
Igor
 
 
 

 

fjhug's picture

Re: emonBase Watchdog

Working for 13 days now. I have it working by resetting error flags if too many attempts. Didn't have to reset the NanodeRF once since.

if (request_attempt > 100) {
#ifdef UNO

      delay(10000); // Reset the nanode if more than 10 request attempts have been tried without a reply
    #else
      ether.begin(sizeof Ethernet::buffer, mymac);
      dhcp_status = 0;
      dns_status = 0;
      request_attempt = 0;
      error=0;

    #endif
}

igordutra's picture

Re: emonBase Watchdog

Mine just frozen now after about 24 hours :( I'll try your suggestion above and let it running over the weekend.

Thanks!

----

Ops, just realised mine hasn't frozen. It was only waiting the end of the loop to reboot...

Paul Reed's picture

Re: emonBase Watchdog

 My 'UNO bootloaded' Nanode reboots every hour using the avr/wdt watchdog library routine, and so far it has recovered from several 'server down - feed frozen' incidents over the past 3 or 4 days. One of which was at 7am this morning, and the Nanode rebooted and reconnected succesfully by itself as shown below about 30 minutes later. 

 

2012-02-03T07:00:23.022890Z,190
2012-02-03T07:31:46.028208Z,197

It's not an elegent solution, but it works!

Paul

 

igordutra's picture

Re: emonBase Watchdog

I believe the UNO bootloader would be a better long term solution, at least the watchdog works properly... However as Glyn pointed out other day it would be good to have a duemilanove sketch working since it's the bootloader that comes as standard on Nanodes.

I'm doing pretty much the same here, rebooting every hour but instead of using the watchdog I'm using asm volatile. So far it's been working well but I've just added the code to reset the error flags as suggested by fjhug. I'll keep monitoring it over the weekend. If it doesn't work I'll go back to the previous solution.

On the other hand the other Nanode/UNO has been posting data to emoncms for about 2 days without a single problem...

Once I get it working I'll simply use UNO bootloaders on all my Nanodes :)

mharizanov's picture

Re: emonBase Watchdog

 only a WDT reset is a true reset. the asm hack only restarts the software, not the hardware.

 

 

mharizanov's picture

Re: emonBase Watchdog

 BTW is a Software reset possible for the ENC28J60 ? I read it is possible to issue SPI reset command, but no concrete code.

fjhug's picture

Re: emonBase Watchdog

 the command "ether.begin(sizeof Ethernet::buffer, mymac);" in the code above effectively resets the ENC28J60 with a soft reset.
It calls the "initialize" function from the ENC28J60 library. That's if you use the EhterCard library. 
mharizanov's picture

Re: emonBase Watchdog

 Thanks, I didn't know that.

I am using the EtherCard library.

 

igordutra's picture

Re: emonBase Watchdog

@fjhug - Using your reset error flags code for 3 days now and not a single need to reboot the Nanode manually so far.

@mharizanov - Asm hack worked for me for a couple days until I decided to test @fjhug's code. Tested it for 24 hours only and it was still (soft) rebooting at regular intervals. But @fjhug's code is probably a better solution! So I'll stick with it for a couple more days to see how it goes.

igordutra's picture

Re: emonBase Watchdog

Nanode/Duemilanove stopped posting data today using fjhug's code, after 4 days. I'll try it on UNO to see how it goes.

---

5 days on UNO and no issues so far...

Scott216's picture

Re: emonBase Watchdog

fjhug,

When resetting the flags, why do you have #ifdef UNO? Will this not work with duemilanove?

fjhug's picture

Re: emonBase Watchdog

 Yes it will on any version, I actually commented out the lines around the UNO. I was initially using Duemilanove to fix the issue. Moved to Uno a couple of weeks ago.

In my latest code, I removed the UNO lines, so the code will work on UNO and Duemilanove.

Scott216's picture

Re: emonBase Watchdog

According to this post on JeeLabs the HTTP response is sent to the serial monitor by tcpip.cpp.

I'm having the same issue about not having access to the HTTP response with the ethercard.h library on my Nanode v5.  I'm not an advanced coder, so I can't really figure this out myself.  I hope someine comes up with a solution to detect errors when sending data to pachube.

 

fjhug's picture

Re: emonBase Watchdog

 Using the code in this post gives you control of what to do with the reply from the server.

You could check the returned HTTP code. It should be a good starting point.

Good luck

Paul Reed's picture

Re: emonBase Watchdog

Has anyone tried the recent improvement to Ethercard on the Jeelabs site?

They have made an addition to get an Ethernet reply back after you call tcpSend() in the EtherCard library, which looks promising.

 

Paul

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.