Overview
I’ve been using a PlantSmart for a few years to gather environmental data in various spots around my yard. It’s pretty good, but it doesn’t give me access to the raw data. That, and it’s not wireless! So, this weekend, I added a photo resistor to a Spark and put them side-by-side on my back deck for two days.
I polled the Core every minute and recorded the JSON output into a text file for later parsing. After manually removing some of the bad lines, I was able to parse the JSON into a CSV format using PHP. Once I had the data in CSV format, I imported it into a Google Spreadsheet to graph the data. After intense visual analysis (or a quick 2-second look), I realized that it was pretty much a 1-to-1 match.
The Setup
The electronic components of the setup is a pretty straightforward voltage divider setup with a photo resistor. I don’t have any fancy schematics off-hand, but the Using a Photocell article on Adafruit can help you out.
Here’s a picture of my setup. The photo resistor and 10K resistor were bent a little to squish them into a small tupperware container.
To poll the Core, I set up a cron job (scheduled task) on a Linux “box” at home. You can use your own computer at home, a server (physical or virtual) on the Internet, or even @kareem613’s atomiot.com project. There are many other online polling and graphing options available, but I’m a sucker for raw data at unnecessary intervals, so I did all this manually.
Once I stopped the polling, I used my PHP parser script to grab the light readings and timestamp and output them to a CSV format. I forgot to log the timestamp for each call, so I used the coreInfo.last_heard
timestamp instead. It’s probably not the best case scenario, but I wasn’t going to throw away 2 days of data and start over again!
The Code
Spark Core
uint8_t snsLight;
void setup() {
Spark.variable("snsLight", &snsLight, INT);
pinMode(A0, INPUT);
}
void loop() {
snsLight = getLight();
}
int getLight() {
// This converts the analog readings into percentage readings (from 0 through 4095 to 0 through 100)
return map(analogRead(A0), 0, 4095, 0, 100);
}```
#### Polling Script
This is called once per minute by a Linux cron job and appended to a `log.json` file.
#!/bin/sh
echo curl https://api.spark.io/v1/devices/DEVICE_ID/snsLight?access_token=WOULDNT_YOU_LIKE_TO_KNOW
>> /home/garrett/log.json
echo ‘,’ >> /home/garrett/log.json
The contents of the `log.json` file look something like this:
[
{ “cmd”: “VarReturn”, “name”: “snsLight”, “result”: 83, “coreInfo”: { “last_app”: “”, “last_heard”: “2014-05-03T14:42:03.279Z”, “connected”: true, “deviceID”: “51ff6f065067545708350687” } }
,
{ “cmd”: “VarReturn”, “name”: “snsLight”, “result”: 83, “coreInfo”: { “last_app”: “”, “last_heard”: “2014-05-03T14:43:34.297Z”, “connected”: true, “deviceID”: “51ff6f065067545708350687” } }
,
…
]```
PHP Parser Script
Once the data was gathered, this script was used to extract the result
and coreInfo.last_heard
and return each reading in a CSV format.
#!/usr/bin/php
<?php
date_default_timezone_set('America/New_York');
$json = json_decode(file_get_contents('log.json'), TRUE);
foreach($json as $j) {
if(!isset($j['result']))
continue;
echo date('Y-m-d H:i:s', @strtotime($j['coreInfo']['last_heard'])).','.$j['result'].PHP_EOL;
}
?>
The CSV output looks something like this:
2014-05-03 10:42:03,83
2014-05-03 10:43:34,83
...
The Results
I imported the CSV data into a Google Spreadsheet to generate an easy chart. It looks like the PlantSmart’s “very low light” readings correlate to the 75% mark from the Spark Core readings.
PlantSmart Chart
Spark Core Chart
What this means to me is that I can replicate the functionality of a commercial device that I have general faith in with a Spark Core, but I also have control of my own data, and it’s friggin’ wifi-enabled!
The next few rounds will be to wire up a quick soil moisture sensor and figure out how to deploy this into my yard against the elements either running on batteries or a solar setup.