Skip to content

Instantly share code, notes, and snippets.

@dmarlow
Created July 22, 2014 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmarlow/4784d2080db9c7a51590 to your computer and use it in GitHub Desktop.
Save dmarlow/4784d2080db9c7a51590 to your computer and use it in GitHub Desktop.
SMS2MQTT Python
import mosquitto, json
client = mosquitto.Mosquitto("weather-app")
# callbacks
def on_connect(mosq, obj, rc):
print("rc: " + str(rc))
def on_message(mosq, obj, msg):
m = msg.payload.decode("utf-8")
print(msg.topic + " " + str(msg.qos) + " " + m)
obj = json.loads(m)
client.publish("weather-app/reply", (json.dumps({"cmdId": obj["cmdId"], "body": obj["body"]})), 2)
def on_subscribe(mosq, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))
# Register callbacks
client.on_message = on_message
client.on_connect = on_connect
client.on_subscribe = on_subscribe
# Connect
client.username_pw_set("weather_user1","$ecr3t")
client.connect("q.smscmd.net", 1883)
# Start subscribe, with QoS level 2
client.subscribe("weather-app/cmds", 2)
# Wait for error
rc = 0
while rc == 0:
rc = client.loop()
print("rc: " + str(rc))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment