MyEngineeringStuffs
Published © LGPL

ESP8266-01 Based Weather Forecasting

This project demonstrates how we can fetch the weather data from the website OpenWeatherMap using the API key of that site.

IntermediateShowcase (no instructions)8 hours4,904
ESP8266-01 Based Weather Forecasting

Things used in this project

Story

Read more

Schematics

ESP 8266 WIFI MODULE WEATHER FORECASTING

Code

ESP 8266 WIFI MODULE WEATHER FORECASTING

C/C++
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h> 


WiFiClient client;
WiFiClientSecure sclient;
LiquidCrystal_I2C lcd(0x3F,16,2);


String weatherMain = "";
String weatherDescription = "";
String weatherLocation = "";
String country;
int humidity;
int pressure;
int temp;
int tempMin, tempMax;
int clouds;
int windSpeed;
String weatherString;

char tempT[5],tempMinT[5],tempMaxT[5];
char humi[5],presS[5],cloudS[5],WindSpeed[5];


// =======================================================================
// Your config below!
// =======================================================================
const char* ssid     = "YOUR_SSID_NAME";      // SSID of local network
const char* password = "YOUR_SSID_PASSWORD";    // Password on network

//openweathermap.org api key:8b2f088e8a7A307c759ef39dcb085ae4
String weatherKey  = "YOUR_API_KEY";   // openweathermap.org key
String weatherLang = "&lang=en";  //  english or other language

const char *weatherHost = "api.openweathermap.org";
// =======================================================================
byte up[8] = {
                      0b00100,
                      0b01110,
                      0b10101,
                      0b00100,
                      0b00100,
                      0b00100,
                      0b00100,
                      0b00000
                    };
  byte down[8] = {
                        0b00000,
                        0b00100,
                        0b00100,
                        0b00100,
                        0b00100,
                        0b10101,
                        0b01110,
                        0b00100
                      };           


void setup() {
  Wire.begin(2, 0); //SDA-2 , SCL-0
  Serial.begin(115200);
  lcd.begin(); // initializing the LCD
  lcd.display(); //Enable or turn on the text
  lcd.backlight(); // Enable or Turn On the backlight 
  lcd.createChar(1,up);
  lcd.createChar(2,down);
  Serial.print("Connecting to WiFi ");
  lcd.setCursor(0,0);
  lcd.print("Connecting to...");
  lcd.setCursor(0,1);
  lcd.print("      WiFi      ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  lcd.clear();
  delay(2000);
  Serial.println("");
  Serial.print("MyIP: "); Serial.println(WiFi.localIP());
  lcd.setCursor(0,0);
  lcd.print("MY Local IP: "); 
  lcd.setCursor(0,1);
  lcd.println(WiFi.localIP());
  delay(4000);
  lcd.clear();
}

// =======================================================================
//this void function is really usetherful; it adds a "0" to the beginning of the number, 
//so that 5 minutes is displayed as "05", ra than "5 "
void printDigits2(int digits)  
{
  if(digits < 10) 
  {
    lcd.print("0");
    lcd.print(digits);
  }
  else
  {
    lcd.print(digits);
  }
}
void pressHumiCloud(){
  lcd.setCursor(2,1);
  lcd.print("Pressure:"); 
  lcd.setCursor(11,1);
  printDigits2(pressure);
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("                ");
  lcd.setCursor(2,1);
  lcd.print("Humidity:"); 
  lcd.setCursor(11,1);
  printDigits2(humidity);
  lcd.print("%");
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("                ");
  lcd.setCursor(1,1);
  lcd.print("WindSpeed:"); 
  lcd.setCursor(11,1);
  printDigits2(windSpeed);
  lcd.print("m/s");
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("                ");
  lcd.setCursor(2,1);
  lcd.print("Clouds:"); 
  lcd.setCursor(9,1);
  printDigits2(clouds);
  lcd.print("%"); 
  delay(1000);
}
void getWeatherData()
{
  Serial.print("connecting to "); Serial.println(weatherHost);
  delay(4000);
  lcd.clear();
  if (client.connect(weatherHost, 80)) {
    client.println(String("GET /data/2.5/weather?q=Bhubaneshwar,IN&") + "&units=metric&appid=" + weatherKey + weatherLang + "\r\n" +
                "Host: " + weatherHost + "\r\nUser-Agent: ArduinoWiFi/1.1\r\n" +
                "Connection: close\r\n\r\n");
  } else {
    Serial.println("connection failed");
    return;
  }
  String line;
  int repeatCounter = 0;
  while (!client.available() && repeatCounter < 10) {
    delay(500);
    Serial.println("w.");
    repeatCounter++;
  }
  while (client.connected() && client.available()) {
    char c = client.read(); 
    if (c == '[' || c == ']') c = ' ';
    line += c;
  }

  client.stop();

  DynamicJsonBuffer jsonBuf;
  JsonObject &root = jsonBuf.parseObject(line);
  if (!root.success())
  {
    Serial.println("parseObject() failed");
    return;
  }

  weatherDescription = root["weather"]["description"].as<String>();
  weatherDescription.toLowerCase();
  temp = root["main"]["temp"];
  humidity = root["main"]["humidity"];
  pressure = root["main"]["pressure"];
  tempMin = root["main"]["temp_min"];
  tempMax = root["main"]["temp_max"];
  windSpeed = root["wind"]["speed"];
  clouds = root["clouds"]["all"];
  Serial.println(String("temp=")+temp);
  Serial.println(String("temp_min=")+tempMin);
  Serial.println(String("temp_max=")+tempMax);
  Serial.println(String("pressure=")+pressure);
  Serial.println(String("humidity=")+humidity);
  Serial.println(String("wind_speed=")+windSpeed);
  Serial.println(String("clouds=")+clouds);
  lcd.setCursor(0,0);
  lcd.print("TEMP:"); 
  lcd.setCursor(5,0);
  lcd.print(temp);
  lcd.setCursor(7,0);
  lcd.print(",");
  lcd.setCursor(8,0);
  lcd.print(tempMin);
  lcd.setCursor(10,0);  
  lcd.write(2);
  lcd.setCursor(11,0);
  lcd.print(",");
  lcd.setCursor(12,0);
  lcd.print(tempMax);
  lcd.setCursor(14,0);  
  lcd.write(1);
  pressHumiCloud();
}


// =======================================================================

void loop()
{
  
   getWeatherData();
   delay(5000);
}

Credits

MyEngineeringStuffs

MyEngineeringStuffs

13 projects โ€ข 71 followers
I ๐Ÿ˜ to help people, students are always FIRST, and Yes๐Ÿ‘‰https://paypal.me/pawanbehera1 you can buy me a COFFEE if U๐Ÿ˜ my WORK, NAMASTE ๐Ÿ™

Comments