#include <M5StickCPlus.h>
#include "Adafruit_SGP30.h"
#include <WiFi.h>
#include <WiFiMulti.h>
#include <WiFiClient.h>
#include <HTTPClient.h>
WiFiMulti WiFiMulti;
Adafruit_SGP30 sgp;
TFT_eSprite spu = TFT_eSprite(&M5.Lcd); // Sprite object
long last_millis = 0; // for 15sec wait timer
void setup() {
M5.begin(115200);
M5.Lcd.setRotation(3);
spu.setColorDepth(8);
spu.createSprite(240, 135);
M5.Lcd.setTextSize(2);
M5.Lcd.fillScreen(TFT_YELLOW);
M5.Lcd.setTextSize(2);
M5.Lcd.setTextColor(TFT_BLACK);
M5.Lcd.setCursor(5, 5);
while (!sgp.begin()) { // Init the sensor.
M5.Lcd.println("Sensor not found");
delay(5000);
}
// M5.Lcd.println("Initialization...");
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("ics-ap", "jikkenics");
// wait for WiFi connection
Serial.print("Waiting for WiFi to connect...");
while ((WiFiMulti.run() != WL_CONNECTED)) {
Serial.print(".");
delay(50);
}
Serial.println(" connected");
}
void loop() {
static int i = 3;
while (i > 0) {
if (millis() - last_millis > 1000) {
last_millis = millis();
i--;
spu.fillSprite(TFT_YELLOW);
spu.setTextColor( BLACK, YELLOW );
spu.setCursor(30, 4, 4); spu.setTextSize(1);
spu.println("Initializing...");
spu.setCursor(60, 34, 4); spu.setTextSize(2);
spu.printf(" %d ", i );
spu.pushSprite(0, 0);
delay(100);
}
}
if (!sgp.IAQmeasure()) { // Commands the sensor to take a single eCO2/VOC
// measurement. 命令传感器进行一次eCO2/VOC测量
Serial.println("Measurement failed");
return;
}
if ( sgp.eCO2 >= 1000) {
spu.fillSprite( DARKGREY ); //変更点
spu.setCursor(93, 5, 4); spu.setTextSize(2);
spu.setTextColor( WHITE, BLUE );
spu.printf(" %d \n", sgp.TVOC);
spu.setCursor(93, 65, 4); spu.setTextSize(2);
spu.setTextColor( WHITE, BLUE );
spu.printf(" %d \n", sgp.eCO2);
spu.setTextColor( BLUE, CYAN);
spu.setCursor(10, 20, 4); spu.setTextSize(1);
spu.println("TVOC : ");
spu.setCursor(10, 80, 4); spu.setTextSize(1);
spu.println("eCO2 : ");
spu.pushSprite(0, 0); // 画面に反映させる
} else if ( sgp.eCO2<1000 && sgp.eCO2 >= 700 ) {
spu.fillSprite( DARKGREEN ); //変更点
spu.setCursor(93, 5, 4); spu.setTextSize(2);
spu.setTextColor( WHITE, BLUE );
spu.printf(" %d \n", sgp.TVOC);
spu.setCursor(93, 65, 4); spu.setTextSize(2);
spu.setTextColor( WHITE, BLUE );
spu.printf(" %d \n", sgp.eCO2);
spu.setTextColor( BLUE, CYAN);
spu.setCursor(10, 20, 4); spu.setTextSize(1);
spu.println("TVOC : ");
spu.setCursor(10, 80, 4); spu.setTextSize(1);
spu.println("eCO2 : ");
spu.pushSprite(0, 0); // 画面に反映させる
} else if ( sgp.eCO2<700 && sgp.eCO2 >= 450 ) {
spu.fillSprite( GREEN ); // 変更点
spu.setCursor(93, 5, 4); spu.setTextSize(2);
spu.setTextColor( WHITE, BLUE );
spu.printf(" %d \n", sgp.TVOC);
spu.setCursor(93, 65, 4); spu.setTextSize(2);
spu.setTextColor( WHITE, BLUE );
spu.printf(" %d \n", sgp.eCO2);
spu.setTextColor( BLUE, CYAN);
spu.setCursor(10, 20, 4); spu.setTextSize(1);
spu.println("TVOC : ");
spu.setCursor(10, 80, 4); spu.setTextSize(1);
spu.println("eCO2 : ");
spu.pushSprite(0, 0); // 画面に反映させる
} else {
spu.fillSprite( CYAN );
spu.setCursor(93, 5, 4); spu.setTextSize(2);
spu.setTextColor( WHITE, BLUE );
spu.printf(" %d \n", sgp.TVOC);
spu.setCursor(93, 65, 4); spu.setTextSize(2);
spu.setTextColor( WHITE, BLUE );
spu.printf(" %d \n", sgp.eCO2);
spu.setTextColor( BLUE, CYAN);
spu.setCursor(10, 20, 4); spu.setTextSize(1);
spu.println("TVOC : ");
spu.setCursor(10, 80, 4); spu.setTextSize(1);
spu.println("eCO2 : ");
spu.pushSprite(0, 0); // 画面に反映させる
}
Serial.printf("TVOC: %d eCO2: %d\n", sgp.TVOC, sgp.eCO2);
// eCO2が1000を超えたら音を鳴らす
if (sgp.eCO2 > 1000) {
M5.Beep.setBeep(523, 500); M5.Beep.beep();
sendLineNotify();
}
M5.update();
if (M5.Axp.GetBtnPress() == 2) {
M5.Axp.Write1Byte(0x32, M5.Axp.Read8bit(0x32) | 0x80); // Power Off
}
delay(1000);
}
int sendLineNotify() {
HTTPClient http;
if (http.begin("https://notify-api.line.me/api/notify")) {
http.addHeader("Authorization", "Bearer ssJzyHVJE5pELzp0BdX5aieaovE33Md0gcmugu1A6qv");
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); // format of postdata
String postdata = "message=換気をしてください";
int httpCode = http.POST(postdata);
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTP] Unable to connect\n");
}
return 0;
}