Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

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.
Please be positive and constructive with your questions and comments.

Running own code while connecting to WiFi

by litamin on Fri Jan 22, 2021 11:54 am

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!

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

Re: Running own code while connecting to WiFi

by dastels on Fri Jan 22, 2021 1:14 pm

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

dastels
 
Posts: 6110
Joined: Tue Oct 20, 2015 3:22 pm

Re: Running own code while connecting to WiFi

by iamnotsafe on Sat Jan 23, 2021 1:40 pm

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.

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

Re: Running own code while connecting to WiFi

by dastels on Sat Jan 23, 2021 5:21 pm

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 | TOGGLE FULL SIZE
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 | TOGGLE FULL SIZE
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 | TOGGLE FULL SIZE
f();


See section 7.7 of "The C++ Programming Language" or https://www.cprogramming.com/tutorial/function-pointers.html

Dave

dastels
 
Posts: 6110
Joined: Tue Oct 20, 2015 3:22 pm

Re: Running own code while connecting to WiFi

by litamin on Mon Jan 25, 2021 8:09 am

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 | TOGGLE FULL SIZE
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 | TOGGLE FULL SIZE
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?

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

Re: Running own code while connecting to WiFi

by dastels on Mon Jan 25, 2021 10:58 am

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 | TOGGLE FULL SIZE
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 | TOGGLE FULL SIZE

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

dastels
 
Posts: 6110
Joined: Tue Oct 20, 2015 3:22 pm

Re: Running own code while connecting to WiFi

by litamin on Tue Jan 26, 2021 7:45 pm

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)

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

Please be positive and constructive with your questions and comments.