/*CAMPUS Alexandre 10986751
The program follow the behaviour below:
1-initiate ranging
2-get the statut of the parking slot
3-enable wifi and setup esp now
4-send the parking slot statut
5-disable wifi and enter deep sleep for 6 seconds
The following assumption were done in order to reduce the theoric power consumption by keeping the wifi on the shortest time possible and having minimal feedback:
The OnDataSent callback is only used to confirm that the data is really sent to the sink node
The OnDataRecv is not used as the device isn't suppose to receive data and is put into deep sleep before
The loop() doesn't do anything as there is nothing to loop inside a transmission cycle
feel free to uncomment OnDataRecv and line 80 as well as comment line 91 and 100 if you want to see the echo of data sent
*/
#include <WiFi.h>
#include <esp_now.h>
#define PIN_TRIG 13 //Pin used to trigger ranging
#define PIN_ECHO 12 //Pin used to mesure distance
#define uS_TO_S_FACTOR 1000000
#define TIME_TO_SLEEP 6 //Slep for 6 seconds as personcode 10986751%50+5=6
// MAC receiver
uint8_t broadcastAddress[] = {0x8C, 0xAA, 0xB5, 0x84, 0xFB, 0x90};
esp_now_peer_info_t peerInfo;
//String sent to sink node
String message;
// Sending callback
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
//print the statut and the value of the data sent
Serial.print("Send Status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Ok Data sent: "+message : "Error");
}
//Receiving Callback (not used)
/*
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));
}*/
//Function use to start ranging and get the distance in cm
int GetDist(void){
digitalWrite(PIN_TRIG, HIGH); //pulse to start ranging
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
int distcm = pulseIn(PIN_ECHO, HIGH)/58; //pulse received as range
return distcm;
}
void setup() {
Serial.begin(115200);
//set the output and input pin of the sensor
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
//Start ranging and determine the statut of the parking slot
if(GetDist()>=50){
message = "FREE";
}
else{
message = "OCCUPIED";
}
//enable WIFI and setup esp nom communication
WiFi.mode(WIFI_STA);
esp_now_init();
//send callback
esp_now_register_send_cb(OnDataSent);
//receive callback is not used
//esp_now_register_recv_cb(OnDataRecv);
// Peer Registration
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer (Is possible to register multiple peers)
esp_now_add_peer(&peerInfo);
//send the statut of the parking slot and turn off the wifi
esp_now_send(broadcastAddress, (uint8_t*)message.c_str(), sizeof(message));
WiFi.mode(WIFI_OFF);
Serial.println("wifi off");
//set 6 second timer to deep sleep after having sent the data
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("ESP32 sleep every " + String(TIME_TO_SLEEP));
//enter deep sleep for 6 sec
esp_deep_sleep_start();
}
void loop() {//not used nothing to loop never reached
}