아두이노와 날씨API를 이용한 날씨 알리미(Wemos-D1) Project/Weather Display2017. 10. 12. 02:32
#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)); } |
'Project > Weather Display' 카테고리의 다른 글
아두이노와 날씨API를 이용한 날씨 알리미(Wemos-D1, 리모컨추가) (0) | 2017.10.12 |
---|---|
아두이노와 날씨API를 이용한 날씨 알리미 (2) | 2017.09.30 |