Last active
June 15, 2020 16:06
-
-
Save ma2shita/b1b8f4d761a6f6bd4236ef4032de781c 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
/* | |
* "Take a thing" notifier with 3G (All-in the cloud) | |
* | |
* 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 <HTTPClient.h> // Why? see https://qiita.com/ma2shita/items/97bf1a0c3158b848019a | |
#include <ArduinoHttpClient.h> | |
TinyGsmClient ctx(modem); | |
HttpClient http = HttpClient(ctx, "uni.soracom.io", 80); | |
#include <ArduinoJson.h> | |
void loop() { | |
const size_t capacity = JSON_ARRAY_SIZE(5) + JSON_OBJECT_SIZE(1); // Code generate by https://arduinojson.org/v6/assistant/ | |
DynamicJsonDocument doc(capacity); | |
JsonArray values = doc.createNestedArray("values"); | |
for (int i = 0 ; i < 5 ; i++) { // sampling | |
values.add(tof.readRangeContinuousMillimeters()); | |
delay(50); // sampling interval | |
} | |
char buf[1024]; | |
serializeJson(doc, buf); | |
console.println(buf); | |
connect_check_and_reconnect(); | |
http.post("/", "application/json", buf); | |
console.print("responseStatusCode(): "); console.println(http.responseStatusCode()); | |
http.stop(); | |
delay(10*1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment