Notifications
Clear all

Joining 2 separate examples into 1 program

37 Posts
5 Users
5 Likes
4,207 Views
(@drjoelleon)
Member
Joined: 3 years ago
Posts: 41
Topic starter  

I have downloaded the esp32-cam example and the Soft Access Point example separately and successfully was able to run them. What I’m trying to do is to place the camera in a remote place ad access the streaming from the helm of a boat. I tried to just append one after the other but got error message when compiling. I would appreciate any guidance.

Thanks

Joel in sunny Florida, USA

 

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include "camera_pins.h"
#define LED_BUILTIN 4
#include "esp_timer.h"
#include "img_converters.h"
#include "Arduino.h"
#include "fb_gfx.h"
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include "esp_http_server.h"

//
// WARNING!!! PSRAM IC required for UXGA resolution and high JPEG quality
// Ensure ESP32 Wrover Module or other board with PSRAM is selected
// Partial images will be transmitted if image exceeds buffer size
//

// Select camera model
//#define CAMERA_MODEL_WROVER_KIT // Has PSRAM
//#define CAMERA_MODEL_ESP_EYE // Has PSRAM
//#define CAMERA_MODEL_M5STACK_PSRAM // Has PSRAM
//#define CAMERA_MODEL_M5STACK_V2_PSRAM // M5Camera version B Has PSRAM
//#define CAMERA_MODEL_M5STACK_WIDE // Has PSRAM
//#define CAMERA_MODEL_M5STACK_ESP32CAM // No PSRAM
#define CAMERA_MODEL_AI_THINKER // Has PSRAM
//#define CAMERA_MODEL_TTGO_T_JOURNAL // No PSRAM

/*const char* ssid = "eero";
const char* password = "xxxxxx44";*/

void startCameraServer();

void setup() {
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 PSRAM IC present, init with UXGA resolution and higher JPEG quality
// for larger pre-allocated frame buffer.
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;
}

#if defined(CAMERA_MODEL_ESP_EYE)
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
#endif

// camera init
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();
// 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
}
// drop down frame size for higher initial frame rate
s->set_framesize(s, FRAMESIZE_QVGA);

#if defined(CAMERA_MODEL_M5STACK_WIDE) || defined(CAMERA_MODEL_M5STACK_ESP32CAM)
s->set_vflip(s, 1);
s->set_hmirror(s, 1);
#endif;

/* AP WiFi Starts Here %%%%%%%%%%%%%%%%%%%%%%.

WiFiAccessPoint.ino creates a WiFi access point and provides a web server on it.

Steps:
1. Connect to the access point "yourAp"
2. Point your web browser to http://192.168.4.1/H to turn the LED on or http://192.168.4.1/L to turn it off
OR
Run raw TCP "GET /H" and "GET /L" on PuTTY terminal with 192.168.4.1 as IP address and 80 as port

Created for arduino-esp32 on 04 July, 2018
by Elochukwu Ifediora (fedy0)
*/

/*
WiFiAccessPoint.ino creates a WiFi access point and provides a web server on it.

Steps:
1. Connect to the access point "yourAp"
2. Point your web browser to http://192.168.4.1/H to turn the LED on or http://192.168.4.1/L to turn it off
OR
Run raw TCP "GET /H" and "GET /L" on PuTTY terminal with 192.168.4.1 as IP address and 80 as port

Created for arduino-esp32 on 04 July, 2018
by Elochukwu Ifediora (fedy0)
*/

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>

#define LED_BUILTIN 4 // Set the GPIO pin where you connected your test LED or comment this line out if your dev board has a built-in LED

// Set these to your desired credentials.
const char *ssid = "JML's Soft Access Point";
const char *password = "";

WiFiServer server(80);

void setup() {
pinMode(LED_BUILTIN, OUTPUT);

Serial.begin(115200);
Serial.println();
Serial.println("Configuring access point...");

// You can remove the password parameter if you want the AP to be open.
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();

Serial.println("Server started for JML's Soft Access Point!!!!!!!!!");
}
/*THIS IS WHERE THE ERROR OCCURS "a function definition is not allowed here before "{" token"*/

