ESP8266 Weather Station - Getting Started Guide
ESP8266 Weather Station - Getting Started Guide
ESP8266 Weather Station - Getting Started Guide
Daniel Eichhorn
This book is for sale at http://leanpub.com/esp8266weatherstationgettingstartedguide
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Required Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
ESP8266 Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
OLED Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Wires & Cables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Tool Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Download and Install the Serial Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
The Arduino IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Install the ESP8266 tool chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Testing the Setup: WiFi Scanner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Trouble Shooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
More Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
The ESP8266 PlaneSpotter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
The ESP8266 WorldClock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Introduction
Since the end of 2014 the ESP8266 chip by Chinese manufacturer Espressif has gained a lot of
popularity in the DIY community, due to its rich set of features but also due to the very attractive
price. First it was only available as a WiFi extension to existing development boards, cutting the
price of comparable products from USD $60 to a mere $6! Suddenly all the Arduino developers
had an affordable way to connect their devices to the internet. Not long after, clever hackers and
engineers realized that the ESP8266 could be used beyond the rather simple AT firmware. A software
development kit (SDK) was available but badly documented, so they reverse-engineered the SDK
and used Google Translate to understand the Chinese manual.
At first the process to set up a development environment was complicated and cumbersome. Files
had to be downloaded from different sources and copied to various locations. But then several groups
started to provide simplifications to this process. One of the first simplifications was the NodeMCU
LUA firmware which could interpret scripts written in the language LUA at runtime. The firmware
also provided bindings into Espressifs API from the LUA language so that the pins of the ESP8266
could be easily controlled with just a few lines of code.
A few months later another huge simplification became available: the integration of the C/C++
API into the Arduino IDE! Suddenly it was possible to profit from the simplicity of the Arduino
ecosystem, which not only provided a vast number of libraries but also made the C programming
start of your project a lot easier. Since code developed in the Arduino IDE compiled into a very
efficient binary the often scarce resources of the ESP8266 were also used more efficiently. For
instance, the interpreter (the program that reads and executes scripts) of the LUA firmware needed
a lot of memory just for itself and did not leave much for your script code.
After having used the LUA firmware for a while I got frustrated by its instability and lack of
peripheral support. So I just jumped on the possibility to program the ESP8266 from the Arduino
IDE - and I loved it from the beginning. I didnt have to worry about a complicated tool installation:
it was as simple as copying a URL into the right spot in the Arduino IDE. And also many libraries
programmed for the standard Arduino ATmega chips worked out of the box for the ESP8266 as
well! So I went to work and ported some of the projects I had written for the LUA firmware to the
Arduino/ESP8266 platform.
However, I was struggling in LUA with one peripheral module I already had successfully working:
a wonderfully crisp OLED display. There were several libraries available for the Arduino using that
display but I just couldnt get them to run: the extremely versatile and rich u8glib used a lot of
ATmega specific code and just wouldnt compile. The Adafruit library on the other hand was made
for slightly different displays and wouldnt work for me either. So I set out and started to write my
own (and very first) library for the Arduino/ESP8266 platform.
To verify the library I implemented a few ideas which involved the OLED display. One of them was
the ESP8266 WeatherStation. After getting the code to work I wrote a blog post about it and had
Introduction 2
it running somewhere in my apartment - and I forgot about it until I saw that suddenly the visits
on that blog post spiked and that many visitors came from Thingiverse. From a 3D printing project
built around my WeatherStation code, that was the moment when I realized that I had something
interesting and people had found the WeatherStation appealing.
I decided to provide the right components needed for building the WeatherStation and to sell it as
a kit for the ESP8266 WeatherStation. Quickly I had set up a simple PayPal shop on my blog. A
supplier in China would ship the kit directly to buyers all over the world and after a few months
WeatherStations were being programmed in more than 20 countries.
You are now holding a guide to the WeatherStation in your hands. Thank you for your interest! You
might have just found this guide on Amazon and you dont have the hardware yet. Or you have
already acquired the components on your own and are now looking for a guide to use them. Or you
have bought the kit from my shop or my listing on Amazon. In all of these cases you quickly want
to get started with the ESP8266 and Ive tried very hard to make this as easy as possible for you.
Please let me know if you see mistakes. You can reach out to me through dani.eichhorn@squix.ch
or on Twitter: https://twitter.com/squix78
Also make sure that you subscribe to my newsletter to stay updated with latest news around
the ESP8266. You will get a maximum of 1-2 emails per month, I promise! https://blog.squix.org/
subscribe
One more thing! If you like this project please have a look at my shop. I recently created a hardware
kit containing a beautiful color display with touch screen. There are are several projects you can
build with it.
https://blog.squix.org/product/esp8266-wifi-color-display-kit-2-4
Required Hardware
The Starter Kit is available from two shops. You can buy it from the shop on my blog and shipping
is available to almost all destinations: https://blog.squix.org/product-category/starter-kits
If you live in the US you can purchase the WeatherStation from Amazon (https://www.amazon.com/
dp/B01KE7BA3O/) as well. Get a 10% discount by using this code during checkout on the Amazon
store: SKH3MS43
The Squix ESP8266 WeatherStation Kit has the advantage that everything fits together, but you can
of course also get the components from your preferred supplier. In this chapter I will quickly go
through the minimal requirements and the options you have to build your first WeatherStation.
ESP8266 Module
There are many different modules available based on ESP8266s; they differ in a number of aspects
such as the quantity of available GPIO pins or if they can be programmed easily without need of
an additional Serial-to-USB converter. If you are a beginner I suggest you use a developer-friendly
module like the NodeMCU V1.0 or the Wemos D1 mini. They come with a USB connector and have
the maximum number of available pins ready for your usage. The absolute minimal requirement is
that your ESP8266 module has at least two free GPIO pins to connect it to the OLED display.
Required Hardware 4
OLED Display
With the display you also have many options: do you want the pixels to be white or blue, or do
you even prefer a two color display where the footer is in one color and the rest in another? What
really matters is the driver chip and the protocol. The OLED library currently supports I2C and SPI
for both the SSD1306 and the SH1106 chip. The first is often used for 0.96 inch displays while the
second one is used for 1.3 displays. Displays with SPI interface will consume more of your free
GPIO pins.
should have a micro-USB connector on the module side and a normal USB connector for your PC
or Mac.
Tool Setup
In this chapter we will prepare your development environment by installing all the tools necessary.
Drivers are needed to communicate with the ESP8266, a tool called Arduino IDE will let us write
code, and a sample project will prove that the components are working well together.
In case your module comes with a CH340 serial-to-USB converter then download the drivers from
here:
Win: http://blog.squix.org/downloads/CH341SER.zip
Mac: https://blog.squix.org/wp-content/uploads/2016/12/CH34x_Install_V1.3.zip
For Mac OS X you can download a ZIP file which you then have to extract. Take the extracted
application Arduino and move it to your Applications folder.
For Windows you have the option between an executable installer and a ZIP file. The ZIP file
might be the better option if you do not have administrator permissions on your system. The
installer on the other hand can put the libraries in the proper places.
Now you have a bare Arduino IDE which brings everything needed to write programs for the
standard Arduino ATmega chips. But we want to write and compile code for the ESP8266, right?
From time to time you want to come back to the Board Manager and make sure that you have the
latest version of the ESP8266 tool chain installed. To do that simply click on the ESP8266 entry and
select the latest version from the dropdown. Then click Update.
So lets pick the correct board. If you bought the original Squix Starter Kit you will have to choose
a NodeMCU 1.0: Go to Tools > Board: * > NodeMCU 1.0 (ESP-12E Module)
There is a plentitude of modules available. Please make sure that you have the correct board selected
before you continue.
Serial interface: At the hardware level the ESP8266 is programmed through a serial
interface. In short this is a very common communication interface which normally requires
three lines: transmit (TX), receive (RX) and ground (GND). Both devices involved in the
communication need to agree on the rate the characters are sent over the wire. This rate is
usually measured in BAUD. 10 BAUD is equal to 1 character per second. Your average PC
or Mac doesnt have such a serial interface, so how can we program the ESP8266? This is
done through a Serial-to-USB converter. Some ESPs already come with a built-in converter;
others need an external one for programming.
In an earlier step you already installed the drivers for this converter. If everything went well and
the board is plugged into your computer you should now be able to select the serial connection. It
should show up in the Menu under Tools > Port. On my Mac the device is called /dev/cu.SLAB_-
USBtoUART. On a PC it should be listed as a COM port labelled COM# (where # is some number).
If you cannot see a device that looks like the NodeMCU, try to unplug the ESP module and re-plug
it after a few seconds. Also try a different USB socket. If that doesnt help consider restarting your
computer Make sure that you installed the driver as mentioned in the chapter about drivers.
If you see Done uploading. in the window, then click on the magnifying glass on the top right of
the window. This is the serial console that you can use to see output from the NodeMCU module,
or to send commands to the device. Make sure that the baud rate is set to 115200. This rate is also
set in the example code, and if you have a different setting the ESP will talk with a different speed
than your PC listens. You can set the baud rate on the bottom left of the serial monitor. My output
looks like this:
Tool Setup 11
If you see something similar: congratulations! You have just set all the preconditions to run the
WeatherStation code.
Trouble Shooting
Let me be honest: there are many reasons why this setup might not work. But dont give up so
quickly! With a careful and analytical approach we will manage to get the ESP8266 running! The
following paragraphs are structured by symptom and I will give you some ideas how to find the
problem and how to solve it.
First please make sure that you have installed the correct driver, either for the CP2102 or the
CH340. If you are not sure which one your ESP8266 has then better install both. The extra
driver will only be used if you attach a matching hardware. The photo below shows how the
CP2102 from Silabs looks like.
Tool Setup 12
Another possible and frequent culprit is the USB cable. If you are sure that you installed the
right drivers then try to use a different USB cable with the ESP8266. As a cross check you can
also use the USB cable with another device (e.g. smartphone) and connect it to your PC. If
the device is not recognized by your computer (and it is one that should be recognized) then
throw the faulty cable away
Sometimes it helps to restart your computer or choose another USB port. It happened to me
several times that one USB port stopped working and only after a restart or changing the port
the device would show up.
It also happens relatively often that the NodeMCU is dead. But it is relatively hard to be
100% sure that it is really not working. If you previously didnt identify driver or cable as
the cause for the problem we should focus on the NodeMCU module. Lets have a close look
at the device. There are two LEDs: one on the ESP8266 module close to the antenna and the
other one closer to the buttons. Do you see anything blink when you plug in the USB cable
and connect it to your PC? If it blinks then the ESP8266 could be OK but the Serial-to-USB
converter could be damaged. If there is no light then there are still many possibilities.
This means that for a number of reasons your computer could not upload the firmware to the
NodeMCU. To understand what might be the cause we need to see what is happening during the
upload of a new binary. Before the availability of easy-to-use developer modules like the NodeMCU
you had to manually connect some pins of the ESP8266 to boot it into flash mode after a reset. This
was very annoying since for every change in the code you had to compile, connect the pins, reset
the ESP, wait until upload was complete, disconnect the boot mode pins and do a reset. Modules
like the NodeMCU make this a lot easier since they have a special circuit which does all that when
the serial-to-usb converter detects a special signal from your computer. Wonderful, right? Except: it
doesnt always work. First lets try if the serial connection is working at all. Connect the NodeMCU
to your computer and open the serial console. Now press the RST button and check what will be
printed in the console. Depending on the selected transfer speed (lower right corner of the serial
monitor) you either see strange characters or something similar to this:
I had to set the speed to 74880 baud to get this output. If you see this text then your computer and
your ESP8266 can communicate with each other. Now we try to fix it by one of these measures:
Press and hold the button labelled FLASH while pressing the button labelled RST. Then try
again if the upload works. This button combination will manually set the ESP8266 into flash
mode
The settings in the Arduino Tool menu are also a frequent source of problems: have you
selected the right board (e.g. NodeMCU V1.0) and the right USB/Serial port? Try also different
upload speeds. The NodeMCU should automatically detect the requested transfer speed but
this does not always work.
Summary
Before we continue to the WeatherStation project lets have a closer look at what we just
accomplished:
Tool Setup 14
1. We installed a driver which lets us program the ESP8266 with custom code that we wrote.
Which driver needs to be installed depends on the Serial-to-USB converter we use. Some ESP
modules already have such a converter; others will need an additional one.
2. We downloaded and installed the Arduino IDE. In the IDE we write the code, compile it and
transfer it to the embedded device. If our code supports it we can even use the Serial Monitor
to communicate with the device.
3. We used an example project, called a Sketch, to test our setup. The sample project installs
firmware which uses the WiFi module to scan for available WiFi access points. It repeatedly
writes this data to the serial line, and we can display it by opening the Serial Monitor tool.
Remember, in a serial communication both parties need to agree on the speed the characters
are getting transmitted. The example sets this to 115200 baud.
ESP8266 Programming Basics
In this chapter we will have a look at the building blocks of an Arduino sketch. This will help you
to understand and modify the WeatherStation which we will build in the next chapter. If you just
want to get the WeatherStation running you can skip this chapter.
Preparation
In this chapter we will work with exercises which you can download from GitHub. They contain
several Arduino projects for the ESP8266. For an exercise open the related project in your Arduino
IDE and try to solve the given task. If you get stuck or want to see an alternative solution open the
project which ends with _Solution:
Now download the Zip file from GitHub and extract it in a place you will find it later. There is a
green Clone or download button which lets you download a Zip file:
https://github.com/squix78/esp8266-getting-started
1 Serial.begin(115200);
This tells the Serial object that you want to communicate with a baud rate of 115200. Remember
to set the same transfer rate later in the serial console on your computer. Both partners in the
communication need to have the same speed settings or you will just see garbage. If you want
to send a message from your program to your computer you just do this:
1 Serial.print("Hello ");
2 Serial.println("World");
Please have a look at the little difference between the first and the second line. The first uses a
method called print() and the second println(). The only difference is that the latter adds a line
break to the output.
ESP8266 Programming Basics 17
If that was successful open the serial console by clicking on the magnifying glass:
If you want to learn more about the Serial object you can find more in the following link:
http://esp8266.github.io/Arduino/versions/2.3.0/doc/reference.html#serial
The exercise contains another important built-in function:
1 delay(1000);
This instructs the processor to wait 1000 milliseconds (1 second) before continuing with the
execution. As mentioned earlier with this command it also gives the processor time to handle other
ESP8266 Programming Basics 18
tasks, such as receiving or sending network packets over WiFi. In this context a call to yield() does
the same as delay(0).
1 pinMode(PIN, INPUT);
2 int state = digitalRead(PIN);
Unless you want to change the mode of a pin you only need to call pinMode() once. Please note that
depending on the pin you can also use INPUT_PULLUP or INPUT_PULLDOWN. Writing to a pin is not
much different:
1 pinMode(PIN, OUTPUT);
2 digitalWrite(PIN, HIGH); // or
3 digitalWrite(PIN, LOW);
The second statement will show a HIGH level on PIN which will be 3.3V. The third statement will set
the pin to LOW which is 0V.
What values for PIN can you use? If you are using a generic ESP8266 module (not a NodeMCU) your
pins will be labeled GPIO0, GPIO1, etc. To use pin GPIO0 you would write digitalWrite(0, HIGH);
If you are using a NodeMCU things get a little bit more complicated. The original creators of the
NodeMCU LUA firmware and the development module of the same name had the idea to give the
pins different names. They are called D0, D1, etc. That by itself would not be confusing yet but they
are not using the same digits, e.g. GPIO1 is not equal to D1. Here is a table to map the pins:
ESP8266 Programming Basics 19
Interrupts
Depending on your age you might remember interrupts from your PC. They were always important
to get your sound card to play beautiful music. The ESP8266 can also be controlled by interrupts. In
the previous exercises we were checking regularly for the state of a GPIO pin. This is fine if you are
not doing anything else in the main loop. But you might miss a change in a state if it is very short,
and that is were the interrupts can help. With an interrupt handler you can tell the processor that
you are interested in a specific type of change and a given pin. This is how it works:
1 void buttonPressed() {
2 ...
3 }
4
5 void setup() {
6 pinMode(PIN, INPUT);
7 attachInterrupt(digitalPinToInterrupt(PIN), buttonPressed, CHANGE);
8 }
buttonPressed() is a method without parameters that gets called when there is a change on PIN.
Instead of CHANGE you can also use RISING which triggers the callback when the pin changes from
LOW to HIGH, or FALLING for a change in the opposite direction. Please do not execute long tasks in
the callback method. The ESPs watchdog will reset the processor if calling the interrupt takes too
much time. You should not do much more than changing a flag.
You can also use the ADC to measure the input voltage without any additional wiring. You have to
instruct the processor that you want to measure the supply voltage rather than the value on A0 with
a special command outside the setup() and loop() method. Here is an example:
1 ADC_MODE(ADC_VCC);
2
3 void setup() {
4 Serial.begin(115200);
5 }
6
7 void loop() {
8 Serial.println(ESP.getVcc());
9 delay(500);
10 }
WiFi
The last few chapters were all about built-in functions of the Arduino/ESP8266 platform. Now we
will start using libraries which are part of the platform and are already installed. So how can we use
the WiFi module of the ESP8266? First of all you need to know that the ESP8266 can operate as a
WiFi client (like a smartphone or laptop) and/or as an access point (like a WiFi router or extender).
You can set this mode with:
1 WiFi.mode(m);
where m must be one of the following modes: WIFI_AP (access point), WIFI_STA (client), WIFI_AP_STA
(AP and client) or WIFI_OFF.
Now lets connect to your access point:
1 WiFi.begin(WIFI_SSID, WIFI_PWD);
This will connect you to an access point given its SSID and the password. Please note that this call
is not blocking. This means that the code will immediately proceed to the next instruction whether
the ESP successfully connects to the access point or not.
ESP8266 Programming Basics 23
HTTP
By connecting to the internet you can exchange data between your ESP8266 and the network. Lets
look at how we can load content from a web server using the Hyper Text Transfer Protocol (HTTP).
This protocol is the foundation of the World Wide Web.
1 #include <ESP8266WiFi.h>
2
3 const char* ssid = "SSID";
4 const char* password = "PASSW0RD";
5
6 const char* host = "www.squix.org";
7
8 void setup() {
9 Serial.begin(115200);
10
11 Serial.print("Connecting to ");
12 Serial.println(ssid);
13
14 WiFi.begin(ssid, password);
15
16 // Wait until WiFi is connected
17 while (WiFi.status() != WL_CONNECTED) {
18 delay(500);
19 Serial.print(".");
20 }
21
22 Serial.println("");
23 Serial.println("WiFi connected");
24 Serial.println("IP address: ");
25 Serial.println(WiFi.localIP());
26 }
27
28 void loop() {
29 delay(5000);
30
31 Serial.print("connecting to ");
32 Serial.println(host);
33
34 // Use WiFiClient class to create TCP connections
35 WiFiClient client;
36 const int httpPort = 80;
ESP8266 Programming Basics 24
37 if (!client.connect(host, httpPort)) {
38 Serial.println("connection failed");
39 return;
40 }
41
42 // We now create a URI for the request
43 String url = "/guide/";
44
45 Serial.print("Requesting URL: ");
46 Serial.println(url);
47
48 // This will send the request to the server
49 client.print(String("GET ") + url + " HTTP/1.1\r\n" +
50 "Host: " + host + "\r\n" +
51 "Connection: close\r\n\r\n");
52
53 unsigned long timeout = millis();
54 while (client.available() == 0) {
55 if (millis() - timeout > 5000) {
56 Serial.println(">>> Client Timeout !");
57 client.stop();
58 return;
59 }
60 }
61
62 // Read all the lines of the reply from server and print them to Serial
63 while(client.available()){
64 String line = client.readStringUntil('\r');
65 Serial.print(line);
66 }
67
68 }
How does this work? First we define the SSID and password of the WiFi access point we want to
connect to. Please note that there are better ways to do that. The WiFiManager (https://github.com/
tzapu/WiFiManager) for instance starts the ESP8266 as an access point if it cannot connect to any
SSID. You then use your smartphone to configure the WiFi credentials and there is no need to hard
code these into your firmware. But for the sake of simplicity lets ignore this here.
On line 14 we start connecting to the defined access point and wait until the connection is
established. After all there is no point to send requests to a server if the network connection is
not confirmed yet.
Line 49 sends the request to the server. The command GET /guide/ HTTP/1.1\r\n might look
ESP8266 Programming Basics 25
strange to you. This is how your browser talks to the web server. GET is the command for the
webserver, /guide/ is the resource on the server we want to get and HTTP/1.1 is the protocol that
we are using. If you are interested how this works in detail have a look at this Wikipedia article:
https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol.
On line 63 we print out the response line by line as long as there is text coming in.
Sadly this is quite complicated. Especially if we want to add encryption in the form of SSL to the
connection. This protects your data and makes sure that you are talking to the right server. With the
following command we can verify that the host matches the given SHA1 fingerprint.
1 if (client.verify(fingerprint, host)) {
2 Serial.println("certificate matches");
3 } else {
4 Serial.println("certificate doesn't match");
5 return;
6 }
How can you find this fingerprint? Your browser can help you with this. I will show it with Chrome.
First open the page you need the fingerprint for, in my case www.google.ch. Then click on the little
lock symbol and then on Details:
A click on View Certificate will bring up the detail window about Googles certificate:
ESP8266 Programming Basics 26
Scroll down to the bottom of the window and copy the value behind SHA1. This is the fingerprint to
verify that you are actually talking to www.google.ch.
Libraries: If you are new to programming you might ask what libraries are. When we
develop programs we use libraries to not have to invent the wheel over and over again.
Libraries contain functionality that might be used in different places without creating
copies of code which is hard to maintain. So for you libraries are a wonderful thing: you
can concentrate on the things that really matter to you. In the case of the WeatherStation
they provide a lot of functionality which normally would take you a lot of time to write
yourself.
In order to get the WeatherStation to compile you will have to download three libraries. The first
library is the WeatherStation itself. This will give you some new entries in the Example menu of the
Arduino IDE. The second one is to read and understand the data which the program gets from the
weather forecast service. And the third is needed to use the beautiful OLED display.
Go to Sketch > Include Library > Manage Libraries and install the following three libraries.
Make sure that you always have the latest version of the libraries installed. Users have reported
many issues which could be reduced by simply updating the library. Also make sure that you only
have one version of each of the libraries installed.
API (Key): What is an API and what is an API Key? Application Programming Interfaces
(APIs) are a well-defined way on how one piece of code can talk to another. This can be on
the same device, but often refers to the communication between two devices connected
by a network. For the WeatherStation we need to get current and forecast data in a
machine-readable format. To do this we will call the API of a service called Wunderground.
Wunderground has different price plans and we will use the Free plan which has some
limitations to distinguish it from the per-pay plan. To have better control over the users
who access the service we will have to get a short text value - the API key - before we can
call it. You should treat API keys like a password and be careful with them. For instance,
do not post them to a forum, and dont commit them to a public code repository. If you do
your key may be cancelled, and all your projects will fail!
The ESP8266 WeatherStation 29
If you should forget your key you can always come back and get it here.
Lets start with the WiFi Settings. Replace yourssid with the name of your WiFi network and
yourpassw0rd with its password. I had problems with a network that contained a dash (-)
in the SSID. If you are having problems consider this hint
Next is the update interval. This value specifies how often the weather data should be updated
from the internet. The default is 600 seconds (10 minutes). In my experience this is a good
value, because you dont have unlimited requests in your free Wunderground API account
and the weather doesnt change so often anyway.
Now to the Display Settings. If you attach the display as I show in the next chapter you dont
have to change anything here. D3 and D4 are the pin names in the NodeMCU module. If you
get compilation errors about them make sure that you have set your board to NodeMCU V1.0,
if that is the module you are using. If you have another board just replace the pin numbers
with the proper pin number, e.g. 5 or 6.
Use the Time Client Settings section to adjust your local time zone offset compared to the
UTC time zone. It also allows a half-hour offset, thanks to the user who lives in such a time
zone and made me implement that. (Ignorance is bliss until you get confronted with it)
The ESP8266 WeatherStation 30
In the Wunderground section you can now use the API key you received in the previous
section. Also set the country and city of the place you want to show. To figure out which val-
ues work you can modify this URL: http://api.wunderground.com/api/3APIKEY/conditions/
q/CA/San_Francisco.json and replace APIKEY with yours and CA and San_Francisco with your
state or country and city.
For the moment ignore the ThingSpeak settings. You might use it in a future project (have a look at
the More Projects chapter). Now we are almost ready to get the weather station running on the
ESP8266 for the first time. But we need to wire the display to the NodeMCU first.
VCC and GND are the power supply of the display. VCC is the positive supply voltage and GND
stands for ground. They will be connected to 3V3 and GND on the NodeMCU board
SCL and SDA are the data lines of the I2C protocol. SCL stands for Serial Clock and SDA for
Serial Data.
In the following diagram I used a breadboard and male-to-male jumpers to connect the components.
But you can also connect them directly with four (colored) female-to-female jumpers. They come
with the WeatherStation Kit. Just peel the first four wires off of the bundle and connect them
according to the picture. The colors do not matter.
The ESP8266 WeatherStation 31
Please note: there are versions of the OLED display with swapped GND and VCC pins. Be careful to
connect them according to the printed labels, not (necessarily) this diagram!
As mentioned earlier, there exists a little confusion about the pin names. The Arduino IDE uses
the GPIO number given by the chip. The NodeMCU team who designed the board changed the
pin naming for their LUA firmware. If you are programming a NodeMCU module you can use the
printed D# names. If you use a generic ESP8266 module then you have to use the corresponding
GPIO numbers. Here is a table of the mapping:
The ESP8266 WeatherStation 32
The NodeMCU Index is the name on the board, whereas the ESP8266 Internal column is the one you
use in the Arduino IDE code: e.g. D5 on the board is pin GPIO14 in C/C++.
First Run
Now were all set to run the WeatherStation software for the first time. Click on the Upload arrow
and wait until the compilation and the transfer have ended. Now you should see the OLED display
lighting up and displaying a WiFi icon. The module should now be trying to acquire access to the
wireless network you have defined earlier.
This is just the beginning. In the next chapter Ill give you some ideas of what else you can build
with the WeatherStation hardware.
Summary
If everything went well you now have a working ESP8266 WeatherStation. Congratulations! Lets
look back what we did in this chapter:
1. We used the WeatherStation example and created a working copy for us. All changes will be
applied to the copy, not the original example. If you accidentally make your code unusable
you can always go back to the example and start with a fresh copy.
2. We installed several libraries by using the Arduino IDE Library Manager. Libraries help us to
reuse code or binaries in many places without using barely maintainable copy/paste code.
3. We created an API key from Wunderground. Every time we call the Wunderground API to
update weather data we will send this key along so that Wunderground knows who we are.
Many service providers use a similar scheme to control and limit usage of their services.
4. We changed a few lines in the code to configure the WiFi settings, update interval, display
pins, timezone and the API key for Wunderground.
5. We connected the OLED display and the ESP8266 and uploaded the firmware.
The WeatherStation Code Explained
In this chapter we will have a look at the building blocks of the WeatherStation. This project is a
relatively complex piece of code and I hope to improve this chapter over time with new details.
Please consider subscribing at https://blog.squix.org/subscribe to get notified about updates.
The Grammar
The following image describes the grammar of a valid JSON object in a very concise way. It means
that a JSON object knows three basic types:
object
array and
value
JSON grammar
Objects always start and end with curly brackets. They can be either empty (line to the top) or
contain string/value pairs, separated by a colon. These pairs can be repeated by adding a comma
The WeatherStation Code Explained 35
between them. Arrays start and end with square brackets. They can be either empty or contain a
value. At this point we dont know yet what a value is. Values in an array can be repeated and must
be separated by a comma. Values were already used for the two previous definitions and here lies
the power of this kind of grammar: because a value can contain a simple text, a number, an object
(yes, the object we defined before!!!), an array (also defined before), booleans or a null value.
This is so powerful because we are reusing the definitions and we are nesting them within each
other: an object can contain a value, a value can contain an array or an object. And finally an array
can contain a value, repeatedly! Isnt this beautiful?
1 class JsonListener {
2 public:
3 virtual void whitespace(char c) = 0;
4 virtual void startDocument() = 0;
5 virtual void key(String key) = 0;
6 virtual void value(String value) = 0;
7 virtual void endArray() = 0;
8 virtual void endObject() = 0;
9 virtual void endDocument() = 0;
10 virtual void startArray() = 0;
11 virtual void startObject() = 0;
12 };
The methods here are callback methods which will get invoked if the respective event happens while
parsing the document. Lets start with an example. For the JSON object {name: Eichhorn} we
get the following invocations:
I often just implement (AKA write code) for the key() and the value() methods. In the key()
method I store the value of the key parameter. Then later in the value() method I check what the
last key was I had seen and then I store the value in the appropriate variable. For the example from
before I would do
In the stream of the object {name: Eichhorn} we will first get a call to the method key() with
the value name which we store in currentKey_. Next the parser will detect a value and call our
value() method with the value Eichhorn. The parser can now make the connection (or create a
context) that after the key name the value Eichhorn should be stored in the member variable
name_.
If this example was too simple then have a look here: https://github.com/squix78/esp8266-weather-
station/blob/master/WundergroundClient.cpp This is the code which parses the responses from
Wunderground for my WeatherStation.
Conclusion
For a document or object of the size we had in the example a streaming parser is usually an extreme
overkill. It is complicated to use, requires you to write a lot of code and is memory-wise probably
even worse than a tree parser. It is only recommended to implement a streaming parser if you have
big objects or if you just dont know how big your object might be. In those cases a streaming parser
will be a good friend, since it only requires memory for the objects you actually want to use from
the whole big document. You can find my library here: https://github.com/squix78/json-streaming-
parser
Collecting and Displaying Local Data
So far we have created a device which connects to the internet and uses an API to fetch weather
data. But this is not really an Internet-of-Things application; after all we are just displaying data. In
this chapter we will change that.
I will call this combo The Climate Node. Now use the female-to-female jumpers to connect them
like this:
Collecting and Displaying Local Data 38
Thingspeak Setup
Thingspeak is a free cloud service which allows you to easily post (sensor) data, to visualize it
and to retrieve it again using simple HTTP methods. Id like to point out that you could also use
Wunderground to store your climate data and it probably would be very easy to do as well. But I
believe that for education purposes Thingspeak permits more degrees of freedom. After all you could
also send readouts from a motion sensor and visualize this information on a chart. Thingspeak has
some nice additional features which lets you program webhooks to trigger a push notification on
your cell phone, etc.
So first of all you have to sign up for a (free) account on Thingspeak. Go to https://thingspeak.com/
users/sign_up and create the account. After you completed that process, log in to your new account
and go to My Channels:
Collecting and Displaying Local Data 39
Navigate to My Channel
Then you click on the New Channel button, and fill out the form:
Explanation: The name just helps you to recognize your channel among many others that you might
create over time. The important parts are the field names. These names will later show up in the
chart and with this you are telling Thingspeak that the value you send later with the field1 attribute
should be displayed as Temperature.
Now navigate to the API Keys tab and note the two generated keys:
Collecting and Displaying Local Data 40
The first one will allow you to write to this channel in Thingspeak, and the second one will allow
you to read from it. Treat them as secrets and with care. Others might be able to spam your channel
or to steal your data. We will use these keys soon enough Also note the channel ID on top of the
screen, in my case 76642.
Collecting and Displaying Local Data 41
1 //Thingspeak Settings
2 const String THINGSPEAK_CHANNEL_ID = "67284";
3 const String THINGSPEAK_API_READ_KEY = "L2VIW20QVNZJBLAK";
If you didnt remove the climate node section in your weather station code you just need to flash
your WeatherStation with the updated API key and channel ID and voil: you just successfully sent
the temperature and humidity data from the next room once around the world just to display it on
a tiny OLED display. I know some people (including my wife) who wouldnt be impressed by that
at all;-).
Explanation: This last step might have been a bit confusing, since the TV cook had prepared
everything hours before the show actually started, so heres the summary in slow motion: the
Weather Station library now comes with a class called ThingspeakClient which does all the
legwork. You pass in the channel id and API key and it downloads a JSON object and picks out just
the last data set, since we are currently interested only in that. Have a look at https://github.com/
squix78/esp8266-weather-station/blob/master/ThingspeakClient.cpp to understand what happens
and maybe adapt/extend it to your needs. Why not display the chart of the last 24 hours on the
OLED display?
More Projects
In the last chapters you successfully set up the development environment to program the ESP8266
and got your first Internet-of-Things device running. While this chapter concludes this Getting
Started Guide I hope it is just the beginning of many interesting IoT projects you will build.
The PlaneSpotter uses the currently free API of adsbexchange.com to fetch information on airplanes
close to the given coordinates every 30 seconds. Adsbexchange gets its data from hundreds of low-
cost repurposed DVB-T dongles which receive the ADS-B signal transmitted by aircraft. Since data
coverage in my area was not so good at the time I built my own receiver with a Raspberry Pi and a
USD $10 USB TV dongle.
To build this project you can use the PlaneSpotterDemo which comes with the WeatherStation
library: File > Examples > ESP8266 Weather Station > PlaneSpotterDemo
If you want to try out the Platformio IDE there is also a separate project on Github:
More Projects 45