Rizwan Ahmed
Published

ESP-01 based Weather Notifier

My first IoT Project which is based on weather notifier that provides me the essential information related to today's weather.

IntermediateFull instructions provided3 hours3,119
ESP-01 based Weather Notifier

Things used in this project

Hardware components

ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
5 mm LED: Green
5 mm LED: Green
×1
0.96" OLED 64x128 Display Module
ElectroPeak 0.96" OLED 64x128 Display Module
×1
mic29150
×1
Capacitor 100 µF
Capacitor 100 µF
×1
Slide Switch
Slide Switch
×2
tp4056 LiPo battery module
×1
DC POWER JACK 2.1MM BARREL-TYPE PCB MOUNT
TaydaElectronics DC POWER JACK 2.1MM BARREL-TYPE PCB MOUNT
×1
Male-Header 36 Position 1 Row- Long (0.1")
Male-Header 36 Position 1 Row- Long (0.1")
×1
2 pin connector
×2
18650 battery case
×1
18650 battery
×1
push button 6mm
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Weather Station

Code

Weather Station

Arduino
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET  LED_BUILTIN   //Reset pin for oled
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
int button = 1;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const char *ssid = "YOUR_SSID_NAME";
const char *password = "YOUR_SSID_PASWORD";

String Location = "YOUR_LOCATION";   //Mine was "Karachi.pk"
String API_Key = "YOUR_API"; // Enter a 32 digit API
int httpCode;
String payload;
void setup()
{
  pinMode (button, INPUT_PULLUP);
  Serial.begin(115200);
  delay(1000);
  Wire.begin(0, 2);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //turning on the oled

  display.clearDisplay();
  display.setTextColor(WHITE, BLACK);
  display.setTextSize(1);
  display.setCursor(20, 0);
  display.println("Weather Station");
  display.setCursor(15, 18);
  display.print("By Rizwan Ahmed");   // To make it intresting, you an change the name, and make your own. 
  display.display();
  delay(1000);

  display.clearDisplay();
  display.display();

  WiFi.begin(ssid, password);

  //Serial.println("Connecting");
  display.setCursor(0, 24);
  display.println("Connecting");
   while (WiFi.status() != WL_CONNECTED)
   {
    delay(500);
    //Serial.print("*");
    display.print("*");
   }
   //Serial.println("Connected");
   display.print("Server Connected");
   display.display();  //to clear the old screen
   delay(1000);
}

void loop ()
{
  if (WiFi.status() == WL_CONNECTED)
  {
    HTTPClient http;

    http.begin("http://api.openweathermap.org/data/2.5/weather?q=" + Location + "&APPID=" + API_Key);
    httpCode = http.GET();

    if (httpCode > 0)
    {
      payload = http.getString();
      
      DynamicJsonBuffer jsonBuffer(512);

      JsonObject& root = jsonBuffer.parseObject(payload);
      if (!root.success())
      {
        Serial.println(F("Paring failed"));
        return; 
      }
      float temp = (float)(root["main"]["temp"]) - 273.15;
      int humidity = root ["main"]["humidity"];
      float pressure = (float)(root["main"]["pressure"]) / 1000;
      float wind_speed = root["wind"]["speed"];
      int wind_degree = root["wind"]["deg"];

      Serial.printf("Temperature = %f°C\r\n",temp);
      Serial.printf("Humidiy = %d %%\r\n", humidity);
      Serial.printf("Pressure = %.3f bar \r\n", pressure);
      Serial.printf("Wind Speed = %.1f m/s\r\n", wind_speed);
      Serial.printf("Wind Degree = %d`\r\n\r\n", wind_degree);

      display.clearDisplay();
      display.display();

      if (digitalRead(button) == HIGH)
      {
        display.setCursor(15, 0);
        display.print("Station - Karachi");  // You can add your city name here
        display.setCursor(0, 12);
        display.print("Creator: Rizwan Ahmed");  // You can add your name here
        display.setCursor(0, 24);
        display.print("Temperature :");
        display.setCursor(77, 24);
        display.print(temp);
        display.print("C");
        display.setCursor(0, 32);
        display.print("Humidity    :");
        display.setCursor(77, 32);
        display.print(humidity);
        display.print("%");
        display.setCursor(0, 40); 
        display.print("Pressure    :");
        display.setCursor(77, 40); 
        display.print(pressure, 3);
        display.print("bar");
        display.setCursor(0, 48);
        display.print("Wind speed  :");
        display.setCursor(77, 48);
        display.print(wind_speed, 1);
        display.print("m/s");
        display.setCursor(0, 56);
        display.print("Wind degree :");
        display.setCursor(77, 56);
        display.println(wind_degree);
        display.drawRect(109, 24, 3, 3, WHITE); // put degree symbol ( ° )
        display.drawRect(97, 56, 3, 3, WHITE);
        display.display();
        delay(2000);
      }
      else if (digitalRead(button) == LOW)
      {
        display.clearDisplay();
        display.display();
      }
    }

    http.end();
  }
  delay(60000);
}

Credits

Rizwan Ahmed

Rizwan Ahmed

4 projects • 1 follower
A junior IoT engineer who likes to play with the Arduino and Raspberry Pi to gain skills and knowledge

Comments