/Users/joelleon/Documents/Arduino/Camera_Wifi_Soft_Access_COMBINED/Camera_Wifi_Soft_Access_COMBINED.ino: In function 'void setup()':
Camera_Wifi_Soft_Access_COMBINED:156:14: error: a function-definition is not allowed here before '{' token
void setup() {
^
Camera_Wifi_Soft_Access_COMBINED:173:13: error: a function-definition is not allowed here before '{' token
void loop() {
^
Camera_Wifi_Soft_Access_COMBINED:222:1: error: expected '}' at end of input
}
^
Multiple libraries were found for "WiFi.h"
Used: /Users/joelleon/Library/Arduino15/packages/esp32/hardware/esp32/1.0.6/libraries/WiFi
Not used: /Users/joelleon/Desktop/Arduino.app/Contents/Java/libraries/WiFi
exit status 1
a function-definition is not allowed here before '{' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

void loop() {
WiFiClient client = server.available(); // listen for incoming clients

if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character

// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();

// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> to turn ON the LED.<br>");
client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>");

// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}

// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
}

This topic was modified 3 years ago by drjoelleon

   
Quote
MadMisha
(@madmisha)
Member
Joined: 4 years ago
Posts: 340
 

@drjoelleon

Here is a link to someone who had a problem in the same area

"You should run the serial output of the crash through the ESP32 Exception Decoder."

 

But you might be having issues with telling the ESP32 to run in 2 different modes at the same time. I am not 100% on this but I believe you can only have it in one mode at the same time and they might be conflicting. That's really just a shot in the dark though.

 

What exactly are you trying to do? I really hope that wasn't the exact code you uploaded. You would need to combine each section with only one setup and one loop. If what you are attempting is use the camera with the ESP as your access point, that will be easy. If that is the case then take your camera sketch and include the following in the appropriate sections:

 

//At the top include
const char* ssid = "yournametoshowhere";
contt char* password = "yourpasswordhere";

//In your setup section
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP: ");
Serial.println(IP);
startCameraServer();

//Then remove all existing code that pertains to wifi/AP

   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

   
ReplyQuote
(@drjoelleon)
Member
Joined: 3 years ago
Posts: 41
Topic starter  

@madmisha

Thanks ,

I’ll try this tomorrow and update you

joel


   
ReplyQuote
(@drjoelleon)
Member
Joined: 3 years ago
Posts: 41
Topic starter  

Thanks robotbuilder

i’m sorry but I am a real neophyte and do not understand your suggestion

Joel


   
ReplyQuote
(@drjoelleon)
Member
Joined: 3 years ago
Posts: 41
Topic starter  

@madmisha

How do I locate  "You should run the serial output of the crash through the ESP32 Exception Decoder."

I think I did what you recommended but I got a new error about the "LED_BUTTON was not declared".

Again thanks for your help!

This is the modified code:

 

//
// WARNING!!! PSRAM IC required for UXGA resolution and high JPEG quality
// Ensure ESP32 Wrover Module or other board with PSRAM is selected
// Partial images will be transmitted if image exceeds buffer size
//

// Select camera model
//#define CAMERA_MODEL_WROVER_KIT // Has PSRAM
//#define CAMERA_MODEL_ESP_EYE // Has PSRAM
//#define CAMERA_MODEL_M5STACK_PSRAM // Has PSRAM
//#define CAMERA_MODEL_M5STACK_V2_PSRAM // M5Camera version B Has PSRAM
//#define CAMERA_MODEL_M5STACK_WIDE // Has PSRAM
//#define CAMERA_MODEL_M5STACK_ESP32CAM // No PSRAM
#define CAMERA_MODEL_AI_THINKER // Has PSRAM
//#define CAMERA_MODEL_TTGO_T_JOURNAL // No PSRAM

 

 

const char *ssid = "jml's Soft Access Point";
const char *password = "";

#include "esp_camera.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include "camera_pins.h"
#define LED_Builtin 4

WiFiServer server(80);

 

 

 

void startCameraServer();

void setup(){
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP: ");
Serial.println(IP);
startCameraServer();
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 PSRAM IC present, init with UXGA resolution and higher JPEG quality
// for larger pre-allocated frame buffer.
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;
}

#if defined(CAMERA_MODEL_ESP_EYE)
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
#endif

// camera init
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();
// 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
}
// drop down frame size for higher initial frame rate
s->set_framesize(s, FRAMESIZE_QVGA);

