Project
Get weather data through Weather API using Pywapi Python Library.
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 pywapi
import signal
import sys
import time
from threading import Thread
def functionApiWeather():
data = pywapi.get_weather_from_weather_com('MXJO0043', 'metric')
message = data['location']['name']
message = message + ", Temperature " + data['current_conditions']['temperature'] + " C"
message = message + ", Atmospheric Pressure " + data['current_conditions']['barometer']['reading'][:-3] + " mbar"
return message
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()
print "API Weather: %s " % functionApiWeather()
time.sleep(5)
# End of File
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# python main.py
Hello Internet of Things 101
Data Sensor: 11513
Data Sensor Mqtt Published!
API Weather: Guadalajara, JO, Mexico, Temperature 18 C, Atmospheric Pressure 842 mbar
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: 11617
API Weather: Guadalajara, JO, Mexico, Temperature 18 C, Atmospheric Pressure 842 mbar
Data Sensor Mqtt Published!
^Z
[6]+ 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.5.1 API: Project: Weather
Get weather data through Weather API using Pywapi Python Library
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 f16fc1a] Internet of Things 101: 5.5.1 API: Project: Weather
1 file changed, 9 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), 790 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
To https://github.com/xe1gyq/TheIoTLearningInitiative.git
b8e396b..f16fc1a master -> master
root@edison:~/TheIoTLearningInitiative/InternetOfThings101#
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git show
commit f16fc1aeb5239032aa69d8711961de3c0f155ddd
Author: Abraham Arce <xe1gyq@gmail.com>
Date: Sun May 1 15:32:04 2016 +0000
Internet of Things 101: 5.5.1 API: Project: Weather
Get weather data through Weather API using Pywapi Python Library
Signed-off-by: Abraham Arce <xe1gyq@gmail.com>
diff --git a/InternetOfThings101/main.py b/InternetOfThings101/main.py
index 2e5cc10..bac4ae3 100644
--- a/InternetOfThings101/main.py
+++ b/InternetOfThings101/main.py
@@ -2,12 +2,20 @@
import paho.mqtt.client as paho
import psutil
+import pywapi
import signal
import sys
import time
from threading import Thread
+def functionApiWeather():
+ data = pywapi.get_weather_from_weather_com('MXJO0043', 'metric')
+ message = data['location']['name']
+ message = message + ", Temperature " + data['current_conditions']['temperature'] + " C"
+ message = message + ", Atmospheric Pressure " + data['current_conditions']['barometer'+['reading'][:-3] + " mbar"
+ return message
+
def functionDataActuator(status):
print "Data Actuator Status %s" % status
@@ -57,6 +65,7 @@ if __name__ == '__main__':
while True:
print "Hello Internet of Things 101"
print "Data Sensor: %s " % functionDataSensor()
+ print "API Weather: %s " % functionApiWeather()
time.sleep(5)
# End of File
(END)
Last updated