Commit 28d12dfe authored by Ishankha K.C's avatar Ishankha K.C

Add arduino files

parent b117ac95
This diff is collapsed.
#include <WebServer.h>
#include <WiFi.h>
#include <esp32cam.h>
#include <time.h>
#include <ArduinoJson.h>
const char* WIFI_SSID = "SLT-Fiber-Home";
const char* WIFI_PASS = "Home@1122$1";
const char* DEVICE_ID = "8563efa6";
// Flashlight pin
const int FLASHLIGHT_PIN = 4;
// Time interval to consider as daytime or nighttime
const int DAY_START_HOUR = 6; // 6 AM
const int NIGHT_START_HOUR = 18; // 6 PM
// Time zone offset (5 hours 30 minutes) in seconds
const int TIME_ZONE_OFFSET = 5 * 3600 + 30 * 60;
WebServer server(80);
static auto loRes = esp32cam::Resolution::find(320, 240);
static auto midRes = esp32cam::Resolution::find(350, 530);
static auto hiRes = esp32cam::Resolution::find(800, 600);
void serveJpg()
{
auto frame = esp32cam::capture();
if (frame == nullptr) {
Serial.println("CAPTURE FAIL");
server.send(503, "", "");
return;
}
Serial.printf("CAPTURE OK %dx%d %db\n", frame->getWidth(), frame->getHeight(),
static_cast<int>(frame->size()));
server.setContentLength(frame->size());
server.send(200, "image/jpeg");
WiFiClient client = server.client();
frame->writeTo(client);
}
void handleJpgLo()
{
if (!esp32cam::Camera.changeResolution(loRes)) {
Serial.println("SET-LO-RES FAIL");
}
serveJpg();
}
void handleJpgHi()
{
if (!esp32cam::Camera.changeResolution(hiRes)) {
Serial.println("SET-HI-RES FAIL");
}
serveJpg();
}
void handleJpgMid()
{
if (!esp32cam::Camera.changeResolution(midRes)) {
Serial.println("SET-MID-RES FAIL");
}
serveJpg();
}
// API handler to turn the flashlight on
void handleFlashlightOn()
{
digitalWrite(FLASHLIGHT_PIN, HIGH);
// Create a JSON document
StaticJsonDocument<200> jsonDoc;
jsonDoc["status"] = "ON";
jsonDoc["message"] = "Flashlight turned ON";
// Serialize JSON document to a string
String response;
serializeJson(jsonDoc, response);
// Send the JSON response
server.send(200, "application/json", response);
}
// API handler to turn the flashlight off
void handleFlashlightOff()
{
digitalWrite(FLASHLIGHT_PIN, LOW);
// Create a JSON document
StaticJsonDocument<200> jsonDoc;
jsonDoc["status"] = "OFF";
jsonDoc["message"] = "Flashlight turned OFF";
// Serialize JSON document to a string
String response;
serializeJson(jsonDoc, response);
// Send the JSON response
server.send(200, "application/json", response);
}
void setup(){
Serial.begin(115200);
Serial.println();
pinMode(FLASHLIGHT_PIN, OUTPUT);
digitalWrite(FLASHLIGHT_PIN, LOW); // Ensure flashlight is off initially
{
using namespace esp32cam;
Config cfg;
cfg.setPins(pins::AiThinker);
cfg.setResolution(hiRes);
cfg.setBufferCount(2);
cfg.setJpeg(80);
bool ok = Camera.begin(cfg);
Serial.println(DEVICE_ID);
Serial.println(ok ? "CAMERA OK" : "CAMERA FAIL");
}
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.print("");
Serial.print("Connecting to WiFi.");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.print("/" + String(DEVICE_ID));
Serial.println("");
Serial.println(" /cam-lo.jpg");
Serial.println(" /cam-hi.jpg");
Serial.println(" /cam-mid.jpg");
Serial.println(" /flashlight/on");
Serial.println(" /flashlight/off");
server.on("/"+String(DEVICE_ID)+"/cam-lo.jpg", handleJpgLo);
server.on("/"+String(DEVICE_ID)+"/cam-hi.jpg", handleJpgHi);
server.on("/"+String(DEVICE_ID)+"/cam-mid.jpg", handleJpgMid);
server.on("/"+String(DEVICE_ID)+"/flashlight/on", handleFlashlightOn);
server.on("/"+String(DEVICE_ID)+"/flashlight/off", handleFlashlightOff);
server.begin();
// Initialize time
configTime(0, 0, "pool.ntp.org");
}
unsigned long lastPrintTime = 0; // Last time the time was printed
const unsigned long printInterval = 10000; // Interval to print time (10 seconds)
void loop()
{
server.handleClient();
unsigned long currentMillis = millis();
if (currentMillis - lastPrintTime >= printInterval) {
// It's been 10 seconds, so print the time
struct tm timeinfo;
if (getLocalTime(&timeinfo)) {
// Adjust time for GMT+5:30
timeinfo.tm_hour += 5;
timeinfo.tm_min += 30;
if (timeinfo.tm_min >= 60) {
timeinfo.tm_min -= 60;
timeinfo.tm_hour++;
}
if (timeinfo.tm_hour >= 24) {
timeinfo.tm_hour -= 24;
}
int hour = timeinfo.tm_hour;
int minute = timeinfo.tm_min;
int second = timeinfo.tm_sec;
// Print the adjusted time
Serial.printf("Time: %02d:%02d:%02d\n", hour, minute, second);
} else {
Serial.println("Failed to obtain time");
}
// Update the last print time
lastPrintTime = currentMillis;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment