#include <WiFi.h>
#include <WiFiClientSecure.h> // ← ADD THIS for HTTPS
/* ================= ULTRASONIC SENSOR ================= */
#define TRIG_PIN 32
#define ECHO_PIN 33
long duration;
float distance;
/* ================= WIFI DETAILS ================= */
const char* ssid = "Wokwi-GUEST";
const char* password = "";
/* ================= EMAIL API DETAILS ================= */
const char* host = "www.circuitdigest.cloud";
const int port = 443; // ← CHANGED to HTTPS port
const char* apiKey = "cd_fir_020626_oPZv1N";
const char* toEmail = "[email protected]";//22200000033333
const int templateID = 1001;
/* ================= FLAGS ================= */
bool emailSent = false;
/* ================= READ DISTANCE ================= */
float readDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH, 30000);
distance = duration * 0.034 / 2;
return distance;
}
/* ================= SEND EMAIL ================= */
void sendEmail(float dist) {
WiFiClientSecure client; // ← CHANGED to secure client
client.setInsecure(); // ← ADD THIS (skip certificate validation)
if (!client.connect(host, port)) {
Serial.println("❌ Server connection failed");
return;
}
/* -------- COMPLETE TEMPLATE PAYLOAD -------- */
String payload = "{";
payload += "\"to_email\":\"" + String(toEmail) + "\",";
payload += "\"template_id\":1001,";
payload += "\"variables\":{";
payload += "\"subject\":\"Ultrasonic Sensor Alert\",";
payload += "\"title\":\"Distance Critical\",";
payload += "\"description\":\"Object detected below 30cm safety threshold.\",";
payload += "\"var1\":\"Sensor Reading\",";
payload += "\"var2\":\"" + String(dist, 2) + " cm\"";
payload += "}}";
Serial.println("Payload:");
Serial.println(payload);
/* -------- HTTP REQUEST -------- */
client.println("POST /api/v1/email/send HTTP/1.1");
client.println("Host: www.circuitdigest.cloud");
client.println("Authorization: " + String(apiKey));
client.println("Content-Type: application/json");
client.println("Content-Length: " + String(payload.length()));
client.println("Connection: close");
client.println();
client.println(payload);
/* -------- WAIT FOR RESPONSE -------- */
unsigned long timeout = millis();
while (!client.available()) {
if (millis() - timeout > 10000) {
Serial.println("❌ Server timeout");
client.stop();
return;
}
}
/* -------- STATUS LINE -------- */
String statusLine = client.readStringUntil('\n');
Serial.println("STATUS: " + statusLine);
/* -------- SKIP HEADERS -------- */
while (client.available()) {
String line = client.readStringUntil('\n');
if (line == "\r") break;
}
/* -------- RESPONSE BODY -------- */
String body = client.readString();
Serial.println("BODY:");
Serial.println(body);
client.stop();
}
/* ================= SETUP ================= */
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.println("\nConnecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\n✅ WiFi Connected");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
/* ================= LOOP ================= */
void loop() {
float dist = readDistance();
Serial.println(dist);
/* ---- TRIGGER EMAIL BELOW 30 cm ---- */
if (dist > 0 && dist < 30 && !emailSent) {
Serial.println("🚨 Distance below 30 cm — sending email");
sendEmail(dist);
emailSent = true;
}
/* ---- RESET WHEN OBJECT MOVES AWAY ---- */
if (dist >= 30) {
emailSent = false;
}
delay(1000);
}