Last active
May 2, 2020 06:00
-
-
Save fukayatsu/0f782519d4ddbc0a0144807073644005 to your computer and use it in GitHub Desktop.
This file contains 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
#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