Created
March 19, 2018 21:19
-
-
Save TaDahCorp/180ab0d9261a8326dca67d3d63179b0c to your computer and use it in GitHub Desktop.
Hologram.io Nova receive message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import datetime | |
from Hologram.HologramCloud import HologramCloud | |
retvalue = "NA" | |
i=1 | |
def holo_heartbeat(): | |
recv = hologram.sendMessage("!", topics=["heartbeat","pigate"]) | |
time.sleep(5) # give it time to think about it | |
return recv | |
hologram = HologramCloud(dict(),network='cellular') | |
result = hologram.network.connect() | |
ts = time.time() | |
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') | |
message = "NovaAction.py connected to Holo." | |
print (message) | |
if result == False: | |
print 'Failed to connect to cell network' | |
else: | |
connect_time = time.time() | |
try: | |
hologram.openReceiveSocket() | |
print "ready to receive data" | |
while True: | |
i += 1 | |
ts = time.time() | |
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') | |
elapsed = int(ts-connect_time) | |
# print "Elapsed since last heartbeat: " + str(elapsed) | |
if elapsed > 840: # 14 minutes between heartbeats | |
result = holo_heartbeat() | |
message = "Heartbeat sent: " + str(result) + ". Logged." | |
print (message) | |
connect_time = ts | |
else: | |
receivedMsg = hologram.popReceivedMessage() | |
connect_status = str(hologram.network.getConnectionStatus()) | |
print connect_status + ' ' + timestamp + " (elapsed seconds since heartbeat: " + str(elapsed) + ") - received: " + str(receivedMsg) | |
if len(str(receivedMsg))>4: | |
ts = time.time() | |
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') | |
if result == "invalid SMS": | |
message= result + ": " + receivedMsg | |
print message | |
else: | |
print receivedMsg | |
time.sleep(120) | |
time.sleep(1) # seconds | |
except KeyboardInterrupt: | |
print 'aborting the listening' | |
except Exception as e: | |
message = "Exception triggered" + str(e) | |
print (message) | |
ts = time.time() | |
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') | |
finally: | |
hologram.closeReceiveSocket() | |
hologram.network.disconnect() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$therealdevice = "12345"; | |
$mydevicekey = "ABCSECRET"; | |
$mydata = "--GPIOs=5 --action=click --duration=1 --from=7025551212"; | |
$url = “https://dashboard.hologram.io/api/1/devices/messages/$therealdevice/$mydevicekey1”; | |
$jsonData = array( | |
‘deviceid’ => $deviceid, | |
‘protocol’ => ‘TCP’, | |
‘port’ => 4010, | |
‘data’ => $mydata | |
); | |
$jsonDataEncoded = json_encode($jsonData); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: application/json’)); | |
$result = curl_exec($ch); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Every second it does a hologram.popReceiveMessage().
If there is data (as sent by send2holo.php), it grabs the data. Additional code, not included, acts on that data. It then sleeps for 2 minutes.
You will notice code that calls a heartbeat every 840 seconds (14 minutes). The intent is to kick the socket alive by posting on the Dashboard a single byte character (!).
Unfortunately, this doesn't work. The Dashboard always receives the ! character, for days on end. But after a few hours, send2holo.php fails with a Socket error: timed out message appearing in the Dashboard.
The only solution is to manually restart the Pi or to do a bash hologram connect.