Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Esp8266 WiFi Weather
- * Adapted from a sketch by Nick of Educ8s.tv
- *
- * 21 MAY 2017
- *
- * www.youtube.com/c/learnelectronics
- */
- #include <ESP8266WiFi.h> //wifi library
- #include <Wire.h> //I2C library
- #include <Adafruit_SSD1306.h> //oled driver
- #include <ArduinoJson.h> //json library
- Adafruit_SSD1306 display(LED_BUILTIN); //create instance of SSD1306 called display
- const char* ssid = "ATT6s3ENT4"; // SSID of local network
- const char* password = "69z5f2s4+e4p"; // Password on network
- String APIKEY = "cf3d2e98ed2b74c727655849c4d78bf0"; //openweathermap.org api key
- String CityID = "5174095"; //my city id
- WiFiClient client; //start wifi client
- char servername[]="api.openweathermap.org"; //specify server name
- String result; //string var to hold result
- int counter = 60; //counter var
- String weatherDescription =""; //empty string to hold descrip
- String weatherLocation = ""; //empty string to hold location
- String Country; //dl'd var for country
- float Temperature; //dl'd var for temp
- float Humidity; //dl'd var for humidity
- float Pressure; //dl'd var for pressure
- void setup() {
- Serial.begin(9600); //start serial comms for debug
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //start the OLED @ Hex addy 0x3C
- display.display(); //show the Adafruit Logo
- delay(2); //for 2 milli seconds
- display.clearDisplay(); //clear display @ beginning of each loop
- display.setTextSize(1); //set smallest text size
- display.setTextColor(WHITE); //set text color to WHITE
- display.setCursor(0,0); //cursor to uper left
- display.print(" Connecting"); //setup info
- Serial.println("Connecting"); //setup info
- WiFi.begin(ssid, password); //connect to wifi
- display.display(); //show display
- delay(1000); //wait
- display.clearDisplay(); //clear display
- display.print("Connected!"); //show info
- Serial.println("Connected"); //show info
- delay(1000); //wait
- display.display(); //show display
- delay(1000); //wait
- display.clearDisplay(); //clear display
- }
- void loop() {
- if(counter == 60) //Get new data every 10 minutes
- {
- counter = 0; //reset counter
- displayGettingData(); //callfunction
- delay(1000); //wait
- getWeatherData(); //call function
- }else //otherwise
- {
- counter++; //increment counter
- displayWeather(weatherLocation,weatherDescription); //call function
- delay(5000); //wait
- displayConditions(Temperature,Humidity,Pressure); //callfunction
- delay(5000); //wait
- }
- }
- void getWeatherData() //client function to send/receive GET request data.
- {
- if (client.connect(servername, 80)) { //starts client connection, checks for connection
- client.println("GET /data/2.5/weather?id="+CityID+"&units=metric&APPID="+APIKEY);
- client.println("Host: api.openweathermap.org");
- client.println("User-Agent: ArduinoWiFi/1.1");
- client.println("Connection: close");
- client.println();
- }
- else {
- Serial.println("connection failed"); //error message if no client connect
- Serial.println();
- }
- while(client.connected() && !client.available()) delay(1); //waits for data
- while (client.connected() || client.available()) { //connected or data available
- char c = client.read(); //gets byte from ethernet buffer
- result = result+c;
- }
- client.stop(); //stop client
- result.replace('[', ' ');
- result.replace(']', ' ');
- Serial.println(result);
- char jsonArray [result.length()+1];
- result.toCharArray(jsonArray,sizeof(jsonArray));
- jsonArray[result.length() + 1] = '\0';
- StaticJsonBuffer<1024> json_buf;
- JsonObject &root = json_buf.parseObject(jsonArray);
- if (!root.success())
- {
- Serial.println("parseObject() failed");
- }
- String location = root["name"];
- String country = root["sys"]["country"];
- float temperature = root["main"]["temp"];
- float humidity = root["main"]["humidity"];
- String weather = root["weather"]["main"];
- String description = root["weather"]["description"];
- float pressure = root["main"]["pressure"];
- weatherDescription = description;
- weatherLocation = location;
- Country = country;
- Temperature = temperature;
- Humidity = humidity;
- Pressure = pressure;
- }
- void displayWeather(String location,String description) //show location & weather function
- {
- display.clearDisplay();
- display.setCursor(0,0);
- display.print(location);
- display.print(", ");
- display.println(Country);
- display.println(description);
- display.display();
- delay(2000);
- }
- void displayConditions(float Temperature,float Humidity, float Pressure) //show conditions function
- {
- display.clearDisplay();
- display.setCursor(0,0);
- display.print("Temp :");
- display.print(Temperature,1);
- //display.print((char)223);
- display.println("C ");
- //Printing Humidity
- display.print("Humidity :");
- display.print(Humidity,0);
- display.println(" %");
- //Printing Pressure
- display.print("Pressure :");
- display.print(Pressure,1);
- display.println(" hPa");
- delay(5000);
- display.display();
- }
- void displayGettingData() //getting data display function
- {
- display.clearDisplay();
- display.setCursor(0,0);
- display.print("Getting data");
- display.display();
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement