#include <WiFi.h>
#include <esp_now.h>
//we have this value to multipy to our time to sleep
//as the esp lib be using time as uS
#define uS_TO_S_FACTOR 1000000
// The leader's STUDENT CODE IS 10947943.
// So that the time to sleep would be 48s
#define TIME_TO_SLEEP 48 //Sleep for 48 seconds
RTC_DATA_ATTR int bootCount = 0;
//The ultrasonic physical pins connecting to esp phisical pins
const int trigPin = 5;
const int echoPin = 18;
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
//These are variables that we will be using for
// measuring distance with ultrasonic
long duration;
float distance;
// MAC receiver
uint8_t broadcastAddress[] = {0x8C, 0xAA, 0xB5, 0x84, 0xFB, 0x90};
esp_now_peer_info_t peerInfo;
// Defining function for each part of the code
void Deep_sleep(){
//this function will set the time to sleep and
// also turn the esp in deep sleep mode
Serial.println("Going to sleep now");
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("ESP32 sleep every " + String(TIME_TO_SLEEP) + "seconds");
Serial.flush();
esp_deep_sleep_start();
}
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
//Checking if the sink node is sending data
Serial.print("Send Status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Ok" : "Error");
}
void OnDataRecv(const uint8_t * mac, const uint8_t *data, int len) {
//in this function the message from sink node
// is recievied by the main esp and then it will be printed
Serial.print("Message received: ");
char receivedString[len];
memcpy(receivedString, data, len);
Serial.println(String(receivedString));
}
int ultra_function () {
//this function is calculating and returning the distance using ultrasonic
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distance = duration * SOUND_SPEED/2;
delay(1000);
return distance;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); // Starts the serial communication
//ultra setup
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
//2 esp talking setup
WiFi.mode(WIFI_STA);
esp_now_init();
//send callback
esp_now_register_send_cb(OnDataSent);
//receive callback
esp_now_register_recv_cb(OnDataRecv);
// Peer Registration
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
esp_now_add_peer(&peerInfo);
}
void loop() {
//In our main loop firstly, we call the ultrasonic function
// to get the distance value
ultra_function();
delay(10); // this speeds up the simulation
//Then, we check the distance value.
//If it is more than 50cm, it means that our parking space is not occupied
//so, the message that has to be sent to the main esp is "FREE" and
//after that we call the deep sleep function and
// the sink esp will be go in sleep mode for 48s
//the 1ms delay is for esp to process the ultra sonic function and if function.
//if we dont put it here, the esp will not be able to function
//as it goes to deep sleep mode immediately
if(distance> 50){
String message = "FREE";
esp_now_send(broadcastAddress, (uint8_t*)message.c_str(), message.length() + 1);
delay(1);
Deep_sleep();
}
//If the distance is less than or egual to 50,
//it means that the parking space is occupied
//the rest is the same as above.
else {
String message = "OCCUPIED";
esp_now_send(broadcastAddress, (uint8_t*)message.c_str(), message.length() + 1);
delay(1);
Deep_sleep();
}
}