I've already written the code and this blog is about how to use it. In the next blog posting, I'll talk about how the code actually works.
Goal: create a virtual Web thing to get weather data into the Things Gateway for use in rules. Specifically, make a rule that turns a green light on when the wind speed is high enough to fly a kite.
|Item||What's it for?||Where I got it|
|an RPi running the Things Gateway||It's our target to have the weather station provide values to the Things Gateway||General Download & Install Instructions|
or see my own install instructions:
General Install & Zigbee setup,
Philip Hue setup,
IKEA TRÅDFRI setup,
|A laptop or desktop PC||the machine to run the Virtual Weather Station. You can use the RPi itself.||My examples will be for a Linux machine|
|a couple things set up on the Things Gateway to control||this could be bulbs or switches||I'm using Aeotec Smart Switches to run red and green LED bulbs.|
|the webthing and configman Python3 packages||these are libraries used by the Virtual Weather Station||see the pip install directions below|
|a clone of the pywot github repository||it is where the the Virtual Weather Station code lives||see the git clone directions below|
|a developer key for online weather data||this gives you the ability to download data from Weather Underground||it's free from Weather Underground|
Step 1: Download and install the configman and webthing Python 3 packages. Clone the pywot github repository in a local directory appropriate for software development. While this can be done directly on the RPi, I'm choosing to use my Linux workstation. I like its software development environment better.
$ sudo pip3 install configman
$ sudo pip3 install webthing
$ git clone https://github.com/twobraids/pywot.git
$ cd pywot
$ export PYTHONPATH=$PYTHONPATH:$PWD
$ cd demo
So what is configman ?
This is an obscure library for configuration that I wrote years and years ago. I continue to use it because it is really handy. It combines command line, config files, the environment or anything conforming to the abstract type collections.Mapping to universally manage program configuration. Configuration requirements can be spread across classes and then used for dynamic loading and dependency injection. For more information, see my slides for my PyOhio 2014 talk: Configman.
What is webthing?
webthing is a Mozilla package for Python 3, that implements the Web Things api. It provides a set of classes that represent devices and their properties, giving them an implementation that can be controlled over an HTTP connection.
What is pywot?
Step 2: In the …/pywot/demo directory, there are several example files. virtual_weather_station.py is our focus today. In this posting, we're just going to run it, then we'll tear it apart and analyze it in the next posting.
Get a developers account for Weather Underground. Take note of your API key that they assign to you. You'll need this in the next step.
Step 3: Using your WU API key, your city and state, run the program like this:
$ ./virtual_weather_station.py -K YOUR_WU_API_KEY --city_name=Missoula --state_code=MT
Step 4: We're going to assume that there are two light bulbs already configured and named: Red, Green. Add the virtual weather station to the Things Gateway. by pressing the "+" key.
Sometimes, I've noticed that the Things Gateway doesn't immediately find my Virtual Weather Station. I've not nailed it down as to why, but something about mDNS on my network can be very slow to update - sometimes up to ten minutes. In this case, you don't have to wait, just press "Add by URL..." and then enter the IP address of the machine running the Virtual Weather Station with this URL template: "http://IP_ADDRESS:8888"
Step 5: The Virtual Weather Station is now fetching weather data every five minutes (as controlled by the configuration value called seconds_between_polling, you can change that on the command line) . The Things Gateway should have that data immediately: press the "splat" on the "THING" icon for the weather station:
Step 6: Now we can make a rule to turn on the "Green" light whenever the wind speed exceeds the minimum rated speed for our kite.
Select RULES from the drop down menu. Drag the Weather Station up into the top half of the screen; select "Wind Speed" from the drop down box; change the "<" to ">"; use the up/down buttons to set the minimum wind speed threshold. I'm choosing 5.
That should be about it.
Remember that making a rule implies the creation of a converse rule. The rule that I made above says the Green light should come on when the wind speed is greater than 5mph. The converse rule says that wind speeds below 5mph, the light will go out.
If the wind speed was greater than five at the moment that the rule was created, there may be some counterintuitive behavior. It appears that rules aren't applied immediately as they're created. They trigger on an "event" that happens when a property changes value. If the wind was greater than 5mph when the rule was created, the rule didn't yet exist when the "event" happened. The kite light will still work once the wind speed changes again at the next five minute polling point. Be patient.
That created a config file called: ./config.ini
$ ./virtual_weather_station.py -K YOUR_WU_API_KEY --admin.dump_conf=config.ini
Open up ./config.ini in an editor and uncomment the line that has your WU API key. Save the file. You can specify the the config file on the command line when you run the Virtual Weather Station. Any of the parameters can be loaded from the ini file.
Still too much typing? Instead of the config file, you could just set any/all of the parameters as environment variables:
$ ./virtual_weather_station.py -admin.conf=config.ini --city_name=Missoula --state_code=MT
In my next blog post, I'm going to explain the code that runs the Virtual Weather Station in great detail.