Astranome

esp8288_1602_weather_informer

Jun 7th, 2020
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.93 KB | None | 0 0
  1. #include <SPI.h>
  2.  
  3. #include <Wire.h>
  4.  
  5. // #include <WiFi.h>
  6. #include <LiquidCrystal_I2C.h>
  7. #include <ArduinoJson.h>
  8. #include <ESP8266WiFi.h>
  9.  
  10. LiquidCrystal_I2C lcd(0x27, 16, 2);
  11. const char* ssid     = "Keenetic-2295";      // SSID of local network
  12. const char* password = "00000000";   // Password on network
  13. String APIKEY = "ca3c93f21b9ff85c73b74a8b13a14fd9";
  14. String CityID = "571476"; //BRN
  15. const char* host = "api.openweathermap.org";
  16. String result;
  17. // WiFiClient client;
  18.  
  19. // char host[] = "api.openweathermap.org"; // remote server we will connect to
  20.  
  21.  
  22. int  counter = 60;
  23.  
  24. String weatherDescription = "";
  25. String weatherLocation = "";
  26. String Country;
  27. float Temperature;
  28. float Humidity;
  29. float Pressure;
  30. float Wind;
  31. float Clouds;
  32.  
  33.  
  34.  
  35. void setup() {
  36.   Serial.begin(115200);
  37.   Serial.println();
  38.   lcd.init();
  39.   lcd.backlight();
  40.   lcd.clear();
  41.  
  42.   // connect to wifi.
  43.   WiFi.begin(ssid, password);
  44.   lcd.print("   Connecting");
  45.   Serial.println("Connecting");
  46.  
  47.  
  48.    while (WiFi.status() != WL_CONNECTED) {
  49.     Serial.print(".");
  50.     lcd.print(".");
  51.     delay(500);
  52.   }
  53.  
  54.   lcd.clear();
  55.   lcd.print("   Connected!");
  56.   Serial.println("Connected");
  57.   delay(1000);
  58.  
  59. }
  60.  
  61. void loop() {
  62.   if (counter == 60) //Get new data every 10 minutes
  63.   {
  64.     counter = 0;
  65.     displayGettingData();
  66.     delay(1000);
  67.     getWeatherData();
  68.   } else
  69.   {
  70.     counter++;
  71.     displayWeather(weatherLocation, weatherDescription);
  72.     delay(5000);
  73.     serialMonitorData();
  74.     displayConditions(Temperature, Humidity, Pressure, Wind, Clouds);
  75.     delay(5000);
  76.   }
  77. }
  78.  
  79. void getWeatherData() //client function to send/receive GET request data.
  80. {
  81.    WiFiClient client;
  82.   if (client.connect(host, 80)) {  //starts client connection, checks for connection
  83.     client.println("GET /data/2.5/weather?id=" + CityID + "&units=metric&APPID=" + APIKEY);
  84.     client.println("Host: api.openweathermap.org");
  85.     client.println("User-Agent: ArduinoWiFi/1.1");
  86.     client.println("Connection: close");
  87.     client.println();
  88.   }
  89.   else {
  90.     Serial.println("connection failed"); //error message if no client connect
  91.     Serial.println();
  92.   }
  93.  
  94.   while (client.connected() && !client.available()) delay(1); //waits for data
  95.   while (client.connected() || client.available()) { //connected or data available
  96.     char c = client.read(); //gets byte from ethernet buffer
  97.     result = result + c;
  98.   }
  99.  
  100.   client.stop(); //stop client
  101.   result.replace('[', ' ');
  102.   result.replace(']', ' ');
  103.   Serial.println(result);
  104.  
  105.   char jsonArray [result.length() + 1];
  106.   result.toCharArray(jsonArray, sizeof(jsonArray));
  107.   jsonArray[result.length() + 1] = '\0';
  108.  
  109.   StaticJsonBuffer<1024> json_buf;
  110.   JsonObject &root = json_buf.parseObject(jsonArray);
  111.   if (!root.success())
  112.   {
  113.     Serial.println("parseObject() failed");
  114.   }
  115.  
  116.   String location = root["name"];
  117.   String country = root["sys"]["country"];
  118.   float temperature = root["main"]["temp"];
  119.   float humidity = root["main"]["humidity"];
  120.   String weather = root["weather"]["main"];
  121.   String description = root["weather"]["description"];
  122.   float pressure = root["main"]["pressure"];
  123.   float wind = root["wind"]["speed"];
  124.   float clouds = root["clouds"]["all"];
  125.  
  126.   weatherDescription = description;
  127.   weatherLocation = location;
  128.   Country = country;
  129.   Temperature = temperature;
  130.   Humidity = humidity;
  131.   Pressure = pressure;
  132.   Wind = wind;
  133.   Clouds = clouds;
  134. }
  135.  
  136. void displayWeather(String location, String description)
  137. {
  138.   lcd.clear();
  139.   lcd.setCursor(0, 0);
  140.   lcd.print(location);
  141.   lcd.print(", ");
  142.   lcd.print(Country);
  143.   lcd.setCursor(0, 1);
  144.   lcd.print(description);
  145.  
  146. }
  147.  
  148. void displayConditions(float Temperature, float Humidity, float Pressure, float Wind, float Clouds)
  149. {
  150.   lcd.clear();
  151.   lcd.print("Temp : ");
  152.   lcd.print(Temperature, 1);
  153.   lcd.print((char)223);
  154.   lcd.print("C ");
  155.  
  156.   //Printing Humidity
  157.   lcd.setCursor(0, 1);
  158.   lcd.print("Hum  : ");
  159.   lcd.print(Humidity, 1);
  160.   lcd.print("%");
  161.  
  162.   delay(5000);
  163.   lcd.clear();
  164.  
  165.  //Printing clouds
  166.   lcd.setCursor(0, 0);
  167.   lcd.print("Clouds : ");
  168.   lcd.print(Clouds, 1);
  169.   lcd.print(" %");
  170.  
  171.   //Printing Wind
  172.   lcd.setCursor(0, 1);
  173.   lcd.print("Wind : ");
  174.   lcd.print(Wind, 1);
  175.   lcd.print(" m/s");
  176.  
  177.   delay(5000);
  178.   lcd.clear();
  179.  
  180.  
  181.  
  182.   //Printing Pressure
  183.   lcd.setCursor(0, 0);
  184.   lcd.print("Pres : ");
  185.   lcd.setCursor(0, 1);
  186.   lcd.print(Pressure, 1);
  187.   lcd.print(" hPa");
  188.  
  189.  
  190.  
  191. }
  192.  
  193. void displayGettingData()
  194. {
  195.   lcd.clear();
  196.   lcd.print("Getting data");
  197. }
  198.  
  199. void serialMonitorData()
  200. {
  201.   Serial.print("Temp : ");
  202.   Serial.print(Temperature, 1);
  203.   Serial.print("*C ");
  204.  
  205.   Serial.print("  Hum  : ");
  206.   Serial.print(Humidity, 1);
  207.   Serial.print("%");
  208.  
  209.   Serial.print(" Pres : ");
  210.   Serial.print(Pressure, 1);
  211.   Serial.print(" hPa");
  212.  
  213.   Serial.print("  Wind : ");
  214.   Serial.print(Wind, 1);
  215.   Serial.print(" m/s");
  216.  
  217.   Serial.print("  Clouds : ");
  218.   Serial.print(Clouds, 1);
  219.   Serial.println(" %");
  220. }
Add Comment
Please, Sign In to add comment