🪒 Cleans up main and assembles all configurations
This commit is contained in:
+113
-179
@@ -1,121 +1,87 @@
|
||||
#include <Arduino.h>
|
||||
#include "OV2640.h"
|
||||
#include <WiFi.h>
|
||||
|
||||
#include <index_other.h>
|
||||
|
||||
//#define USE_SONAR
|
||||
//#define USE_MPU
|
||||
//#define USE_OLED
|
||||
//#define USE_PWM
|
||||
#define USE_WEBSOCKET
|
||||
//#define USE_BUTTON
|
||||
|
||||
#define FILESYSTEM SPIFFS
|
||||
#if FILESYSTEM == FFat
|
||||
#include <FFat.h>
|
||||
#endif
|
||||
#if FILESYSTEM == SPIFFS
|
||||
#include <AsyncTCP.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <DNSServer.h>
|
||||
#include <ESPmDNS.h>
|
||||
#include <SPIFFS.h>
|
||||
#endif
|
||||
#include <esp_camera.h>
|
||||
|
||||
#ifdef USE_SONAR
|
||||
#include <Wire.h>
|
||||
#include <NewPing.h>
|
||||
#endif
|
||||
//#include <SPI.h>
|
||||
#include "Wire.h"
|
||||
#ifdef USE_OLED
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#endif
|
||||
#ifdef USE_MPU
|
||||
#include <MPU6050_light.h>
|
||||
#endif
|
||||
#ifdef USE_PWM
|
||||
#include <Adafruit_PWMServoDriver.h>
|
||||
#endif
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
|
||||
#include <AsyncJpegStreamHandler.h>
|
||||
#include <CaptivePortalHandler.h>
|
||||
#include <WebsocketHandler.h>
|
||||
|
||||
#define CAMERA_MODEL_AI_THINKER
|
||||
#include <config.h>
|
||||
#include <camera_pins.h>
|
||||
|
||||
const char* ssid = "";
|
||||
const char* password = "";
|
||||
DNSServer dnsServer;
|
||||
AsyncWebSocket ws(WEBSOCKET_PATH);
|
||||
AsyncEventSource events(EVENTSOURCE_PATH);
|
||||
AsyncWebServer server(HTTP_PORT);
|
||||
|
||||
#ifdef USE_BUTTON
|
||||
int buttonLed = 2;
|
||||
int button = 16;
|
||||
bool servosEnabled = true;
|
||||
int currentButtonState = 0;
|
||||
#endif
|
||||
|
||||
#ifdef USE_SONAR
|
||||
NewPing sonar[2] = {
|
||||
NewPing(12, 12, 200),
|
||||
NewPing(13, 13, 200)
|
||||
NewPing(USS_LEFT, USS_LEFT, USS_MAX_DISTANCE),
|
||||
NewPing(USS_RIGHT, USS_RIGHT, USS_MAX_DISTANCE)
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef USE_MPU
|
||||
MPU6050 mpu(Wire);
|
||||
bool MPU_READY = false;
|
||||
#endif
|
||||
MPU6050 mpu(Wire);
|
||||
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40);
|
||||
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
|
||||
|
||||
#ifdef USE_PWM
|
||||
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40);
|
||||
bool PWM_READY = false;
|
||||
|
||||
#endif
|
||||
#ifdef USE_OLED
|
||||
#define SCREEN_WIDTH 128
|
||||
#define SCREEN_HEIGHT 64
|
||||
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
|
||||
bool OLED_READY = false;
|
||||
#endif
|
||||
|
||||
OV2640 cam;
|
||||
bool MPU_READY = false;
|
||||
bool PWM_READY = false;
|
||||
bool OLED_READY = false;
|
||||
|
||||
const bool USE_CAPTIVE_PORTAL = false;
|
||||
|
||||
long timer = 0;
|
||||
|
||||
bool setupCamera(){
|
||||
camera_config_t camera_config;
|
||||
camera_config.ledc_channel = LEDC_CHANNEL_0;
|
||||
camera_config.ledc_timer = LEDC_TIMER_0;
|
||||
camera_config.pin_d0 = Y2_GPIO_NUM;
|
||||
camera_config.pin_d1 = Y3_GPIO_NUM;
|
||||
camera_config.pin_d2 = Y4_GPIO_NUM;
|
||||
camera_config.pin_d3 = Y5_GPIO_NUM;
|
||||
camera_config.pin_d4 = Y6_GPIO_NUM;
|
||||
camera_config.pin_d5 = Y7_GPIO_NUM;
|
||||
camera_config.pin_d6 = Y8_GPIO_NUM;
|
||||
camera_config.pin_d7 = Y9_GPIO_NUM;
|
||||
camera_config.pin_xclk = XCLK_GPIO_NUM;
|
||||
camera_config.pin_pclk = PCLK_GPIO_NUM;
|
||||
camera_config.pin_vsync = VSYNC_GPIO_NUM;
|
||||
camera_config.pin_href = HREF_GPIO_NUM;
|
||||
camera_config.pin_sscb_sda = SIOD_GPIO_NUM;
|
||||
camera_config.pin_sscb_scl = SIOC_GPIO_NUM;
|
||||
camera_config.pin_pwdn = PWDN_GPIO_NUM;
|
||||
camera_config.pin_reset = RESET_GPIO_NUM;
|
||||
camera_config.xclk_freq_hz = 20000000;
|
||||
camera_config.pixel_format = PIXFORMAT_JPEG;
|
||||
|
||||
if(psramFound()){
|
||||
camera_config.frame_size = FRAMESIZE_VGA;
|
||||
camera_config.jpeg_quality = 10;
|
||||
camera_config.fb_count = 2;
|
||||
} else {
|
||||
camera_config.frame_size = FRAMESIZE_SVGA;
|
||||
camera_config.jpeg_quality = 12;
|
||||
camera_config.fb_count = 1;
|
||||
}
|
||||
esp_err_t err = esp_camera_init(&camera_config);
|
||||
if (err != ESP_OK) {
|
||||
printf("Camera probe failed with error 0x%x", err);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#ifdef USE_MPU
|
||||
bool setupMPU(){
|
||||
byte status = mpu.begin();
|
||||
Serial.print(F("MPU6050 status: "));
|
||||
Serial.println(status);
|
||||
if(status != 0){ return 0; }
|
||||
Serial.println(F("Calculating offsets, do not move MPU6050"));
|
||||
delay(1000);
|
||||
mpu.calcOffsets(true,true);
|
||||
Serial.println("Done!\n");
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_OLED
|
||||
|
||||
bool setupOLED(){
|
||||
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
|
||||
Serial.println(F("SSD1306 allocation failed"));
|
||||
@@ -127,114 +93,82 @@ bool setupOLED(){
|
||||
display.clearDisplay();
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_PWM
|
||||
|
||||
void setupWiFi(){
|
||||
if(USE_CAPTIVE_PORTAL){
|
||||
WiFi.softAP(HOSTNAME);
|
||||
dnsServer.start(53, "*", WiFi.softAPIP());
|
||||
} else {
|
||||
WiFi.begin(SSID, PASS);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.print("WiFi connected: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
if(OLED_READY){
|
||||
display.setTextSize(2);
|
||||
display.setCursor(0,0);
|
||||
display.println(WiFi.localIP());
|
||||
display.display();
|
||||
}
|
||||
}
|
||||
MDNS.addService("http", "tcp", HTTP_PORT);
|
||||
MDNS.begin(HOSTNAME);
|
||||
}
|
||||
|
||||
void setupServer(){
|
||||
ws.onEvent(onWsEvent);
|
||||
server.addHandler(&ws);
|
||||
server.on("/stream", HTTP_GET, streamJpg);
|
||||
server.addHandler(new CaptiveRequestHandler()).setFilter(ON_AP_FILTER);
|
||||
server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html");
|
||||
server.begin();
|
||||
}
|
||||
|
||||
bool setupMPU(){
|
||||
byte status = mpu.begin();
|
||||
Serial.print(F("MPU6050 status: "));
|
||||
Serial.println(status);
|
||||
if(status != 0){ return 0; }
|
||||
Serial.println(F("Calculating offsets, do not move MPU6050"));
|
||||
delay(1000);
|
||||
mpu.calcOffsets(true,true);
|
||||
Serial.println("Done!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool setupPWMController(){
|
||||
pwm.begin();
|
||||
pwm.setOscillatorFrequency(27000000);
|
||||
pwm.setPWMFreq(50);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
bool debounce() {
|
||||
static uint16_t state = 0;
|
||||
state = (state<<1) | digitalRead(button) | 0xfe00;
|
||||
return (state == 0xff00);
|
||||
}
|
||||
#endif
|
||||
void setup() {
|
||||
//WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
|
||||
|
||||
Serial.begin(115200);
|
||||
Serial.setDebugOutput(false);
|
||||
|
||||
Wire.begin(14, 15);
|
||||
void setup(){
|
||||
Serial.begin(BAUDRATE);
|
||||
Serial.setDebugOutput(SERIAL_DEBUG_OUTPUT);
|
||||
|
||||
Wire.begin(SDA, SCL);
|
||||
SPIFFS.begin();
|
||||
|
||||
#ifdef USE_BUTTON
|
||||
pinMode(buttonLed, OUTPUT);
|
||||
pinMode(button, INPUT);
|
||||
#endif
|
||||
|
||||
#ifdef USE_MPU
|
||||
MPU_READY = setupMPU();
|
||||
#endif
|
||||
#ifdef USE_PWM
|
||||
PWM_READY = setupPWMController();
|
||||
#endif
|
||||
#ifdef USE_OLED
|
||||
OLED_READY = setupOLED();
|
||||
#endif
|
||||
|
||||
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_sscb_sda = SIOD_GPIO_NUM;
|
||||
config.pin_sscb_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;
|
||||
|
||||
if(psramFound()){
|
||||
config.frame_size = FRAMESIZE_VGA;
|
||||
config.jpeg_quality = 10;
|
||||
config.fb_count = 2;
|
||||
} else {
|
||||
config.frame_size = FRAMESIZE_SVGA;
|
||||
config.jpeg_quality = 12;
|
||||
config.fb_count = 1;
|
||||
}
|
||||
|
||||
// Camera init
|
||||
cam.init(config);
|
||||
// Wi-Fi connection
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected");
|
||||
|
||||
Serial.print("Camera Stream Ready! Go to: http://");
|
||||
Serial.print(WiFi.localIP());
|
||||
#ifdef USE_OLED
|
||||
if(OLED_READY){
|
||||
display.setTextSize(2);
|
||||
display.setTextColor(WHITE);
|
||||
display.setCursor(0,0);
|
||||
display.println(WiFi.localIP());
|
||||
display.display();
|
||||
}
|
||||
#endif
|
||||
|
||||
MPU_READY = setupMPU();
|
||||
PWM_READY = setupPWMController();
|
||||
|
||||
setupCamera();
|
||||
setupWiFi();
|
||||
setupServer();
|
||||
}
|
||||
|
||||
|
||||
void loop(){
|
||||
if(USE_CAPTIVE_PORTAL) dnsServer.processNextRequest();
|
||||
ws.cleanupClients();
|
||||
|
||||
if(millis() - timer > 500) {
|
||||
String message = String(WiFi.RSSI());
|
||||
ws.textAll(message);
|
||||
timer = millis();
|
||||
}
|
||||
|
||||
#ifdef USE_BUTTON
|
||||
if (debounce()) {
|
||||
servosEnabled = !servosEnabled;
|
||||
digitalWrite(buttonLed, servosEnabled);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user