• LearnElectronics

IoT Weather Station project using NodeMCU

Code:

/*
    HTTP over TLS (HTTPS) example sketch

    This code demonstrates how to use
    WiFiClientSecure, ArduinoJson to access and parse HTTPS API.
    We fetch and display the status of real time weather
    of a particular location in India.
    Using esp8266/NodeMCU board.

    Created by HARSHIT GUPTA Electronics Hobbyist & Electronics Engineer, 2020.
    This example is in public domain.
*/

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>              // for transferring serial data 

const char* ssid = "vivo 1801";
const char* password = "harshit123";

const char* host = "api.openweathermap.org";
const int httpsPort = 443;

// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char* fingerprint = "eeaa586d4f1f42f4185b7fb0f20a4cdd97477d99";

const int Time = 600000;                               // 10 minutes in milli second
unsigned long CurrentTime , PreviousTime = 0;

String weather_Location, weather_country, weather_description;
String country;

int flag = 1;
float clouds, wind, Temperature, humidity, pressure;

LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup() {
  Serial.begin(115200);
  lcd.begin();
  Wire.begin(D2, D1);
  
  Serial.println();
  Serial.print("connecting to ");
  lcd.clear();
  lcd.setCursor(3,1);
  lcd.print("connecting....");
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  lcd.clear();
  lcd.setCursor(5,1);
  lcd.print("connected");

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  delay(1000);

  lcd.clear();
  lcd.setCursor(5, 1);
  lcd.print("Real Time");
  lcd.setCursor(3, 2);
  lcd.print("Weather Station");

  OpenWeatherData();
}


void loop()
{
  CurrentTime = millis();
  if (CurrentTime - PreviousTime >= Time)
  {
    Serial.println(CurrentTime);
    OpenWeatherData();
    PreviousTime = CurrentTime;
  }
  else
  {
    lcd.clear();
    lcd.setCursor(2, 1);
    lcd.print(weather_Location + ",");
    lcd.print(weather_country);
    lcd.setCursor(2, 2);
    lcd.print(weather_description);
    delay(3000);
    
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(" Temp  : ");
    lcd.print(Temperature-273,1);
    lcd.print((char)223);
    lcd.print("C");
    
    lcd.setCursor(0,1);
    lcd.print(" Pres  : ");
    lcd.print(pressure,1);
    lcd.print(" hPa");
    
    lcd.setCursor(0,2);
    lcd.print(" Humi  : ");
    lcd.print(humidity,1);
    lcd.print("%");
    
    lcd.setCursor(0,3);
    lcd.print(" Wind  : ");
    lcd.print(wind);
    lcd.print(" m/s");
    delay(10000);
    
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(" Clouds  : ");
    lcd.print(clouds,1);
    lcd.print("%");
    delay(4000);
    
  }
}

void OpenWeatherData()
{
  // Use WiFiClientSecure class to create TLS connection
  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);

  Serial.printf("Using fingerprint :'%s'\n", fingerprint);
  client.setFingerprint(fingerprint);

  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }

  String url = "/data/2.5/weather?q=your_city&APPID=xxxxxxxxx YOUR APT KEY xxxxxxxxxxx";

  Serial.print("requesting URL: ");
  Serial.println(url);

  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: BuildFailureDetectorESP8266\r\n" +
               "Connection: close\r\n\r\n");

  Serial.println("request sent");

  while (client.connected()) 
  {
    if (flag == 1)
    {
      lcd.clear();
      lcd.setCursor(4, 1);
      lcd.print("Fetching.....");
      lcd.setCursor(0, 2);
      lcd.print("Current Weather Data");
      delay(3000);
      lcd.clear();
      lcd.setCursor(7, 1);
      lcd.print("From:");
      lcd.setCursor(3, 2);
      lcd.print("openWeatherMap");
      flag = 0;
    }
    String line = client.readStringUntil('\n');
    if (line == "\r") 
    {
      Serial.println("headers received");
      flag = 1;
      break;
    }
  }

  String line = client.readStringUntil('\n');
  if (line.startsWith("{\"coord\"")) {
    Serial.println("esp8266 CI successfull!");
  } else {
    Serial.println("esp8266 CI has failed");
  }
  Serial.println("reply was:");
  Serial.println("==========");
  Serial.println(line);
  Serial.println("==========");
  Serial.println("closing connection");

  line.replace('[', ' ');
  line.replace(']', ' ');

  Serial.println(line);

  const int BUFFER_SIZE = 1024;

  StaticJsonDocument<BUFFER_SIZE> doc;

  char json[line.length() + 1];
  line.toCharArray(json, sizeof(json));
  json[line.length() + 1] = '\0';

  DeserializationError error = deserializeJson(doc, json);

  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return;
  }

  String Location = doc["name"];
  String country = doc["sys"]["country"];
  String description = doc["weather"]["description"];
  Temperature = doc["main"]["temp"];
  humidity = doc["main"]["humidity"];
  pressure = doc["main"]["pressure"];
  wind = doc["wind"]["speed"];
  clouds = doc["clouds"]["all"];


  weather_Location = Location;
  weather_country = country;
  weather_description = description;

  Serial.println(Location);
  Serial.println(country);
  Serial.println(description);
  Serial.println(Temperature);
  Serial.println(humidity);
  Serial.println(pressure);
  Serial.println(wind);
  Serial.println(clouds);
}


5 views
  • Facebook
  • YouTube
  • Instagram
  • LinkedIn
LearnElectronics
Contact Us
© Copyright - LearnElectronics 2021