ESP32 emitter board code:
#include <Arduino.h>
#include <WiFi.h>
#include <esp_now.h>
#include <DHT.h>
#include <Adafruit_Sensor.h>
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
#define DHTPIN 26
#define DHTTYPE DHT11
typedef struct struct_message
{
int a;
float b;
float c;
} struct_message;
struct_message myDataSen;
esp_now_peer_info_t peerInfo;
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
{
}
const int sensor=32;
const int bomba=27;
const int doneTpl=25;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
dht.begin();
pinMode(sensor, INPUT_PULLDOWN);
pinMode(bomba, OUTPUT);
pinMode(doneTpl, OUTPUT);
if (esp_now_init() != ESP_OK)
{
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(OnDataSent);
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
Serial.println("Failed to add peer");
return;
}
}
void loop() {
digitalWrite(doneTpl, LOW);
int sensorValue = analogRead(sensor);
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Read temperature as Fahrenheit
float f = dht.readTemperature(true);
// Compute heat index in Fahrenheit
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius
float hic = dht.computeHeatIndex(t, h, false);
myDataSen.b=h;
myDataSen.c=hic;
myDataSen.a=sensorValue;
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&myDataSen, sizeof(myDataSen));
delay(1000);
if(sensorValue>=1730){
digitalWrite(bomba, HIGH);
delay(3000);
digitalWrite(bomba, LOW);
}
digitalWrite(doneTpl, HIGH);
delay(2000);
}
Platform.ini file to import the libraries.:
[env:az-delivery-devkit-v4]
platform = espressif32
board = az-delivery-devkit-v4
framework = arduino
lib_deps =
adafruit/DHT sensor library @ ^1.4.3
Receiver board code to display the content per terminal on the computer:
#include <Arduino.h>
#include <WiFi.h>
#include <esp_now.h>
typedef struct struct_message {
int a;
float b;
float c;
} struct_message;
// Create a struct_message called myData
struct_message myDataRec;
esp_now_peer_info_t peerInfo;
String array[3];
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myDataRec, incomingData, sizeof(myDataRec));
array[0]=myDataRec.a;
array[1]=myDataRec.b;
array[2]=myDataRec.c;
Serial.print("Humedad tierra: ");
Serial.print(array[0]);
Serial.print(", humedad ambiente: ");
Serial.print(array[1]);
Serial.print(", temperatura ambiente: ");
Serial.print(array[2]);
Serial.println("");
}
void setup() {
// Initialize Serial Monitor
Serial.begin(9600);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
void loop() {
}
Text displayed by terminal when collecting data:
Leave a Reply Cancel reply