Attempting to create gateway between Nova and MQTT, hologram events not firing correctly.


#1

I am trying to create a python script that uses events to receive data and sms messages and place them onto my local MQTT bus. I have the MQTT events working, but the Hologram events do not seem to be able to work in the way I expect. SMS message receive events do not fire unless I continuously call .enableSMS in a loop, and data message from the cloud never arrive. My code is below, I think I am missing something simple to make the Hologram events fire correctly.

import time
import paho.mqtt.client as mqtt
from Hologram.HologramCloud import HologramCloud
import sys

credentials = {'devicekey': 'XXXXXXXX'}
hologram = HologramCloud(credentials, network='cellular', authentication_type='csrpsk')

Broker = "localhost"
sub_topic_data = "hologram/outbound/data"
pub_topic_data = "hologram/inbound/data"
sub_topic_sms = "hologram/outbound/sms"
pub_topic_sms = "hologram/inbound/sms"
destination_number = "+1NPANNNSSSS"

# when connecting to mqtt do this;
def on_connect(node, userdata, flags, rc):
    print "Connected with result code "+str(rc)
    client.subscribe(sub_topic_data)
    client.subscribe(sub_topic_sms)

# when receiving a mqtt message do this;
def on_message(node, userdata, msg):
    response = 0
    print "on_message entered"
    message = str(msg.payload)
    print msg.topic+" "+message
    if msg.topic == sub_topic_data:
       print "data message"
       try:
           response = hologram.sendMessage(message, topics=[msg.topic, sub_topic_data])
       except:
           print "Error in sendMessage"
       print hologram.getResultString(response)
    elif msg.topic == sub_topic_sms:
       print "sms message"
       try:
           response = hologram.sendSMS(destination_number, message)
       except:
           print "Error in sendSMS"
       print hologram.getResultString(response)
    else:
       print "Invalid message topic received"

def sayHelloReceivedAndPopMessage():
  print "hello! I received an cloud message (via event)!"
  recv = hologram.popReceivedMessage()
  if recv is not None:
     print recv
     client.publish(pub_topic_data, recv)


def sayHelloReceivedSMSAndPopMessage():
  print "hello! I received an SMS message (via event)!"
  recv = hologram.popReceivedSMS()
  if recv is not None:
     print recv.message
     client.publish(pub_topic_sms, recv.message)

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(Broker, 1883, 60)
client.loop_start()

hologram.event.subscribe('sms.received', sayHelloReceivedSMSAndPopMessage)
print 'subscribed to receive SMS message events'
hologram.event.subscribe('message.received', sayHelloReceivedAndPopMessage)
print 'subscribed to receive cloud message events'
hologram.openReceiveSocket()
print 'Ready to receive data...'
hologram.enableSMS()
print 'Ready to receive SMS...'

try:
    while True:
        time.sleep(5)
        print 'looping...'
        hologram.enableSMS()

except KeyboardInterrupt as e:
    print 'Closing socket...'
    hologram.closeReceiveSocket()

    if not hologram.network.at_sockets_available:
        hologram.network.disconnect()

    sys.exit(e)

#2

Is no one using hologram events like this?


closed #3

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