Publishing data by MQTT

After installing Mosquitto on Omega2+ in the blog post Using MQTT on Onion Omega2+ the subscription of messages was shown. Here I will send data to the MQTT broker to subscribe and access them from anywhere in the world.

For data generation I use the script wunderweather.phy again. The script mqtt.sh reads the requested data (temperature & humidity) and publishes these to my MQTT broker. Look for the files at Github.

mqtt.sh:

#!/bin/sh

#MQTT
BROKER="m20.cloudmqtt.com"
BRUSER="rawyjpid"
BRPASSW="ah52k3gjd8JS"
BRPORT=12394
TEMPTOP="OMEGA2/WU/temperature"
HUMITOP="OMEGA2/WU/humidity"

echo "Send data to MQTT Broker"

DATE="$(date +"%d-%m-%Y")"
read TEMP < /home/TEMP 
echo "Temperature = $TEMP *C"
read HUMI < /home/HUMI 
echo "Rel. Humidity = $HUMI %"

mosquitto_pub -h $BROKER -u $BRUSER -P $BRPASSW -p $BRPORT -t $TEMPTOP -m $TEMP
mosquitto_pub -h $BROKER -u $BRUSER -P $BRPASSW -p $BRPORT -t $HUMITOP -m $HUMI

The screenshot of my MQTT client MyMQTT shows messages from two publishers. WU labels the data received from Weather Underground requestet here and ASH2200 is a sensor placed outside of my house. The difference between the measured values is due to different measuring locations.

screenshot_20170202-103009

Advertisements
Publishing data by MQTT

Get weather data for your location

Find more about Weather in Altendorf, SW
Click for weather forecast

If you have no own weather station but are interested in local weather data then weather data from Weather Underground  (wunderground) can help to close this gap.

I built three small scripts to ask wunderground for data, send the to the thingspeak server for visualization and cover both scripts. You will find them on Github for download.

wunderweather.py asks Wunderground for locale weather data. Local means here Altendorf in Switzerland.

#!/usr/bin/python

# Reading weather data from wunderground network

import urllib2
import json

f = urllib2.urlopen('http://api.wunderground.com/api/a50aba1a6119706f/geolookup/conditions/q/Switzerland/Altendorf.json')
json_string = f.read()
parsed_json = json.loads(json_string)

location = parsed_json['location']['city']
temp_c = parsed_json['current_observation']['temp_c']
rel_hum = parsed_json['current_observation']['relative_humidity']
weather = parsed_json['current_observation']['weather']
station = parsed_json['current_observation']['station_id']
updated = parsed_json['current_observation']['observation_time_rfc822']
print("Current temperature in %s is: %s *C" % (location, temp_c))
print("Current relative humidity is: %s %%" % (rel_hum))
print("Weather is %s " % (weather))
print("Weather station is %s" % (station))
print("Last updated: %s" % (updated))

f.close()

f = open("TEMP","w")
f.write(str(temp_c))
f.close()

f = open("HUMI","w")
f.write(str(rel_hum))
f.close()

thingspeak.sh sends temperature und humidity to the Thingspeak server:

#!/bin/sh

echo "Send data to Thingspeak Server"

#Thingspeak
 api_key='CRPX6X538QZ1DZHY'

DATE=date
read TEMP < /home/TEMP
echo "Temperature = $TEMP *C"
read HUMI < /home/HUMI     echo "Rel. Humidity = $HUMI %"     curl --data \       "api_key=$api_key&field1=$TEMP&field2=$HUMI&field3=$DATE" \       https://api.thingspeak.com/update > log 2>&1

As you can see from both scripts you need access data for the APIs. Therefore you have to register on both services.

The last script weather.sh covers both calls. That’s all:

#!/bin/sh

/home/wunderweather.py
/home/thingspeak.sh
echo "Done."

Temperature Graph

Humidity Graph

 

Get weather data for your location