Monday, July 14, 2014

Weather Station v0.1

The winds in Las Cruces can get pretty fierce and I have lost some shingles off the roof, so I decided to measure the "local" wind speed.  I wanted to learn a little about the power consumption of small Wi-Fi connected devices, so rather than buy an off the shelf unit, I decided to build something from a kit.

The article Weather Station Wirelessly Connected to Wunderground was an excellent starting point and so I sourced the parts from Sparkfun.  It looked like the Electric Imp had enough input capacity to do what I needed, so rather than use an Arduino to run the weather software and the Imp as a dumb modem, I attempted to use the Imp in both roles.

The Sparkfun Weather Shield "is an easy to use Arduino shield that grants you access to barometric pressure, relative humidity, luminosity and temperature. There are also connections on this shield to optional sensors such as wind speed, direction, rain gauge and GPS for location and super accurate timing"

One potential mapping of these variables to the Imp is:

Shield variable Type Imp Pin
Wind speed DI 1
Wind direction AI 2
Rain gauge DI 5
Luminosity AI 7
Battery voltage AI unused
Barometric pressure I2C 8, 9
Humidity/Temperature I2C 8, 9

Looking at the schematic for the Weather Shield, it is seen that the shield variables swing 0-5V, whereas the Imp is a 3.3V device.  Now there is a bus transceiver chip, the 74LVC245, to interface the digital inputs and an op amp/divider will fix the analog inputs.  But it was then I realized this speedbump was a wonderful opportunity to give me pause, get something working ... a version 0.1 of a weather station (and thus the title of this blog entry).

So I started with the following signal allocation:
Variable Type Imp Pin
Wind speed DI 5
Battery voltage AI 7
Temperature AI 9

A small piece of perfboard, served to mount the Imp and handful of components.


v0.1 sensor board + Imp



The Imp was powered by a large, sealed lead acid battery, a UB645 chosen for its price and durabilty, rather than being the smallest battery for the job. The wind speed was measured using an input, configured as "DIGITAL_IN_PULLUP", which allowed a handler function to be called every time the windspeed reed switch closed.  The battery voltage is measured via a simple divider off Vin.  Note that Vin is after the reverse polarity protection MOSFET on the battery input, so 0.6V lower than the actual battery voltage.

For only a few lines, the Squirrel code is pretty easy to get going.  The raw data is serialized to the ElectricImp server, where the communication to Wunderground takes place. As noted in the Sparkfun blog entry announcing their data service, Wunderground does not allow custom fields in their data sets, and I wanted to track battery voltage.  So I added code at the ElectricImp server to push data to a public stream on data.sparkfun, to record weather + battery voltage and RSSI ... nothing fancy, but dead simple.

Note, to form the query URL to Wunderground and Sparkfun, it is necessary to concatenate the strings in Squirrel for the various key/value pairs.  Rather than using the "+" operator, which generates a lot of garbage, consider using a blob as write buffer:

    buffer <- blob="" p="">    ....
    buffer.seek(0, 'b');
    buffer.writestring(sfDataSparkfunStr);
    buffer.writestring(sfPublicKey);
    buffer.writestring("?");
    buffer.writestring(sfPrivateKeyPrefix);
    buffer.writestring(sfPrivateKey);
    ....
    ....
    local len = buffer.tell();
    buffer.seek(0, 'b');
    local url = buffer.readstring(len);


Now that I have been collecting data for a few days, time to look at interfacing the Weather Shield proper to the Imp.