I’m trying to construct a very simple IOT device to monitor and control a remote cabin. I have attempted to code a first program that will receive periodic data (hourly data) and I want to send that information to the Hologram cloud where I have a route defined to forward that to a local email address. I have a working python script that hourly sends the temperature data in the form of a topic/message of topic: cabin/hour message:{“time”: date and time string, “temp”: local temperature}. The code to receive this topic/message (attached below with credentials removed) will run the first time with a response code of “Message sent successfully” (and I receive the expected email with the data), then then next time the topic/message is sent (one hour later) I get a “Unknown response code” result and the message does not get sent to the Hologram cloud. Is there something that I am missing that I need to include in the code? I am using a NOVA SARA-R410M-02B with the latest developer python api.
-----python code --------
import time
import paho.mqtt.client as mqtt
from Hologram.HologramCloud import HologramCloud
import sys
credentials = {‘devicekey’: ‘myCredentialsRemoved’}
hologram = HologramCloud(dict(), network=‘cellular’)
Broker = ‘localhost’
when connecting to MQTT do this
def on_connect(node, userdata, flags, rc):
print("Connected with Result code "+str(rc)+’\n’)
def on_log(client, userdata, level, buf):
print("log: ", buf)
when receiving a mqtt message do this
def on_message(node, userdata, msg):
global hologram
message = str(msg.payload.decode(“utf-8”))
topic = str(msg.topic)
print("on message: “+ topic +”: "+message)
if topic == ‘cabin/furnace/state’:
response = hologram.sendMessage(message, topics=[topic])
print(hologram.getResultString(response))
if topic == 'cabin/hour':
response = hologram.sendMessage(message, topics = [topic])
print(hologram.getResultString(response) , ' Hourly Temperature ', message)
client = mqtt.Client()
client.on_connect = on_connect
client.on_log = on_log
client.on_message = on_message
client.connect(Broker, 1883, 60)
client.subscribe(‘cabin/#’)
client.loop_start()
result = hologram.network.connect()
if result == False:
print(“Failed to Connect to network”)
try:
while True:
time.sleep(1)
except KeyboardInterrupt as e:
print(‘closing socket …’)
hologram.closeReceiveSocket()
if not hologram.network.at_sockets_available:
hologram.network.disconnect()
sys.exit(e)
-----End python code -------