Skip to content

Instantly share code, notes, and snippets.

@ma2shita
Last active April 27, 2020 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ma2shita/0e5e0cba42b06114ec6a96dbd6fa0318 to your computer and use it in GitHub Desktop.
Save ma2shita/0e5e0cba42b06114ec6a96dbd6fa0318 to your computer and use it in GitHub Desktop.
/* See: https://www.lp.soracom.jp/202005-online-seminar/ */
/*
* TV power monitor (using Light sensor) / Local sensing only version
*
* Copyright (c) 2020 SORACOM, INC.
* Released under the MIT license
* https://opensource.org/licenses/mit-license.php
*/
#include <WioLTEforArduino.h>
WioLTE Wio;
#define console SerialUSB
#include <ArduinoJson.h>
/* global vals for dynamic config by SORACOM Air metadata */
int TV_OFF_THRESHOLD;
int TV_ON_DARK_THRESHOLD;
int TV_ON_BRIGHT_THRESHOLD;
int SAMPLING_COUNT;
int SENSING_INTERVAL_MS;
void setup() {
delay(3000);
console.println("");
console.println("--- START ---");
Wio.Init();
Wio.PowerSupplyGrove(true);
/* set for light sensor */
pinMode(WIOLTE_A6, INPUT_ANALOG);
/* set for device config */
String json = "{\"TV_OFF_THRESHOLD\": 4, \"TV_ON_DARK_THRESHOLD\": 30, \"TV_ON_BRIGHT_THRESHOLD\": 200, \"SENSING_INTERVAL_MS\": 1000, \"SAMPLING_COUNT\": 1}";
console.println(json);
const size_t capacity = JSON_OBJECT_SIZE(5) + 110; // Code generate by https://arduinojson.org/v6/assistant/
DynamicJsonDocument doc(capacity);
deserializeJson(doc, json); // NOTE: Need error handling
/* read from JSON */
TV_OFF_THRESHOLD = doc["TV_OFF_THRESHOLD"];
console.print("TV OFF threshold is ");
console.println(TV_OFF_THRESHOLD);
SENSING_INTERVAL_MS = doc["SENSING_INTERVAL_MS"];
console.print("Sensing interval is every ");
console.print(SENSING_INTERVAL_MS / 1000.0);
console.println(" seconds");
SAMPLING_COUNT = doc["SAMPLING_COUNT"];
console.print("Send interval is every ");
console.print(SAMPLING_COUNT * SENSING_INTERVAL_MS / 1000.0);
console.println(" seconds");
TV_ON_DARK_THRESHOLD = doc["TV_ON_DARK_THRESHOLD"];
TV_ON_BRIGHT_THRESHOLD = doc["TV_ON_BRIGHT_THRESHOLD"];
}
void sensing_data_display(WioLTE &wio, int analogRead_val) {
if (analogRead_val <= TV_OFF_THRESHOLD) {
Wio.LedSetRGB(1, 0, 0); /* RED when OFF the TV */
} else if (analogRead_val <= TV_ON_DARK_THRESHOLD) {
Wio.LedSetRGB(0, 1, 0); /* GREEN */
} else if (analogRead_val <= TV_ON_BRIGHT_THRESHOLD) {
Wio.LedSetRGB(0, 0, 1); /* BLUE */
} else {
Wio.LedSetRGB(1, 1, 1); /* WHITE when over bright */
}
}
#include <vector>
std::vector<int> v;
void loop() {
int val = analogRead(WIOLTE_A6);
sensing_data_display(Wio, val);
v.push_back(val);
console.print(v.size()); console.print(" ");
if (SAMPLING_COUNT <= v.size()) { // aggregation and send to cloud
console.println();
console.println("---");
for (auto itr = v.begin(); itr != v.end(); ++itr) { console.println(*itr); }
console.println("---");
auto power_on_count = std::count_if(v.begin(), v.end(), [](int x) { return TV_OFF_THRESHOLD < x; });
console.println(power_on_count);
float power_on_rate = (float) power_on_count / (float) v.size();
console.println(power_on_rate);
v.clear();
/* build to JSON */
const size_t capacity = JSON_OBJECT_SIZE(1); // Code generate by https://arduinojson.org/v6/assistant/
DynamicJsonDocument doc(capacity);
doc["power_on_rate"] = power_on_rate;
char buf[1024];
serializeJson(doc, buf);
console.println(buf);
}
delay(SENSING_INTERVAL_MS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment