Skip to content

Instantly share code, notes, and snippets.

@fukayatsu
Last active May 2, 2020 06:00
Show Gist options
  • Save fukayatsu/0f782519d4ddbc0a0144807073644005 to your computer and use it in GitHub Desktop.
Save fukayatsu/0f782519d4ddbc0a0144807073644005 to your computer and use it in GitHub Desktop.
#include <M5StickC.h>
#include <WiFi.h>
#include <HTTPClient.h>
#define REMO_API_URL "https://api.nature.global/1/appliances"
#define REMO_AUTH "Bearer ***"
#define WIFI_SSID "***"
#define WIFI_PASSWORD "***"
HTTPClient http;
void setup() {
M5.begin();
M5.Axp.ScreenBreath(10);
M5.Lcd.setRotation(3);
M5.Lcd.setTextSize(2);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.println("start wifi");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
M5.Lcd.print(".");
delay(500);
}
M5.Lcd.println("connected!");
M5.Lcd.println(WiFi.localIP());
delay(500);
}
void loop(){
if (WiFi.status() != WL_CONNECTED) {
M5.Lcd.println("Error. \nWiFi not connected");
return;
}
http.begin(REMO_API_URL);
http.addHeader("Accept", "application/json");
http.addHeader("Authorization", REMO_AUTH);
http.GET();
String payload = http.getString();
http.end();
int p0 = payload.indexOf("epc\":231"); // measured_instantaneous
int p1 = payload.indexOf("val", p0);
int p2 = payload.indexOf("updated_at", p1);
String val = payload.substring(p1 + 6, p2 - 3);
M5.Lcd.setRotation(0);
M5.Lcd.setCursor(2, 140);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.print(val);
M5.Lcd.println(" W");
int intVal = val.toInt();
int color;
if (intVal > 3000) {
color = TFT_RED;
} else if (intVal > 2000) {
color = TFT_YELLOW;
} else if (intVal > 1000) {
color = TFT_GREEN;
} else {
color = TFT_BLUE;
}
M5.Lcd.fillCircle(40, 40, 40, color);
delay(10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment