Kazuhiro Hamaya
Published © CC BY

Remembering you

Suddenly do you ever think of a friend and family who lives far away? This gadget will give you the opportunity to remember them.

BeginnerShowcase (no instructions)562
Remembering you

Things used in this project

Hardware components

ESP32 Basic Core IoT Development Kit
M5Stack ESP32 Basic Core IoT Development Kit
×1
LED (generic)
LED (generic)
×3
M5Stack Fan Module v1.1 (STM32F030)
M5Stack Fan Module v1.1 (STM32F030)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

Upper

Upper parts

Sketchfab still processing.

Base

Base parts

Sketchfab still processing.

Code

Okimono_OpenWeather

Arduino
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <TimeLib.h>

#include "SD.h"
#include <M5Unified.h>
#include <m5_module_fan.hpp>

// Path of the WAV file on the TF card 
static constexpr const char* WAV_FILE[] = {"/Thunder.wav","/Rain.wav"};

#define PIN_RAIN 5  //Rain LED_blue
#define PIN_THUNDER 2  //Thunder LED_yellow
#define PIN_WIND 26 //Wind LED_white

String pumi ="+";
float lata = 0;
int lats[6]={1,3,6,0,0,0};
float lona = 0;
int lons[7]={1,1,3,9,5,5,7};
int a = 36;
int iti = 0;
int id = 0;
int btd[2]={12,31};
int btdtr = 0;
int bdmt = 6;
int bdday = 20;
int bdhour =1;

int repeats_rain=0;
int repeats_thunder=0;
int repeats_wind = 0;

const char *ssid = "YOUR SSID"; //Wi-Fi setting
const char *password = "PASSWORD"; //Wi-Fi setting 

const String endpoi1 = "https://api.openweathermap.org/data/3.0/onecall?lat=";
const String endpoi2 = "&lon=";
const String endpoi3 = "&exclude=curent&appid=";
const String key = "OPEN WEATHER API KEY"; //OpenWeather API-key
String endpoi4 ="";
String endpoi5 ="";

// Fan Module Instance
M5ModuleFan moduleFan;
// Fan module I2C address
uint8_t deviceAddr = MODULE_FAN_BASE_ADDR;
// Fan Duty Cycle(0-100%)
uint8_t dutyCycle  = 80;
// Fan Status
bool fan_status = true;

//Sound file processing
static constexpr const size_t buf_num = 3;
static constexpr const size_t buf_size = 1024;
static uint8_t wav_data[buf_num][buf_size];

struct __attribute__((packed)) wav_header_t
{
  char RIFF[4];
  uint32_t chunk_size;
  char WAVEfmt[8];
  uint32_t fmt_chunk_size;
  uint16_t audiofmt;
  uint16_t channel;
  uint32_t sample_rate;
  uint32_t byte_per_sec;
  uint16_t block_size;
  uint16_t bit_per_sample;
};

struct __attribute__((packed)) sub_chunk_t
{
  char identifier[4];
  uint32_t chunk_size;
  uint8_t data[1];
};

