In this Instructable I show you how I used ESP-NOW in my home automation. In the schematic you can see the communication flows
I show you:
- how the sender works
- how the receiver works
- how the serial communication with a Raspberry Pi works
I do not show how MQTT and my home automation works, you can find other good instructions for that.
Credits: This Instructable was inspired by:
Step 1: Back-up Via WiFi and MQTT
I found out that ESP-NOW is extremely fast and quite reliable, but sometimes fails. In my program I added a back-up via normal WiFi and MQTT connection.
Step 2: The Remote/sender
In the first scheme you see how the program of the sender works:
- The esp8266 starts up from power up or from reset
- ESP-NOW is initialized
- The module sends ESP-NOW for a defined time
- The module checks whether the ESP-NOW message was received by the receiver in a call-back function.
- If the ESP-NOW message was received, the module goes in a low power deep sleep
- If the ESP-NOW message was not received, the module starts a WiFi connection and a MQTT connection as a back-up connection.
The program for the sender is in my Github (Master_sender.ino). The programs sends both the topic and the message for the MQTT home automation in one combined ESP-NOW message, combined at a '&' sign for an easy split at the receiver.
I made two different remotes/senders:
Sender 1: The esp8266 is in deep sleep and is activated by a reset.
The led in the button lights up and dims when the module is done. If the voltage of the battery is to low, the led blinks. If the ESP-NOW failed, the led blinks before changing to WiFi/MQTT connections. The esp8266 is powered from a LiPo battery with a HT7333 voltage regulator to 3.3V
Sender 2: The esp8266 is powered off and is powered by pressing the button.
Normally the ESP-NOW connection is done after the button 'clicks'. The esp8266 is powered directly from a LiPo battery. I do not know if the esp8266 gets damaged by the high voltage of a fully charged battery (up to 4.2 V) when it is powered on just a few 100 ms. It is higher then the specs (3.0 - 3.6V).
Note: if the battery power is low, the ESP-NOW fails.
Step 3: The Receiver - Hardware (esp8266 and Raspberry Pi)
The desciption of the receiver is split in two parts:
The receiving esp-01 is connected to my Raspberry Pi Zero which runs my home automation system (Openhab2) and Node Red.
In the schematic the simple connections are shown:
- RX of the esp-01 to TX of the RasPi
- TX of the esp-01 to RX of the RasPi
- GND to GND
- The esp-01 VCC is powered by the 3.3V pin of the Raspi
- On the esp-01 both RST and CH_PD are pulled high.
I made an interface connector to mount it easily
In one picture you can see my wire connections to my Raspberry Pi 3 (which has the same pinout, but I used another 3.3V pin to connect to CH_PD).
The esp-01 was programmed as showed in my other Instructable (see step 3).
Step 4: The Receiver - Software (Serial, Node Red, MQTT)
Program on esp8266
The program for the esp-01 receiver is in my Github (Master_sender.ino). The program is really simple, it only prints the received ESP-NOW message on the Serial port to the Raspberry Pi.
On the Raspberry Pi, you must activate the Serial Port (on raspbian, execute 'sudo raspi-config') for normal serial communication.
The name of the serial port is:
- Raspberry Pi Zero: /dev/ttyAMA0
- Raspberry Pi 3: /dev/ttyS0
You can check the serial port by running 'dmesg | grep tty' or 'ls /dev'
In Node Red, select the serial input Node and setup the serial port (see the attached pictures). Note the baudrate of the serial connection, this must match the baudrate as programmed in the esp8266.
The output of the serial node is transformed by a function node which splits the message at the '&' sign and sets the msg.topic and the msg.payload. The output of the function is led in to the mqtt output node which sends the message to the MQTT server. Note the topic field of the mqtt node is blank, because the topic is set in the message.