I am running the following code that send temperature and humidity data to the hologram.io cloud which works perfectly at sending data to the Hologram Console but when the raspberry pi is rebooted, or the power is disconnected it stops sending data. I’d like to have a file that runs automatically upon power up or reboot.
I created a line of code in the rc.local file with path name (python /home/pi/main.py &) but upon reboot it only sends 1 data set (temp + humidity) and then no more data is sent.
It there a more successful way to ensure restart after a power failure or reboot?
I’m using a Nova LTE connected to a raspberry pi
import RPi.GPIO as GPIO ## Import GPIO library
import Adafruit_DHT ## Import temp/hum library
from Hologram.HologramCloud import HologramCloud ## Import Hologram cloud library
from time import sleep
DHT_SENSOR = Adafruit_DHT.DHT22
GPIO.setmode(GPIO.BCM) ## Use broadcom pin numbering
#hologram = HologramCloud(“xxxxx”, network=‘cellular’, enable_inbound=False) ## Saved this just in case using Device Key is useful
hologram = HologramCloud(dict(), network=‘cellular’)
hologram.network.connect() ## connect from the cellular netowork
DHT_PIN = 21 ## GPIO pin the DHT sensor is attached to
ALERT_ACTIVE = False
def getTemp(DHT_PIN):
temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)[1]
temperature = temperature * 9/5.0 + 32 ## Convert temp to Fahrenheit
if temperature is not None:
return str(temperature) + "F "
else:
return 'Sensor Error'
def getHum(DHT_PIN):
humidity = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)[0]
if humidity is not None:
return str(humidity) + "H "
else:
return 'Sensor Error'
def toggleAlert():
if ALERT_ACTIVE == False:
ALERT_ACTIVE = True
else:
ALERT_ACTIVE = False
def sendMessage(message, topic):
sent = hologram.sendMessage(message, topics=topic)
if sent == 0:
print 'Success! Message sent to the cloud.'
print message
else:
print 'Error type [' + sent + ']'
print 'Error descriptions: https://hologram.io/docs/reference/cloud/python-sdk/#-sendmessage-message-topics-none-timeout-5-'
try:
while True:
hum = getHum(DHT_PIN)
temp = getTemp(DHT_PIN)
sleep(3)
print("Console: Current Temperature is " + temp + "-Humidity is " + hum)
if ALERT_ACTIVE == True and temp > 71.0:
message = "ALERT: Temperature is WICKED HOT! Current reading " + temp + hum + "!!"
sendMessage(message, ["alert"])
else:
message = "Current Temperature is " + temp + "- Humidity is " + hum
sendMessage(message, ["status"])
sleep(30)
finally:
GPIO.cleanup() ## reset all pins
hologram.network.disconnect() ## Exercise 06 - disconnect from the cellular network