static bool playSdWav(const char* filename)
{
  auto file = SD.open(filename);

  if (!file) { return false; }

  wav_header_t wav_header;
  file.read((uint8_t*)&wav_header, sizeof(wav_header_t));

  ESP_LOGD("wav", "RIFF           : %.4s" , wav_header.RIFF          );
  ESP_LOGD("wav", "chunk_size     : %d"   , wav_header.chunk_size    );
  ESP_LOGD("wav", "WAVEfmt        : %.8s" , wav_header.WAVEfmt       );
  ESP_LOGD("wav", "fmt_chunk_size : %d"   , wav_header.fmt_chunk_size);
  ESP_LOGD("wav", "audiofmt       : %d"   , wav_header.audiofmt      );
  ESP_LOGD("wav", "channel        : %d"   , wav_header.channel       );
  ESP_LOGD("wav", "sample_rate    : %d"   , wav_header.sample_rate   );
  ESP_LOGD("wav", "byte_per_sec   : %d"   , wav_header.byte_per_sec  );
  ESP_LOGD("wav", "block_size     : %d"   , wav_header.block_size    );
  ESP_LOGD("wav", "bit_per_sample : %d"   , wav_header.bit_per_sample);

  if ( memcmp(wav_header.RIFF,    "RIFF",     4)
    || memcmp(wav_header.WAVEfmt, "WAVEfmt ", 8)
    || wav_header.audiofmt != 1
    || wav_header.bit_per_sample < 8
    || wav_header.bit_per_sample > 16
    || wav_header.channel == 0
    || wav_header.channel > 2
    )
  {
    file.close();
    return false;
  }

  file.seek(offsetof(wav_header_t, audiofmt) + wav_header.fmt_chunk_size);
  sub_chunk_t sub_chunk;

  file.read((uint8_t*)&sub_chunk, 8);

  ESP_LOGD("wav", "sub id         : %.4s" , sub_chunk.identifier);
  ESP_LOGD("wav", "sub chunk_size : %d"   , sub_chunk.chunk_size);

  while(memcmp(sub_chunk.identifier, "data", 4))
  {
    if (!file.seek(sub_chunk.chunk_size, SeekMode::SeekCur)) { break; }
    file.read((uint8_t*)&sub_chunk, 8);

    ESP_LOGD("wav", "sub id         : %.4s" , sub_chunk.identifier);
    ESP_LOGD("wav", "sub chunk_size : %d"   , sub_chunk.chunk_size);
  }

  if (memcmp(sub_chunk.identifier, "data", 4))
  {
    file.close();
    return false;
  }

  int32_t data_len = sub_chunk.chunk_size;
  bool flg_16bit = (wav_header.bit_per_sample >> 4);

  size_t idx = 0;
  while (data_len > 0) {
    size_t len = data_len < buf_size ? data_len : buf_size;
    len = file.read(wav_data[idx], len);
    data_len -= len;

    if (flg_16bit) {
      M5.Speaker.playRaw((const int16_t*)wav_data[idx], len >> 1, wav_header.sample_rate, wav_header.channel > 1, 1, 0);
    } else {
      M5.Speaker.playRaw((const uint8_t*)wav_data[idx], len, wav_header.sample_rate, wav_header.channel > 1, 1, 0);
    }
    idx = idx < (buf_num - 1) ? idx + 1 : 0;
  }
  file.close();

  return true;
}
//Sound file processing

