Skip to content

Instantly share code, notes, and snippets.

@ma2shita
Last active June 15, 2020 16:01
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/a0ccf01f9a99e34ddd2699779c6511a1 to your computer and use it in GitHub Desktop.
Save ma2shita/a0ccf01f9a99e34ddd2699779c6511a1 to your computer and use it in GitHub Desktop.
/*
* "Take a thing" notifier with 3G (All-in the edge)
*
* Copyright (c) 2020 Kohei MATSUSHITA
* Released under the MIT license
* https://opensource.org/licenses/mit-license.php
*/
#include <M5Stack.h>
#define console Serial
//https://www.switch-science.com/catalog/5219/
#define ToF_ADDR 0x29 // the iic address of tof
#include <VL53L0X.h> // from Lib. manager: https://github.com/pololu/vl53l0x-arduino/blob/master/examples/Continuous/Continuous.ino
VL53L0X tof;
#define TINY_GSM_MODEM_UBLOX
#include <TinyGsmClient.h>
TinyGsm modem(Serial2); /* Serial2 is Modem of 3G Module */
void connect_check_and_reconnect() {
console.println((modem.isGprsConnected()) ? "isGprsConnected(): true" : "isGprsConnected(): false");
long s = millis();
if (!modem.isGprsConnected()) {
console.print("modem.restart(): ");
Serial2.begin(115200, SERIAL_8N1, 16, 17);
modem.restart();
console.print("getModemInfo(): ");
String modemInfo = modem.getModemInfo();
console.println(modemInfo);
console.print("waitForNetwork(): ");
while (!modem.waitForNetwork()) console.print(".");
console.print("gprsConnect(soracom.io): ");
modem.gprsConnect("soracom.io", "sora", "sora");
console.print("isNetworkConnected(): ");
while (!modem.isNetworkConnected()) console.print(".");
console.print("localIP(): ");
console.println(modem.localIP());
}
long e = millis();
console.print("Modem bootup elapsed(ms): "); console.println(e - s);
}
void setup() {
console.begin(115200);
M5.begin();
M5.Power.begin();
M5.Lcd.sleep();
M5.Lcd.setBrightness(0);
Wire.begin();
tof.setAddress(ToF_ADDR);
tof.setTimeout(500);
tof.init();
tof.startContinuous();
connect_check_and_reconnect();
}
#include <Ticker.h>
Ticker stay_timer;
volatile boolean stay_timer_timeout = false;
void onTimer() {
stay_timer_timeout = true;
}
#include <HTTPClient.h> // Why? see https://qiita.com/ma2shita/items/97bf1a0c3158b848019a
#include <ArduinoHttpClient.h>
TinyGsmClientSecure ctx(modem);
HttpClient http = HttpClient(ctx, "maker.ifttt.com", 443);
#include <ArduinoJson.h>
#include <vector>
std::vector<int> v;
#include <algorithm>
int current_state = 1;
void loop() {
v.clear();
for (int i = 0 ; i < 5 ; i++) { // sampling
v.push_back(tof.readRangeContinuousMillimeters());
delay(50); // sampling interval
}
for (auto it = v.begin(); it != v.end();) { // instead of #erase_if(), C++11 not impl.
if ( !(10 <= *it && *it <= 1000) ) { // noise cut
it = v.erase(it);
} else {
++it;
}
}
if (v.empty()) v.push_back(1000); // avoiding in case of all removed
const float distance_mm = std::accumulate(v.begin(), v.end(), 0.0) / v.size();
// state: 1=not exists, 2=exists, 3=long stay
if (20 <= distance_mm && distance_mm <= 75) { // in range
if (current_state == 1) { // Put_in
current_state = 2;
stay_timer.once(300, onTimer);
} else if (current_state == 2 && stay_timer_timeout) { // timeout
current_state = 3;
stay_timer_timeout = false;
const size_t capacity = JSON_OBJECT_SIZE(2); // Code generate by https://arduinojson.org/v6/assistant/
DynamicJsonDocument doc(capacity);
doc["value1"] = "coffee_cup2";
doc["value2"] = distance_mm;
char buf[1024];
serializeJson(doc, buf);
console.println(buf);
connect_check_and_reconnect();
http.connectionKeepAlive(); // Currently, this is needed for HTTPS
http.post("/trigger/ifttt_email_notifier/with/key/cRrvcVeNk6tLusEeTqN4i6", "application/json", buf);
console.print("responseStatusCode(): "); console.println(http.responseStatusCode());
http.stop();
}
} else { // Take_out
current_state = 1;
}
console.print("current_state:"); console.println(current_state);
delay(10*1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment