From e78051fc3df67a1ac0c434eb9c683e203f14e63b Mon Sep 17 00:00:00 2001 From: Ths2-9Y-LqJt6 Date: Wed, 22 Mar 2017 23:32:08 -0700 Subject: [PATCH] 0.1 ftw --- README.md | 37 +++++++++++++++++++++------------- data/2017-03-22 | 36 +++++++++++++++++++++++++++++++++ parse_and_save.php | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 14 deletions(-) create mode 100644 data/2017-03-22 create mode 100755 parse_and_save.php diff --git a/README.md b/README.md index f209abf..0092b7a 100644 --- a/README.md +++ b/README.md @@ -3,21 +3,21 @@ Yet Another Pi Weather Station (YANPIWS)- My explorations in getting a Rasberry ## Background -With a daily workflow that invoves checking out a repo, making +With a daily workflow that involves checking out a repo, making commits, and the stopping work, it only made sense that I'd do the same for my efforts to write a little weather app for my -Pi. This habbit means my work is always backed up and ready -for others to review. +Pi. This habit means my work is always backed up and ready +for others to review or for me to load up on the another computer. Goals for this project are: +* Show live weather from local, wireless sensors +* Show past weather from local, wireless sensors * Show current time * Show sunset/sunrise times * Show moonrise/moonset times * Show weather forecast from some provider * Show show current weather from some provider -* Show live weather from local, wireless sensors -* Show past weather from local, wireless sensors ## Hardware @@ -33,11 +33,11 @@ we'll use the USB SDR dongle to read the temps from the sensors with a a call like this: ``` -rtl_433 -C customary -F json -q | parse.php +rtl_433 -C customary -F json -q | php -f parse_and_save.php ``` -And then, since I'm a PHP guy, we'll have that parse.php, be, -you know php, that parses it, but my proof of concept looks +And then, since I'm a PHP guy, we'll have that ``parse_and_save.php``, +be, you know php, that parses it, but my proof of concept looks like this (thanks http://stackoverflow.com/a/11968298): @@ -46,11 +46,11 @@ http://stackoverflow.com/a/11968298): $key); + } + + $today = date('Y-m-d', time()); + saveArrayToCsv($path, $today, $saveMeArray); +} + +/** + * we likely shouldn't trust data sent over unencrypted + * un-authenticated channels. Let's make sure it's on the + * up and up. uses regex to replace all but "^0-9\. :-" + * @param string $data input to cleanse + * @return string + */ +function cleanseData($data) +{ + $data = trim($data); + return preg_replace("/[^0-9\. :-]+/", "", $data); +} + +/** + * @param string $path + * @param array $array of k/v pairs to save + */ +function saveArrayToCsv($path, $file, $array) +{ + if (is_dir($path) && is_writable($path . '/' . $file) && is_array($array) && sizeof($array) > 0) { + echo 'It\'s ' . $array[2] . ' at ID ' . $array[1] . " - data written to " . $path . '/' . $file ."\n"; + file_put_contents($path . '/' . $file, implode(',' , $array) . "\n",FILE_APPEND); + } else { + echo "failed to write to $path - not a dir, not writable or invalid/empty array passed :("; + } +} +