ESP8266 Weather Display
Hey guys, welcome to today’ s tutorial, today we will be building an esp8266 based weather monitor/display using the Wemos D1 board and the 16×2 LCD keypad shield for Arduino.
A few weeks back, I published a tutorial about the impressive WeMOS D1 development board which is a development board based on the esp8266 but with the Arduino Uno form factor and today, we are going to build our first project with the board. The board is pin compatible with the Arduino Uno, although most add-ons that work with the Arduino do not work with it yet due to library issues. The board, however, has all the features and capabilities of the esp8266 module, especially the superb WiFi functionality and it can be programmed using the Arduino IDE.
This project is a good beginner project for anyone interested in learning about building IoT based project as this project will totally be based on the internet.
Using the Esp8266 wifi functionality on the Wemos D1, our setup will join a specified WiFi network to access the internet and pull weather data from open weather map and display on the LCD Every 10 minutes. Open weather map is an openly sourced weather reporting platform through which anyone can subscribe to either hourly, weekly or daily update on the weather in their particular location. The platform also provides APIs on top of which users can build their own applications that makes use of the large amount of data which open weather map asks.
Required Parts and Where to Buy
The following components/parts are required to build this project and they can each be bought through the link in front of each component.
- Wemos D1 Board: https://educ8s.tv/part/WemosD1
- Keypad Display: https://educ8s.tv/part/KeypadShield
- Xiaomi Powerbank: https://educ8s.tv/part/Powerbank
Full disclosure: All of the links above are affiliate links. I get a small percentage of each sale they generate. Thank you for your support!
[adsense]Schematics
The connections for this project is simple. The 16×2 LCD display comes as a shield so all we need to do is plug the shield on the Wemos Board, ensuring that it is well fixed.
Code
The code of this project is really simple if we compare the technicalities involved with the fact that it needs less than 150 lines of code. We use 2 familiar libraries in this project, the ESP8266WiFi library and the LiquidCrystal library for the display. In addition to these two libraries, we will also be using the ArduinoJson library which allows us to process JSON files on the Arduino.
While the LiquidCrystal library comes with the Arduino, the esp8266wifi library and the Arduino JSON library can either be downloaded via the Arduino library manager or via the link below.
LIBRARIES
ArduinoJSON Library: https://github.com/bblanchon/ArduinoJson
ESP8266WiFi: https://github.com/esp8266/Arduino
When we request weather data from the openweathermap.org website, the data is returned to our Arduino in JSON. The ArduinoJson library makes it really easy for us to extract the exact data we need from the JSON tree reply from the server and save them in our own variables. A flow chart that explains it better is shown below.
As soon as the data is received, its parsed and displayed on the 16×2 LCD shield.
To take a brief look at the code. we start out by including the needed libraries after which we map the pins of the WEMOS D1 to the pins of the Arduino UNO so it is easy for us to interface the LCD.
#include <ESP8266WiFi.h> #include <LiquidCrystal.h> #include <ArduinoJson.h> #define D0 3 // GPIO3 maps to Ardiuno D0 #define D1 1 // GPIO1 maps to Ardiuno D1 #define D2 16 // GPIO16 maps to Ardiuno D2 #define D3 5 // GPIO5 maps to Ardiuno D3 #define D4 4 // GPIO4 maps to Ardiuno D4 #define D5 0 // GPIO14 maps to Ardiuno D5 #define D6 2 // GPIO12 maps to Ardiuno D6 #define D7 14 // GPIO13 maps to Ardiuno D7 #define D8 12 // GPIO0 maps to Ardiuno D8 #define D9 13 // GPIO2 maps to Ardiuno D9 #define D10 15 // GPIO15 maps to Ardiuno D10
With that done, we declare the SSID and password of the WiFi to which we want to connect to for internet access.
const char* ssid = "yourSSIDgoesHere"; // SSID of local network const char* password = "yourWiFiPasswordGoesHere"; // Password on network
Next, we need to 0btain the APIKEY from operweathermap.org website and enter it into our Arduino code. In order to create your own API key, you have to sign up on their website. The API key is used as a unique identifier to recognize each individual’s request to the open weather map server.
Getting current weather data and forecast is free but the website offers more options if you are willing to pay.
String APIKEY = "yourAPIKEYgoesHere";
Next, we have to find the id of our location on the open weather map website. Find your location, copy the ID and store in the CityID variable as shown below.
String CityID = "253394"; //Sparta, Greece
With that done, we create some of the other variables that will be used within our sketch.
int counter = 60; String weatherDescription =""; String weatherLocation = ""; String Country; float Temperature; float Humidity; float Pressure;
At the beginning of the void setup function, we initialize the serial monitor as it will be used for debugging we then initialize the LCD and instruct the system to connect to the access point defined initially. The result of the connection attempt is displayed on the screen.
void setup() { Serial.begin(115200); int cursorPosition=0; lcd.begin(16, 2); lcd.print(" Connecting"); Serial.println("Connecting"); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); lcd.setCursor(cursorPosition,2); lcd.print("."); cursorPosition++; } lcd.clear(); lcd.print(" Connected!"); Serial.println("Connected"); delay(1000); }
The algorithm for the code content of the void loop function is simple, after every 10 mins, the device makes a request to open weather map server to get the current weather data after which the data is parsed with the Arduino JSON library and displayed on the LCD.
void setup() { Serial.begin(115200); int cursorPosition=0; lcd.begin(16, 2); lcd.print(" Connecting"); Serial.println("Connecting"); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); lcd.setCursor(cursorPosition,2); lcd.print("."); cursorPosition++; } lcd.clear(); lcd.print(" Connected!"); Serial.println("Connected"); delay(1000); }
The complete code for this project which includes some of the other functions used can be downloaded from the link below.
——————–
CODE OF THE PROJECT
——————–
Connect the LCD to the Arduino, upload the code and power it up, after a few minutes, the device should connect to the internet and you should see the weather information displayed on the LCD as shown in the image below.
That’s it for this tutorial guys thanks for reading and watching, don’t forget to subscribe so as to get informed as soon as new tutorials are published.
SUBSCRIBE ON YOUTUBE
——————–
Never miss a video: Subscribe to educ8s.tv
Hi,
First, thank you so much for publishing this. I am just starting to learn about Arduino and it is a great getting started project. I ordered the parts, but they have no arrived yet so I am trying to get the program compiled and make some changes. I have run into a problem and it is probably my ignorance. Maybe you could help.
The problem is that I get an error in the code. The error is
WiFiWeatherDisplay:25: error: ‘D9’ was not declared in this scope
LiquidCrystal lcd(D8,D9,D4,D5,D6,D7);
I have my board set to WeMos R1 D2 & Mini
I am running the Arduino software 1.6.9
I believe the call to LiquidCrystal lcd(D8,D9,D4,D5,D6,D7); is to define the pins connected to the LCD Panel. Am I using the wrong library? As I said, I do not have the hardware so cannot check the pin connections.
Thanks for any answer and thanks for all your hard work.
^
The problem is the I use the Wemos D1 R1 board and you are using the D1 R2. In the second revision they have removed the D9 pin, I don’t know why, that’s why the code does not compile. Please add this line of code at line 3 of the code and please tell if it works this way.
#define D9 2 // GPIO2 maps to Ardiuno D9
Thanks!
Thanks Nick. I understand now and it will work fine.
I have update the code in order to reflect the changes made on the Wemos D1 R2 board. It should work now. Please check it out. Thanks!
The code in the link is not equal to the code in the video. Could you please link to the correct code?
Sorry for the mistake. It is now fixed!
hello nick,
i have downloaded your code, but i is not the same as in the video. I tried to copy the code from your video, but the video stopped at line 111. so i don’t know what the rest of the code is,
please can you update the code, so i can give it a try,
thanks,
Peter
Sorry for the mistake. It is now fixed!
Hi. The downloaded code does not match that shown in the video. It contains no reference to openweathermap.org or ArduinoJson :-(
Sorry for the mistake. It is now fixed!
Thank you, and keep up the awesome work :-) Any plans on getting a Wemos D1 Mini?
Already got one! Review is coming soon!
Cool!
I am thinking about using this code for showing several locations on the display. Would this be difficult?
Thanks for this rear opportunity of educating us on the wemos; i’m new to it though i have bought Wemos D1,i have series of problem to resolve:
1. after the installation of arduino IDE 1.6.9 and update of Wemos to reflect on the tools i discovered that the system did not detect my serial com of the D1 despite installation of CH341SER successful. that is my major problem right now i picked Wemos D1(retired) since there is no Wemos D1 on the tools.
Many thanks for your great assistant.
Hi , thank you for sharing this project, I am fairly new to arduino and was looking for a way to send data from the dht22 wirelessly to an arduino uno using nRF24L01+ modules and your code was the closest I could get to the components and the librarys I am using I had to make changes to the code to make it work for me, what I would like to know is how to change from Celcius to Fahrenheit? I would be grateful if you could let me know.
Use this command instead float f = dht.readTemperature(true);
What would the command be if using the openweathermap data instead of a dht22 sensor?
Disregard, i see where to set the getWeatherData api call to ‘imperial’
Arduino: 1.6.7 (Windows 10), Board: “WeMos D1 R2 & mini, 80 MHz, 921600, 4M (3M SPIFFS)”
C:\Users\Mark\Downloads\WiFiWeatherDisplay\WiFiWeatherDisplay.ino: In function ‘void loop()’:
WiFiWeatherDisplay:66: error: ‘displayGettingData’ was not declared in this scope
displayGettingData();
^
WiFiWeatherDisplay:68: error: ‘getWeatherData’ was not declared in this scope
getWeatherData();
^
WiFiWeatherDisplay:72: error: ‘displayWeather’ was not declared in this scope
displayWeather(weatherLocation,weatherDescription);
^
WiFiWeatherDisplay:74: error: ‘displayConditions’ was not declared in this scope
displayConditions(Temperature,Humidity,Pressure);
^
Multiple libraries were found for “LiquidCrystal.h”
Used: C:\Program Files (x86)\Arduino\libraries\LiquidCrystal
Not used: C:\Users\Mark\Documents\Arduino\libraries\NewliquidCrystal
exit status 1
‘displayGettingData’ was not declared in this scope
This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.
Hi anybody have a clue why I’m getting this?
Hi ok, got it all compiled now. Display says connected then all goes to dots two lines of.
Help please.
Hi Mark,
You do not explain how you fixed the “displayGettingData’ was not declared in this scope” errors. I have same problem. Can you please explain?
Mark H.
Now working as it should. Left out part of the ssid Doh!!
what was the problem please ?
First, may i add my thanks for all your hard work!
My DFRobot LCD Keypad Shield is a 5V unit and works well with my arduino UNO, but not with the Wemos R1 D1. Your code puts all the weather info on the Serial Monitor beautifully, but not on the LCD. Is this a voltage problem? Thank you again.
I will check the code again I will post another message. Stay tuned!
It’s OK, Nick, I got it working on an I2C-driven 20X4 LCD, using a logic level converter. Therefore, I assume the problem was voltage related. Your code is perfect. Cheers … Barry.
i got arduino software 1.6.12 and i got the exact things you have but my lcd shows blank after i upload code…. please update :(
Yes, I will try it soon.
Thank you for your post. I was looking for the correct lcd pins, and you saved me a lot of time. Thanks :)
THANK YOU ! Spend 2 days trying to parse json data with Http request via Thingspeak and didn’t manage to do it. Your code is so clear, just had to replace no more than 3 or 4 things in order to adapt it! Thanks again, made my day, ahah :D
…and this is (what to replace) ?
I get the message after uploading and connecting to wifi
I get the message
parseObject() failed
Please update the ArduinoJson library to the latest one.
thank you for this project first of all,
my problem is that I cannot get the data to display it self onto the LCD, I am using the DFRobot LCD keypad shield and the wemos D1 R1 board. I also get some random characters in the serial monitor with ‘parseObject() failed’ and then the weather data shows in the serial monitor but not on the LCD. Any help would be greatly appreciated.
Thanks!! :)
I have the same problem as you…
what ssid and password should i use?
I’M new please help
im having problem too
can you show us where to add the library
and which board I select from IDE
Build options changed, rebuilding all
error: ESP8266WiFi.h: No such file or directory
#include
^
compilation terminated.
exit status 1
Error compiling for board Arduino Uno WiFi.
You need to install the EPS8266 files for the Arduino IDE. You are compiling for the Arduino Uno Wifi and not for an ESP8266 board.
When trying to upload the code I get the following error
An error occurred while uploading the sketch
esptool v0.4.9 – (c) 2014 Ch. Klippel
setting board to nodemcu
setting baudrate from 115200 to 921600
setting port from /dev/ttyUSB0 to /dev/ttyUSB0
setting address from 0x00000000 to 0x00000000
espcomm_upload_file
espcomm_upload_mem
opening port /dev/ttyUSB0 at 921600
error: cannot access /dev/ttyUSB0
error: espcomm_open failed
error: espcomm_upload_mem failed
any suggestions?
This sorted a few pin issues for me. (cheep china version)
https://forum.wemos.cc/assets/uploads/files/1491123926188-wemos-d1r2-pinout.jpeg
Notice where D0 to D8 are.
hi all,
i have a problem with wemos d1 r2 connecting wire, read and write SD card-RTC data logger from Deek Robot .. it monitor always show “SD card failed”.
would u give some execise with it, please?
thank you very much
Hi Nick,
Thanks for the code! Although i do have to point out something so that others may not repeat the same mistakes as i did. For anyone using the older WEMOS D1 board you have need to comment out the pin definitions in the code like this:
//#define D0 3 // GPIO3 maps to Ardiuno D0
//#define D1 1 // GPIO1 maps to Ardiuno D1
//#define D2 16 // GPIO16 maps to Ardiuno D2
//#define D3 5 // GPIO5 maps to Ardiuno D3
//#define D4 4 // GPIO4 maps to Ardiuno D4
//#define D5 14 // GPIO14 maps to Ardiuno D5
//#define D6 12 // GPIO12 maps to Ardiuno D6
//#define D7 13 // GPIO13 maps to Ardiuno D7
//#define D8 0 // GPIO0 maps to Ardiuno D8
//#define D9 2 // GPIO2 maps to Ardiuno D9
//#define D10 15 // GPIO15 maps to Ardiuno D10
Else you will not be getting any text displayed on the LCD.
Additionally if your lcd is show showing any “boxes” i.e. just blanks, use a screwdriver and turn the potetiometer on the lcd clockwise until you see the text. Initially i thought my lcd shield was faulty but i tried it on my UNO running a sample code under “Example use of LiquidCrystal library” from https://www.dfrobot.com/wiki/index.php/Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009) which proves it was working fine.
Good Luck and cheers!
any hope to fix the parseObject() failed ?
Dears,
for those that are using the D1 R1 board
the pins that worked with me are
LiquidCrystal lcd(0, 2, 4, 14, 12, 13);
Dears, one more issue that i found strange in the code,
when it call the data from the online API in getWeatherData, the data get stored in string variable named “result”
this variable has to be set to empty string before calling the weather server
otherwise the next call to the data will build up on top of the old result.
void getWeatherData() //client function to send/receive GET request data.
{
result = “”; //// this is needed.
ets Jan 8 2013,rst cause:4, boot mode:(1,7)
wdt reset
How can I help you get the error?
Hello,
I just tried to test on a Wemos D1R1. The connecting to Wifi works perfectly. Also the message “getting data” appears on the display.
Unfortunately instead of the ctiy only a comma “,” appears; Temp, pressure and humidity are all displayed as “0”
Do you have a hint why the weather data are not imported? Openweather api key works on my lineage os mobile phone.
Thank you in advance
HiHiMan
Niko kalispera.
Exw analavei mia ptyxiaki ergasia stin opoia tha prepei na ftiaksw mia plaketa pou tha kanei to idio pragma me to video pou anevases. to provlima einai oti o kathigitis mou protine na parw tin arduino uno wifi rev2. ti prepei na allaksw ston kwdika gia na leitourgisei?
Hello, if I add dht22 will it work? It’s about the external sensor, so that it also says how much is inside the building. I tried with bme280 and it didn’t work. bme I used sda and scl
Hello Nick,
Thank you for all tutorials. I was checking the wemos d1 temperature reading from a website and display on a LCD..
Do you have such example with stock market or currencies exchange rates?
Thanks. Geert
hello how do i setup for local time?
and how do i make the words shown into capital letters?
Hallo Nick,
Corona machts möglich – jetzt habe ich die Zeit mich ein bisschen mehr um die Bastelei mit den ESP zu kümmern und da habe ich im Netzt Deinen Beitrag gefunden. Hab ihn mit dem wifi&bluetoothR 32 – D1-R32 von AZ Delivery (München) realisiert. Hat auf Anhieb wunderbar funktioniert – dachte ich….Leider hat er die Wetterdaten nicht automatisch aktualisiert – muss immer neu resetten. Was habe ich übersehen ?
Vielen Dank erstmal und ein Frohes Fest…
Code is not working for ArduinoJson 7.0 librarie.
Thanks for the project, but code update is needed for latest ArduinJson librarie available