// ESP32 Rain Alarm2 with WiFiManager // Read LINE TOKEN from SPIFFS // Upload the token.txt containing the CHANNEL ACCESS TOKEN // to the SPIFFS in advance. #include #include #include #include #include RTC_DATA_ATTR int bootCount = 0; // Number of boot String token; // for CHANNEL ACCESS TOKEN // Broadcast message notification via LINE Messaging API void notify(char* message) { HTTPClient http; http.begin("https://api.line.me/v2/bot/message/broadcast"); http.addHeader("Content-Type", "application/json"); http.addHeader("authorization", "Bearer " + token); char body[256]; sprintf(body, "{\"messages\":[{\"type\": \"text\",\"text\":\"%s\"}]}", message); http.POST(body); Serial.println(http.getString()); http.end(); } //setup Function void setup() { // Init Serial Serial.begin(115200); // Start SPIFFS if (!SPIFFS.begin(true)) { Serial.println("SPIFFS initialization failed!"); for (;;) ; } // Open token.txt File file = SPIFFS.open("/token.txt", "r"); if (!file) { Serial.println("Failed to open file for reading"); for (;;) ; } // Read LINE TOKEN from file token = ""; while (file.available()) { token += (char)file.read(); } token.trim(); //WiFiManager WiFiManager wifiManager; wifiManager.autoConnect("AutoConnectAP"); //GPIO27 INPUT pinMode(GPIO_NUM_27, INPUT); // Serial.begin(115200); if (bootCount == 0) { delay(1000); } Serial.printf("ESP32 Rain Alarm\n"); // Number of boot bootCount++; Serial.printf("Number of boot: %d ", bootCount); // Notifies LINE when waking up from Deep Sleep. esp_sleep_wakeup_cause_t wakeup_reason; wakeup_reason = esp_sleep_get_wakeup_cause(); if (wakeup_reason == ESP_SLEEP_WAKEUP_EXT0) { Serial.printf("WAKEUP EXT0\n"); notify("☂ 雨が降ってきました。"); } //GPIO27 returns from deep sleep at LOW esp_sleep_enable_ext0_wakeup(GPIO_NUM_27, LOW); } //loop Function void loop() { // GPIO_NUM_27 is HIGH -> go to Deep Sleep if (digitalRead(GPIO_NUM_27) == HIGH) { Serial.printf("Go to Deep Sleep\n"); esp_deep_sleep_start(); delay(1000); } }