#if defined(CAMERA_MODEL_M5STACK_WIDE) || defined(CAMERA_MODEL_M5STACK_ESP32CAM)
s->set_vflip(s, 1);
s->set_hmirror(s, 1);
#endif;
}

/*WiFi Starts here&&&&&&&&&&&&&&&&&&&&&&&&&&&&& */

 

/*void setup() {

pinMode(LED_BUILTIN, OUTPUT);

Serial.begin(115200);
Serial.println();
Serial.println("Configuring access point...");

// You can remove the password parameter if you want the AP to be open.
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();

Serial.println("Server started for jml's AP soft &&&&&&&&&&&&&");

}
*/
void loop() {
WiFiClient client = server.available(); // listen for incoming clients

if (client) { // if you get a client,
Serial.println("New Client**************************."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character

// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();

// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> to turn ON the LED.<br>");
client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>");

// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}

// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
}


   
ReplyQuote
(@drjoelleon)
Member
Joined: 3 years ago
Posts: 41
Topic starter  

   
ReplyQuote
MadMisha
(@madmisha)
Member
Joined: 4 years ago
Posts: 340
 

   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1680
 

@drjoelleon

Hi Joel,

  It appears I was trying to untangle your code at the same time as @madmisha. If the code above works, great, my contribution is redundant. (And the code above looks neater!) I agree with comments above.

I tried to minimise changes, but when I tried to run my version of the code with the obvious bugs removed, it seemed to change the baud rate to the monitor, as it started printing garbage. I confess I didn't spend a lot of time figuring out why. I simply moved the WiFi start until after initialising the camera, which seemed to fix it.

Sorry, I haven't looked into how the LED brightness is actually controlled by the browser, so although it now compiles, I don't know if that bit works. It does however run the usual web server and shows the camera image, so somewhere to start from.

It is 'not obvious', unless you look at the content of some of the ".h" files, but it is only necessary to call about 3, as the others are then automatically included by the the first few. Also, the .h files should normally be included before you get into the code sections. #define and other #xxs are not code.

I'll include my version in case it is still helpful.

Good luck!

 


   
ReplyQuote
MadMisha
(@madmisha)
Member
Joined: 4 years ago
Posts: 340
 
Posted by: @davee

Also, the .h files should normally be included before you get into the code sections. #define and other #xxs are not code.

I agree wholeheartedly. I am actually a little surprised that the example code just threw it in at that spot. Also, the commenting seems to annoy me too. All I did above was use the example and add softAP that was included in the loop section already. I have a feeling that the HTML LED toggling was actually the example for softAP and is not @drjoelleon's intent. They will have to confirm this though.


   
ReplyQuote
(@drjoelleon)
Member
Joined: 3 years ago
Posts: 41
Topic starter  

@madmisha

Thanks again. I’ll try this first thing tomorrow and let you know. 


   
ReplyQuote
(@drjoelleon)
Member
Joined: 3 years ago
Posts: 41
Topic starter  

@davee

ill try both solutions in the morning.

Thsnks Dave

Joel


   
ReplyQuote
MadMisha
(@madmisha)
Member
Joined: 4 years ago
Posts: 340
 

   
ReplyQuote
(@drjoelleon)
Member
Joined: 3 years ago
Posts: 41
Topic starter  

I tried your sketch and go and error stating the "camera_pins.h" is not defined.


   
ReplyQuote
(@drjoelleon)
Member
Joined: 3 years ago
Posts: 41
Topic starter  

@davee

The sketch did not compile for me. The error was the Camera_pins.h ,no such file directory. Its funny because in the sketch that worked. the compiling was not stopped there.

Is the Arduino that brittle a language that it works and doesn't work for no reason ? 


   
ReplyQuote
Page 1 / 3