Oct 16 2013 0

Improvements to collection of sensor data (stubborn DHT22 sensor)

From time to time the script collecting data from sensors attached to my Raspberry Pi would stop working and the only thing that seemed to resolve the issue was to reboot the device. To have this somewhat automated I extended to rules module that would allow me to specify a shell command to be executed, in this case the command would be sudo reboot.

Also to make a bit more useful I can specify when the command needs to be executed, being always, never or results. The first two are self explanatory. When results is specified the command will only be executed if the rule query itself yields a result. You can find the code in my GitHub repository raspberrypi.

The following files involved are

Rules framework

  • Rules.php — PHP class
  • Rules.py — Python class

Scripts using the rules framework

  • check_rules.php — PHP script called by crontab to check rules every five minutes
  • ds18b20.py — Python sensor data collection script

Definitions of my current rules

  • rules.sql — Content for the MySQL rules table
  • rules_proc.sql — MySQL stored procedures used by some of the rules

This solution to reboot the Raspberry Pi seemed to work fine for some time. But more recently the Raspberry Pi needed to be rebooted every 10 to 15 minutes, so a closer look at the real cause was in order.

Looking at the data collected by the script ds18b20.py it always seem to hang when it was trying to collect data from the DHT22 sensor. Searching the internet for similar issues I found some altered code for Adafruit_DHT.c (in a blogpost comment over at RPi-Experiences) that would quit if for some reason the data pin would not change state within a certain amount of time. I implemented the code provided there with a slight modification (didn’t like the combined condition).

Original code (potential endless loop) 1

while ( bcm2835_gpio_lev(pin) == 1 ) {
    usleep(1);
}

Modified code 1

// wait for pin to drop?
int safety = 0;
int max_wait = 1000;
while ( bcm2835_gpio_lev(pin) == 1 ) {
    // if no answer, quit
    if ( safety > max_wait ) {
        return 2;
    }

    usleep(1);
    safety++;
}

This doesn’t prevent the DHT22 sensor from not responding in a timely fashion but at least it doesn’t stop the collection process from collecting data from all of the other sensors attached to the Raspberry Pi.

For now this is good enough.

UPDATE:

The image below displays one hours worth of data collected from both the DS18B20 and DHT sensor. All the red lines indicate where the DHT sensor failed to respond in a timely fashion.

DHT sensor not respondingDHT sensor not responding


Previous post
Hubsan X4 v2 Camera mod I recently bought a Hubsan X4 quad-copter and after some flying and crashes (already had to replace one of the rotors, blades and solder the power
Next post
Additional Hubsan X4 Mods After watching the video ‘Hubsan X4 Tips & Tricks 2’ I decided to implement some of these tips Sometimes the LiPo battery is somewhat hard to
This blog is powered by Blot