Willkommen bei bytebang » The blog about all and nothing » How to publish weatherdata via MQTT

How to publish weatherdata via MQTT

Aug 20 2016

The Problem

I am running an off-site weather station which is connected to meteohub. This enables me to create beautiful graphs and charts, but i wanted to display the the data on my arduino yun at home. So i had to find a way how to transmit this data via the Internet,

The Solution

Since it did not want to create a VPN tunnel I choose a solution where the weatherstation (to be more precise meteohub) is actively pushing the data to the Internet using MQTT. From there on everybody can fetch it and use it for his/her own purpose.

Step 1: Fetch the data

The setup is really simple. If you take a deep-dive into the documentation of meteohub, then you discover that it exposes all of its data (current weather data as well as aggegated data) via the network port 5558. So if you run netcat (the swiss army knife of TCP/IP) to read out this data the you will get a list of all the values.

netcat meteohub.my.home.tld 5558

Step 2: Filter the data

Since i am only interested in the most actual data (and not in the 100 other ones) i had to filter the data. This can be done via grep. Just have a look at the examples.

Step 3: Publish them via MQTT

The first step is to make sure that we can publis the data. For this purpose i am using mosquitto_pub which is a simple mqtt client that will publish a message on a single topic and exit.

So if we put it all together then we end up with a command like this:

netcat meteohub.my.home.tld 5558 | grep actual | awk '{printf("mosquitto_pub -h iot.eclipse.org -t myweatherstation/%s -m \"%s\"\n",$1, $2)}' | sh

Well - this one-liner does all that we want, by combining all the tools in the commandline.

  1. It fetches all available data from my meteohub station via netcat
  2. It filters out all messages but the ones which are containing the string actual
  3. it reassembles a command using awk
  4. it passes this command to the shell which executes it.
This script uses a public mqtt server (iot.exclise.org). Please be sure to support this project (either by money or by code) or set up your own mqtt server.

Get Social


(c) 2024, by bytebang e.U. - Impressum - Datenschutz / Nutzungsbedingungen
-