void setup() {
  auto cfg = M5.config();
    cfg.output_power = true;
  M5.begin(cfg);
  Serial.begin(115200);
  M5.Lcd.setTextSize(2);

  // Speaker Volume(0-255)
  M5.Speaker.setVolume(192);

  // GPIO PIN Setting
  pinMode(PIN_RAIN, OUTPUT);  
  pinMode(PIN_THUNDER, OUTPUT);  
  pinMode(PIN_WIND, OUTPUT);  
  digitalWrite(PIN_RAIN, LOW);
  digitalWrite(PIN_THUNDER, LOW);
  digitalWrite(PIN_WIND, LOW);
  
  // Initializing the TF card
  if (!SD.begin(GPIO_NUM_4, SPI, 25000000)) {
      M5.Lcd.println("TF card initialization failed!");
      return;
  }
  
  // Initialization Fan module
  // I2C Address=deviceAddr、SDA=21、SCL=22、Speed=400000Hz
  while (!moduleFan.begin(&Wire1, deviceAddr, 21, 22, 400000)) {
      Serial.printf("Module FAN Init faile\r\n");
      delay(1000);
  }
  // Run the fan at 80% duty cycle
  moduleFan.setPWMDutyCycle(dutyCycle);
  // Set the PWM frequency to 1KHz
  moduleFan.setPWMFrequency(PWM_1KHZ);
  // Fan Status First turn it OFF.
  moduleFan.setStatus(MODULE_FAN_DISABLE);

  // Latitude Setting
  M5.Lcd.print("Latitude");
  M5.Lcd.setCursor(0,40);
  M5.Lcd.print(" **.***");
  M5.Lcd.setCursor(64,220);
  M5.Lcd.print("+       -     Enter");
  while(lata==0){ 
      while(iti == 0){
        M5.update();
        M5.Lcd.setCursor(120,40);
        M5.Lcd.setCursor(0,40);
        M5.Lcd.print(pumi);
        if (M5.BtnA.wasPressed()) { //Button A "+"
          pumi ="+";
          lats[iti]=1;
        }
        else if(M5.BtnB.wasPressed()) { //Button B "-" 
          pumi = "-";
          lats[iti]=-1;
        }
        else if (M5.BtnC.wasPressed()) { //Button C "Enter"
          M5.Lcd.print(pumi);
          iti = 1;
        }
      }

      while(iti == 1){
        M5.update();
        M5.Lcd.setCursor(12,40);
        M5.Lcd.print(lats[iti]);        
        if (M5.BtnA.wasPressed()) { //Button A CountUP
          if(lats[iti]>8){
            lats[iti]=9;  //Limit
          }else{
            lats[iti]=lats[iti]+1;
          }
        }
        else if (M5.BtnB.wasPressed()) { //Button B Countdown
          if(lats[iti]<1){
            lats[iti]=0;  //Limit
          }else{
            lats[iti]=lats[iti]-1;
          }
        } 
        else if (M5.BtnC.wasPressed()) {
          iti = 2;
        }
      }
      while(iti == 2){
        M5.update();
        M5.Lcd.setCursor(24,40);
        M5.Lcd.print(lats[iti]);
        if (M5.BtnA.wasPressed()) { 
          if(lats[iti]>8){
            lats[iti]=9;
          }else{
            lats[iti]=lats[iti]+1;
          }
        }
        else if (M5.BtnB.wasPressed()) {
           if(lats[iti]<1){
            lats[iti]=0;
          }else{
            lats[iti]=lats[iti]-1;
          }
        } 
        else if (M5.BtnC.wasPressed()) {
          iti = 3;
          M5.Lcd.setCursor(36,40);
          M5.Lcd.print(".");
        }
      }
      while(iti == 3){
        M5.update();
        M5.Lcd.setCursor(48,40);
        M5.Lcd.print(lats[iti]);
        if (M5.BtnA.wasPressed()) { 
          if(lats[iti]>8){
            lats[iti]=9;
          }else{
            lats[iti]=lats[iti]+1;
          }
        }
        else if (M5.BtnB.wasPressed()) {
           if(lats[iti]<1){
            lats[iti]=0;
          }else{
            lats[iti]=lats[iti]-1;
          }
        } 
        else if (M5.BtnC.wasPressed()) {
          iti = 4;
        }
      }
      while(iti == 4){
        M5.update();
        M5.Lcd.setCursor(60,40);
        M5.Lcd.print(lats[iti]);
        if (M5.BtnA.wasPressed()) { 
          if(lats[iti]>8){
            lats[iti]=9;
          }else{
            lats[iti]=lats[iti]+1;
          }
        }
        else if (M5.BtnB.wasPressed()) {
           if(lats[iti]<1){
            lats[iti]=0;
          }else{
            lats[iti]=lats[iti]-1;
          }
        } 
        else if (M5.BtnC.wasPressed()) {
          iti = 5;
        }
      }
      while(iti == 5){
        M5.update();
        M5.Lcd.setCursor(72,40);
        M5.Lcd.print(lats[iti]);
        if (M5.BtnA.wasPressed()) { 
          if(lats[iti]>8){
            lats[iti]=9;
          }else{
            lats[iti]=lats[iti]+1;
          }
        }
        else if (M5.BtnB.wasPressed()) {
           if(lats[iti]<1){
            lats[iti]=0;
          }else{
            lats[iti]=lats[iti]-1;
          }
        } 
        else if (M5.BtnC.wasPressed()) {
          iti = 6;
        }
      }
      lata = lats[0]*(lats[1]*10+lats[2]+lats[3]*0.1+lats[4]*0.01+lats[5]*0.001);
    }

    M5.Lcd.clear();
    M5.Lcd.setCursor(0,0);
    M5.Lcd.print("Latitude = ");
    M5.Lcd.print(String(lata,3));
    Serial.println(String(lata,3));
    delay(500);

    // Longitude Setting
    M5.Lcd.setCursor(0,60);
    M5.Lcd.println("Longitude");
    iti = 0;
    pumi ="+";
    M5.Lcd.setCursor(0,90);
    M5.Lcd.print(" ***.***");
    M5.Lcd.setCursor(64,220);
    M5.Lcd.print("+       -     Enter");
    while(lona==0){ 
      while(iti == 0){
        M5.update();
        M5.Lcd.setCursor(140,90);
        M5.Lcd.setCursor(0,90);
        M5.Lcd.print(pumi);
        if (M5.BtnA.wasPressed()) { //Button A "+"
          pumi = "+";
          lons[iti]=1;
        }
        else if(M5.BtnB.wasPressed()) { //Button B "-"
          pumi = "-";
          lons[iti]=-1;
        }
        else if (M5.BtnC.wasPressed()) { //Button C "Enter"
          iti = 1;
        }
      }
      while(iti == 1){
        M5.update();
        M5.Lcd.setCursor(12,90);
        M5.Lcd.print(lons[iti]);
        if (M5.BtnA.wasPressed()) { 
          lons[iti]=1;
        }
        else if (M5.BtnB.wasPressed()) {
          lons[iti]=0;
          } 
        else if (M5.BtnC.wasPressed()) {
          iti = 2;
        }
      }
      while(iti == 2){
        M5.update();
        M5.Lcd.setCursor(24,90);
        M5.Lcd.print(lons[iti]);
         if (M5.BtnA.wasPressed()) { 
          if(lons[iti]>8){
            lons[iti]=9;
          }else{
            lons[iti]=lons[iti]+1;
          }
        }
        else if (M5.BtnB.wasPressed()) {
           if(lons[iti]<1){
            lons[iti]=0;
          }else{
            lons[iti]=lons[iti]-1;
          }
        } 
        else if (M5.BtnC.wasPressed()) {
          iti = 3;
        }
      }
      while(iti == 3){
        M5.update();
        M5.Lcd.setCursor(36,90);
        M5.Lcd.print(lons[iti]);
        if (M5.BtnA.wasPressed()) { 
          if(lons[iti]>8){
            lons[iti]=9;
          }else{
            lons[iti]=lons[iti]+1;
          }
        }
        else if (M5.BtnB.wasPressed()) {
           if(lons[iti]<1){
            lons[iti]=0;
          }else{
            lons[iti]=lons[iti]-1;
          }
        } 
        else if (M5.BtnC.wasPressed()) {
          iti = 4;
          M5.Lcd.setCursor(48,90);
          M5.Lcd.print(".");
        }
      }
      while(iti == 4){
        M5.update();
        M5.Lcd.setCursor(60,90);
        M5.Lcd.print(lons[iti]);
        if (M5.BtnA.wasPressed()) { 
          if(lons[iti]>8){
            lons[iti]=9;
          }else{
            lons[iti]=lons[iti]+1;
          }
        }
        else if (M5.BtnB.wasPressed()) {
           if(lons[iti]<1){
            lons[iti]=0;
          }else{
            lons[iti]=lons[iti]-1;
          }
        } 
        else if (M5.BtnC.wasPressed()) {
          iti = 5;
        }
      }
      while(iti == 5){
        M5.update();
        M5.Lcd.setCursor(72,90);
        M5.Lcd.print(lons[iti]);
        if (M5.BtnA.wasPressed()) { 
          if(lons[iti]>8){
            lons[iti]=9;
          }else{
            lons[iti]=lons[iti]+1;
          }
        }
        else if (M5.BtnB.wasPressed()) {
           if(lons[iti]<1){
            lons[iti]=0;
          }else{
            lons[iti]=lons[iti]-1;
          }
        } 
        else if (M5.BtnC.wasPressed()) {
          iti = 6;
        }
      }
      while(iti == 6){
        M5.update();
        M5.Lcd.setCursor(84,90);
        M5.Lcd.print(lons[iti]);
        if (M5.BtnA.wasPressed()) { 
          if(lons[iti]>8){
            lons[iti]=9;
          }else{
            lons[iti]=lons[iti]+1;
          }
        }
        else if (M5.BtnB.wasPressed()) {
           if(lons[iti]<1){
            lons[iti]=0;
          }else{
            lons[iti]=lons[iti]-1;
          }
        } 
        else if (M5.BtnC.wasPressed()) {
          iti = 7;
        }
      }
      lona = lons[0]*(lons[1]*100+lons[2]*10+lons[3]*1+lons[4]*0.1+lons[5]*0.01+lons[6]*0.001);
      Serial.println(String(lona,3));
    } 
    
    M5.Lcd.clear();
    M5.Lcd.setCursor(0,0);
    M5.Lcd.print("Latitude= ");
    M5.Lcd.print(String(lata,3));
    Serial.println(String(lata,3));
    M5.Lcd.setCursor(0,20);
    M5.Lcd.print("Longitude= ");
    M5.Lcd.print(String(lona,3));
    Serial.println(String(lona,3));
    iti = 0;
    M5.Lcd.setCursor(0,90);
    M5.Lcd.print("Celebration day");
    M5.Lcd.setCursor(0,110);
    M5.Lcd.print("**/**");
    M5.Lcd.setCursor(64,220);
    M5.Lcd.print("+       -     Enter");

  //Celebration Day
    while(btdtr==0){ 
      while(iti == 0){
        M5.update();
        if (btd[iti]<10){
            M5.Lcd.setCursor(0,110);
            M5.Lcd.print("0");
            M5.Lcd.setCursor(12,110);
            M5.Lcd.print(btd[iti]);
        }else{
            M5.Lcd.setCursor(0,110);
            M5.Lcd.print(btd[iti]);
        }
        if (M5.BtnA.wasPressed()) { //Button A CountUP
          if(btd[iti]>11){
            btd[iti]=12;
          }else{
            btd[iti]=btd[iti]+1;
          }
        }
        else if(M5.BtnB.wasPressed()) { //Button B CountDown
          if(btd[iti]<2){
            btd[iti]=1;
          }else{
            btd[iti]=btd[iti]-1;
          }
        }
        else if (M5.BtnC.wasPressed()) {
          Serial.println(btd[iti]);
          iti = 1;
        }
      }
      M5.Lcd.setCursor(24,110);
      M5.Lcd.print("/");
      while(iti == 1){
        M5.update();
        if (btd[iti]<10){
            M5.Lcd.setCursor(36,110);
            M5.Lcd.print("0");
            M5.Lcd.setCursor(48,110);
            M5.Lcd.print(btd[iti]);
        }else{
        M5.Lcd.setCursor(36,110);
        M5.Lcd.print(btd[iti]);
        }
        if (M5.BtnA.wasPressed()) { //Button A CountUP
          if(btd[iti]>30){
            btd[iti]=31;
          }else{
            btd[iti]=btd[iti]+1;
          }
        }
        else if(M5.BtnB.wasPressed()) { //Button B CountDown
          if(btd[iti]<2){
            btd[iti]=1;
          }else{
            btd[iti]=btd[iti]-1;
          }
        }
        else if (M5.BtnC.wasPressed()) {
          Serial.println(btd[iti]);
          iti = 2;
        }
      }
      btdtr = 1;
    }

    M5.Lcd.clear();
    M5.Lcd.setCursor(0,0);
    M5.Lcd.print("Latitude= ");
    M5.Lcd.print(String(lata,3));
    Serial.println(String(lata,3));
    M5.Lcd.setCursor(0,20);
    M5.Lcd.print("Longitude= ");
    M5.Lcd.print(String(lona,3));
    Serial.println(String(lona,3));
    M5.Lcd.setCursor(0,40);
    M5.Lcd.print("Celebration Day:");
    M5.Lcd.print(btd[0]);
    M5.Lcd.print("/");
    M5.Lcd.println(btd[1]);

    delay(2000);

    endpoi4 = String(lata,3);
    endpoi5 = String(lona,3);
  
  M5.Lcd.println();
  M5.Lcd.setTextSize(1);
  WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED){
        delay(500);
        M5.Lcd.print('.');
    }

    M5.Lcd.print("\r\nWiFi connected\r\nIP address: ");
    M5.Lcd.println(WiFi.localIP());
    delay(5000);
    M5.Lcd.clear();
}

