Connect the ESP8266 WiFi Chip to your Raspberry Pi
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 article:
- Raspberry Pi board
- Adafruit ESP8266 breakout board
- USB 5V FTDI converter
- DHT11 sensor
- Breadboard
- Jumper wires
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.
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 project:
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.
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
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.
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.