Shares
Connect the ESP8266 WiFi Chip to your Raspberry Pi

Connect the ESP8266 WiFi Chip to your Raspberry Pi

Last Update: / by Marco Schwartz

In this project, we are going to make a typical home automation project: a WiFi weather measurement station. We will connect a DHT11 sensor to an ESP8266 board, and access the data via WiFi. However, here, we are actually going to grab that data from a Raspberry Pi, and make the Pi display the data on a simple graphical interface.

To do so, we will run a simple web server on the ESP8266 chip. The Raspberry Pi will then access this data via WiFi, and display it graphically. The nice thing is can you can apply this in several other projects, to make the Raspberry Pi the ‘hub’ of your home, with several ESP8266-based devices connected to it. Let’s start!

Hardware & Software Requirements

For this project, you will of course need an ESP8266 chip. Here, I used the Adafruit ESP8266 board.

You will also need a fully configured Raspberry Pi board, with the Raspbian operating system installed on it.

You will also need a temperature sensor. I used a DHT11 sensor, which is cheap, very easy to use & that will allow us to measure the ambient temperature & humidity.

This is a list of all the extra components that will be used in this chapter:

You will need to have the latest version of the Arduino IDE installed. You will also need the DHT library. You can install it from the Arduino library manager, that you can access from Sketch>Include Libraries>Manage Libraries inside the Arduino IDE.

On your Raspberry Pi, you will also need to have Node.js installed. To do so, follow the instructions from:

https://learn.adafruit.com/node-embedded-development/installing-node-dot-js

Hardware Configuration

We are first going to see how to configure the hardware to use the ESP8266 board. First, place the Adafruit ESP8266 board on the breadboard.

Once this is done, simply insert the DHT11 sensor on the breadboard. Then, connect the left pin of the sensor to the 3.3V pin of the ESP8266, the right pin to GND (blue power rail), and the pin next to VCC to the GPIO5 pin on your ESP8266 board. This is the final result, not showing the USB-to-Serial FTDI converter:

Connect the ESP8266 WiFi Chip to your Raspberry Pi

Make sure that you connected everything according to the instructions above, or you won’t be able to continue.

Testing the Sensor

We are now going to test the sensor. Again, remember that we are using the Arduino IDE, so we can code just like we would do using an Arduino board. Here, we will simply print the value of the temperature inside the Serial monitor of the Arduino IDE.

This is the complete code for this part:

// Libraries
#include "DHT.h"

// Pin
#define DHTPIN 5

// Use DHT11 sensor
#define DHTTYPE DHT11

// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE, 15);

void setup() {

// Start Serial
Serial.begin(115200);

// Init DHT
dht.begin();
}

void loop() {

// Reading temperature and humidity
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();

// Display data
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");

// Wait a few seconds between measurements.
delay(2000);

}

Let’s see the details of the code. You can see that all the measurement part is contained inside the loop() function, which makes the code inside it repeat every 2 seconds.

Then, we read data from the DHT11 sensor, print the value of the temperature & humidity on the Serial port.

Note that the complete code can also be found inside the GitHub repository of the book:

https://github.com/openhomeautomation/connect-esp8266-rpi

You can now paste this code in the Arduino IDE. Then, go in Tools>Boards, and select the Adafruit ESP8266 board from the list.

After that, we need to put the board in bootloader mode, so we can program it. To do so, refer to the first chapter of this part of the book.

Now, upload the code to the board, and open the Serial monitor when this is done. Also set the Serial monitor speed to 115200.

You should immediately see the temperature & humidity readings inside the Serial monitor. My sensor was reading around 24 degrees Celsius when I tested it, which is a realistic value.

Accessing the Sensor via WiFi

At this point, we are sure that the sensor is working and that data can be read by the ESP8266 chip. Now, we are going to build the sketch that will connect to your WiFi network, and then make the measurements accessible by the Raspberry Pi.

As this sketch is quite long, I will only detail the most important parts here. You can of course find the complete code for this project inside the GitHub repository of the project.

It starts by including the required libraries for this project:

#include "ESP8266WiFi.h"
#include 
#include "DHT.h"

Then, you need to set up your own WiFi network name & password in the code:

const char* ssid = "your_wifi_network_name";
const char* password = "your_wifi_network_password";

We also declare the aREST API, that will help us control the board remotely from the Raspberry Pi:

aREST rest = aREST();

Now, we also declare two variables that will contain the measurements made by the sensor:

int temperature;
int humidity;

After that, we create a web server on port 80:

WiFiServer server(80);

Then, inside the setup() function of the sketch, we connect the ESP8266 to the WiFi network:

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

Then, we expose the two measurement variables to the aREST API, so they can be access from the outside:

