Running own code while connecting to WiFi

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
litamin
 
Posts: 16
Joined: Mon Jan 04, 2021 1:10 pm

Running own code while connecting to WiFi

Post by litamin »

If the Wifi connection gets abrupty disconnected (say after having had a succesful connection for a while), the io.run() function freezes in the "while (status() < AIO_CONNECTED)" part until it gets reconnected. This is all good and well, but what if we want to run our own code during this time?
I guess I can add my code into the AdafruitIO.cpp file, but I'd rather not mess in there too much, plus all my defined functions and variables in my main arduino file cannot be reached there (AFAIK).

Is there any better way to have your code running when this happens?

- The serial error message I get when the connectoin gets lost: [E][ssl_client.cpp:33] _handle_error(): [data_to_read():270]: (-76) UNKNOWN ERROR CODE (004C)
- I'm using a ESP32
- I'm a new to mid level programmer

Thanks!

User avatar
dastels
 
Posts: 15831
Joined: Tue Oct 20, 2015 3:22 pm

Re: Running own code while connecting to WiFi

Post by dastels »

I'd add the ability to provide a function (which would be a function pointer) to the AdafruitIO code that would then be called from that loop. That would give you maximum flexibility with minimum change.

Dave

User avatar
iamnotsafe
 
Posts: 1
Joined: Wed Jan 20, 2021 6:32 am

Re: Running own code while connecting to WiFi

Post by iamnotsafe »

Thanks! Do you think you can show me how to do that? A function pointer. Not sure I've learned how to do that yet.

User avatar
dastels
 
Posts: 15831
Joined: Tue Oct 20, 2015 3:22 pm

Re: Running own code while connecting to WiFi

Post by dastels »

You just need to refer to a function without calling it (i.e. without the trailing parens.

Let's define a function:

Code: Select all

void func()
{
  // do stuff
}
Declare the variable f as a pointer to the kind of function func is (takes no args and returns nothing) and have it be a function pointer to func:

Code: Select all

void (*f)() = func;
Then you can use the function pointer to call it. You could also pass the function pointer into another function, etc.

Code: Select all

f();
See section 7.7 of "The C++ Programming Language" or https://www.cprogramming.com/tutorial/f ... nters.html

Dave

User avatar
litamin
 
Posts: 16
Joined: Mon Jan 04, 2021 1:10 pm

Re: Running own code while connecting to WiFi

Post by litamin »

Thanks so much for guiding me!

I'm afaid I dont fully follow you though. I have a function defined in my main file (main.cpp), called

Code: Select all

void hello(){
// my code
}
and now I want it to run in the AdafriutIO.cpp file, inside the "waiting to connected"-while loop like so:

Code: Select all

while (status() < AIO_CONNECTED){
// bunch of adafruit code
hello(); // my function
}
But of course this wont work since main.cpp and AdafruitIO.cpp are several folders apart. How do I get this to work?

User avatar
dastels
 
Posts: 15831
Joined: Tue Oct 20, 2015 3:22 pm

Re: Running own code while connecting to WiFi

Post by dastels »

You would need to change the AdafruitIO code to store a pointer to a function that you can set (likely by adding a setCallback() function). Soo...

In the AdafruitIO code:

Code: Select all

void (*callback_func)() = NULL;

void set_aio_callback((*f)())
{
    callback_func = f;
}

...

while (status() < AIO_CONNECTED){
    // bunch of adafruit code
    if (callback_func) {
        callback_func(); // my function
    }
}
Then in your code:

Code: Select all


void set_aio_callback((*f)());

void hello(){
    // my code
}

set_aio_callback(hello);

...
This is just off the top of my head, so it might need some tweaking.

Function pointers can be incredibly useful, so it's worth learning about them.

Dave

User avatar
litamin
 
Posts: 16
Joined: Mon Jan 04, 2021 1:10 pm

Re: Running own code while connecting to WiFi

Post by litamin »

Ok I tried, didnt work.. :/

I'll have to read up on function pointers. But I usually try to stay away from the advanced C++ coding and keep to simple tweaks.

I guess what I was looking for was a way to easily access the while (io.status() < AIO_CONNECTED) loop from inside the main.cpp file where the rest of my code is. This is for example available in void setup() when trying to connect for the absolute first time. Wish it was the same for when already having been connected, then losing the connection and then when the program tries to reconnect. I feel like that would be a great feature to be able to do stuff while it is trying to reconnect, instead of being stuck in a possibly infinite loop. (say when the wifi goes down)

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”