달력

3

« 2024/3 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

기본 기능 : 날씨 표시

(날씨, 온도, 습도, 바람세기)

* 리모컨으로 lcd백라이트를 껐다 켰다 할 수 있는 기능 추가(OK버튼을 누르면 켜지고, 그 외의 버튼을 누르면 꺼집니다)

* 1분에 1회씩 날씨가 갱신됨.



필요 부품

Wemos D1 R2

20x4 CLCD I2C

리모컨 키트




소스코드

#include <Arduino.h>

#include <ESP8266WiFi.h>

#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <Wire.h> 

#include <LiquidCrystal_I2C.h>

#include <IRremoteESP8266.h>

#include <IRrecv.h>

#include <IRutils.h>


#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;


LiquidCrystal_I2C lcd(0x3f,20,4);


uint16_t RECV_PIN = 14;

IRrecv irrecv(RECV_PIN);

decode_results results;


long currentMillis = 0;

long previousMillis = 0;

boolean firstTimeFlag = true;


void setup() {

    USE_SERIAL.begin(115200);

    lcd.init();

    lcd.backlight();

    lcd.setCursor(0,1);


    irrecv.enableIRIn();  // Start the receiver

    

    for(uint8_t t = 4; t > 0; t--) {

        USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);

        USE_SERIAL.flush();

        delay(1000);

    }

    lcd.print("Server Connecting...");

    WiFiMulti.addAP("SSID", "PASSWORD");

}


void loop() {

    if (irrecv.decode(&results)) {

      serialPrintUint64(results.value, HEX);

      Serial.println("");

  

      if(results.value == 0xFF38C7){

        Serial.println("ON");

        lcd.backlight();

      }else if(results.value == 0xFFFFFFFFFFFFFFFF){

        ;

      }else{

        Serial.println("OFF");

        lcd.noBacklight();

      }

      

      irrecv.resume();

    }

    delay(100);


    currentMillis = millis();

    if((WiFiMulti.run() == WL_CONNECTED) && (currentMillis - previousMillis > 60000) || firstTimeFlag == true) {

        HTTPClient http;


        USE_SERIAL.print("[HTTP] begin...\n");

        http.begin("http://api.openweathermap.org/data/2.5/weather?q=Euijeongbu,kr&appid=308916b0a9502052c20b263a8be5bf89"); //HTTP


        USE_SERIAL.print("[HTTP] GET...\n");

        int httpCode = http.GET();


        if(httpCode > 0) {

            USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);


            if(httpCode == HTTP_CODE_OK) {

                String payload = http.getString();

                USE_SERIAL.println(payload);

                

                String description = jsonParser(payload,"description\":\"","\",\"icon");

                USE_SERIAL.println(description);

                lcd.setCursor(0,0);

                lcd.print("                    ");

                lcd.setCursor(0,0);

                lcd.print("weather : ");

                lcd.print(description);


                String temp = jsonParser(payload,"temp\":"    ,    ",\"pressure");

                USE_SERIAL.println(temp.toFloat() - 273.0);

                lcd.setCursor(0,1);

                lcd.print("                    ");

                lcd.setCursor(0,1);

                lcd.print("temp    : ");

                lcd.print(temp.toFloat() - 273.0);


                String humidity = jsonParser(payload,"humidity\":"    ,    ",\"temp_min");

                USE_SERIAL.println(humidity);

                lcd.setCursor(0,2);

                lcd.print("                    ");

                lcd.setCursor(0,2);

                lcd.print("humidity: ");

                lcd.print(humidity);


                String wind = jsonParser(payload,"speed\":"    ,    ",\"deg");

                USE_SERIAL.println(wind);

                lcd.setCursor(0,3);

                lcd.print("                    ");

                lcd.setCursor(0,3);

                lcd.print("wind    : ");

                lcd.print(wind);

            }

        } else {

            USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());

        }


        http.end();

        previousMillis = millis();

        results.value = 0xFFFFFFFF;

        firstTimeFlag = false;

    }

}



String jsonParser(String payloadStr, String beforeStr, String afterStr) { 

  return payloadStr.substring(payloadStr.indexOf(beforeStr) + beforeStr.length(), payloadStr.indexOf(afterStr)); 

}


:
Posted by 클레잇