/*
Using ESP32-C3 for similation and ESP32-ESP32S2-AnalogWrite library
https://github.com/Dlloydev/ESP32-ESP32S2-AnalogWrite
*/
#include <WebServer.h>
#include <WiFi.h>
#include <pwmWrite.h>
#include "esp_camera.h"
const char* ssid = "Redmi";
const char* password = "11111111";
const int sX = 6;
const int sY = 7;
WebServer server(80);
Pwm pwm = Pwm();
const char index_html[] PROGMEM = R"rawliteral(
<body>
<img id="img">
<br>
<button onclick="camUp()">capture</button>
<br>
<label id="lv">90</label>
<br>
<input type="range" onchange="servo()" id="v" min="0" max="180" step="1" value="90">
<br>
<label id="lh">90</label>
<br>
<input type="range" onchange="servo()" id="h" min="0" max="180" step="1" value="90">
<script>
function camUp() {
img = document.getElementById("img")
img.src = "/capture?" + new Date().getTime()
}
function servo() {
let xhr = new XMLHttpRequest()
xhr.open("GET", "/servo?v="+v.value+"&h="+h.value, false)
xhr.send()
}
window.onload = () => {
camUp()
h = document.getElementById("h")
v = document.getElementById("v")
lv = document.getElementById("lv")
lh = document.getElementById("lh")
v.addEventListener("input", (event) => {
lv.innerHTML = event.target.value
})
h.addEventListener("input", (event) => {
lh.innerHTML = event.target.value
})
}
</script>
</body>
)rawliteral";
void setup() {
Serial.begin(115200);
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = 5;
config.pin_d1 = 18;
config.pin_d2 = 19;
config.pin_d3 = 21;
config.pin_d4 = 36;
config.pin_d5 = 39;
config.pin_d6 = 34;
config.pin_d7 = 35;
config.pin_xclk = 0;
config.pin_pclk = 22;
config.pin_vsync = 25;
config.pin_href = 23;
config.pin_sscb_sda = 26;
config.pin_sscb_scl = 27;
config.pin_pwdn = 32;
config.pin_reset = -1;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
config.frame_size = FRAMESIZE_QVGA;
config.jpeg_quality = 10;
config.fb_count = 1;
/*
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return;
}
*/
//sX.attach(14);
//sY.attach(15);
/*
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println(WiFi.localIP());
server.on("/", root);
server.on("/capture", capture);
server.on("/servo", servo);
server.begin();
Serial.println("Server started");
*/
}
void loop() {
server.handleClient();
for (int pos = 0; pos <= 180; pos += 1) {
pwm.writeServo(sX, pos);
pwm.writeServo(sY, 180 - pos);
delay(10);
}
for (int pos = 180; pos >= 0; pos -= 1) {
pwm.writeServo(sX, pos);
pwm.writeServo(sY, 180 - pos);
delay(10);
}
}
void root() {
Serial.println("Root");
server.send(200, "text/html", index_html);
}
void capture() {
/*
Serial.println("Capture");
camera_fb_t * fb = esp_camera_fb_get();
esp_camera_fb_return(fb);
fb = NULL;
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
return;
}
server.sendHeader("Content-Type", "image/jpeg");
server.sendContent((const char *)fb->buf, fb->len);
esp_camera_fb_return(fb);
*/
}
void servo() {
/*
if (server.args()>0){
delay(500);
sX.write(server.arg("v").toInt());
sY.write(server.arg("h").toInt());
}
delay(500);
*/
}
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1