Saturday, December 13, 2014

AcuRite Weather Station, Raspberry Pi and a USB interface On the Web Part 4

Part 3 of this is here <link>

After I finished up changing how my various processes communicate in the last post, I got back to the weather station.  Frankly, it's no fun reading lines of text, it's much better to put things in guages or charts.  I took the easy way out, and just put the entire JSON string I created in the data data base as a string, not separate records.

No, there wasn't any technical reason for this, I just didn't want to think about how to organize the schema to handle it.  It does mean that I haven't done anything with the rain fall counter though.  This part of the station is a simple accumulator that counts rainfall in 0.01 inch increments.  We have to save the measurement periodically and calculate from the latest reading to have it make sense.  I'll get to that some time this winter, but I have the wind direction and speed, temperature, and humidity working just fine.

It took some experimentation to get the JSON string out of the database and converted into values in PHP though.  Take a look at the code involved:

$ws = timedQuerySingle(
 'select "json" from "weather";');
$db->close();
# The weather string is a pain, this is converting it, and
# since I can reuse variables and I'm tired of thinking up names
# I use the same name over and over again just to confuse
# anyone reading this.
# First, there an extra set of quotes around the string
$ws=substr($ws,1,strlen($ws)-2);
# Now I have to get rid of the \" that I had to use to put it in
# the database
$ws=str_replace("\\","",$ws);
#Now, convert the json into variables
$ws = json_decode($ws,true);

The entire string is surrounded by quotes and every single quote inside is 'escaped' with a backslash.  So, I cut out the middle between the quotes and then remove the backslashes.  This won't work if there's a backslash in the string, but I built the string and know what's in it.  Once I'm done with this, the various items in the string are available as a named array index. Here's how to refer to the four values temperature, wind speed, wind direction and humidity:

$ws["windSpeed"]["WS"]
$ws["windDirection"]["WD"]
$ws["humidity"]["H"]
$ws["temperature"]["T"]);

If you remember, I stored the time I recorded the reading with each item, so the times would be something like,

$ws["windDirection"]["t"]

for each measurement.  I don't know if I need it, but it's better to have it now while I'm figuring out what I want to keep.

To display it I added even more SteelSeries gauges to my display for the items.  I now looks like this:


Yes, it's a little busy, but I can glace at it and tell what's going on in a second; besides, it's mine, not some other piece of software that I have to mess with forever to get what I want.  I suspect it will get even more elaborate when I add in something for rainfall.  Heck, I may use one of the web authoring tools and really fancy up the display to impress people with, but this is adequate for taking a look at what it's like outside, and it's only a few seconds behind real time.  I'm actually pretty proud of it.

And yes, it's raining today; the humidity is usually around 12 or so.  The difference between the two temperatures is related to location.  The weather station is up on the roof, and the other temperature monitor is on a fence post at about 5 feet.  On sunny days I expect the temperature difference to be even larger.  As before, the code is available on github to grab and change to suit your needs.

Have fun.

Part 5 to this incredibly long series is here <link>.

4 comments:

  1. Love the weather station steel gauges. Only thing with these weather stations is that their temperature gauge is subject to heating. It's not like being in a stephenson screen, or even in the shade.

    ReplyDelete
    Replies
    1. That's true. I'll be watching the temperature and humidity for problems when the sun comes back. This particular device has a fan inside run by a solar cell to constantly move air while the sun is hitting it. The other temperature sensor I have is inside a stephenson screen, and has compared really well with the 'real' sensors around me. What I may do is totally ignore the temp sensor in the station and rely on my good old sensor out on the fence.

      The rain gauge has been right on the money. I lucked out an had a rain storm to test it.

      Delete
  2. Have you noticed that the wind speed reported in the two sub-types of the first report are generally not similar. I haven't figured it out yet, but I'm thinking one of them is more of a peak over the last x minutes (similar to the peak listed to the right of the current wind speed on the display). I'm getting 4.96 consistently from one and 7.44 consistently from the other. Although I have started to create a bit of a Frankenstein's monster of an existing weewx "driver" for another usb attached weather station and your sniffed parameters for the control transfer & decoding of the reports, and I may have mixed something up. I'm not at the device right now, so I can't tell for sure what the display is saying to compare with what I'm getting from USB. I wasted way too much time trying to get the parameters for PyUSB right to query the console before I realized that I'd set it back to "PC Connect mode 2" (or STANDALONE CONSOLE) a week or so ago.

    ReplyDelete
    Replies
    1. No I haven't. When I was watching it as it reported, it seemed to be pretty stable with the two values corresponding very closely. When I get some wind (mine has read 1 or 0 for two days now), I'll take a look. The leave aren't even rustling and the pool looks like glass.

      Delete