Project
Project: MQTT Publish
Eclipse Paho MQTT Python client library, which implements versions 3.1 and 3.1.1 of the MQTT protocol. Paho Mqtt
Publish sensor data through MQTT Protocol using test.mosquitto.org server under "IoT101/DataSensor" topic
Go to your git "TheIoTLearningInitiative" repository, under "InternetOfThings101" directory
root@board:~# cd TheIoTLearningInitiative/InternetOfThings101
root@board:~/TheIoTLearningInitiative/InternetOfThings101#
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# vi main.py
#!/usr/bin/python
import paho.mqtt.client as paho
import psutil
import signal
import sys
import time
from threading import Thread
def functionDataActuator():
print "Data Actuator"
def functionDataSensor():
netdata = psutil.net_io_counters()
data = netdata.packets_sent + netdata.packets_recv
return data
def functionDataSensorMqttOnPublish(mosq, obj, msg):
print "Data Sensor Mqtt Published!"
def functionDataSensorMqttPublish():
mqttclient = paho.Client()
mqttclient.on_publish = functionDataSensorMqttOnPublish
mqttclient.connect("test.mosquitto.org", 1883, 60)
while True:
data = functionDataSensor()
topic = "IoT101/DataSensor"
mqttclient.publish(topic, data)
time.sleep(1)
def functionSignalHandler(signal, frame):
sys.exit(0)
if __name__ == '__main__':
signal.signal(signal.SIGINT, functionSignalHandler)
threadmqttpublish = Thread(target=functionDataSensorMqttPublish)
threadmqttpublish.start()
while True:
print "Hello Internet of Things 101"
print "Data Sensor: %s " % functionDataSensor()
time.sleep(5)
# End of File
Execute main.py watching its output and listen for sensor data with mosquitto_sub app or any mqtt cellphone application using a test.mosquitto.org server susbcribing to "IoT101/DataSensor"
root@board:~# mosquitto_sub -h test.mosquitto.org -p 1883 -t IoT101/DataSensor
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# python main.py
Hello Internet of Things 101
Data Sensor: 5385
Data Sensor Mqtt Published!
Data Sensor Mqtt Published!
Data Sensor Mqtt Published!
Data Sensor Mqtt Published!
Data Sensor Mqtt Published!
Hello Internet of Things 101
Data Sensor: 5436
Data Sensor Mqtt Published!
Data Sensor Mqtt Published!
Data Sensor Mqtt Published!
Data Sensor Mqtt Published!
Data Sensor Mqtt Published!
Hello Internet of Things 101
Data Sensor: 5466
Data Sensor Mqtt Published!
^Z
[7]+ Stopped(SIGTSTP) python main.py
root@edison:~/TheIoTLearningInitiative/InternetOfThings101#
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git add main.py
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git commit -s
Internet of Things 101: 5.4.2 Protocols: Project: MQTT Publish
Publish sensor data through MQTT Protocol using test.mosquitto.org
server under "IoT101/DataSensor" topic
Signed-off-by: Name LastName <email@gmail.com>
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Your branch is up-to-date with 'origin/master'.
#
# Changes to be committed:
# modified: main.py
#
master 79c53c7] Internet of Things 101: 5.4.2 Protocols: Project: MQTT Publish
1 file changed, 19 insertions(+)
root@edison:~/TheIoTLearningInitiative/InternetOfThings101#
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git push
Username for 'https://github.com': xe1gyq
Password for 'https://xe1gyq@github.com':
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 1014 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To https://github.com/xe1gyq/TheIoTLearningInitiative.git
9a1b981..79c53c7 master -> master
root@edison:~/TheIoTLearningInitiative/InternetOfThings101#
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git show
commit 79c53c7bd039f77a3c895ef6e66d977648fa970c
Author: Name LastName <email@gmail.com>
Date: Sun May 1 13:23:20 2016 +0000
Internet of Things 101: 5.4.2 Protocols: Project: MQTT Publish
Publish sensor data through MQTT Protocol using test.mosquitto.org
server under "IoT101/DataSensor" topic
Signed-off-by: Name LastName <email@gmail.com>
diff --git a/InternetOfThings101/main.py b/InternetOfThings101/main.py
index f6cdffa..1b27944 100644
--- a/InternetOfThings101/main.py
+++ b/InternetOfThings101/main.py
@@ -1,10 +1,13 @@
#!/usr/bin/python
+import paho.mqtt.client as paho
import psutil
import signal
import sys
import time
+from threading import Thread
+
def functionDataActuator():
print "Data Actuator"
@@ -13,6 +16,19 @@ def functionDataSensor():
data = netdata.packets_sent + netdata.packets_recv
return data
+def functionDataSensorMqttOnPublish(mosq, obj, msg):
+ print "Data Sensor Mqtt Published!"
+
+def functionDataSensorMqttPublish():
+ mqttclient = paho.Client()
+ mqttclient.on_publish = functionDataSensorMqttOnPublish
+ mqttclient.connect("test.mosquitto.org", 1883, 60)
+ while True:
+ data = functionDataSensor()
+ topic = "IoT101/DataSensor"
+ mqttclient.publish(topic, data)
+ time.sleep(1)
+
def functionSignalHandler(signal, frame):
sys.exit(0)
@@ -20,6 +36,9 @@ if __name__ == '__main__':
signal.signal(signal.SIGINT, functionSignalHandler)
+ threadmqttpublish = Thread(target=functionDataSensorMqttPublish)
+ threadmqttpublish.start()
+
while True:
print "Hello Internet of Things 101"
print "Data Sensor: %s " % functionDataSensor()
(END)
Project: MQTT Subscribe
Subscribe to "IoT101/DataActuator" topic through MQTT Protocol using test.mosquitto.org server to change the status of Data Actuator
Go to your git "TheIoTLearningInitiative" repository, under "InternetOfThings101" directory
root@board:~# cd TheIoTLearningInitiative/InternetOfThings101
root@board:~/TheIoTLearningInitiative/InternetOfThings101#
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# vi main.py
#!/usr/bin/python
import paho.mqtt.client as paho
import psutil
import signal
import sys
import time
from threading import Thread
def functionDataActuator(status):
print "Data Actuator Status %s" % status
def functionDataActuatorMqttOnMessage(mosq, obj, msg):
print "Data Sensor Mqtt Subscribe Message!"
functionDataActuator(msg.payload)
def functionDataActuatorMqttSubscribe():
mqttclient = paho.Client()
mqttclient.on_message = functionDataActuatorMqttOnMessage
mqttclient.connect("test.mosquitto.org", 1883, 60)
mqttclient.subscribe("IoT101/DataActuator", 0)
while mqttclient.loop() == 0:
pass
def functionDataSensor():
netdata = psutil.net_io_counters()
data = netdata.packets_sent + netdata.packets_recv
return data
def functionDataSensorMqttOnPublish(mosq, obj, msg):
print "Data Sensor Mqtt Published!"
def functionDataSensorMqttPublish():
mqttclient = paho.Client()
mqttclient.on_publish = functionDataSensorMqttOnPublish
mqttclient.connect("test.mosquitto.org", 1883, 60)
while True:
data = functionDataSensor()
topic = "IoT101/DataSensor"
mqttclient.publish(topic, data)
time.sleep(1)
def functionSignalHandler(signal, frame):
sys.exit(0)
if __name__ == '__main__':
signal.signal(signal.SIGINT, functionSignalHandler)
threadmqttpublish = Thread(target=functionDataSensorMqttPublish)
threadmqttpublish.start()
threadmqttsubscribe = Thread(target=functionDataActuatorMqttSubscribe)
threadmqttsubscribe.start()
while True:
print "Hello Internet of Things 101"
print "Data Sensor: %s " % functionDataSensor()
time.sleep(5)
# End of File
Execute main.py watching its output and publish actuator data with mosquitto_pub app or any mqtt cellphone application using a test.mosquitto.org server publishing to "IoT101/DataActuator"
root@board:~# mosquitto_pub -h test.mosquitto.org -p 1883 -t IoT101/DataActuator -m On
root@board:~# mosquitto_pub -h test.mosquitto.org -p 1883 -t IoT101/DataActuator -m Off
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# python main.py Hello Internet of Things 101
Data Sensor: 10369
Data Sensor Mqtt Published!
Data Sensor Mqtt Published!
Data Sensor Mqtt Published!
Data Sensor Mqtt Published!
Data Sensor Mqtt Published!
Data Sensor Mqtt Subscribe Message!
Data Actuator Status On
Hello Internet of Things 101
Data Sensor: 10450
Data Sensor Mqtt Published!
Data Sensor Mqtt Published!
Data Sensor Mqtt Subscribe Message!
Data Actuator Status Off
Data Sensor Mqtt Published!
Data Sensor Mqtt Published!
Data Sensor Mqtt Published!
^Z[1] Killed python main.py
[2]+ Stopped(SIGTSTP) python main.py
root@edison:~/TheIoTLearningInitiative/InternetOfThings101#
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git add main.py
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git commit -s
Internet of Things 101: 5.4.2 Protocols: Project: MQTT Subscribe
Subscribe to "IoT101/DataActuator" topic through MQTT Protocol using
test.mosquitto.org server to change the status of Data Actuator
Signed-off-by: Name LastName <email@gmail.com>
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Your branch is up-to-date with 'origin/master'.
#
# Changes to be committed:
# modified: main.py
#
[master b8e396b] Internet of Things 101: 5.4.2 Protocols: Project: MQTT Subscribe
1 file changed, 17 insertions(+), 2 deletions(-)
root@edison:~/TheIoTLearningInitiative/InternetOfThings101#
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git push
Username for 'https://github.com': xe1gyq
Password for 'https://xe1gyq@github.com':
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 788 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
To https://github.com/xe1gyq/TheIoTLearningInitiative.git
ae41fba..b8e396b master -> master
root@edison:~/TheIoTLearningInitiative/InternetOfThings101#
root@edison:~/TheIoTLearningInitiative/InternetOfThings101#
commit b8e396b018a9e0c3ebd47f8ae3dc83159a32edd8
Author: Abraham Arce <xe1gyq@gmail.com>
Date: Sun May 1 15:04:54 2016 +0000
Internet of Things 101: 5.4.2 Protocols: Project: MQTT Subscribe
Subscribe to "IoT101/DataActuator" topic through MQTT Protocol using
test.mosquitto.org server to change the status of Data Actuator
Signed-off-by: Abraham Arce <xe1gyq@gmail.com>
diff --git a/InternetOfThings101/main.py b/InternetOfThings101/main.py
index 9c2af2d..2e5cc10 100644
--- a/InternetOfThings101/main.py
+++ b/InternetOfThings101/main.py
@@ -8,8 +8,20 @@ import time
from threading import Thread
-def functionDataActuator():
- print "Data Actuator"
+def functionDataActuator(status):
+ print "Data Actuator Status %s" % status
+
+def functionDataActuatorMqttOnMessage(mosq, obj, msg):
+ print "Data Sensor Mqtt Subscribe Message!"
+ functionDataActuator(msg.payload)
+
+def functionDataActuatorMqttSubscribe():
+ mqttclient = paho.Client()
+ mqttclient.on_message = functionDataActuatorMqttOnMessage
+ mqttclient.connect("test.mosquitto.org", 1883, 60)
+ mqttclient.subscribe("IoT101/DataActuator", 0)
+ while mqttclient.loop() == 0:
+ pass
def functionDataSensor():
netdata = psutil.net_io_counters()
@@ -39,6 +51,9 @@ if __name__ == '__main__':
threadmqttpublish = Thread(target=functionDataSensorMqttPublish)
threadmqttpublish.start()
+ threadmqttsubscribe = Thread(target=functionDataActuatorMqttSubscribe)
+ threadmqttsubscribe.start()
+
while True:
print "Hello Internet of Things 101"
print "Data Sensor: %s " % functionDataSensor()
(END)
Last updated