forked from abhash-rai/wild-watch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Esp32_server.c++
126 lines (106 loc) · 3.24 KB
/
Esp32_server.c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <WiFi.h>
#include <WebServer.h>
#include <Wire.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>
const char *ssid = "Sunway_304";
const char *password = "sunway@123";
WebServer server(80);
const int buzzerPin = 13;
const unsigned long buzzerDuration = 5000;
const unsigned long displayResetDuration = 7000;
unsigned long lastBuzzerTime = 0;
unsigned long lastDisplayUpdateTime = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void handleAnimalDetection()
{
if (server.method() == HTTP_POST)
{
String receivedData = server.arg("plain");
Serial.print("Received data: ");
Serial.println(receivedData);
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, receivedData);
if (error)
{
Serial.print("JSON parsing error: ");
Serial.println(error.c_str());
return;
}
JsonArray animalNames = doc["animal_names"];
if (animalNames.size() == 0)
{
digitalWrite(buzzerPin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No Animal Detected");
}
else if (animalNames.size() == 1)
{
digitalWrite(buzzerPin, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(animalNames[0].as<String>() + " Detected");
lastBuzzerTime = millis();
}
else
{
digitalWrite(buzzerPin, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Multiple Animals Detected");
lastBuzzerTime = millis();
}
lastDisplayUpdateTime = millis();
server.sendHeader("Access-Control-Allow-Origin", "*");
server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
server.sendHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
server.send(200, "text/plain", "Data received successfully");
}
else
{
server.send(405, "text/plain", "Method Not Allowed");
}
}
void handleOptions()
{
server.sendHeader("Access-Control-Allow-Origin", "*");
server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
server.sendHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
server.send(200);
}
void setup()
{
Serial.begin(115200);
pinMode(buzzerPin, OUTPUT);
lcd.init();
lcd.backlight();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
server.on("/api/animal-detected", HTTP_POST, handleAnimalDetection);
server.on("/api/animal-detected", HTTP_OPTIONS, handleOptions);
server.begin();
Serial.println("HTTP server started");
}
void loop()
{
server.handleClient();
if (millis() - lastBuzzerTime >= buzzerDuration)
{
digitalWrite(buzzerPin, LOW);
}
if (millis() - lastDisplayUpdateTime >= displayResetDuration)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Alert System");
lastDisplayUpdateTime = millis();
}
}