달력

4

« 2024/4 »

  • 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



#include <Arduino.h>

#include <ESP8266WiFi.h>

#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <Wire.h> 

#include <LiquidCrystal_I2C.h>


#define USE_SERIAL Serial


ESP8266WiFiMulti WiFiMulti;


LiquidCrystal_I2C lcd(0x3f,20,4);


void setup() {

    USE_SERIAL.begin(115200);

    lcd.init();

    lcd.backlight();

    lcd.setCursor(0,1);

    

    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((WiFiMulti.run() == WL_CONNECTED)) {

        HTTPClient http;


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

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


        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();

    }

    delay(60000);

}



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

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

}


:
Posted by 클레잇