Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //OpenCV Lane Detection 04
- const char* ssid = "LAB_SIEMENS_6";
- const char* password = "123456789";
- //AP http://192.168.4.1
- const char* apssid = "esp32-cam";
- const char* appassword = "12345678";
- #include <WiFi.h>
- #include "soc/soc.h"
- #include "soc/rtc_cntl_reg.h"
- #include <esp32-hal-ledc.h>
- #include "esp_http_server.h"
- #include "esp_camera.h"
- #include "img_converters.h"
- String Feedback="";
- String Command="";
- String cmd="";
- String P1="";
- String P2="";
- String P3="";
- ////////////////////////////////////////////////
- String F = "0";
- String L = "1";
- String R = "-1";
- String U = "0";
- String R2 = "-3";
- String L2 = "3";
- int SpeedR=180,SpeedL=180,VALUE_R ,VALUE_L ;
- int Set_Point = 0;
- int Max_Set_Point = 8;
- bool Revert = 1 ;
- //////////////////////////////////////////////
- byte ReceiveState=0;
- byte cmdState=1;
- byte strState=1;
- byte questionstate=0;
- byte equalstate=0;
- byte semicolonstate=0;
- typedef struct {
- httpd_req_t *req;
- size_t len;
- } jpg_chunking_t;
- #define PART_BOUNDARY "123456789000000000000987654321"
- static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
- static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
- static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
- httpd_handle_t stream_httpd = NULL;
- httpd_handle_t camera_httpd = NULL;
- #define PWDN_GPIO_NUM 32
- #define RESET_GPIO_NUM -1
- #define XCLK_GPIO_NUM 0
- #define SIOD_GPIO_NUM 26
- #define SIOC_GPIO_NUM 27
- #define Y9_GPIO_NUM 35
- #define Y8_GPIO_NUM 34
- #define Y7_GPIO_NUM 39
- #define Y6_GPIO_NUM 36
- #define Y5_GPIO_NUM 21
- #define Y4_GPIO_NUM 19
- #define Y3_GPIO_NUM 18
- #define Y2_GPIO_NUM 5
- #define VSYNC_GPIO_NUM 25
- #define HREF_GPIO_NUM 23
- #define PCLK_GPIO_NUM 22
- // L298N pinout (ESP32)
- #define L_PWM 12
- #define ENA_L 13
- #define ENB_L 15
- #define ENA_R 14
- #define ENB_R 2
- #define R_PWM 16
- void setup() {
- WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
- WiFi.disconnect(true);
- WiFi.mode(WIFI_OFF);
- Serial.begin(115200);
- Serial.setDebugOutput(true);
- Serial.println();
- 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_UXGA;
- config.jpeg_quality = 10;
- config.fb_count = 2;
- } else {
- config.frame_size = FRAMESIZE_SVGA;
- config.jpeg_quality = 12;
- 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);
- ESP.restart();
- }
- sensor_t * s = esp_camera_sensor_get();
- // initial sensors are flipped vertically and colors are a bit saturated
- if (s->id.PID == OV3660_PID) {
- s->set_vflip(s, 1); // flip it back
- s->set_brightness(s, 1); // up the brightness just a bit
- s->set_saturation(s, -2); // lower the saturation
- }
- s->set_framesize(s, FRAMESIZE_QVGA); //程式內定使用QVGA(320x240),不可改此設定
- ledcAttachPin(4, 4);
- ledcSetup(4, 5000, 8);
- // Setup Left and right motor PWM
- ledcAttachPin(L_PWM, 5);
- ledcSetup(5, 15000, 8);
- ledcAttachPin(R_PWM, 6);
- ledcSetup(6, 15000, 8);
- // Setup Left and right motor control
- pinMode(ENA_L, OUTPUT);
- pinMode(ENB_L, OUTPUT);
- pinMode(ENA_R, OUTPUT);
- pinMode(ENB_R, OUTPUT);
- //WiFi.mode(WIFI_AP_STA); //其他模式 WiFi.mode(WIFI_AP); WiFi.mode(WIFI_STA);
- WiFi.mode(WIFI_STA); //其他模式 WiFi.mode(WIFI_AP); WiFi.mode(WIFI_STA);
- for (int i=0;i<2;i++) {
- WiFi.begin(ssid, password); //執行網路連線
- delay(1000);
- Serial.println("");
- Serial.print("Connecting to ");
- Serial.println(ssid);
- long int StartTime=millis();
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- if ((StartTime+5000) < millis()) break; //等待10秒連線
- }
- if (WiFi.status() == WL_CONNECTED) { //若連線成功
- WiFi.softAP((WiFi.localIP().toString()+"_"+(String)apssid).c_str(), appassword); //設定SSID顯示客戶端IP
- Serial.println("");
- Serial.println("STAIP address: ");
- Serial.println(WiFi.localIP());
- Serial.println("");
- for (int i=0;i<3;i++) { //若連上WIFI設定閃光燈快速閃爍
- ledcWrite(4,10);
- delay(200);
- ledcWrite(4,0);
- delay(200);
- }
- break;
- }
- }
- startCameraServer();
- pinMode(4, OUTPUT);
- digitalWrite(4, LOW);
- }
- unsigned long period = 100; //ระยะเวลาที่ต้องการรอ
- unsigned long last_time = 0; //ประกาศตัวแปรเป็น global เพื่อเก็บค่าไว้ไม่ให้ reset จากการวนloop
- unsigned int Count_Stop = 0 ;
- unsigned int Count_R2 = 0 ;
- bool Python_Offline = 0;
- void loop() {
- if( millis() - last_time > period) {
- last_time = millis();
- if ( P1 != "" ){
- int Buf = P1.toInt();
- P1 = "" ;
- Set_Point = Buf ;
- if ( Set_Point == 0 ) {
- VALUE_R = SpeedR ;
- VALUE_L = SpeedL ;
- }
- else if ( Set_Point < 0 ) {
- VALUE_R = SpeedR + ( Set_Point );
- VALUE_L = SpeedL ;
- }
- else{
- VALUE_R = SpeedR ;
- VALUE_L = SpeedL - ( Set_Point );
- }
- Serial.print(P1);Serial.print(" ");
- Serial.print(Set_Point);Serial.print(" ");
- Serial.print(VALUE_R);Serial.print(" ");
- Serial.println(VALUE_L);
- if ( Revert == 0 ){
- ledcWrite(5, VALUE_L);// Left motor PWM
- ledcWrite(6, VALUE_R);// Right motor PWM
- digitalWrite(ENA_L, 1);
- digitalWrite(ENB_L, 0);
- digitalWrite(ENA_R, 1);
- digitalWrite(ENB_R, 0);
- }
- if ( Revert == 1 ){
- ledcWrite(5, VALUE_L);// Left motor PWM
- ledcWrite(6, VALUE_R);// Right motor PWM
- digitalWrite(ENA_L, 0);
- digitalWrite(ENB_L, 1);
- digitalWrite(ENA_R, 0);
- digitalWrite(ENB_R, 1);
- }
- Count_Stop = 0;Python_Offline = 0;
- }
- if ( Count_Stop != 10 ) { Count_Stop++; }
- else {
- // Do soft break
- ledcWrite(5, 0);// Left motor PWM
- ledcWrite(6, 0);// Right motor PWM
- digitalWrite(ENA_L, 0);
- digitalWrite(ENB_L, 0);
- digitalWrite(ENA_R, 0);
- digitalWrite(ENB_R, 0);
- if ( Python_Offline == 0){
- Python_Offline =1;
- Serial.println("Python Offline");
- }
- }
- }
- }
- static size_t jpg_encode_stream(void * arg, size_t index, const void* data, size_t len){
- jpg_chunking_t *j = (jpg_chunking_t *)arg;
- if(!index){
- j->len = 0;
- }
- if(httpd_resp_send_chunk(j->req, (const char *)data, len) != ESP_OK){
- return 0;
- }
- j->len += len;
- return len;
- }
- static esp_err_t capture_handler(httpd_req_t *req){
- camera_fb_t * fb = NULL;
- esp_err_t res = ESP_OK;
- fb = esp_camera_fb_get();
- if (!fb) {
- Serial.println("Camera capture failed");
- httpd_resp_send_500(req);
- return ESP_FAIL;
- }
- httpd_resp_set_type(req, "image/jpeg");
- httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=capture.jpg");
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- size_t fb_len = 0;
- if(fb->format == PIXFORMAT_JPEG){
- fb_len = fb->len;
- res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
- } else {
- jpg_chunking_t jchunk = {req, 0};
- res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk)?ESP_OK:ESP_FAIL;
- httpd_resp_send_chunk(req, NULL, 0);
- fb_len = jchunk.len;
- }
- esp_camera_fb_return(fb);
- return res;
- }
- static esp_err_t cmd_handler(httpd_req_t *req){
- char* buf;
- size_t buf_len;
- char variable[128] = {0,};
- char value[128] = {0,};
- String myCmd = "";
- buf_len = httpd_req_get_url_query_len(req) + 1;
- if (buf_len > 1) {
- buf = (char*)malloc(buf_len);
- if(!buf){
- httpd_resp_send_500(req);
- return ESP_FAIL;
- }
- if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) {
- if (httpd_query_key_value(buf, "var", variable, sizeof(variable)) == ESP_OK &&
- httpd_query_key_value(buf, "val", value, sizeof(value)) == ESP_OK) {
- }
- else {
- myCmd = String(buf);
- }
- }
- } else {
- httpd_resp_send_404(req);
- return ESP_FAIL;
- }
- //Serial.println(myCmd);//Serial.print(" ");
- if (myCmd=="Revert") { Revert != Revert ; P1 = "";myCmd = "" ; }
- P1 = myCmd;
- // if (myCmd=="Front") { P1 = F; }
- // else if (myCmd=="Left") { P1 = L; }
- // else if (myCmd=="Right") { P1 = R; }
- // else if (myCmd=="U_turn") { P1 = U; }
- // else if (myCmd=="R2") { P1 = R2; }
- // else if (myCmd=="L2") { P1 = L2; }
- const char *resp = Feedback.c_str();
- httpd_resp_set_type(req, "text/html"); //設定回傳資料格式
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); //允許跨網域讀取
- return httpd_resp_send(req, resp, strlen(resp));
- }
- static esp_err_t stream_handler(httpd_req_t *req){
- camera_fb_t * fb = NULL;
- esp_err_t res = ESP_OK;
- size_t _jpg_buf_len = 0;
- uint8_t * _jpg_buf = NULL;
- char * part_buf[64];
- res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
- if(res != ESP_OK){
- return res;
- }
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- while(true){
- fb = esp_camera_fb_get();
- if (!fb) {
- Serial.println("Camera capture failed");
- res = ESP_FAIL;
- } else {
- if(fb->format != PIXFORMAT_JPEG){
- bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
- esp_camera_fb_return(fb);
- fb = NULL;
- if(!jpeg_converted){
- Serial.println("JPEG compression failed");
- res = ESP_FAIL;
- }
- } else {
- _jpg_buf_len = fb->len;
- _jpg_buf = fb->buf;
- }
- }
- if(res == ESP_OK){
- res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
- }
- if(res == ESP_OK){
- res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
- }
- if(res == ESP_OK){
- size_t hlen = snprintf((char *)part_buf, 64, _STREAM_PART, _jpg_buf_len);
- res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
- }
- if(fb){
- esp_camera_fb_return(fb);
- fb = NULL;
- _jpg_buf = NULL;
- } else if(_jpg_buf){
- free(_jpg_buf);
- _jpg_buf = NULL;
- }
- if(res != ESP_OK){
- break;
- }
- }
- return res;
- }
- void startCameraServer(){
- httpd_config_t config = HTTPD_DEFAULT_CONFIG(); //可在HTTPD_DEFAULT_CONFIG()中設定Server Port
- //http://192.168.xxx.xxx/control
- httpd_uri_t cmd_uri = {
- .uri = "/control",
- .method = HTTP_GET,
- .handler = cmd_handler,
- .user_ctx = NULL
- };
- //http://192.168.xxx.xxx/capture
- httpd_uri_t capture_uri = {
- .uri = "/capture",
- .method = HTTP_GET,
- .handler = capture_handler,
- .user_ctx = NULL
- };
- //http://192.168.xxx.xxx:81/stream
- httpd_uri_t stream_uri = {
- .uri = "/stream",
- .method = HTTP_GET,
- .handler = stream_handler,
- .user_ctx = NULL
- };
- Serial.printf("Starting web server on port: '%d'\n", config.server_port); //Server Port
- if (httpd_start(&camera_httpd, &config) == ESP_OK) {
- httpd_register_uri_handler(camera_httpd, &cmd_uri);
- httpd_register_uri_handler(camera_httpd, &capture_uri);
- }
- config.server_port += 1; //Stream Port
- config.ctrl_port += 1; //UDP Port
- Serial.printf("Starting stream server on port: '%d'\n", config.server_port);
- if (httpd_start(&stream_httpd, &config) == ESP_OK) {
- httpd_register_uri_handler(stream_httpd, &stream_uri);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment