From 8d845bc8c02a1275e19b4cc3d073736868f3ecb2 Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Fri, 5 May 2023 02:48:24 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=AA=92=20Cleans=20up=20main=20and=20assem?= =?UTF-8?q?bles=20all=20configurations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/config.h | 56 ++++++++- src/main.cpp | 292 ++++++++++++++++++----------------------------- 2 files changed, 168 insertions(+), 180 deletions(-) diff --git a/include/config.h b/include/config.h index 5b9c1c9..bd33c2b 100644 --- a/include/config.h +++ b/include/config.h @@ -1,3 +1,57 @@ +/* + * User settings + */ #define HOSTNAME "Leika" #define SSID "" -#define PASS "" \ No newline at end of file +#define PASS "" + + +/* + * Server settings + */ +#define HTTP_PORT 80 +#define WEBSOCKET_PATH "/" +#define EVENTSOURCE_PATH "/events" + + +/* + * Camera module + */ +#define CAMERA_MODEL_AI_THINKER +// #define CAMERA_MODEL_WROVER_KIT +// #define CAMERA_MODEL_ESP_EYE +// #define CAMERA_MODEL_M5STACK_PSRAM +// #define CAMERA_MODEL_M5STACK_V2_PSRAM +// #define CAMERA_MODEL_M5STACK_WIDE +// #define CAMERA_MODEL_M5STACK_ESP32CAM +// #define CAMERA_MODEL_TTGO_T_JOURNAL +// #define CAMERA_MODEL_ARDUCAM_ESP32S_UNO + + +/* + * OLED Settings + */ +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 64 + + +/* + * I2C software connection + */ +#define SDA 14 +#define SCL 15 + + +/* + * Serial settings + */ +#define BAUDRATE 115200 +#define SERIAL_DEBUG_OUTPUT true + + +/* + * Ultra sonic sensors + */ +#define USS_LEFT 12 +#define USS_RIGHT 13 +#define USS_MAX_DISTANCE 200 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 9a7dbbd..3e06c45 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,121 +1,87 @@ -#include -#include "OV2640.h" #include - -#include - -//#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 -#endif -#if FILESYSTEM == SPIFFS #include #include +#include +#include #include -#endif +#include -#ifdef USE_SONAR +#include #include -#endif -//#include -#include "Wire.h" -#ifdef USE_OLED -#include -#include -#endif -#ifdef USE_MPU #include -#endif -#ifdef USE_PWM #include -#endif +#include +#include #include #include #include -#define CAMERA_MODEL_AI_THINKER +#include #include -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 } -