|
| 1 | +# Subscribing to I3 Marketplace data |
| 2 | + |
| 3 | +<b>This code walkthrough explains how to subscribe to AstroPiOTA data published at the I3 Marketplace.</b> AstroPiOTA publishes environment data every 30 minutes. |
| 4 | + |
| 5 | +## Setting up your subscription |
| 6 | +Follow the [Connecting an IoT Device to the I3 Marketplace](https://github.com/NelsonPython/Connect_IoT_Device_to_I3) guide and subscribe to Your AstroPiOTA. The guide also contains the I3 Marketplace IP address and port. |
| 7 | + |
| 8 | +## Subscribing to data |
| 9 | +``` |
| 10 | +#!/usr/bin/python |
| 11 | +
|
| 12 | +""" |
| 13 | +Purpose: subscribing to AstroPiOTA data from I3 Marketplace |
| 14 | +""" |
| 15 | +``` |
| 16 | + |
| 17 | +Import the [Eclipse Paho MQTT Python client library](https://pypi.org/project/paho-mqtt/) so you can subscribe to your data |
| 18 | +``` |
| 19 | +import paho.mqtt.client as mqtt |
| 20 | +``` |
| 21 | +In order for your data to be meaningful, you must report the time it was sensed. Import time and datetime libraries |
| 22 | + |
| 23 | +``` |
| 24 | +import time |
| 25 | +import datetime |
| 26 | +``` |
| 27 | +Data is passed using a json format so import json libraries |
| 28 | +``` |
| 29 | +import json |
| 30 | +``` |
| 31 | +Import the Iota libraries so you can send a transaction to the Tangle |
| 32 | +``` |
| 33 | +from iota import Iota |
| 34 | +from iota import ProposedTransaction |
| 35 | +from iota import Address |
| 36 | +from iota import Tag |
| 37 | +from iota import TryteString |
| 38 | +``` |
| 39 | +### on_connect function |
| 40 | + |
| 41 | +This function connects to the broker and prints the status of the connection |
| 42 | +``` |
| 43 | +def on_connect(client, userdata, flags, rc): |
| 44 | + """ reporting IoT device connection """ |
| 45 | +
|
| 46 | + try: |
| 47 | + m = "Connected flags " + str(flags) + "\nResult code " + str(rc) + "\nClient_id " + str(client) |
| 48 | + print(m) |
| 49 | + print("\n") |
| 50 | + except e: |
| 51 | + print("Hmmm I couldn't report the IoT connection: ", e) |
| 52 | +``` |
| 53 | +### on_message function |
| 54 | + |
| 55 | +This function receives data, prints it, stores it in csv format in the AstroPiOTA.csv file, and stores it on the IOTA Tangle |
| 56 | + |
| 57 | +``` |
| 58 | +def on_message(client, userdata, msg): |
| 59 | + """ receiving data""" |
| 60 | +
|
| 61 | + try: |
| 62 | + sensors = msg.payload |
| 63 | + sensors = json.loads(sensors.decode('utf-8')) |
| 64 | + except e: |
| 65 | + print("Check the message: ",e) |
| 66 | +
|
| 67 | + logfile = open("AstroPiOTA.csv","a") |
| 68 | + print(str(sensors["timestamp"]),",",str(sensors["lng"]),",",\ |
| 69 | + str(sensors["lat"]),",",str(sensors["device_name"]),",",str(sensors["temperature"]),",",\ |
| 70 | + str(sensors["humidity"]),",",str(sensors["pressure"]),",",str(sensors["pitch"]),",",\ |
| 71 | + str(sensors["roll"]),",",str(sensors["yaw"]),",",str(sensors["x"]),",",\ |
| 72 | + str(sensors["y"]),",",str(sensors["z"]),",",str(sensors["device_owner"]),",",str(sensors["city"]),file=logfile) |
| 73 | + logfile.close() |
| 74 | +``` |
| 75 | +Print the data to the screen |
| 76 | +``` |
| 77 | + # this prints the AstroPiOTA data message |
| 78 | + print("\nTimestamp: ", str(sensors["timestamp"])) |
| 79 | + print("Device: ", sensors["device_name"]) |
| 80 | + print("Device owner email: ", sensors["device_owner"]) |
| 81 | + print("Device location: ", sensors["city"], " at longitude: ", sensors["lng"], " and latitude: ", sensors["lat"]) |
| 82 | +
|
| 83 | + print("Temperature: ", sensors["temperature"]) |
| 84 | + print("Humidity: ", sensors["humidity"]) |
| 85 | + print("Pressure: ", sensors["pressure"]) |
| 86 | +
|
| 87 | + print("Pitch: ", sensors["pitch"]) |
| 88 | + print("Roll: ", sensors["roll"]) |
| 89 | + print("Yaw: ", sensors["yaw"]) |
| 90 | +
|
| 91 | + print("Accelerometer x: ", sensors["x"]) |
| 92 | + print("Accelerometer y: ", sensors["y"]) |
| 93 | + print("Accelerometer z: ", sensors["z"]) |
| 94 | +``` |
| 95 | +Store the data in the Tangle |
| 96 | +``` |
| 97 | + api = Iota('https://nodes.devnet.iota.org:443') |
| 98 | + address = '999999999999999999999999999999999999999999999999999999999999999999999999999999999' |
| 99 | + tx = ProposedTransaction( |
| 100 | + address=Address(address), |
| 101 | + #message=TryteString.from_unicode(sensors), |
| 102 | + message=TryteString.from_unicode(json.dumps(sensors)), |
| 103 | + tag=Tag('YOURASTROPIOTATAG'), |
| 104 | + value=0 |
| 105 | + ) |
| 106 | + try: |
| 107 | + tx = api.prepare_transfer(transfers=[tx]) |
| 108 | + except: |
| 109 | + print("PREPARE EXCEPTION",tx) |
| 110 | + try: |
| 111 | + result = api.send_trytes(tx['trytes'], depth=3, min_weight_magnitude=9) |
| 112 | + except: |
| 113 | + print("EXCEPTION", result) |
| 114 | +``` |
| 115 | +### test_sub() function |
| 116 | +This is the main loop. Enter the topic you subscribed to along with your account and password |
| 117 | + |
| 118 | +``` |
| 119 | +def test_sub(): |
| 120 | +
|
| 121 | + topic = "YOUR ASTROPIOTA" |
| 122 | + account = 'YOUR-ACCOUNT' |
| 123 | + pw = 'YOUR-PASSWORD' |
| 124 | +``` |
| 125 | +Connect to the broker |
| 126 | +``` |
| 127 | + sub_client = mqtt.Client(account) |
| 128 | + sub_client.on_connect = on_connect |
| 129 | + sub_client.on_message = on_message |
| 130 | + sub_client.username_pw_set(account, pw) |
| 131 | + sub_client.connect('IP ADDRESS', PORT, 60) #connect to broker |
| 132 | + sub_client.subscribe(topic) |
| 133 | +``` |
| 134 | +This script will listen until it is interrupted. |
| 135 | +``` |
| 136 | + rc = sub_client.loop_forever() |
| 137 | + time.sleep(1) |
| 138 | + print("Return code ", rc) |
| 139 | +``` |
| 140 | +Test_sub is a loop. It can be stopped using the keyboard interrupt, `ctrl-c`. |
| 141 | +``` |
| 142 | +if __name__ == '__main__': |
| 143 | + try: |
| 144 | + test_sub() |
| 145 | + except KeyboardInterrupt: |
| 146 | + rc = sub_client.loop_stop() |
| 147 | + print("\nI'm stopping now") |
| 148 | +``` |
| 149 | +### Sample output |
| 150 | + |
| 151 | +<img src="images\Screenshot from 2019-08-13 19-58-21.png" /> |
0 commit comments