Automatic reconnection after reboot

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

Hey @hickse,

These all looks correct to me. Are you getting any errors or warnings?

Follows is the error presented in the terminal (I’m using a different program that generates a JSON message) but the failure persists, with the following message:

Testing Hologram Cloud class…
Cloud type: HologramCloud
Traceback (most recent call last):
File “dht_hologram.py”, line 41, in
timeout = 3)
File “/usr/local/lib/python2.7/dist-packages/Hologram/HologramCloud.py”, line 112, in sendMessage
result = super(HologramCloud, self).sendMessage(output, timeout)
File “/usr/local/lib/python2.7/dist-packages/Hologram/CustomCloud.py”, line 78 , in sendMessage
resultbuf = self.network.send_message(message)
File “/usr/local/lib/python2.7/dist-packages/Hologram/Network/Cellular.py”, li ne 132, in send_message
return self.modem.send_message(data)
File “/usr/local/lib/python2.7/dist-packages/Hologram/Network/Modem/Modem.py”, line 253, in send_message
raise SerialError(‘Timeout occurred waiting for message status’)
Exceptions.HologramError.SerialError: Timeout occurred waiting for message status

Sudo halogram disconnect does not allow for a successful reconnection. Only after a power down of the Nova and a restart does the program successfully continue for another period of time before it crashes. That makes me wonder if the connection to the cellular network may be the issue.

I run the script below on a Pi at boot and every 15 minutes using crontab. I’m sure this script could be improved with some more robust ping checking, retries, and a recheck of ping after re-connection. Can someone with mad Python skills help us out?

This works almost all the time, but occasionally the Nova R410 modem goes completely offline (even the blue light goes out) and I can’t recover without a reboot or remove/install the USB modem. Yes, I have a very beefy power supply on the Pi. I need an additional recovery step for a apparently lost USB modem. Anyone else seeing this?
------------
#!/bin/bash
# PING ONCE. IF FAILED, DISCONNECT - RECONNECT MODEM
if ping -q -c 1 -W 1 google.com >/dev/null; then
echo “Ping Successful”
else
echo “Ping Failed”
sudo /usr/local/bin/hologram network disconnect
sleep 5
sudo /usr/local/bin/hologram modem reset
sleep 15
sudo /usr/local/bin/hologram network connect
echo “Connected”
fi
-----------------

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.