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


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)

# 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"
           response = hologram.sendMessage(message, topics=[msg.topic, sub_topic_data])
           print "Error in sendMessage"
       print hologram.getResultString(response)
    elif msg.topic == sub_topic_sms:
       print "sms message"
           response = hologram.sendSMS(destination_number, message)
           print "Error in sendSMS"
       print hologram.getResultString(response)
       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)

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'
print 'Ready to receive data...'
print 'Ready to receive SMS...'

    while True:
        print 'looping...'

except KeyboardInterrupt as e:
    print 'Closing socket...'

    if not



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.