I use the main.cpp file to put code for the ESP32 in VS Code with PIO. As the code grows I wish to split it in multiple files like we do, for example, in the Arduino IDE. Is this possible?
Hi Jürgen,
Yes, that works in the usual way a C/C++ program is being structured.
So you can include your ‘jurgen.c’ or ‘jurgen-def.h’ file by adding
#include jurgen.c
before your (void) main (void) { } or infinit loop declaration.
By the way: If you’re working on some more complex stuff, I got an external debugger board from Espressif running. That dramatically helps you understanding and following code on your ESP and just costs < 20€!
Looky here: ESP32 Debugging
By the way, instructions are for ESP-Wrover-32. I’ll add more ESP boards soon.
Happy coding!
LoetLuemmel
Hi,
thank you LoetLuemmel for your response. But it does not work …
So I prepared a example code that do nothing but compiles without error.
First all in one file “main.cpp”: it includes 2 libraries, defines 2 constants and 2 variables. Then there is one function initWIFI() and finally the setup- and loop-routines:
————— main.cpp ——————–
#include <Arduino.h>
#include <WiFi.h>
const char* ssid = "home";
const char* pass = "here";
String oi = "oi";
unsigned long lastTime;
// ========================================================
void initWIFI(){
WiFi.begin(ssid, pass);
Serial.print("Connecting ..");
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.print("Connected to WiFi network");
}
// ========================================================
void setup(){
Serial.begin(115200);
initWIFI();
lastTime = millis();
}
// ========================================================
void loop(){
if ((millis() - lastTime) > 10000){
if(WiFi.status() == WL_CONNECTED){
// do something
}
lastTime = millis();
}
}
————– end main.cpp —————
Now I want to split this code into 2 files, say “main.cpp” and “wifi.cpp”. Only the initWIFI() function should be shifted to “wifi.cpp”; the rest should stay in “main.cpp”:
—————– main.cpp ——————
#include <Arduino.h>
#include <WiFi.h>
const char* ssid = "home";
const char* pass = "here";
String oi = "oi";
unsigned long lastTime;
// ========================================================
void setup(){
Serial.begin(115200);
initWIFI();
lastTime = millis();
}
// ========================================================
void loop(){
if ((millis() - lastTime) > 10000){
if(WiFi.status() == WL_CONNECTED){
// do something
}
lastTime = millis();
}
}
———— end main.cpp —————–
—————- wifi.cpp ———————
void initWIFI(){
WiFi.begin(ssid, pass);
Serial.print("Connecting ..");
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.print("Connected to WiFi network");
}
————– end wifi.cpp —————-
Now the compiler indicates a lot of errors:
1. in setup(): does not find more the initWIFI()-function
2. in initWIFI(): does not recognize:
- “WIFI.begin”
- the constants “ssid” and “pass”
- “Serial”,
- the library constant WL_CONNECTED and
- “delay”
Question: which references do I have to enter for this example to run?
Danke
Uoopsi!
Let me quickly put this into the same environment like you have, in order to check what’s going wrong.
If time allows, create a zip.file and attach it here.
So we would have the same baseline.
Later,
LoetLuemmel
Hi Jürgen,
No zip file necessary!
There is one problem, your wifi.cpp would try a redefinition of some reserved WiFi expressions.
I created the following two files:
#include <Arduino.h>
#include <WiFi.h>
#include "jurgen.h"
unsigned long lastTime;
// ========================================================
void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.printf(“WiFi Failed!\n”);
return;
} else {
Serial.printf(“\nConnected!\n\r”);
}
}
// ========================================================
void loop(){
if ((millis() – lastTime) > 10000){
if(WiFi.status() == WL_CONNECTED){
Serial.printf(“.”);
}
lastTime = millis();
Serial.printf(“\n%c”, oi[0]);
Serial.printf(“%c\n\r”, oi[1]);
juergen();
}
}
And another header file Jurgen.h:
String oi = "oi";
const char* ssid = “SuperSecret”;
const char* pass = “openWeb”;
void juergen (void)
{
Serial.printf(“\nLoetLuemmel was here!\n\r”);
}
With that sample it might be possible for you to extend your code into two or more parts.
I don’t know why initWIFI() was not accepted and assume that very method has been already reserved. You might be able to check in the include files, if that is the case.
I also learned something, because my output console for Serial.printf() has been gone. So I did program in PIO and looked the console in the Arduino IDE. J
I hope I could have provided you with some assistance and remain with a ‘happy coding!”
LoetLuemmel
P.S.: Looks like I screwed-up the HTML. – I hope you still get the giss how it works.
Hi Jürgen,
2nd thought: From the behavior, it looks like the Arduino paradigm has a defined sequence.
‘void setup ()’ is being called 1st, thus ignoring your definition of ‘void initWIFI()’.
If you would have defined elements for ‘void loop()’, things should have worked OK.
But with ‘void setup()’ the Arduino environment has defined a different programming paradigm from classic C/C++ approaches.
I assume this has been done to make programming easier for new starters with programming.
Happy coding!
LoetLuemmel
Hi.
Don’t call your other file wifi.cpp, try with another name.
That filename is already being used by the WiFi library.
Regards,
Sara
To Sara,
thank you for your response.
I renamed wifi.cpp but it does not change anything – the same 6 errors remain.
To LoetLuemmel,
thank you, too.
I do not understand why you did change a lot of the code – now are more errors in it than before. The code itself is not the problem – the problem is how to declare variables and function when they are in different files and relate to each other.
Jurgen
Hi Jurgen,
Sorry to confuse you with changed code (and the formatting, as <code></code> didn’t work as expected).
The reason I took different code was, as also Sara already pointed out, that you were trying to reuse an already reserved name.
They way Arduino simplified programming seems to also cause confusion, especially for a ‘classic’ C/C++ programmer.
Warren Gay did highlight that topic in his book ‘FreeRTOS for ESP32 Arduino’.
It can be simplified looking at the SerialMonitor output of such code:
// test.ino
static void initialize_led() {
ptintf("initialize_led()\n");
}
static void initialize_sensor() {
ptintf("initialize_sensor()\n");
}
void setup() {
delay(5000);
printf("Hello from setup()\n");
initialize_led();
initialize_sensor();
}
void loop() {
printf("Hello from loop()\n");
delay(2000);
}
Sorry for this new code again, but your WiFi sample unfortunately was most difficult approach for a simple code explanation.
Please give it a try and you’ll quickly see how the Arduino paradigm is constructed.
Happy coding,
LoetLuemmel
Hi Jurgen,
Sorry to confuse you with changed code (and the formatting, as <code></code> didn’t work as expected).
The reason I took different code was, as also Sara already pointed out, that you were trying to reuse an already reserved name.
They way Arduino simplified programming seems to also cause confusion, especially for a ‘classic’ C/C++ programmer.
Warren Gay did highlight that topic in his book ‘FreeRTOS for ESP32 Arduino’.
It can be simplified looking at the SerialMonitor output of such code:
// test.ino
static void initialize_led() {
ptintf("initialize_led()\n");
}
static void initialize_sensor() {
ptintf("initialize_sensor()\n");
}
void setup() {
delay(5000);
printf("Hello from setup()\n");
initialize_led();
initialize_sensor();
}
void loop() {
printf("Hello from loop()\n");
delay(2000);
}
Sorry for this new code again, but your WiFi sample unfortunately was most difficult approach for a simple code explanation.
Please give it a try and you’ll quickly see how the Arduino paradigm is constructed.
Happy coding,
LoetLuemmel