shubham gupta
Published

Live Weather Update with Display for Multiple Cities

The project displays the weather forecast (i. e the current temperature and concise weather description) on a 16x2 LCD Display.

BeginnerShowcase (no instructions)260
Live Weather Update with Display for Multiple Cities

Things used in this project

Story

Read more

Code

Multi_City_Weather_Display.ino

Arduino
#include <ESP8266WiFi.h>
#include <LiquidCrystal.h>

// Time delays
const long refreshInterval = 1 * 60 * 1000; //milliseconds. Current refresh rate is once a minute
long long lastRefresh = -1 * refreshInterval;

// WiFi Connection details
const char ssid[] = "YOUR_SSID";
const char password[] = "YOUR_PASSWORD";

// Website details
const char hostname[] = "api.openweathermap.org";
String uri;
const int port = 80;

// City Info
const int numberOfCities = 4; 
String cityNames[numberOfCities] = {"Delhi", "Mumbai", "Abu Dhabi", "Aachen"};
String cityLinks[numberOfCities] = {"/data/2.5/weather?q=Delhi,in&appid=YOUR_API_KEY",
                                    "/data/2.5/weather?q=Mumbai,in&appid=YOUR_API_KEY",
                                    "/data/2.5/weather?q=Abu%20Dhabi,ae&appid=YOUR_API_KEY",
                                    "/data/2.5/weather?q=Aachen,de&appid=YOUR_API_KEY"};
int cityNum = 0;

// WiFi Client declaration
WiFiClient client;

// LCD object
LiquidCrystal lcd(4,5,12,13,14,15);
  /* Connect ESP8266 pin 4 -> RS pin on LCD
                     pin 5 -> E pin
                     pin 12 -> D4
                     pin 13 -> D5
                     pin 14 -> D6
                     pin 15 -> D7
     To find out which pin is which on your ESP8266, google "ESP8266 Nodemcu v2 pin layout" */

// Button Info
const int buttonPin = 2;
int btn_state = HIGH;
int btn_prev = HIGH;
const int debounce_delay = 40;
int last_debounce_time;
bool cityChanged = false;

void setup(){
  Serial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP);
  delay(500);
  
  connectToWiFi(ssid, password);
  lcd.begin(16,2);
}

void loop(){
  int btn_read = digitalRead(buttonPin);
  if(btn_read != btn_prev){
    last_debounce_time = millis();
  }

  if(millis() > last_debounce_time + debounce_delay){
    if(btn_state != btn_read){
      btn_state = btn_read;
      if(btn_state == LOW){
        cityNum = (cityNum + 1) % numberOfCities;
        cityChanged = true;
      }
    }
  }
  btn_prev = btn_read;
  
  if((millis() > lastRefresh + refreshInterval) || cityChanged){ 
    setupTCPConnection(hostname, port);

    uri = cityLinks[cityNum];
    sendGETRequest(hostname, uri);
    delay(100); //Wait for GET request to be sent and data be fetched

    String city = cityNames[cityNum];
    String weatherDescrition;
    float temperature;
    parseIncomingData(weatherDescrition, temperature);
    displayInfoOnLCD(city, temperature, weatherDescrition);
    
    closeTCPConnection();
    
    lastRefresh = millis();
    cityChanged = false;
  }
}

void connectToWiFi(const char ssid[], const char password[]){
  Serial.print("Connecting to WiFi");
  WiFi.begin(ssid, password);
  while(WiFi.status() != WL_CONNECTED){
    Serial.print(".");
    delay(500);
  }
  Serial.print("\nConnected to ");
  Serial.println(ssid);

  Serial.print("Local IP: ");
  Serial.println(WiFi.localIP());
}

void setupTCPConnection(const char hostname[], const int port){
  Serial.println("Connecting to website");
  if(client.connect(hostname, port) == 0){
    Serial.println("Could not connect to website");
  }
  Serial.print("Connected to ");
  Serial.println(hostname);
}

void sendGETRequest(const char hostname[], const String uri){
  String getReq = "GET " + uri + " HTTP/1.1\r\n"
                 + "Host: " + hostname + "\r\n"
                 + "Connection: close\r\n"
                 + "\r\n";
  client.print(getReq);
}

void parseIncomingData(String& description, float& temp){
  String data = client.readString();

  int index = data.indexOf("description");
  String subString = data.substring(index + 10 + 4);
  index = subString.indexOf('"');
  description = subString.substring(0, index);

  index = subString.indexOf("temp");
  subString = subString.substring(index + 3 + 3);
  index = subString.indexOf(',');
  String temperature = subString.substring(0, index);
  temp = temperature.toFloat() - 273.15;
}

void closeTCPConnection(){
  client.stop();
}

void displayInfoOnLCD(String cityName, float temp, String description){
  lcd.clear();
  lcd.print(cityName);
  lcd.setCursor(12,0);
  lcd.print(temp);
  lcd.setCursor(0,1);
  lcd.print(description);
}

Credits

shubham gupta
1 project • 0 followers

Comments