#include <WiFi.h>
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include <esp_now.h>
#include <Arduino.h>
#include <HCSR04.h>
#include <esp_sleep.h>

// Define your SSID and Password
#define WIFI_SSID "Wokwi-GUEST"
#define TIME_TO_SLEEP 3
#define uS_TO_S_FACTOR 1000000

//#define WIFI_PASS "opopopop"
// Define the MAC address of the ESP-NOW partner
uint8_t broadcastAddress[] = {0x8C, 0xAA, 0xB5, 0x84, 0xFB, 0x90}; // Replace with the actual MAC address

RTC_DATA_ATTR int bootCount = 0;

  // Register peer
  esp_now_peer_info_t peerInfo;

// Define the trigger and echo pins for the HC-SR04 sensor
const int TRIGPIN = 36; // Replace with the actual pin
const int ECHOPIN = 42;    // Replace with the actual pin

HCSR04 hc(TRIGPIN, ECHOPIN); //创建HCSR04对象(参数:trig引脚 , echo引脚)

// Define sleep duration in seconds
//const int sleepDuration = 5; // 1 minute sleep duration, adjust as needed

void setup() {
  Serial.begin(115200);
  delay(2000);
  initWiFi();
  initESPNow();
  WiFi.mode(WIFI_STA);
  delay(2000);
  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));
  esp_now_init();
  esp_now_register_send_cb(OnDataSent);
  esp_now_register_recv_cb(OnDataRecv);
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;
  peerInfo.encrypt = false;
  esp_now_add_peer(&peerInfo);
   while (!Serial.available()){};
    float distance = hc.dist();    //获取距离数据
    String message;
    if(distance<50){
      message = "OCCUPIED";
       esp_now_send(broadcastAddress, (uint8_t*)message.c_str(), message.length() + 1);
    }
    else{
      message = "FREE";
       esp_now_send(broadcastAddress, (uint8_t*)message.c_str(), message.length() + 1);
    }

  delay(5000);
  Serial.println("Entering deep sleep");
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP *1000000);
  Serial.flush();
  esp_deep_sleep_start();
}


void loop() {
  // Perform distance measurement using HC-SR04
 
  //esp_now_send(broadcastAddress, (uint8_t*)message.c_str(), message.length() + 1);
  
  
  //esp_sleep_enable_timer_wakeup(sleepDuration); // Convert seconds to microseconds
  //  Serial.println("Ending deep sleep");
   // Serial.println(esp_deep_sleep_get_wakeup_cause());

  //Serial.println(esp_sleep_get_wakeup_cause());
}

void initWiFi() {
  // Connect to Wi-Fi
  WiFi.begin(WIFI_SSID);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
}

void initESPNow() {
  // Initialize ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("ESP-NOW initialization failed");
    ESP.restart();
  }
}



  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");
  }

  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));

  }
 
  //peerInfo.channel = 0;
  //peerInfo.encrypt = false;

 // if (esp_now_add_peer(&peerInfo) != ESP_OK) {
 //   Serial.println("Failed to add peer");
 //   ESP.restart();
 // }

/*
float measureDistance() {
  // Measure distance using HC-SR04
  // Implement HC-SR04 distance measurement code here
}

void sendData(float distance) {
  // Send data using ESP-NOW
  // Implement ESP-NOW data sending code here
}
*/

/*
#define TRIGPIN 36      //TRIG
#define ECHOPIN 42     //ECHO
 
HCSR04 hc(TRIGPIN, ECHOPIN); //创建HCSR04对象(参数:trig引脚 , echo引脚)
 
void setup()
{
    Serial.begin(115200);
}
 
void loop()
{
    float distance = hc.dist();    //获取距离数据
    Serial.print("当前距离:");
    Serial.println(distance);        //输出距离数据
    Serial.println("CM");
    delay(200);
}
*/
esp:0
esp:1
esp:2
esp:3
esp:4
esp:5
esp:6
esp:7
esp:8
esp:9
esp:10
esp:11
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:20
esp:21
esp:26
esp:33
esp:34
esp:35
esp:36
esp:37
esp:38
esp:39
esp:40
esp:41
esp:42
esp:45
esp:46
esp:3V3
esp:5V
esp:GND.1
esp:TX
esp:RX
esp:RST
esp:GND.2
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND