/********************
* Pablo Martellucci *
* Maryam Nazarian *
********************/
/* Leader personcode 10668552
X = 52 % 50 + 5 this is the deep sleep time [s]
Y = 8552 + 5
Energy = Y this is the battery energy [J]
*/
#include <WiFi.h>
#include <esp_now.h>
#define ECHO 32
#define TRIGGER 33
#define uS_TO_S_FACTOR 1000000
#define TIME_TO_SLEEP 7 // 52 % 50 + 5
#define ENERGY 8557 // 8552 + 5
uint8_t broadcastAddress[] = {0x8C, 0xAA, 0xB5, 0x84, 0xFB, 0x90}; // MAC receiver
RTC_DATA_ATTR int bootCount = 0; // the number of boots is saved in the RTC memory
esp_now_peer_info_t peerInfo;
// Prints the wake-up reason
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason){
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}
// Sending callback
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");
}
//Receiving Callback
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));
}
/* To change the distance while the simulation is running, click on
the HC-SR04 sensor and use the slider to set the distance value.
You can choose any value between 2cm and 400cm
*/
float get_distance() {
// Start a new measurement:
digitalWrite(TRIGGER, LOW); // for safety
delayMicroseconds(2);
digitalWrite(TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER, LOW);
// Read the result:
int duration = pulseIn(ECHO, HIGH);
// Calculates the distance
int distance = duration / 58;
return distance; // [cm]
}
void setup() {
Serial.begin(115200);
pinMode(ECHO, INPUT);
pinMode(TRIGGER, OUTPUT);
float distance;
String message;
Serial.println("++++++++++++++++++++++++++"); // start idle/boot time
Serial.println(millis()); // timestamp
delay(500);
++bootCount; // Increment boot number and print it every reboot
Serial.println("Boot number: " + String(bootCount));
Serial.println("--------------------------"); // end indle time start WiFi On time
Serial.println(millis());
WiFi.mode(WIFI_STA); // Turn on WiFi
Serial.println("WiFi is ON and transmission power is set to 2dBm");
delay(500);
WiFi.setTxPower(WIFI_POWER_2dBm);
// Print the wakeup reason
print_wakeup_reason();
// Initialize ESP-NOW
esp_now_init();
Serial.println("ESP-NOW is ON");
//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;
// Add peer (Is possible to register multiple peers)
esp_now_add_peer(&peerInfo);
Serial.println("--------------------------"); // start measuring time
Serial.println(millis()); // timestamp
distance = get_distance();
Serial.println("Measured distance: " + String(get_distance()) + "cm");
if( distance <= 50 ){ // to estimate the presence of a car: distance < 50cm
message = "OCCUPIED";
}
else{
message = "FREE";
}
Serial.println(millis()); // timestamp end measuring time
Serial.println("--------------------------"); // start transmission time
Serial.println(micros()); // timestamp
esp_now_send(broadcastAddress, (uint8_t*)message.c_str(), message.length() + 1);
Serial.println(micros()); // timestamp
Serial.println("--------------------------"); // end transmission time
WiFi.mode(WIFI_OFF);
Serial.println(millis()); // timestamp end WiFi ON time, start WiFi OFF time
Serial.println("WiFi is OFF");
delay(500);
Serial.println("--------------------------");
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " seconds");
Serial.println("Going to sleep now");
Serial.flush();
Serial.println(millis()); // timestamp end WiFi OFF time
Serial.println("--------------------------");
esp_deep_sleep_start();
Serial.println("This will never be printed"); // deep sleep check
}
void loop() {
// This is not going to be called
}