Newer
Older
d4 / d4.ino
@11104 11104 on 27 Apr 2023 5 KB set default key_notice_time
/*18,20行目の注意書きを必ず読め!!!!!!!!!!!!!*/
#include <Arduino.h>
#include "M5StickCPlus.h"
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <string.h>
#include <stdio.h>
char str[100];
/*
When this code write to M5Stick, remove GND pin from M5Stick.
Then Err won't occurred
*/
//LED of M5StickC voltage is opposite to M5Stack
const char* ssid     = "miura2g";
const char* password = "jikken2022";
const char* host     = "notify-api.line.me";
const char* token    = "";//<--!!!!このトークンは消してからgit add .せよ!!!!絶対に上げるな!!!!!!
const String endpoint = "http://api.openweathermap.org/data/2.5/weather?q=tokyo,jp&APPID=";
const String key = "";//<--!!!!ここも消せ!!!!!!!
int weathertime;
int weatherflag=0;
int forecast=0;
char timenow[30];
int nowy,nowmon,nowd,nowh,nowm,nows,reg;
int systime=millis()/1000;

#define LED_PIN 10
#define DOOR_PIN 26
#define KEY_PIN 0
#define KEY_TIME 20    //seconds
#define weatherapi 30 //minutes
//regular weather notice time
#define seth 7 //hour
#define setm 0 //min
#define ntptime 5//min

void setup() {
    M5.begin();
    M5.Lcd.setRotation(3);
    pinMode(DOOR_PIN, INPUT_PULLUP);
    pinMode(KEY_PIN, INPUT_PULLUP);
    pinMode(LED_PIN,OUTPUT);
    M5.Lcd.fillScreen(BLACK);
    //wifi
    M5.Lcd.printf("WIFI START:%s ", ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        M5.Lcd.print(".");
    }
    M5.Lcd.println(" - READY");
    //weather
    weatherget();
    weathertime=millis();
    ntpsetup();
}
int unflag=0;

void loop(){
    M5.update();
    ntpget();
    if(digitalRead(KEY_PIN)==1){
        digitalWrite(LED_PIN, LOW);
        M5.Lcd.fillScreen(GREEN);
        M5.Lcd.setCursor(10,10);
        M5.Lcd.setTextColor(BLACK);
        M5.Lcd.setTextSize(5);
        M5.Lcd.print("Unlock");
        M5.Lcd.setCursor(10,80);
        M5.Lcd.setTextSize(3);
        M5.Lcd.println(timenow);
        //weather
        if(weatherflag==1&&unflag==0){
            unotice();
            unflag=1;
            unotice();
        }
    }else{
        digitalWrite(LED_PIN, HIGH);
        M5.Lcd.fillScreen(BLACK);
        M5.Lcd.setCursor(10,10);
        M5.Lcd.setTextColor(WHITE);
        M5.Lcd.setTextSize(5);
        M5.Lcd.print("Locked");
        M5.Lcd.setCursor(10,80);
        M5.Lcd.setTextSize(3);
        M5.Lcd.println(timenow);
        unflag=0;

    }
    //display Umbrella in key status page
    if(weatherflag==1){
        M5.Lcd.setCursor(10,50);
        M5.Lcd.setTextSize(4);
        M5.Lcd.setTextColor(RED);
        M5.Lcd.print("Umbrella");
    }else{
        M5.Lcd.setCursor(10,50);
        M5.Lcd.setTextSize(3);
        M5.Lcd.setTextColor(BLUE);
        M5.Lcd.print("Good Weather");
    }
    if(digitalRead(DOOR_PIN)==1){
        //weather
        if(weatherflag==1){
            unotice();
            unotice();
        }
        while(1){
            ntpget();
            M5.Lcd.fillScreen(BLUE);
            M5.Lcd.setTextColor(BLACK);
            M5.Lcd.setTextSize(5);
            M5.Lcd.setCursor(10,10);
            M5.Lcd.printf("Open");
            M5.Lcd.setCursor(10,80);
            M5.Lcd.setTextSize(3);
            M5.Lcd.println(timenow);
            if(digitalRead(DOOR_PIN)==0){
                break;
            }
            delay(1000);
        }
        int seccount=0;
        while(seccount<KEY_TIME){
            M5.Lcd.fillScreen(GREEN);
            M5.Lcd.setCursor(10,10);
            M5.Lcd.setTextColor(BLACK);
            M5.Lcd.setTextSize(5);
            M5.Lcd.print("Unlock");
            M5.Lcd.setCursor(10,80);
            M5.Lcd.setTextSize(3);
            ntpget();
            M5.Lcd.println(timenow);
            if(digitalRead(KEY_PIN)==0){
                digitalWrite(LED_PIN, HIGH);
                M5.Lcd.fillRect(0,0,320,120,BLACK);
                M5.Lcd.setCursor(10,10);
                M5.Lcd.setTextColor(WHITE);
                M5.Lcd.setTextSize(5);
                M5.Lcd.print("Locked");
                M5.Lcd.setCursor(10,80);
                M5.Lcd.setTextSize(3);
                M5.Lcd.println(timenow);
                break;
            }
            delay(1000);
            seccount++;
        }
        if(digitalRead(KEY_PIN)==1){
            M5.Lcd.fillScreen(RED);
            M5.Lcd.setTextColor(BLACK);
            M5.Lcd.setTextSize(5);
            M5.Lcd.setCursor(10,10);
            M5.Lcd.printf("Unlock!!!");
            //line api
            String msg = "戸締まり忘れ検知!";
            line_notify(msg);
            while(1){
                digitalWrite(LED_PIN, HIGH);
                M5.Beep.beep();
                delay(500);
                digitalWrite(LED_PIN, LOW);
                M5.Beep.mute();
                delay(500);
                if(digitalRead(KEY_PIN)==0){
                    break;
                }
            }
        }
    }
    //weather
    if(weatherapi<(millis()-weathertime)/(60*1000)){
        weatherget();
        weathertime=millis();
    }
    if(M5.BtnA.isPressed()){
        forecast=1;
        weatherget();
    }else if(nowh==seth&&nowm==setm&&reg==0){
        reg=1;
        weatherget();
    }
    delay(1000);
}