0% found this document useful (0 votes)
72 views23 pages

Document 10

The document describes an IoT weather station that uses an ESP8266 module to fetch weather data from the OpenWeatherMap API and display it on an LCD screen. The ESP8266 connects to WiFi and uses the API to retrieve temperature and precipitation information without any onboard sensors. It displays this data on a character LCD connected via an I2C I/O expander. The device demonstrates how a custom electronics project can access cloud-based weather data from an online IoT platform.

Uploaded by

G124Rohit Maji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views23 pages

Document 10

The document describes an IoT weather station that uses an ESP8266 module to fetch weather data from the OpenWeatherMap API and display it on an LCD screen. The ESP8266 connects to WiFi and uses the API to retrieve temperature and precipitation information without any onboard sensors. It displays this data on a character LCD connected via an I2C I/O expander. The device demonstrates how a custom electronics project can access cloud-based weather data from an online IoT platform.

Uploaded by

G124Rohit Maji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Monitoring weather conditions is an important task in many human activities.

There are several fields like agriculture, tourism,


transportation etc that are affected by the changes in weather. Considering the importance of weather monitoring, there are
global platforms which keep track of weather conditions around the world. These platforms are IoT ecosystems which can be
accessed online. It is possible to design custom electronic devices that can connect with such platforms and fetch weather related
data from the cloud platform.

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.

Fig. 1: Prototype of ESP8266 and OpenWeatherMap based Weather Station

Components Required –
Fig. 2: List of Components required for making ESP8266 and
OpenWeatherMap based Weather Station
Block Diagram –

Fig. 3: Block Diagram of ESP8266 and OpenWeatherMap based Weather


Station
Circuit Connections –

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.

Fig. 4: Image showing circuit connections of ESP8266 and OpenWeatherMap


based Weather Station
The device has the following components connected as mentioned –

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 –

Fig. 6: Table listing pin configuration of ESP8266 ESP-01 Module


The Chip Enable and VCC pins of the module are connected to the 3.3 V DC while Ground pin is connected to the common
ground. The chip enable pin is connected to VCC via a 10K pull up resistor. The RESET pin is connected to the ground via a tactile
switch where the pin is supplied VCC through a 10K pull up resistor by default. The GPIO-0 pin of the module is connected to
ground via a tactile switch where the pin is supplied VCC through a 10K pull up resistor by default. These pull-up resistors act as
voltage divider circuit which protects the ESP8266 board from high voltage.
The CP2102 which is a single chip USB to UART Bridge is used to load firmware on the ESP modem. It also provides power supply
to the ESP module while bootloading. A CP2102 IC has the following pin configuration –

Fig. 7: Table listing pin configuration of CP2101 USB to UART Chip

Fig. 8: Table listing pin configuration of CP2101 USB to UART Chip

Fig. 9: Table listing pin configuration of CP2101 USB to UART Chip


The ESP board is connected to the CP2102 for bootloading mode and usage purpose. For bootloading, the ESP-01 TX pin is
connected to RX pin of CP2102 and ESP’s RX pin is connected to the TX of CP2102 so that they both can send and receive the
data. The ESP8266-01 needs 3.3 V power supply so ESP’s VCC is connected to the 3V3 of CP2102 and GNDs of both need to be
connected to each other. The reset pin of ESP8266 along with 10k pull-up resistor (bootstrap resistor) is connected to the RTS pin
of CP2102. The GPIO0 is a programming pin which is used to end the ESP-01 into programming mode. The GPIO0 along with 10K
pull-up resistor is connected to DTR pin of CP2102. The CHPD pin of ES-01 is pulled-up with 10K resistor. The circuit connections
between the ESP module and the CP2102 are summarized in the following table –
Fig. 10: Table listing circuit connections between ESP8266 ESP-01 Modem
and CP2101 Module
After connecting the ESP module with the CP2102 adapter, the adaptor must be connected to the PC and the ESP module must
be loaded with the firmware code.

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.

How the circuit works –

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.

Fig. 13: Screenshot of OpenWeatherMap API Website


By clicking the subscribe button, the site navigates to the available plans for the service. The free plan is used here for
demonstration.

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.

Fig. 15: Screenshot of OpenWeatherMap API Subscription


The sign up can be done with an email ID. After Sign Up, the developer can use the API key to access the weather data. The API
key provided after successful subscription is used in the firmware to access the weather data.
Fig. 16: Screenshot of OpenWeatherMap API Sign Up Page
The weather data fetched from the cloud service is wrapped to proper strings and displayed on the LCD module. The ESP modem
sends the data to the LCD module on two wire protocol which is converted to the parallel data using PCF8574P I/O Expander.

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

The following configurations need to be managed in the firmware code –

a) Initialization of Variables to store the received data.

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.

Project Source Code


###

//Program to

//Libraries for ESP, json and I2C LCD

#include

#include

#include

#include
WiFiClient client;

WiFiClientSecure sclient;

LiquidCrystal_I2C lcd(0x20,16,2);

//Initialize the Variable for storing the data

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];

//WiFi Name and Password

const char* ssid = "********"; // SSID of local network

const char* password = "********"; // Password on network

//openweathermap.org api key:8b2f088e8a9c307c759ef39dcb085ae4

String weatherKey = "8b2f088e8a9c307c759ef39dcb085ae4"; // openweathermap.org key


String weatherLang = "&lang=en"; // english or other language

//LCD display of Special Charcters

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);

//Baud rate for serial communication

Serial.begin(115200);

// initializing the LCD

lcd.init();

//Enable or turn on the text

lcd.display();

// Enable or Turn On the backlight

lcd.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();

void loop()

getWeatherData();

delay(5000);

const char *weatherHost = "api.openweathermap.org";

void getWeatherData()

Serial.print("connecting to "); Serial.println(weatherHost);

delay(4000);

lcd.clear();
//connetion to host website to fetch the weather datas

if (client.connect(weatherHost, 80))

client.println(String("GET /data/2.5/weather?q=erode,india&") + "&units=metric&appid=" +


weatherKey +
weatherLang + "rn" +

"Host: " + weatherHost + "rnUser-Agent: ArduinoWiFi/1.1rn" +

"Connection: closernrn");

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();

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,

//so that 5 minutes is displayed as "05", rather than "5 "

void printDigits2(int digits)

if(digits < 1000)

lcd.print("0");

lcd.print(digits);

else

lcd.print(digits);

###
Circuit Diagrams

Circuit-Diagram-ESP8266-OpenWeatherMap-
based-Weather-Station

You might also like