Notifications
Clear all

Joining 2 separate examples into 1 program

Page 2 / 3

MadMisha
(@madmisha)
Reputable Member
Joined: 2 years ago
Posts: 319
 

@drjoelleon

Are you opening the ESP Cam example sketch and replacing it with the code? It will have other files opened up in tabs at the top when it is opened properly in the Arduino editor.

image

ReplyQuote
DaveE
(@davee)
Estimable Member
Joined: 4 months ago
Posts: 137
 

Hi Joel,   @drjoelleon

   As  @madmisha illustrates above, the code expects the same 'environment' as the original camerawebserver.

  Personally I like to keep the existing 'package', and create a clone for each new variant. Tha way I can backtrack when something goes awry. From memory:

  1. File open the original project.
  2. File new in arduino. This opens an 'empty sketch' in a separate window.
  3. File Save as ... to give it a meaningful new (unique) name.
  4. Delete the 'empty sketch' code and replace with my code (or madmisha's) using copy and paste.
  5. File save.
  6. At this point, compare the arduino window for your new sketch with just 1 tab, with the original project window. The original cameraweb server project .. which has three other tabs, each with a project file .. including camera-pins.h. need to copy these files into the new project... as follows:
  7. There maybe an 'arduino' way of copying them .. but I resorted to using the pc's operating system file manager.
  8. I 'pretended' to start an Arduino File Save to find the directory path in both the new project and the existing project.
  9. Copied the three files from the existing project to the new project using pc's file manager.
  10. Now the new project should have 4 tabs ... and hopefully will compile.

Best wishes.


MadMisha liked
ReplyQuote
drjoelleon
(@drjoelleon)
Eminent Member
Joined: 4 months ago
Posts: 39
Topic starter  

@davee

thanks Dave

ill try this tomorrow. 
it’s kind of like playing golf. I’m not sure what I’m really supposed to do but I will try tomorrow. 

 

 


ReplyQuote
drjoelleon
(@drjoelleon)
Eminent Member
Joined: 4 months ago
Posts: 39
Topic starter  

@davee
I think I understand now. Arduino IDE is an object oriented programming language and the WiFi program that connects to the internet server is a different object than the WiFi program that others can connect to(Soft AP). Each different object requires its own tab on top of the Arduino page. 
is this correct?


ReplyQuote
drjoelleon
(@drjoelleon)
Eminent Member
Joined: 4 months ago
Posts: 39
Topic starter  

@davee

problem:

I started a New file and erased the code. I then added new tabs and renamed each and copied and pasted the contents from a functioning program. When I compiled it I got an error in the “camera_pins.h” tab,  #error “camera model not selected” 

looking at the copied code from @madmisha, it contained a line, #define CAMERA_MODEL_AI_THINKER // Has PSRAM

YIKE, I think golf might be easier because I believe there is no solution to learning the swing but stubborn and ignorant, I still think there is a real solution to this problem. 
thanks,

Joel


ReplyQuote
MadMisha
(@madmisha)
Reputable Member
Joined: 2 years ago
Posts: 319
 
Posted by: @drjoelleon

I started a New file and erased the code. I then added new tabs and renamed each and copied and pasted the contents from a functioning program. When I compiled it I got an error in the “camera_pins.h” tab,  #error “camera model not selected” 

looking at the copied code from @madmisha, it contained a line, #define CAMERA_MODEL_AI_THINKER // Has PSRAM

What do you mean rename each? Those other tabs should have the same name, just your main code needs to change and be saved as a different name(just to not mess up your example although you could just redownload it if needed). Also, I assumed that was the camera you were using since it was not commented out. Did I make the right assumption?


ReplyQuote
MadMisha
(@madmisha)
Reputable Member
Joined: 2 years ago
Posts: 319
 

If you unzip this and open the INO, that should work. It should give you the same workspace.


ReplyQuote
drjoelleon
(@drjoelleon)
Eminent Member
Joined: 4 months ago
Posts: 39
Topic starter  

@madmisha

i did not rename the tabs. 
i started over with a functioning camera and server connected to my home router that was working fine

i erased all existing wifi code and inserted your code 

I compiled and loaded it onto the ESP32-can board. I removed the jumper wired used for programming and reset the board. The Serial monitor showed the IP address as 196.168.4.1

i entered it into my web browser but no connection was made. I had added a line of code printing out a message on the serial monitor so I knew the program was working.

I did the with 2 different boards. One was I modified and the other I soldered on the external antenna. How can I check if the external or internal antenna are working?

This is the Serial output screen:

image

 

thanks again for your help, I feel I’m getting closer.

Joel


ReplyQuote
MadMisha
(@madmisha)
Reputable Member
Joined: 2 years ago
Posts: 319
 

@drjoelleon

Do you even see the SSID of the camera when you scan wifi? You typed 196 instead of 192 but I assume that's just a typo here.

 

You should also check your computer(or whatever device you are trying to view it on) and confirm that all the numbers except the ones after the last period are the same. You might have to set a manual IP. I will check to see if it offers DHCP by default.

 

I will note that I have had trouble connecting mobile devices to ESP32s. Some will search for an internet connection and when it gets no ping back, it could disconnect.


ReplyQuote
DaveE
(@davee)
Estimable Member
Joined: 4 months ago
Posts: 137
 

@drjoelleon Hi Joel,

  I can see @madmisha has been giving you some good advice, and I agree with it. I too wondered if you had moved the Wifi handler on your PC away from it's normal router link up to that of ESP board. (ie if you click on the Linux or Windows  wifi 'fan' icon, it should show your PC is connected to your ESP board, not your router name (SSID). You need to restart your browser after making this change as well.

As for your antenna question, the first indication is closely related and a follow up to the paragraph above. i.e. The 'fan' icon on both Linux and Windows gives a crude indication of received signal strength, in terms of how much of the 'fan' is 'bright'. Therefore, if you have your PC linked to your ESP board, you should be able to get a rough idea as to how strong the signal is, particularly if you start with them close to each other, and then move them apart to see how quickly the signal strength reduces. I haven't got a separate antenna for my ESP card, so I can't reproduce this at present, but as you have two cards, one with the external antenna, it should be fairly easy to do a comparison.

I assume that you moved the tiny 0 Ohm resistor on the card with the external antenna to the appropriate position. By the way, I don't know how 'vulnerable' this circuit is, but in general transmitters can be damaged if they are operated with the aerial disconnected, as the transmit power, if not coupled into the antenna, can be reflected back into transmitter/receiver circuit, resulting in 'high' voltages and 'high' power dissipation. ('High' in comparison with normal operation.)

---------

To assist you in your antenna testing, I suggest you revisit Bill's excellent ESP CAM video/blog, which had a short section, including a simple signal strength 'standalone' program. Look for 'Using an External Antenna' towards the end of blog https://  dronebotworkshop.com/esp32-cam-intro/ . (I think it was in the video as well.) This includes a short program that can be dropped into a new arduino project, and after setting the router address etc. in the usual way, shows the signal strength from your router, as seen by the ESP board.

Note that this only documents the ESP board's (internal or external) ability to receive a signal, and it is quite possible for the coupling between the transmitter output stage and the antenna to be badly mismatched, whilst the received signal could be reasonable. However, once again I would hope the external antenna would show itself in terms of improved received signal strength.

I don't play golf, but it sounds like more fun than hitting a small ball with a long stick thing .. 😀 Good luck!


MadMisha liked
ReplyQuote
DaveE
(@davee)
Estimable Member
Joined: 4 months ago
Posts: 137
 

@drjoelleon ... in reply to "Arduino IDE is an object oriented programming language ..."

Hi Joel,

  I am not an expert on the formalities of this situation, but offer the following personal, possibly flawed, perspective.

  Yes, Arduino is a kind of architectural style/structure, written in C++.

And of course C++, is based on the procedural C language, the '++' extension providing the object oriented capability.

The '++' extensions were deliberately aimed to be enhancements, not replacements, for the pre-existing C language, and hence it is quite reasonable to continue to write a program using the original C procedural language syntax and structure. (Strictly, C++ imposes some 'detail' changes to the original C syntax, but they can be ignored for the purposes of this high level discussion.)

Furthermore, I shall hypothesise that even if, C++ had never been invented, the Arduino development environment would be based on C, and still look exactly the same, with separate files in tabs and so on.

NB I refer to 'procedure' below ... in C/C++ syntax, they will be 'function' .. treat as interchangeable terms for now.

A full description would require something on the scale of a course, rather than a side question in a forum, but here goes for few clues:

A simple embedded C/C++ program of the type Arduino uses may take the following form: (say for a temperature controller, using a thermocouple to measure the temperature and switch a heater on and off)

  • make some general statements and declarations that will be useful later in the program
  • Define a setup procedure 
    • void setup()
    •    {code of 'setup ' .... do a list of on-off jobs}
  • Define a 'run round a loop of code forever' procedure [
    • void loop ()
    • {repeat
    •     { do a list of stuff ....}}
    •   until 1 is equal to 0                       -- obviously never true, so always loops back to 'repeat'
    • }
  • Define a function  main()                         // all C/C++ programs traditionally start by calling the function main()
  •   { setup(); 
  •     loop (); }

The C/C++ programmer normally starts by writing the function main(). Arduino 'insists' on providing it for you, but you can imagine the compiler will find it.

Thus the compiler/linker would mash all of the above code into a binary program with a single path of instructions  for the processor to follow, starting at 'main()' . (Obviously 1 will never be equal to 0, so the processor will be stuck in that loop forever.)

In practice, you are likely to want your processor to do a number of things, perhaps as well as switching the heater, it may need to turn on a pump, show heating status on a screen and so on. Nevertheless, the above structure may still be adequate. That is providing the time taken to 'do a list of stuff' is shorter than the response time needed by all of the jobs in that loop, then it is the simplest and safest structure.

Now for a small processor, and a very simple task, all of the above could be in one, probably long, file.

But of course, 99% of the time for a 'real' application, it would be very unwieldy, so we need to break it into smaller chunks ...  the old 'how to eat an elephant' approach.

Clearly you want to put your program source code into a number of files. And here the Arduino development environment conveniently provides an editor which can handle several files at the same time .. one per tab ...

------------------------------------

We can start with:

  • make some general statements and declarations that will be useful later in the program

With C/C++ and Arduino, this is usually put into files named xxxx.h.

Note these files do not 'generally' define any actual program instructions ... they are more useful for defining constants, function prototypes (described below) and so on.

These files are 'combined' into the main program 'text' by lines of the style,  "#include <filename.h>". As programs often call up 10s or 100s of such files, it is normal for some .h files to call up others using the same syntax.

----------------------------------

For small programs, both setup() and loop() procedures may have all of their respective code explicitly typed between the {} brackets that define their start and end. However, this also soon gets unwieldy as the program grows. The 'C' procedural approach is to define new functions, e.g.

     void function Do_1_thing () {

                instructions to 'Do_1_thing;

    }

Then these functions can be listed as a simple 1 line call ...  "Do_1_thing (); "

And of course, this principle can be repeated, where the code for Do_1_thing, may call another function Do_a Part_of_1Thing();

As each function is a bit like the chapter of a book, it may well be given its own file, often named after the function, say Do_1_thing.cpp in C++ or arduino environment  (or Do_1_thing.c if in a C only environment.)

------------------------------

Having introduced the idea of functions, it is natural to consider one of the useful tasks a compiler can do. That is it can make sure the definition of the function matches the call to that function in terms of spelling, parameters, etc. This is one place C++ is much more fussy (and helpful) than C, even when the code is essentially 'C' only.

For a small program, one could list the sub functions before the code that they are called up. Then the compiler could check the definitions. But this soon gets messy. So we normally put a function prototype .. often a copy first line of the function definition, in a .h file. That way the compiler can do the match up, providing it has already seen .h file. Again it is common for  each .c/.cpp to have a matching .h file in bigger projects.

--------------------------

Thus I have introduced .h and .cpp files, each of which is a candidate for a tab in the Arduino environment. However, with larger projects, the number of files would greatly exceed the screen space for tabs, so only those files in active 'editing' appear.

---------------------------

So far I have only mentioned functions/procedures. In general these are bits of code that work on data that is supplied to them when they are called up. e.g. a function to convert a price in US $ to a price in UK £, might be supplied with a number of dollars, and a conversion rate, so the function could return the number of UK £

I have deliberately not mentioned object orientated constructs. That is another chapter, except to say it builds upon the procedural foundations, by including constructions that bring data and code into much closer associations. So far as I am aware, it does not enable the programmer to achieve a program task that could not be achieved with just the 'C' language. If I remember correctly, the 'first generation' C++ code was actually compiled by a translator program that  produced C code. However, it can make the programmer's job a lot easier and more straight-forward when applied in the right circumstances.

---------------------

Also the style of program I have described has a single predictable path to follow.

---------------------------------

'Real' systems often include interrupts ... e.g. a method for an event like someone pushing a button can halt the processor in its tracks, persuade the processor to run a suitable block of code to deal with the event, then the processor returns to its original task at the point where, it left off, to continue as if nothing had happened. I think Arduino supports interrupts, but I haven't used them yet.

And 'bigger' systems can 'multi-task' or 'time-share'. The PC is a classic example. In effect two or more programs are run 'in parallel', except that the processor can only do 1 thing at a time, so it works on 1 program for a short time, then diverts to a second program and so on. From this, it is a small step to having more than 1 processor in a system with shared resources like memory. Some of the ESP32  devices are dual core ... ie two independent processors. I have yet to look at the implications of this, for this particular device or the Arduino system.

------------------------------------------------------

I have yet to look through the whole CAM project, which includes many files not included in the 4 tab Arduino view, so I cannot be sure whether interrupts or other multi-something methods are in play. However, I suspect the code you are looking at currently is a single path, mainly acting as a webserver generating a stream of HTML to send to the browser., and interpreting any HTML being sent from the browser, such as the user controls. Thus, to add in functionality, you need to 'sneak' some extra code into that loop in a manner that does not unduly disrupt the existing process.

A slightly twisted aspect of writing webserver code is that you are writing a program, whose resulting output is itself a program, written in a language the browser will understand...

And here endeth the first chapter .... sorry no golf sticks in sight...

Hope it is of some use. Let me know if it was helpful and of course any queries. Good luck, Dave


MadMisha liked
ReplyQuote
drjoelleon
(@drjoelleon)
Eminent Member
Joined: 4 months ago
Posts: 39
Topic starter  

@madmisha 

@davee

AMAZING, IT WORKS. thank you both sooo much.

 

I had to us the example of home WiFi and camera and substitute the  example WiIAccessPoint sketch for all the existing WiFi code.

I even disconnected it from the computer and the power hub and connected it to a 9v battery through a step down converter(?) to 5v and I can carry it anywhere. Additionally, I left the device in my study and walked all over the house and the signal was strong enough to see the image clearly (once I remove the camera lens protector).

Now for the next issue

Since the anchor locker is not light, I need to be able to turn on the LED. 

I #define LED_BUILTIN 4  // I think I used 4 on a different sketch and it worked but I can’t duplicate this. 
I tried putting a digital.Write(LED_BUILTIN); in many areas but no affect.

I connect the board to the computer thru the power hub and compile and download the program. After disconnecting the power, I remove the jumper wire, turn on the power and the serial monitor and press reset. Lots of stuff populates the screen and it gives the IP address. On my phone, thru Settings, I connect to the board’s WiFi. 
No connection is noted on the Serial monitor. I put in the Board’s IP address in Safari and immediately the camera’s interface appears. Still no connection noted on the Serial monitor. It is only when I activate the “Start Streaming” button that the New Client is connected. 
Any ideas how I can locate the place to put the digital.Write code.

should I paste my code in another reply?

 

Joel

 


ReplyQuote
drjoelleon
(@drjoelleon)
Eminent Member
Joined: 4 months ago
Posts: 39
Topic starter  

@davee

Wow, a personal dissertation. Very professionally written and enlightening. Thanks very much. 


ReplyQuote
MadMisha
(@madmisha)
Reputable Member
Joined: 2 years ago
Posts: 319
 

@drjoelleon

For the LED to work as just an LED, you need to make sure GPIO 0 is disconnected from ground.

 

The LED is on GPIO 33 and is inverse logic(low to turn on). I am not positive about this particular board but I know some ESP32s don't have built in LED integrated. But if you want to use the LED you would remove that connection to ground and write GPIO 33 low.

 

//In the Setup section
pinMode(33, OUTPUT); // Set the pin as output

//In the loop(or wherever you need it)
digitalWrite(33, LOW); //Turn on

You could set it up to if you go to a specific address in your browser it will toggle it. I am about to look into the example and see it I can just add it to the existing camera controls.


ReplyQuote
MadMisha
(@madmisha)
Reputable Member
Joined: 2 years ago
Posts: 319
 

I had a go at it but I can't be sure until I upload it. I ordered one and it should be here tomorrow.


ReplyQuote
Page 2 / 3