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 reads the requested data (temperature & humidity) and publishes these to my MQTT broker. Look for the files at Github.



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.


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. asks Wunderground for locale weather data. Local means here Altendorf in Switzerland.


# Reading weather data from wunderground network

import urllib2
import json

f = urllib2.urlopen('')
json_string =
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 = open("TEMP","w")

f = open("HUMI","w")
f.close() sends temperature und humidity to the Thingspeak server:


echo "Send data to Thingspeak Server"


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" \ > 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 covers both calls. That’s all:


echo "Done."

Temperature Graph

Humidity Graph


Get weather data for your location