Integrate BootWiFi snippet in IDF C project

meneses.leonardo
Posts: 7
Joined: Wed Feb 21, 2018 8:45 pm

Integrate BootWiFi snippet in IDF C project

Postby meneses.leonardo » Tue May 22, 2018 12:25 am

Hey there. I have programmed my ESP32 for networking, sensing, among other functions. Everything is working but connection to WiFi network is HARD-CODED. That's when I found @kolban 's BootWiFi snippet and successfully ported in a new project. It's been really hard for me because I don't know annything about c++ coding, so I started adding C source files to the new project and adding functions to their headers for compatibility with c++. Most functions work as expected and the program is compiled and sent to the device, but ble_gap_api callback functions cause panic halt cpu and the device crashes.

Tried the inverse by putting the bootWifi source file and header in the C program (following forums for porting c++ sources and classes to c) but I don't know how to call these.

BootWiFi* boot;

(in main)
boot = new BootWiFi();
boot->boot();

All help is appreciated.
thanks

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Integrate BootWiFi snippet in IDF C project

Postby kolban » Tue May 22, 2018 1:25 pm

From a C program, there is no way to create a C++ class and call it. If you want your "main" app to be pure C, then you would create another C++ source file and expose a function that can be called from C and call it such that it calls C++. For example.

Code: Select all

File: mycpp.cpp

extern "C" {
   void doit();
};

void doit() {
   BootWifi boot = new BootWiFi();
   boot->boot();
}

In you C program you can now code:

extern void doit();

....

doit();
That all said ... C++ should be a super set of C and you should be able to work just fine in C++ as well as C. If we are finding problems with your C-like logic in a C++ environment that works in a C environment, I'd be very interested to hear about that.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

meneses.leonardo
Posts: 7
Joined: Wed Feb 21, 2018 8:45 pm

Re: Integrate BootWiFi snippet in IDF C project

Postby meneses.leonardo » Fri May 25, 2018 1:31 pm

Thanks, Worked right away. This is a step up in my project. In the future, what would you suggest me? go full c++ with your working snippets community? or stay and bring my code to c? (or something super crazy like micropython :o ).

By the way, this is the tutorial I followed for ble advertising that works successfully in C, but crashes when ported to C++ environment.

http://www.lucadentella.it/en/2018/03/2 ... vertising/

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Integrate BootWiFi snippet in IDF C project

Postby kolban » Fri May 25, 2018 2:35 pm

Howdy,
Its a good question but asking for a recommendation is hard. Whether one works in C or C++ is a function of whether one knows C++ and whether you are a hobbyist or professional in the software business. C++ is supposed to be a superset of C and I find that 98% true and then you get the additional functions (classes etc) that make C++ distinct from C. The benefits of C++ also start appearing when one writes larger and larger applications. One can then "encapsulate" function in a Class and then "forget" how the class works and just use it.

As for why your application crashed when written in C++ but works in C ... that is indeed a puzzle ... but one that could likely be solved. Your description of what failed asked more questions than answers (opinion). You might want to try and recreate that puzzle and document it in super-great detail to allow the community to assist in the diagnosis.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Who is online

Users browsing this forum: Majestic-12 [Bot], Marcosss and 91 guests