Document 10
Document 10
One such global platform is Open Weather Map. The Open Weather Map is an online service that provides weather data to web
services and mobile applications. It provides more than 1 billion forecasts every day from around the world. There are more than
a million users of this platform and thousands of new subscribers are adding each day. The platform provides more than 20 APIs
to render weather related data. In this project, an ESP8266 based device will be designed which could connect to one of the APIs
provided by Open Weather Map and so could display the weather data on an LCD display.
The IoT device designed here is a weather station built by interfacing a character LCD with the ESP8266 board. The device fetches
the weather data of the location it is installed and display it on the LCD screen. It must be noted that there are no sensors used
in this device and the device gets the weather data from an IoT platform i.e. Open Weather Map. The device fetches temperature
and precipitation information using API keys and display it on the LCD. The ESP8266 is a Wi-Fi modem and it can connect to a
Wi-Fi hotspot or router by knowing the SSID and Password. Using the API key, the ESP module can access data from the IoT
service. The ESP8266 is an Arduino compatible module. It needs to loaded with a firmware that could fetch data received over
the internet and display that data to the LCD screen. The Arduino UNO is used to flash the firmware code on the ESP8266 module.
The ESP module can also be flashed with code using a FTDI converter like CP2102. The firmware itself is written in the Arduino
IDE.
Components Required –
Fig. 2: List of Components required for making ESP8266 and
OpenWeatherMap based Weather Station
Block Diagram –
The weather monitoring IoT device designed in this project is built by interfacing a character LCD to the ESP8266 module. The
ESP-01 model is used in this project which has only two GPIO pins. It is not possible to interface a character LCD with only two
GPIO pins. So, an 8-bit I/O expander for I2C bus – PCF8574P is used to interface the LCD with the ESP modem. The ESP modem
can connect to any Wi-Fi Access Point on its own. For programming the ESP module, it is connected to PC via FTDI USB Converter
like CP2102. Once the code is burnt to the module, it is powered by a voltage regulator circuit.
ESP8266 Wi-Fi Modem – The ESP8266 Wi-Fi modem itself control the functionality of the device. It must be loaded with a program
code that could access the API and control LCD. The firmware code is written using Arduino IDE. It is loaded to the ESP8266 board
using the FTDI USB to Serial Converter. A generic ESP8266 board is used in this project. This board does not have any
bootstrapping resistors, no voltage regulator, no reset circuit and no USB-serial adapter. The ESP8266 module operates on 3.3 V
power supply with current greater than or equal to 250 mA. So, CP2102 USB to serial adapter is used to provide 3.3 V voltage
with enough current to run ESP8266 reliably in every situation.
The ESP8266 Wi-Fi Module is a self contained SOC with integrated TCP/IP protocol stack that can access to a Wi-Fi network. The
ESP8266 is capable of either hosting an application or off loading all Wi-Fi networking functions from another application
processor. Each ESP8266 module comes pre-programmed with an AT command set firmware. The module comes available in
two models – ESP-01 and ESP-12. ESP-12 has 16 pins available for interfacing while ESP-01 has only 8 pins available for use. The
ESP-12 has the following pin configuration –
Fig. 5: Table listing pin configuration of ESP8266 ESP-12 Module
The ESP-01 model is used in the project. The ESP-01 model has the following pin configuration –
During compilation of the code, the GPIO0 and RESET switches must be pressed. For uploading program, the RESET switch must
be released while the GPIO0 programming switch must be left pressed, so that the ESP can enter in programming mode. After
uploading the code, the programming switch should also be released as well.
So, Write the firmware code in the Arduino IDE and connect the FTDI converter with the PC via USB cable. Open Arduino IDE and
go to Tools->Port and select the Arduino board (Arduino UNO). It may look like /dev/ttyABM0 (Arduino/Genuino Uno). Select the
correct port name. The port name can be different in different IDE setups. Then Open Serial monitor in the Arduino IDE by
navigating to Tools->Serial Monitor and set the baud rate to 115200 bauds per second. Pass ‘AT’ and ‘AT+GMR’ commands to
test the connection between the Arduino and ESP-01 module. Try different settings for the ‘Line ending’ option of the serial
monitor like Both NL & CR. Try different combinations until the ESP module starts interacting correctly with the serial monitor.
Download Python, PIP and ESPtool. Erase the pre-loaded firmware if the ESP module has any. Flash the firmware code to the
ESP-01 module by writing the commands in serial monitor as instructed for the ESPtool. Check out the following link to write
proper command for flashing the firmware code using ESPtool –
ESPtool Commands
For uploading program, the RESET switch must be released while the GPIO0 programming switch must be left pressed. After
loading the code, the programming switch should also be released as well. The GPIO2 is alternative TX for bootloader mode.
Remove the connections. Now the ESP module is ready to be installed in the project circuit. After loading the code, the ESP
module will automatically access the Wi-Fi point with the SSID given in the firmware. In the project circuit, the ESP8266 is
interfaced with the character LCD via PCF8574P I/O expander.
PCF8574 I/O Expander – The PCF8574 I/O Expander is used to interface the LCD with the ESP Modem. The PCF8574/74A provides
general-purpose remote I/O expansion via the two-wire bidirectional I2C-bus (serial clock (SCL), serial data (SDA)). The devices
consist of eight quasi-bidirectional ports, 100 kHz I2C-bus interface, three hardware address inputs and interrupt output
operating between 2.5 V and 6 V. The quasi-bidirectional port can be independently assigned as an input to monitor interrupt
status or keypads, or as an output to activate indicator devices such as LEDs. The System master can read from the input port or
write to the output port through a single register. The PCF8574P has the following pin configuration –
Fig. 11: Table listing pin configuration of PCF8574P
The LCD display is connected to the PCF8574P which connects the display to the ESP modem. The SDA (Pin 14) and SCL (Pin 15)
pins of IC are connected with GPIO01 and GPIO2 of the ESP8266 respectively. The circuit connections of the LCD module with the
PCF8574P are summarized in the table bellow –
Fig. 12: Table listing circuit connections between PCF8574P and LCD Module
Power Supply – Once the ESP modem is loaded with the firmware code, it is connected in the main circuit. In the circuit it is
powered by a battery or any other DC power supply. The Modem requires 3.3 V DC for its operation. The supply from the battery
is regulated to 3.3 V DC using 1117 IC. The supply from the battery is connected to the Vin pin of the IC while the regulated voltage
is drawn from the Vout pin. The Adjustment pin of the IC is grounded. There are two capacitors of 10 uF connected at the input
and output pins to smooth any ripples.
Once the modem is installed in the device and is powered ON, it starts searching for the available Wi-Fi Access Point. It connects
to a Wi-Fi hotspot using SSID and Password hard-coded in the firmware code. Now the ESP module is ready to fetch data from
the Open Weather Map service. It connects to the OpenWeatherMap.org with an open-source API. The API provided by the Open
Weather Map can be used to access a variety of weather related data of a location. Check out the available data from the Open
Weather Map API from the following website link –
OpenWeatherMap APIs
The ESP modem is programmed to request weather data i.e. current temperature and humidity of a location from the
OpenWeatherMap.org. An HTTP request by POST method is sent to the cloud service, to which it responds by sending back
weather data in JSON format. The device must be subscribed to the API at Open Weather Map website.
Then the data’s retrieved from the site are displayed using the LCD screen. In this plan the weather data updates for every 2
hours and forecast for 5 days can be accessed.
Fig. 14: Screenshot of OpenWeatherMap API Plans
On clicking the get API and start button, the site navigates to a wizard where developer needs to sign up to get the API key.
Programming Guide –
The ESP code begins by importing the required libraries. The <ESP8266WiFi.h> and <ArduinoJson.h> header files are imported.
The <ArduinoJson.h> library is used by ESP modem to ping JSON file while <ESP8266WiFi.h> library allows connecting and
configuring a Wi-Fi connection.
Fig. 17: Screenshot of C Code used for Initialization in ESP Modem based IoT Weather Station
a) Initialization of Wi-Fi connection with Wi-Fi Name and Password using SSID and Password variables.
b) Setting API key to access weather data and do the language setup.
These configurations are done in the Setup() function. A serial connection is started using Serial.begin(115200). The baud rate is
set to 115200 for serial communication. The same Baud Rate must be set on Serial Monitor of Arduino IDE to see the response
output from ESP8266. The Wi-Fi connection is setup by the following code –
Wifi.begin(ssid, password);
While the ESP is still connecting, there remains some delay, so some initial messages and dots are printed on the LCD while the
modem connects to a hotspot or router. These dots can be viewed after logging to the serial terminal. It’s just a way to see that
the little ESP is trying to connect. Then the LCD is initialized to display weather data.
Fig. 18: Screenshot of C Code used in Setup Function of ESP Modem based IoT Weather Station
The loop() function is executed after the setup() function and iterates infinitely. In the loop() function, getWeatherData() function
is called where it connects to the api.openweathermap.org to fetch the weather data which is in JSON format with respect to the
particular city. Then the collected data is stored in the global variables past declared. The collected values are printed to the LCD
screen for monitoring.
Fig. 19: Screenshot of C Code used in Loop Function of ESP Modem based
IoT Weather Station
Check out the complete code from the code section and try it out.
This is a simple weather station that uses an IoT service to access weather information. It is simple to design and can be installed
as a smart gadget in house or office.
//Program to
#include
#include
#include
#include
WiFiClient client;
WiFiClientSecure sclient;
LiquidCrystal_I2C lcd(0x20,16,2);
String country;
int humidity;
int pressure;
int temp;
int clouds;
int windSpeed;
String weatherString;
char tempT[5],tempMinT[5],tempMaxT[5];
char humi[5],presS[5],cloudS[5],WindSpeed[5];
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);
Serial.begin(115200);
lcd.init();
lcd.display();
lcd.backlight();
lcd.createChar(1,up);
lcd.createChar(2,down);
lcd.setCursor(0,0);
lcd.print("Connecting to...");
lcd.setCursor(0,1);
WiFi.begin(ssid, password);
delay(500);
Serial.print(".");
lcd.clear();
delay(2000);
Serial.println("");
lcd.setCursor(0,0);
lcd.setCursor(0,1);
lcd.println(WiFi.localIP());
delay(4000);
lcd.clear();
void loop()
getWeatherData();
delay(5000);
void getWeatherData()
delay(4000);
lcd.clear();
//connetion to host website to fetch the weather datas
if (client.connect(weatherHost, 80))
"Connection: closernrn");
else
Serial.println("connection failed");
return;
String line;
int repeatCounter = 0;
delay(500);
Serial.println("w.");
repeatCounter++;
char c = client.read();
if (c == '[' || c == ']') c = ' ';
line += c;
client.stop();
DynamicJsonBuffer jsonBuf;
if (!root.success())
Serial.println("parseObject() failed");
return;
weatherDescription = root["weather"]["description"].as();
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 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);
//this void function is really useful; it adds a "0" to the beginning of the number,
lcd.print("0");
lcd.print(digits);
else
lcd.print(digits);
###
Circuit Diagrams
Circuit-Diagram-ESP8266-OpenWeatherMap-
based-Weather-Station