Random lockups when polling data

Hi,

I have an Arduino Mega with Ethernet shield with two DHT22 temp/humidity sensors (more to be added later) which is polled by a Python script on my web server every 5 minutes using the urllib2.urlopen function.

This works correctly for a time. After a seemingly random amount of time, sometimes 12+ hours sometimes as little as 10 minutes the Arduino appears to lock up and will no respond to requests.
The only way to return normal function is to power cycle it at which time results then get returned again until the next lock up.

I suspected a power issue and changed from using the USB connector for power to a 12v barrel pin connector but the issue remains.

Any ideas? I have included the sketch below.

Thanks in advance,
David

#include "etherShield.h"
#include "ETHER_28J60.h"
#include "DHT.h"

#define DHTPIN 47
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE); 
#define DHTPIN1 45
#define DHTTYPE1 DHT22
DHT dht1(DHTPIN1, DHTTYPE1); 


static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};   // this just needs to be unique for your network, 
static uint8_t ip[4] = {192, 168, 1, 15};                       // the IP address for your board. Check your home hub
static uint16_t port = 80;                                      // Use port 80 - the standard for HTTP

ETHER_28J60 ethernet;

void setup()
{ 
  ethernet.setup(mac, ip, port);
}

void loop()
{
  if (ethernet.serviceRequest())
  {
    //Get temp and humidity
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    float g = dht1.readHumidity();
    float q = dht1.readTemperature();
    
    //Return Results
    ethernet.print(t);
    ethernet.print(":");
    ethernet.print(h);
    ethernet.print(":");
    ethernet.print(q);
    ethernet.print(":");
    ethernet.print(g);
    ethernet.respond();
  }
  delay(50);
}

Have you considered adding in error compensation because there are retransmissions, especially in windows, and to be honest, your script looks pretty basic. Plus make sure you implement sync ticks, so your client and server can tell if one of them is lagging so they do not disconnect.

I'm not exactly sure what you mean, error compensation on the Arduino if two connections jam it up or something?

When the unit locks up I cannot get anything from it on both Windows and Linux the connection just times out.

The code I have tried to keep as simple as possible as it really doesn't need to accomplish much all the processing is done elsewhere.

It's a Linux machine running the Python script which is setup in cron for every 5 minutes.
This is the part of the code that pulls the data it then goes on to insert it into a mysql database

import time
import MySQLdb
import urllib2
response = urllib2.urlopen('http://192.168.1.15')
html=response.read()

rtdata,rhdata,ltdata,lhdata = html.split(":")

Upon running the Python script this is the return, "No route to host"
If attempted from a browser on windows the connection just times out.

Power cycle the Arduino and it starts functioning correctly again.

Traceback (most recent call last):
  File "arduino.py", line 4, in <module>
    response = urllib2.urlopen('http://192.168.1.15')
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 401, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 419, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1211, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1181, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 113] No route to host>

the router and or operating system can sometimes cause chunks to need to be retransmitted or cause them to be corrupted, plus make sure you are using static ip so that your ip does not change on you in the middle of the night, make sure to change dchp to static. This will be one step in the scientific process. Plus there is this thing called lease where the protocol refreshes the connection every specified time period. So set your lease time to the max possible, and make sure that your computer does not go to sleep, hibernate, power savings mode, turn off, or update. And sometimes the python scripts have an expiry date on the connection, so you will have to research that.

Both the Arduino and the web server have fixed IP's.

Web server runs 24/7 and all sleep modes are disabled.

The Pyhton script just runs every 5 minutes with Cron and then terminates.

Here is the full Python script with db credentials removed:

import time
import MySQLdb
import urllib2
response = urllib2.urlopen('http://192.168.1.15')
html=response.read()

rtdata,rhdata,ltdata,lhdata = html.split(":")

db = MySQLdb.connect(host="localhost",user="user",passwd="password",db="temps")
curs=db.cursor()


try:
        curs.execute ("INSERT INTO rack (id, tdate, ttime, temperature, humidity) VALUES (NULL, CURRENT_DATE(), NOW(), %s, %s) """,  (ltdata, lhdata))
        db.commit()


except db.Error, e:
        print "Error %d: %s" % (e.args[0],e.args[1])
        db.rollback()

try:
        curs.execute ("INSERT INTO laundry (id, tdate, ttime, temperature,  humidity) VALUES (NULL, CURRENT_DATE(), NOW(), %s, %s) """, (ltdata, lhdata))
        db.commit()

except db.Error, e:
        print "Error %d: %s" % (e.args[0],e.args[1])
        db.rollback()


db.close()

A way around this problem is a watchdog timer, basically every so often the script resets the watch dog timer, and if the watchdog timer expires before it gets reset, it resets the power or what ever you want it to do, so basically it's a crash restarter, look into how to utilize the watchdog timer on your board.

Have you tried not using floating point integers, and just using regular integers, it looks to me as if some of the memory is overflowing.

I'll change the float values to int tonight and see how it goes.

Thanks :slight_smile:

I have now changed the variables to INT values and am still having the same issue.

Longest period was about 18 hours without issue.

I have the same issue also with the ENC28J60 chip. I searched online a lot and some suggested to use separate power supply for the chip, but it also failed.

Basically what I may do is to try to send a request from the arduino to my server periodically and wait for a reply back. If no reply is back, I'll re-initialize my ethernet connection.

I am testing this technique now.

AhmedAdel:
I have the same issue also with the ENC28J60 chip. I searched online a lot and some suggested to use separate power supply for the chip, but it also failed.

Basically what I may do is to try to send a request from the arduino to my server periodically and wait for a reply back. If no reply is back, I'll re-initialize my ethernet connection.

I am testing this technique now.

I suspected power supply and have tried two now with the same result.

I would be very interested to hear how you go with that, I was going to resort to a relay or the likes to power cycle the Arduino but this seems like a very brute force way to solve the issue.

Have you tried not using floating point integers, and just using regular integers

What the hell are floating point integers? Integers are whole numbers - 1, 2, 3 - nary a decimal point in sight.

Add serial logging to the sketch? This could easily be a software problem.

MarkT:
Add serial logging to the sketch? This could easily be a software problem.

You misspelled cause...

MarkT:
Add serial logging to the sketch? This could easily be a software problem.

How's the best way to go about this?