Last active
December 29, 2015 12:59
-
-
Save andypiper/7674224 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # Python sample to publish data to Aercloud | |
| # uses mosquitto Python API | |
| # see https://aeriscom.jira.com/wiki/display/DOCS/API+Reference#APIReference-MQTT | |
| # Andy Piper @andypiper November 2013 | |
| import json | |
| import mosquitto | |
| # define callbacks | |
| def on_connect(mosq, obj, rc): | |
| if rc == 0: | |
| print("Connected successfully.") | |
| else: | |
| print("Cannot connect, rc: "+str(rc)) | |
| def on_disconnect(mosq, obj, rc): | |
| print("Disconnected.") | |
| def on_publish(mosq, obj, mid): | |
| print("Message ID "+str(mid)+" published.") | |
| # define variables | |
| broker = "mqtt.aercloud.aeris.com" | |
| port = 1883 | |
| user = "your-numeric-ID" # Aercloud User ID | |
| pw = "your-API-key" # Aercloud API Key | |
| sclID = "your-device" # Aercloud sclID / deviceID (must be provisioned) | |
| container = "your-container" # Aercloud Container | |
| # create MQTT client instance | |
| mqttc = mosquitto.Mosquitto(sclID) | |
| mqttc.username_pw_set(user, pw) # must be user ID + API key | |
| # set callbacks to be triggered | |
| mqttc.on_connect = on_connect | |
| mqttc.on_publish = on_publish | |
| mqttc.on_disconnect = on_disconnect | |
| # connect to broker | |
| # NB call loop() regularly to enable callbacks to be triggered | |
| mqttc.connect(broker, port, 60) | |
| mqttc.loop() | |
| # Add Olympia as a location | |
| msg=json.dumps({ | |
| "Accuracy":"10", | |
| "Latitude":"51.4976", | |
| "LocationTimeStamp":"1366361979315", | |
| "Longitude":"-0.21073" | |
| }) | |
| print("Message data to be published: " + msg) | |
| mqttc.publish(user + "/scls/" + sclID + "/containers/" + container + "/contentInstances", msg) | |
| mqttc.loop() | |
| mqttc.disconnect() | |
| mqttc.loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment