Friday, November 4, 2016

Finally - A Working Sensor Recovery Python Script

Using a Raspberry Pi and learning how to read sensors via GPIO pins using Python is a wonderful learning experience. One of the important things to learn when melding hardware and software is how to 'bullet proof' against system and/or sensor failure.

I initially tried to craft a simple system reboot in response to a failure of a Python 'try' statement. Our power down here in the near Tropics, despite being within the CONUS sometimes is almost Third World. I think Baghdad must have a better electric grid than the one run here by Duke Energy.

Anyhow, I finally came up with a working Python function to reboot my Raspberry Pi 3B in the event of a BME280 sensor read failure. Without this, the system would hang and the script would fail to return to a working state.

So here's the snippet. It parses out of the currently running script to reboot the system and hopefully, restore a working order, as evidenced by yesterday's intermittent power outages/voltage fluctuations - here's the logging output (saved to a lighttpd LAN-readable Web page):

2016-11-03 02:10 error reading sensor - rebooting
2016-11-03 02:20 error reading sensor - rebooting
2016-11-03 02:31 67.6 22.7 100.0
2016-11-03 02:41 error reading sensor - rebooting
2016-11-03 02:51 67.6 22.7 100.0
2016-11-03 03:01 error reading sensor - rebooting
2016-11-03 03:11 67.6 22.7 100.0
2016-11-03 03:21 error reading sensor - rebooting
2016-11-03 03:31 67.6 22.7 100.0
2016-11-03 03:41 error reading sensor - rebooting
2016-11-03 03:52 67.6 22.7 100.0
2016-11-03 04:02 error reading sensor - rebooting
2016-11-03 04:12 67.6 22.7 100.0
2016-11-03 04:22 error reading sensor - rebooting
2016-11-03 04:32 error reading sensor - rebooting
2016-11-03 04:44 error reading sensor - rebooting
2016-11-03 04:54 error reading sensor - rebooting
2016-11-03 05:04 67.6 22.7 100.0
2016-11-03 05:14 error reading sensor - rebooting
2016-11-03 05:25 67.6 22.7 100.0
2016-11-03 05:35 error reading sensor - rebooting
2016-11-03 05:45 67.6 22.7 100.0
2016-11-03 05:55 error reading sensor - rebooting
2016-11-03 06:05 67.6 22.7 100.0
2016-11-03 06:15 error reading sensor - rebooting
2016-11-03 06:25 error reading sensor - rebooting
2016-11-03 06:36 error reading sensor - rebooting
2016-11-03 06:46 error reading sensor - rebooting
2016-11-03 06:56 67.6 22.7 100.0
2016-11-03 07:06 error reading sensor - rebooting
2016-11-03 07:16 69.7 30.2 58.9

As you can see, things started going awry at 0200. The function sets a reboot after 10 minutes. Bogus sensor readings continued until 0716, and the system has been running fine since then. Here's the code snippet/function. I hope it helps you!

def reboot():
  text_file = open("/var/www/html/weather.txt", "a")
  text_file.write(str(datetime.now().strftime('%F %H:%M ')))
  text_file.write("error reading sensor - rebooting\n")
  text_file.close()
  time.sleep(600)
  cmd = "/usr/bin/sudo /sbin/shutdown -r now"
  import subprocess
  process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
  output = process.communicate()[0]

  return  "%s" % output

The function is called like this:

# test bus write
  try:
        bus.write_byte_data(addr, REG_CONTROL, control)
  except IOError:
        reboot()


or like this:

# Read temperature/pressure/humidity
  try:
        data = bus.read_i2c_block_data(addr, REG_DATA, 8)
  except IOError:
        reboot()


Have a happy!

No comments:

Post a Comment