void loop() {
  M5.Lcd.setTextSize(1);
  M5.Lcd.setTextColor(WHITE);
  if ((WiFi.status() == WL_CONNECTED)) {
 
    HTTPClient http;
 
    http.begin(endpoi1 + endpoi4 + endpoi2 + endpoi5 + endpoi3 + key); //URL Setting
    int httpCode = http.GET();

    if (httpCode > 0) {
        String payload = http.getString();  //JSON
        Serial.println(payload);
        StaticJsonDocument<1024> doc;
        DeserializationError error = deserializeJson(doc, payload);

      if (error) {
        M5.Lcd.print(F("deserializeJson() failed: "));
        M5.Lcd.println(error.f_str());
      } else {
        Serial.println("JSON Serialize OK");
        String toshi=doc["timezone"];
        id = doc["current"]["weather"][0]["id"];
        float  temp = doc["current"]["temp"].as<double>() - 273.15;
        float pressure = doc["current"]["pressure"].as<double>();
        float humidity = doc["current"]["humidity"].as<double>();
        unsigned long dt = doc["current"]["dt"].as<unsigned long>()+32400;
        unsigned long Sunrise = doc["current"]["sunrise"].as<unsigned long>()+32400;
        unsigned long Sunset = doc["current"]["sunset"].as<unsigned long>()+32400;
         
        Serial.println(id);
        String weather;
        if (id == 200 ){ weather = "thunderstorm with light rain"; } 
        else if (id == 201 ){ weather = "thunderstorm with rain"; } 
        else if (id == 202 ){ weather = "thunderstorm with heavy rain"; } //Thunder //Rain
        else if (id == 210 ){ weather = "light thunderstorm"; }  
        else if (id == 211 ){ weather = "thunderstorm"; } 
        else if (id == 212 ){ weather = "heavy thunderstorm"; } //Thunder
        else if (id == 221 ){ weather = "ragged thunderstorm"; } //Thunder
        else if (id == 230 ){ weather = "thunderstorm with light drizzle"; } //Thunder
        else if (id == 231 ){ weather = "thunderstorm with drizzle"; } //Thunder
        else if (id == 232 ){ weather = "thunderstorm with heavy drizzle"; } //Thunder
        else if (id == 300 ){ weather = "light intensity drizzle"; }
        else if (id == 301 ){ weather = "drizzle"; }
        else if (id == 302 ){ weather = "heavy intensity drizzle"; }
        else if (id == 310 ){ weather = "light intensity drizzle rain"; }
        else if (id == 311 ){ weather = "drizzle rain"; }
        else if (id == 312 ){ weather = "heavy intensity drizzle rain"; }
        else if (id == 313 ){ weather = "shower rain and drizzle"; }
        else if (id == 314 ){ weather = "heavy shower rain and drizzle"; }
        else if (id == 321 ){ weather = "shower drizzle"; }
        else if (id == 500 ){ weather = "light rain"; } 
        else if (id == 501 ){ weather = "moderate rain"; } 
        else if (id == 502 ){ weather = "heavy intensity rain"; } //Rain
        else if (id == 503 ){ weather = "very heavy rain"; } //Rain
        else if (id == 504 ){ weather = "extreme rain"; } //Rain
        else if (id == 511 ){ weather = "freezing rain"; } 
        else if (id == 520 ){ weather = "light intensity shower rain"; } 
        else if (id == 521 ){ weather = "shower rain"; } 
        else if (id == 522 ){ weather = "heavy intensity shower rain"; } //Rain
        else if (id == 531 ){ weather = "ragged shower rain"; } //Rain
        else if (id == 600 ){ weather = "light snow"; }
        else if (id == 601 ){ weather = "snow"; }
        else if (id == 602 ){ weather = "heavy snow"; }
        else if (id == 611 ){ weather = "sleet"; }
        else if (id == 612 ){ weather = "light shower sleet"; }
        else if (id == 613 ){ weather = "shower sleet"; }
        else if (id == 615 ){ weather = "light rain and snow"; }
        else if (id == 616 ){ weather = "rain and snow"; }
        else if (id == 620 ){ weather = "light shower snow"; }
        else if (id == 621 ){ weather = "shower snow"; } //Wind
        else if (id == 622 ){ weather = "heavy shower snow"; }  //Wind
        else if (id == 701 ){ weather = "mist"; }
        else if (id == 711 ){ weather = "smoke"; }
        else if (id == 721 ){ weather = "haze"; }
        else if (id == 731 ){ weather = "sand/dust whirls"; }
        else if (id == 741 ){ weather = "fog"; }
        else if (id == 751 ){ weather = "sand"; }
        else if (id == 761 ){ weather = "dust"; }
        else if (id == 762 ){ weather = "volcanic ash"; }
        else if (id == 771 ){ weather = "squalls"; }
        else if (id == 781 ){ weather = "tornado"; } //Wind
        else if (id == 800 ){ weather = "clear sky"; }
        else if (id == 801 ){ weather = "few clouds"; }
        else if (id == 802 ){ weather = "scattered clouds"; }
        else if (id == 803 ){ weather = "broken clouds"; }
        else if (id == 804 ){ weather = "overcast clouds"; }
        else {weather = "Etc";}

        //Unix time conversion
        Serial.print("DATE:");
        setTime(dt);
        Serial.printf("%d-%d-%d %d:%d\n",year(),month(),day(),hour(),minute());
        M5.Lcd.setCursor(0,220);
        M5.Lcd.printf("%d-%d-%d %d:%d\n",year(),month(),day(),hour(),minute());
        bdmt = int(month());
        Serial.print("bdmt=");
        Serial.println(bdmt);
        Serial.print("btd[0]=");
        Serial.println(btd[0]);
        
        bdday = int(day());
        Serial.print("bdday=");
        Serial.println(bdday);
        Serial.print("btd[1]=");
        Serial.print(btd[1]);
        bdhour = int(hour());

        M5.Lcd.print("Location:");
        M5.Lcd.print(toshi);

        M5.Lcd.print(" Weather:");
        M5.Lcd.print(weather);
      }
 
    }else {
      Serial.println("Error on HTTP request");
    }
    http.end(); //Free the resources
  }
 
 //id = 202; //Test
 if(id==202 && repeats_wind==0 && repeats_thunder==0){
    digitalWrite(PIN_THUNDER, HIGH);
    delay(500);
    digitalWrite(PIN_THUNDER, LOW);
    delay(300);
    digitalWrite(PIN_THUNDER, HIGH);
    delay(500);
    digitalWrite(PIN_THUNDER, LOW);
    delay(300);
    digitalWrite(PIN_THUNDER, HIGH);
    playSdWav(WAV_FILE[0]);
    repeats_thunder = 1;
    digitalWrite(PIN_THUNDER, LOW);
    delay(100);
    moduleFan.setStatus(MODULE_FAN_ENABLE);
    digitalWrite(PIN_WIND, HIGH);
    delay(2000);
    moduleFan.setStatus(MODULE_FAN_DISABLE);
    repeats_wind = 1;
    digitalWrite(PIN_WIND, LOW);
 
 }else if(id>501 && id<505 && repeats_rain==0){
    digitalWrite(PIN_RAIN, HIGH);
    playSdWav(WAV_FILE[1]);
    repeats_rain = 1;
    digitalWrite(PIN_RAIN, LOW);
 }else if(id==522 && repeats_rain==0){
    digitalWrite(PIN_RAIN, HIGH);
    playSdWav(WAV_FILE[1]);
    repeats_rain = 1;
    digitalWrite(PIN_RAIN, LOW);
 }else if(id==531 && repeats_rain==0){
    digitalWrite(PIN_RAIN, HIGH);
    playSdWav(WAV_FILE[1]);
    repeats_rain = 1;
    digitalWrite(PIN_RAIN, LOW);

 }else if(id>211 && id<233 && repeats_thunder==0){
    digitalWrite(PIN_THUNDER, HIGH);
    delay(500);
    digitalWrite(PIN_THUNDER, LOW);
    delay(300);
    digitalWrite(PIN_THUNDER, HIGH);
    delay(500);
    digitalWrite(PIN_THUNDER, LOW);
    delay(300);
    digitalWrite(PIN_THUNDER, HIGH);
    delay(500);
    digitalWrite(PIN_THUNDER, LOW);
    delay(300);
    digitalWrite(PIN_THUNDER, HIGH);
    playSdWav(WAV_FILE[0]);
    repeats_thunder = 1;
    digitalWrite(PIN_THUNDER, LOW);

 }else if(id==621 && repeats_wind==0){
    moduleFan.setStatus(MODULE_FAN_ENABLE);
    digitalWrite(PIN_WIND, HIGH);
    delay(5000);
    moduleFan.setStatus(MODULE_FAN_DISABLE);
    repeats_wind = 1;
    digitalWrite(PIN_WIND, LOW);
 }else if(id==622 && repeats_wind==0){
    moduleFan.setStatus(MODULE_FAN_ENABLE);
    digitalWrite(PIN_WIND, HIGH);
    delay(5000);
    moduleFan.setStatus(MODULE_FAN_DISABLE);
    repeats_wind = 1;
    digitalWrite(PIN_WIND, LOW);
 }else if(id==781 && repeats_wind==0){
    moduleFan.setStatus(MODULE_FAN_ENABLE);
    digitalWrite(PIN_WIND, HIGH);
    delay(5000);
    moduleFan.setStatus(MODULE_FAN_DISABLE);
    repeats_wind = 1;
    digitalWrite(PIN_WIND, LOW);

 }else{
  repeats_rain = 0;
  repeats_thunder=0;
  repeats_wind = 0;
 }

 delay(1000);

 if(bdmt==btd[0] && bdday==btd[1]){
      M5.Lcd.setTextSize(3);
      M5.Lcd.setTextColor(BLUE);
      M5.Lcd.setCursor(20,110);
      M5.Lcd.print("Congratulations");
      //M5.Lcd.drawPngFile(SD,"/celebration.png"); //Test
    //}
  }
    
  delay(1800000);   //Updated every 30 minutes
  M5.Lcd.clear();

}

Credits

Kazuhiro Hamaya
1 project • 3 followers

Comments