Created
July 22, 2014 17:39
-
-
Save dmarlow/4784d2080db9c7a51590 to your computer and use it in GitHub Desktop.
SMS2MQTT Python
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 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