Advertisement
learnelectronics

Esp8266 WiFi Weather

May 22nd, 2017
4,527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.24 KB | None | 0 0
  1. /*
  2.  * Esp8266 WiFi Weather
  3.  * Adapted from a sketch by Nick of Educ8s.tv
  4.  *
  5.  * 21 MAY 2017
  6.  *
  7.  * www.youtube.com/c/learnelectronics
  8.  */
  9.  
  10. #include <ESP8266WiFi.h>                                    //wifi library
  11. #include <Wire.h>                                           //I2C library                                
  12. #include <Adafruit_SSD1306.h>                               //oled driver
  13. #include <ArduinoJson.h>                                    //json library
  14.  
  15.  
  16. Adafruit_SSD1306 display(LED_BUILTIN);                      //create instance of SSD1306 called display
  17.  
  18. const char* ssid     = "ATT6s3ENT4";                        // SSID of local network
  19. const char* password = "69z5f2s4+e4p";                      // Password on network
  20. String APIKEY = "cf3d2e98ed2b74c727655849c4d78bf0";         //openweathermap.org api key
  21. String CityID = "5174095";                                  //my city id
  22.  
  23.  
  24.  
  25. WiFiClient client;                                          //start wifi client
  26. char servername[]="api.openweathermap.org";                 //specify server name
  27. String result;                                              //string var to hold result
  28.  
  29. int  counter = 60;                                          //counter var
  30.  
  31. String weatherDescription ="";                              //empty string to hold descrip
  32. String weatherLocation = "";                                //empty string to hold location            
  33. String Country;                                             //dl'd var for country
  34. float Temperature;                                          //dl'd var for temp
  35. float Humidity;                                             //dl'd var for humidity
  36. float Pressure;                                             //dl'd var for pressure
  37.  
  38.  
  39. void setup() {
  40.   Serial.begin(9600);                                       //start serial comms for debug
  41.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);                //start the OLED @ Hex addy 0x3C
  42.   display.display();                                        //show the Adafruit Logo
  43.   delay(2);                                                 //for 2 milli seconds
  44.   display.clearDisplay();                                   //clear display @ beginning of each loop
  45.   display.setTextSize(1);                                   //set smallest text size
  46.   display.setTextColor(WHITE);                              //set text color to WHITE
  47.   display.setCursor(0,0);                                   //cursor to uper left
  48.   display.print("   Connecting");                           //setup info
  49.   Serial.println("Connecting");                             //setup info
  50.   WiFi.begin(ssid, password);                               //connect to wifi
  51.  
  52.  
  53.   display.display();                                        //show display
  54.   delay(1000);                                              //wait
  55.   display.clearDisplay();                                   //clear display
  56.   display.print("Connected!");                              //show info
  57.   Serial.println("Connected");                              //show info
  58.   delay(1000);                                              //wait
  59.   display.display();                                        //show display
  60.   delay(1000);                                              //wait
  61.   display.clearDisplay();                                   //clear display
  62.  
  63. }
  64.  
  65. void loop() {
  66.     if(counter == 60)                                       //Get new data every 10 minutes
  67.     {
  68.       counter = 0;                                          //reset counter
  69.       displayGettingData();                                 //callfunction
  70.       delay(1000);                                          //wait
  71.       getWeatherData();                                     //call function
  72.     }else                                                   //otherwise
  73.     {
  74.       counter++;                                            //increment counter
  75.       displayWeather(weatherLocation,weatherDescription);   //call function
  76.       delay(5000);                                          //wait
  77.       displayConditions(Temperature,Humidity,Pressure);     //callfunction
  78.       delay(5000);                                          //wait
  79.     }
  80. }
  81.  
  82. void getWeatherData()                                       //client function to send/receive GET request data.
  83. {
  84.   if (client.connect(servername, 80)) {                     //starts client connection, checks for connection
  85.     client.println("GET /data/2.5/weather?id="+CityID+"&units=metric&APPID="+APIKEY);
  86.     client.println("Host: api.openweathermap.org");
  87.     client.println("User-Agent: ArduinoWiFi/1.1");
  88.     client.println("Connection: close");
  89.     client.println();
  90.   }
  91.   else {
  92.     Serial.println("connection failed");                    //error message if no client connect
  93.     Serial.println();
  94.   }
  95.  
  96.   while(client.connected() && !client.available()) delay(1); //waits for data
  97.   while (client.connected() || client.available()) {         //connected or data available
  98.     char c = client.read();                                  //gets byte from ethernet buffer
  99.       result = result+c;
  100.     }
  101.  
  102.   client.stop();                                             //stop client
  103.   result.replace('[', ' ');
  104.   result.replace(']', ' ');
  105.   Serial.println(result);
  106.  
  107. char jsonArray [result.length()+1];
  108. result.toCharArray(jsonArray,sizeof(jsonArray));
  109. jsonArray[result.length() + 1] = '\0';
  110.  
  111. StaticJsonBuffer<1024> json_buf;
  112. JsonObject &root = json_buf.parseObject(jsonArray);
  113. if (!root.success())
  114. {
  115.   Serial.println("parseObject() failed");
  116. }
  117.  
  118. String location = root["name"];
  119. String country = root["sys"]["country"];
  120. float temperature = root["main"]["temp"];
  121. float humidity = root["main"]["humidity"];
  122. String weather = root["weather"]["main"];
  123. String description = root["weather"]["description"];
  124. float pressure = root["main"]["pressure"];
  125.  
  126. weatherDescription = description;
  127. weatherLocation = location;
  128. Country = country;
  129. Temperature = temperature;
  130. Humidity = humidity;
  131. Pressure = pressure;
  132.  
  133. }
  134.  
  135. void displayWeather(String location,String description)          //show location & weather function
  136. {
  137.   display.clearDisplay();
  138.   display.setCursor(0,0);
  139.   display.print(location);
  140.   display.print(", ");
  141.   display.println(Country);
  142.   display.println(description);
  143.   display.display();
  144.   delay(2000);
  145. }
  146.  
  147. void displayConditions(float Temperature,float Humidity, float Pressure) //show conditions function
  148. {
  149.   display.clearDisplay();
  150.   display.setCursor(0,0);
  151.   display.print("Temp     :");
  152.  display.print(Temperature,1);
  153.  //display.print((char)223);
  154.  display.println("C ");
  155.  
  156.                                                                         //Printing Humidity
  157.  display.print("Humidity :");
  158.  display.print(Humidity,0);
  159.  display.println(" %");
  160.  
  161.                                                                         //Printing Pressure
  162.  display.print("Pressure :");
  163.  display.print(Pressure,1);
  164.  display.println(" hPa");
  165. delay(5000);
  166. display.display();
  167. }
  168.  
  169. void displayGettingData()                                               //getting data display function
  170. {
  171.   display.clearDisplay();
  172.   display.setCursor(0,0);
  173.   display.print("Getting data");
  174.   display.display();
  175.   delay(1000);
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement