Hi,
I'm currently trying to build a "wireless temperature display" to see several heating system temperatures from a different room.
I thought that I could use ESP NOW as an communication protocol, since this system must be standalone.
Sender / Broadcasting unit have a ESP32-C3, four DS18B20 and a LCD 16x2 display.
I already figured how to setup the display and see the sensor temperature on the sender unit.
Receiver would be similar, but without the sensors.
I think the display works now, but I can't get the data through.
I think I already got few bytes through with some example, but somehow I can't get my code to work.
Please someone guide me through this.
Sender code:
// Include the libraries we need
#include <WiFi.h>
#include <esp_now.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
// Data wire for the temperature sensors connected to GPIO4
#define ONE_WIRE_BUS 4
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
DeviceAddress sensor1 = { 0x28, 0x50, 0x38, 0x59, 0x0, 0x0, 0x0, 0xC1 };
DeviceAddress sensor2 = { 0x28, 0xA1, 0x1B, 0x35, 0x0, 0x0, 0x0, 0xB0 };
DeviceAddress sensor3 = { 0x28, 0x49, 0xF4, 0x57, 0x0, 0x0, 0x0, 0x1A };
DeviceAddress sensor4 = { 0x28, 0x7D, 0x8D, 0x5A, 0x0, 0x0, 0x0, 0x28 };
// REPLACE WITH THE MAC Address of your receiver
uint8_t broadcastAddress[] = {0xA0, 0x85, 0xE3, 0x4E, 0x8A, 0xC8};
// Define variables to store DS18B20 sensors readings to be sent
float temperature1;
float temperature2;
float temperature3;
float temperature4;
String success;
//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
float temp1;
float temp2;
float temp3;
float temp4;
} struct_message;
// Create a struct_message called DS18B20Readings to hold sensor readings
struct_message DS18B20Readings;
esp_now_peer_info_t peerInfo;
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char macStr[18];
Serial.print("Packet to: ");
// Copies the sender mac address to a string
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print(macStr);
Serial.print(" send status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void setup(void){
Serial.begin(115200);
sensors.begin();
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
while (!WiFi.STA.started()) { //New bit of code required to start
delay(100);
}
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
// Init ESP-NOW
if (esp_now_init() == ESP_OK) { //Start up esp_now
Serial.println("ESPNow Init Success");
} else { //if esp-now start up fails
Serial.println("ESPNow Init Failed");
ESP.restart();
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
peerInfo.channel = 0;
peerInfo.encrypt = false;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
void loop(void){
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
temperature1 = sensors.getTempC(sensor1);
temperature2 = sensors.getTempC(sensor2);
temperature3 = sensors.getTempC(sensor3);
temperature4 = sensors.getTempC(sensor4);
// Print sensor temperatures
Serial.println("Sensor temperatures:");
Serial.println(temperature1);
Serial.println(temperature2);
Serial.println(temperature3);
Serial.println(temperature4);
// Set values to be send
DS18B20Readings.temp1 = temperature1;
DS18B20Readings.temp2 = temperature2;
DS18B20Readings.temp3 = temperature3;
DS18B20Readings.temp4 = temperature4;
Serial.println("Data to be send:");
Serial.println(DS18B20Readings.temp1);
Serial.println(DS18B20Readings.temp2);
Serial.println(DS18B20Readings.temp3);
Serial.println(DS18B20Readings.temp4);
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &DS18B20Readings, sizeof(DS18B20Readings));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
//Show temperature values on LCD
// set cursor to first column, first row
lcd.setCursor(0, 0);
lcd.print(temperature1);
// set cursor to first column, second row
lcd.setCursor(0,1);
// print message
lcd.print(temperature2);
// set cursor to eighth column, first row
lcd.setCursor(8,0);
// print message
lcd.print(temperature3);
// set cursor to eighth column, second row
lcd.setCursor(8,1);
// print message
lcd.print(temperature4);
delay(5000);
lcd.clear();
}
Sender Serial Monitor:
17:26:31.982 -> Sensor temperatures:
17:26:31.982 -> 22.12
17:26:31.982 -> 21.50
17:26:31.982 -> 22.12
17:26:31.982 -> 22.37
17:26:31.982 -> Data to be send:
17:26:31.982 -> 22.12
17:26:31.982 -> 21.50
17:26:31.982 -> 22.12
17:26:31.982 -> 22.37
17:26:31.982 -> Sent with success
17:26:31.982 -> Packet to: a0:85:e3:4e:8a:c8 send status: Delivery Fail
Receiver code:
// Include the libraries we need
#include <WiFi.h>
#include <esp_now.h>
#include <LiquidCrystal_I2C.h>
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
// Define variables to store incoming readings
float incomingTemp1;
float incomingTemp2;
float incomingTemp3;
float incomingTemp4;
String success;
//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
float temp1;
float temp2;
float temp3;
float temp4;
} struct_message;
// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;
esp_now_peer_info_t peerInfo;
// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
Serial.print("Bytes received: ");
Serial.println(len);
incomingTemp1 = incomingReadings.temp1;
incomingTemp2 = incomingReadings.temp2;
incomingTemp3 = incomingReadings.temp3;
incomingTemp3 = incomingReadings.temp4;
Serial.println(incomingTemp1);
Serial.println(incomingTemp2);
Serial.println(incomingTemp3);
Serial.println(incomingTemp4);
}
void setup(void){
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
while (!WiFi.STA.started()) { //New bit of code required to start
delay(100);
}
Serial.print("ESP Board MAC Address: ");
Serial.println(WiFi.macAddress());
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
// Init ESP-NOW
if (esp_now_init() == ESP_OK) { //Start up esp_now
Serial.println("ESPNow Init Success");
} else { //if esp-now start up fails
Serial.println("ESPNow Init Failed");
ESP.restart();
}
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
}
void loop(void){
// print message
// set cursor to first column, first row
lcd.setCursor(0, 0);
lcd.print(incomingTemp1);
// set cursor to first column, second row
lcd.setCursor(0,1);
// print message
lcd.print(incomingTemp2);
// set cursor to eighth column, first row
lcd.setCursor(8,0);
// print message
lcd.print(incomingTemp3);
// set cursor to eighth column, second row
lcd.setCursor(8,1);
// print message
lcd.print(incomingTemp4);
delay(5000);
lcd.clear();
}
Receiver Serial Monitor:
17:27:31.524 -> ESP Board MAC Address: A0:85:E3:4E:8A:C8
17:27:31.524 -> ESPNow Init Success