/*
ESP32 LINE notify
Author: IZUMO
Date: 2024.08.30
*/
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include "esp32-hal-log.h"
#include <esp_pm.h>
const char* ssid = "ssidをいれる";
const char* password = "パスワードをここに";
// LINE Notify設定
const char* host = "notify-api.line.me";
const char* token = "トークンをここに入れる";
#define LED_Pin 2
#define SW_Pin 15
#define OPEN 1
#define CLOSE 0
#define OPEN_CLOSE 2
volatile int state = LOW;
volatile int flgSw = LOW;
void pm_init() {
esp_pm_config_esp32_t esp_pm_config_esp32;
esp_pm_config_esp32.light_sleep_enable = true;
esp_pm_configure(&esp_pm_config_esp32);
}
void sleep_ms(uint32_t ms) {
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void LED_blink(uint32_t ms){
digitalWrite(LED_Pin, HIGH);
sleep_ms(ms);
digitalWrite(LED_Pin, LOW);
}
void LED_blink_time(uint32_t ms,uint16_t time){
for(uint16_t i=0;i<time;i++){
LED_blink(ms);
vTaskDelay(ms);
}
}
//WiFiに接続
void connectWiFi(){
Serial.print("ssid:");
Serial.print(ssid);
Serial.println(" に接続します。");
WiFi.begin(ssid, password);
Serial.print("WiFiに接続中");
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("接続しました。");
//IPアドレスの表示
Serial.print("IPアドレス:");
Serial.println(WiFi.localIP());
}
int checkPin(){
int stateCount = 0;
int bfrState;
int nowState;
bfrState = digitalRead(SW_Pin);
for(int i=0;i<20;i++){
sleep_ms(200);
nowState = digitalRead(SW_Pin);
if(bfrState == nowState) stateCount++;
if(stateCount>3) break;
bfrState = nowState;
}
return nowState;
}
// line通知
void send_line(const char* msg) {
//wifiに接続
connectWiFi();
// HTTPSへアクセス(SSL通信)するためのライブラリ
WiFiClientSecure client;
// サーバー証明書の検証を行わずに接続する場合に必要
client.setInsecure();
Serial.println("Try");
//LineのAPIサーバにSSL接続(ポート443:https)
if (!client.connect(host, 443)) {
Serial.println("Connection failed");
return;
}
Serial.println("Connected");
// リクエスト送信
String query = String("message=") + String(msg);
String request = String("") +
"POST /api/notify HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Authorization: Bearer " + token + "\r\n" +
"Content-Length: " + String(query.length()) + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n\r\n" +
query + "\r\n";
client.print(request);
// 受信完了まで待機
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
Serial.println(line);
//WiFiを切断
WiFi.mode(WIFI_OFF);
LED_blink(50);
}
void sendData(int doorState){
if(doorState==CLOSE){
send_line("Lock closed.");
}else if(doorState == OPEN){
send_line("Lock opened.");
}
}
void setWakeup(int doorState){
esp_sleep_enable_ext0_wakeup(GPIO_NUM_15, doorState);
}
void lightSleep(uint32_t mSec){
esp_sleep_enable_timer_wakeup(mSec * 1000U); //5secのライトスリープ
esp_light_sleep_start();
//ESP_SLEEP_WAKEUP_TIMER;
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
}
void deepSleep(){
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH , ESP_PD_OPTION_AUTO);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF);
esp_deep_sleep_start();
}
void setup() {
WiFiClientSecure sslclient;
int doorState;
int oldDoorState;
// esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_GPIO);
pinMode(SW_Pin, INPUT);
pinMode(LED_Pin, OUTPUT);
pm_init();
//鍵の状態チェック
LED_blink(250);
doorState = checkPin();
if(doorState == OPEN){
sendData(OPEN);
LED_blink_time(100,3);
setWakeup(CLOSE); //CLOSE待ちにセット
}else{
sendData(CLOSE);
LED_blink_time(100,4);
setWakeup(OPEN); //OPEN待ちにセット
}
//deepSleepに入れる
deepSleep();
}
void loop() {
vTaskDelay(portMAX_DELAY);
//portMAX_DELAY
}
コメント
コメントを投稿