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);
$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,
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.
Part 5 to this incredibly long series is here <link>.