#include <WiFi.h>
#include <esp_now.h>
// Define the MAC address of the central ESP32 sink node
uint8_t broadcastAddress[] = {0x8C, 0xAA, 0xB5, 0x84, 0xFB, 0x90};
// Here we define the pin connections for the HC-SR04 sensor
#define ECHO_PIN 14 // ECHO digital PIN 14
#define TRIG_PIN 12 //TRIG digital PIN 12
// Define the time interval for the duty cycle (15 seconds)
#define DUTY_CYCLE_INTERVAL 15 // // Define the time interval for the duty cycle (15 seconds)
esp_now_peer_info_t peerInfo;
// Callback function to handle data sent event
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("Send Status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Ok" : "Error");
}
// Callback function to handle data received event
void OnDataRecv(const uint8_t *mac, const uint8_t *data, int len) {
Serial.print("Message received: ");
char receivedString[len];
memcpy(receivedString, data, len);
Serial.println(String(receivedString));
}
void setup() {
Serial.begin(115200);
// Set up the HC-SR04 sensor pins
pinMode(ECHO_PIN, INPUT);
pinMode(TRIG_PIN, OUTPUT);
// function which Triggers the sensor to emit an ultrasonic signal, Measures, and Calculates the distance based on the duration
//return string "OCCUPIED" or "FREE".
String message = measurement();
//Operation mode of ESP32 wifi interface to station mode
WiFi.mode(WIFI_STA);
// Initialize ESP-NOW
esp_now_init();
// Register callback function for sending data
esp_now_register_send_cb(OnDataSent);
// Register callback function for receiving data
esp_now_register_recv_cb(OnDataRecv);
// Peer Registration
memcpy(peerInfo.peer_addr, broadcastAddress, 6);//This copies the MAC address of the peer device into the peer_addr field of the peerInfo structure.
peerInfo.channel = 0; // Set the communication channel to be used for communication with the peer device.
peerInfo.encrypt = false; //encryption is disabled.
// Adds the peer device to the ESP-NOW peer list, Once added, the ESP32 can communicate with this peer device using ESP-NOW.
esp_now_add_peer(&peerInfo);
// Send status using ESP-NOW
esp_now_send(broadcastAddress, (uint8_t *)message.c_str(), message.length() + 1); //ends data using ESP-NOW to the specified destination, data inside message variable
delay(5000); // it allows time for the message to be transmitted and processed by the receiver
Serial.println("going to sleep");
esp_sleep_enable_timer_wakeup(DUTY_CYCLE_INTERVAL * 1000000); // // Set up timer to wake up every DUTY_CYCLE_INTERVAL seconds
Serial.flush(); //This line ensures that any pending data in the serial buffer is transmitted before entering deep sleep
// Go back to deep sleep
esp_deep_sleep_start();
}
void loop() {
}
//This measurement() function is responsible for measuring the distance using an HC-SR04 ultrasonic sensor
String measurement() {
// emitting the signal (by activating the trigger pin) for 10 microseconds
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
//we can calculate distance with the duration
//Value stored in variable name duration, long instead of int because can cause overflow
long duration = pulseIn(ECHO_PIN, HIGH);
//Calculate the distance in cm with the sound speed(343 m/s), and store it in "distance"
float Distance = (duration * 0.0343) / 2.0;
// OUTPUT that we want
if (Distance < 50.0)
return "OCCUPIED";
else
return "FREE";
}