rest.variable("temperature",&temperature);
rest.variable("humidity",&humidity);

After that, we set the name & ID of the board:

rest.set_id("1");
rest.set_name("sensor_module");

Then, we start the server, and print the IP address on the Serial port:

// Start the server
server.begin();
Serial.println("Server started");

// Print the IP address
Serial.println(WiFi.localIP());

Inside the loop() function of the sketch, we check if a client is connected to the ESP8266, and handle the request:

WiFiClient client = server.available();
if (!client) {
return;
}
while(!client.available()){
delay(1);
}
rest.handle(client);

Then, we read data from the sensor:

temperature = dht.readTemperature();
humidity = dht.readHumidity();

It’s now time to upload the sketch the board. You can grab the complete code from:

https://github.com/openhomeautomation/connect-esp8266-rpi

Follow the instructions we saw in the previous section to upload the code to the board. Then, open the Serial monitor to get the IP address of the board, you will need it soon.

Connecting the Board to Your Raspberry Pi

We are now going to see how to connect the board to your Raspberry Pi, so it can display the data in a nice graphical interface.

The only thing that you need to change to run this interface is the IP address of your ESP8266 board inside the app.js file:

rest.addDevice('http','192.168.0.100');

You can get all the code from:

https://github.com/openhomeautomation/connect-esp8266-rpi

Place all the files into a folder on your Pi, navigate to this folder with a terminal, and type:

sudo npm install express jade arest

Be patient, this step can take a while. After that, type:

sudo node app.js

This will start the application on your Raspberry Pi. Then, navigate to the URL of your Raspberry Pi on port 3000, for example:

http://192.168.0.104:3000

You should immediately see the interface showing the temperature & humidity readings made by the ESP8266 board:

Connect the ESP8266 WiFi Chip to your Raspberry Pi

How to Go Further

Let’s summarise what we achieved in this project. We built a WiFi measurement station based on the ESP8266 WiFi chip. We used a sensor to measure the local temperature & humidity. Then, we displayed all the measurements on a web page that was served by the Raspberry Pi.

There are many things you can do to improve this project. You can for example deploy more of those sensors boards in your home, and display all the measurements on a single page on your Pi.

Want to learn more? Get my free eBook about the ESP8266!
The ESP8266 is an amazing WiFi chip that you can use to build all sorts of projects. Download today my free eBook "Build a WiFi Weather Station with the ESP8266". Simply click on the button below!

Leave a Comment

Please enter your name.
Please enter your email.
Please enter your comment.
Massimo Miniera 4 days ago
Hi, which ESP8266 model was used in the tutorial. Thanks
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Rodolpho Mello 10 months ago
"Error adding the device" during app.js execution.What could it be?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Ankit a year ago
everything goes well untill sudo npm install express jade arest in my Pi 0 i'm not able to run this command it gives an error as follow: jade has been renamed to pug, please install the latest version of pug instead of jade. and there is no such file or directory,open '/home/pi/downloads/esp-pi/package.json
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Alex Targov Ankit 10 months ago
That is because Jade no longer exist. Instead of installing jade, you would need to install pug. So that would be `npm i pug`
heri a year ago
how to connect the ESP more than one? could you help me?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
RoySeberg a year ago
Suppose I have an ESP12F with an 24 bit ADC that reads a voltage and I wish to send the data bytes every 100 milliseconds or so and then plot the data on a graph, how could I do that?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
AKSHATHA 2 years ago
Sir can we do this for four node mcu
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz AKSHATHA 2 years ago

Sure thing!

Søren Løkkegaard 3 years ago
Would i be possible to switch it around, so the ESP is the client instead of web server.so it would be possible to put it to sleep, and send data at a fixed interval.This way it could be battery operated.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
clinton panjaitan 3 years ago
hei sir.. why i got this problem? https://uploads.disquscdn.c...
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
ROHIT CHAKRABORTY 3 years ago
how to do the same thing without making esp connect to wifi / internet?is there some other way to send data to pi without connecting to internet? as I have a lot of esp to connect to one pi , I cant make all esp to connect to a router.my pi can get internet access.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Dean Greenhough 3 years ago
Hi MarcoCan you kindly update this page as I am unable to get this to work due to a Jade/pug issue. I did purchase your builder pack for both raspI and esp8266, but still with all the extra useful information I am unable to get this to work. Your kind and helpful input would be gratefully apprecited
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz Dean Greenhough 3 years ago
Hi Dean, can you send a message to me directly at [email protected]?
Dean Greenhough 3 years ago
There appears to be an issue with Jade and changing to pug, running app.js gives me an error of cannot find module mime-db. Can anyone help with this please, am i not ptting everything in the correct place?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.