-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesp_ml_connect.ino
More file actions
191 lines (161 loc) · 4.51 KB
/
Copy pathesp_ml_connect.ino
File metadata and controls
191 lines (161 loc) · 4.51 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "esp_camera.h"
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include "Base64.h"
#include <HTTPClient.h>
#include <ESP32Servo.h>
#include <ArduinoJson.h>
#include <Wire.h>
Servo myservo;
// Camera model
#define CAMERA_MODEL_AI_THINKER
#include "camera_pins.h"
// WiFi credentials
const char *ssid = "networkSSID";
const char *password = "password";
// Server settings
const char* server = "192.168.0.184";
const int serverPort = 8000;
// Servo & Ultrasonic
const int servoPin = 13;
#define TRIG_PIN 2 // ✅ SAFE GPIO
#define ECHO_PIN 14
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println();
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
myservo.attach(servoPin);
// Camera config
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sccb_sda = SIOD_GPIO_NUM;
config.pin_sccb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
config.frame_size = FRAMESIZE_QVGA; // ✅ Less memory
config.jpeg_quality = 12;
config.fb_count = psramFound() ? 2 : 1;
config.fb_location = psramFound() ? CAMERA_FB_IN_PSRAM : CAMERA_FB_IN_DRAM;
config.grab_mode = CAMERA_GRAB_LATEST;
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return;
}
sensor_t *s = esp_camera_sensor_get();
if (s->id.PID == OV3660_PID) {
s->set_vflip(s, 1);
s->set_brightness(s, 1);
s->set_saturation(s, -2);
}
WiFi.begin(ssid, password);
WiFi.setSleep(false);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
waitForServerReady();
}
void loop() {
float distance = measureDistance();
Serial.printf("Distance: %.2f cm\n", distance);
if (distance <= 20.0) {
camera_fb_t* fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
return;
}
String res = sendImage(fb->buf, fb->len);
esp_camera_fb_return(fb);
Serial.printf("Free heap: %d\n", ESP.getFreeHeap());
yield(); // feed watchdog
if (res == "1") {
Serial.println("Non biodegradable wasted");
//servo(); // Uncomment only if powered properly
} else {
Serial.println("biodegradable wasted");
}
delay(3000);
}
delay(1000);
yield(); // feed watchdog again
}
void waitForServerReady() {
int status = 0;
while (status != 200) {
status = checkHealth();
if (status == 200) {
Serial.println("Camera Ready!");
break;
} else {
Serial.println("Server not ready");
delay(3000);
}
}
}
int checkHealth() {
HTTPClient http;
String url = "http://" + String(server) + ":" + String(serverPort) + "/health";
http.begin(url);
int httpCode = http.GET();
http.end();
return httpCode;
}
String sendImage(uint8_t* image_data, size_t length) {
HTTPClient http;
WiFiClient client;
String url = "http://" + String(server) + ":" + String(serverPort) + "/predict";
http.begin(client, url);
http.setTimeout(4000); // Important
http.setReuse(false);
http.addHeader("Content-Type", "application/octet-stream");
int httpResponseCode = http.POST(image_data, length);
String response = "";
if (httpResponseCode > 0) {
Serial.printf("Image sent. Server response code: %d\n", httpResponseCode);
response = http.getString();
Serial.println(response);
} else {
Serial.printf("Failed to send image. Error: %s\n", http.errorToString(httpResponseCode).c_str());
}
http.end();
return response;
}
float measureDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30ms timeout
return duration * 0.034 / 2;
}
void servo() {
for (int pos = 0; pos <= 90; pos++) {
myservo.write(pos);
delay(50);
}
for (int pos = 90; pos >= 0; pos--) {
myservo.write(pos);
delay(50);
}
yield(); // feed watchdog
}