/*ESP32: The main microcontroller.
3 IR Sensors: For detecting whether parking spots are occupied.
Thingzmate: Cloud platform for data visualization and control.
Libraries Required:
WiFi.h for Wi-Fi connectivity.
HTTPClient.h for sending data to Thingzmate.
Wiring:
IR Sensors: Connect each IR sensor’s output to a different GPIO pin on the ESP32.
IR Sensor 1: GPIO 13
IR Sensor 2: GPIO 12
IR Sensor 3: GPIO 14*/
#include <WiFi.h>
#include <HTTPClient.h>
#define WIFI_SSID "Sridhar"
#define WIFI_PASSWORD "sridhar2005"
const char *serverUrl = "https://console.thingzmate.com/api/v1/device-types/23304479/devices/23304479/uplink"; // Replace with your server endpoint
String AuthorizationToken = "Bearer e96180a7dd3ed8dfe8692b6cfe5a2aa5";
// Define IR sensor pins and LED pins
#define IR1_PIN 34
#define IR2_PIN 35
#define IR3_PIN 32
#define LED1_PIN 16
#define LED2_PIN 17
#define LED3_PIN 18
void setup() {
Serial.begin(115200);
// Set IR sensor pins as inputs
pinMode(IR1_PIN, INPUT);
pinMode(IR2_PIN, INPUT);
pinMode(IR3_PIN, INPUT);
// Set LED pins as outputs
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(LED3_PIN, OUTPUT);
delay(4000); // Delay to let serial settle
// Connect to WiFi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to WiFi");
}
void loop() {
// Read IR sensor values
int ir1Value = digitalRead(IR1_PIN);
int ir2Value = digitalRead(IR2_PIN);
int ir3Value = digitalRead(IR3_PIN);
// Control LEDs based on IR sensor values
digitalWrite(LED1_PIN, ir1Value ? LOW : HIGH); // LED ON if no vehicle (LOW means unoccupied)
digitalWrite(LED2_PIN, ir2Value ? LOW : HIGH); // LED ON if no vehicle (LOW means unoccupied)
digitalWrite(LED3_PIN, ir3Value ? LOW : HIGH); // LED ON if no vehicle (LOW means unoccupied)
// Prepare HTTP request
HTTPClient http;
http.begin(serverUrl);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", AuthorizationToken);
// Create JSON payload
String payload = "{\"ir1_value\":" + String(ir1Value) +
",\"ir2_value\":" + String(ir2Value) +
",\"ir3_value\":" + String(ir3Value) + "}";
// Send POST request
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("HTTP Response code: " + String(httpResponseCode));
Serial.println(response);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end(); // Free resources
delay(10000); // Wait for 10 seconds before sending the next request
}