Last active
April 27, 2020 03:11
-
-
Save ma2shita/14ce40affe4b4a56bffc265d67b1b1b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* See: https://www.lp.soracom.jp/202005-online-seminar/ */ | |
/* | |
* TV power monitor (using Light sensor) / with LTE | |
* | |
* 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 | |
String get_metadata_by(WioLTE &wio, const char* tag_key, const char* default_value = "") { | |
char url[1024]; | |
sprintf(url, "http://metadata.soracom.io/v1/subscriber.tags.%s", tag_key); | |
char buf[1024]; | |
wio.HttpGet(url, buf, sizeof(buf)); | |
String content = String(buf); | |
content.trim(); | |
if (content == "Specified key does not exist." || /* == 404 */ | |
content == "You are not allowed to access Metadata Server.") { /* == 403 */ | |
content = String(default_value); | |
content.trim(); | |
} | |
return content; | |
} | |
#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); | |
console.println("### Power supply ON."); | |
Wio.PowerSupplyLTE(true); | |
delay(500); | |
console.println("### Turn on or reset."); | |
if (!Wio.TurnOnOrReset()) { | |
console.println("### ERROR! ###"); | |
return; | |
} | |
console.println("### Connecting to \"soracom.io\"."); | |
if (!Wio.Activate("soracom.io", "sora", "sora")) { | |
console.println("### ERROR! ###"); | |
return; | |
} | |
/* set for light sensor */ | |
pinMode(WIOLTE_A6, INPUT_ANALOG); | |
/* set for device config */ | |
// Template for metadata: {"TV_OFF_THRESHOLD": 4, "TV_ON_DARK_THRESHOLD": 30, "TV_ON_BRIGHT_THRESHOLD": 200, "SENSING_INTERVAL_MS": 1000, "SAMPLING_COUNT": 10} | |
String json = get_metadata_by(Wio, "config_json"); | |
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); | |
/* send to cloud */ | |
int http_status_code; | |
if (!Wio.HttpPost("http://unified.soracom.io", buf, &http_status_code)) { | |
console.println("### ERROR! ###"); | |
} | |
console.println(http_status_code); | |
} | |
delay(SENSING_INTERVAL_MS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment