I found this code on another thread and want to try it, but it is not complete or correct yet. Does anyone know how this code should be written?
The main goal is to add a new wifi when my device is in a new location without me having to press and hold the physical ‘SETUP’ button on the board. Once it is in listening mode, I use the app to add wifi.
SYSTEM_MODE(SEMI-AUTOMATIC);
if (unable to connect to the internet >= 30 min) then WiFi.listen(); else continue with firmware code…
Thanks for the quick reply, so far I used this code to have an RGB show if the Photon is connected to wifi with a blue color:
if (Particle.connected()) { strip.setPixelColor(0, 220, 10, 10); // 1ste argument led strip.show();
Only thing I need to solve is the fact that I cannot acces the ‘SETUP’ button on the board which I need to add new wifi through the app. So I need a way to make it go into ‘LISTENING MODE’ through software.
So I think it should be a similar code but instead of if(connectend) it should be if(disconnected) or something
The last part is basically what i need to know: How to get Particle Photon intro Listening mode without the ‘SETUP’ button on the board (because I cannot acces it anymore)
Best case, I have a code which says, after 2 minutes without wifi, go into listening mode, that way I can setup a new wifi on the location i am at
There are different answers depending on how you want your device to behave.
If you want your app to continue to run while waiting for a certain period before activating listening mode. Then as @ScruffR is saying you want
SYSTEM_THREAD(ENABLED) and probably
SYSTEM_MODE(AUTOMATIC);
now in loop() you could put a millis() based timer which checks WiFi.ready() and calls WiFi.listen() if it gets to 2 mins.
__
Or you can read https://docs.particle.io/reference/firmware/photon/#waiting-for-the-system and derive something from that.
__
Or you could add a button to your product that calls WiFi.listen() when you hold it in
I think everyone is waiting for you to take the examples, suggestions, and previous posts and cobble together some real code of your own. If you have trouble with that code, then by all means, post it and someone will take a look. What you have been posting, as @ScruffR put it, are pseudo-code fragments and not real code. As a rule of thumb, I always try to research and understand what I'm trying to do so I can code it myself before asking the forums for some "quick code". Not trying to be mean, rather, just supporting independent thinking. I've found this community to be very generous when they see the effort put in prior to posting.
With that said... I'm feeling generous. I'm not the best at this platform and this is untested code based on the information @ScruffR gave you above, so use at your own risk:
//Before Setup, declare variables.
unsigned long waitingMillis = 0;
bool listeningForWiFi = false;
//********
//In loop
//********
//Check if WiFi is already established.
if (!WiFi.ready()) {
//Check if we are already waiting for WiFi to be ready and not listening for WiFi.
if (waitingMillis && !listeningForWiFi) {
//Check how long we have been waiting for WiFi to be ready.
if (millis() - waitingMillis > 120000) { //2 minutes wait time.
listeningForWiFi = true; //Set a flag so that we call WiFi.listen() once and not on every loop.
WiFi.listen(); //Put WiFi into listening mode.
}
} else {
//Record the time that we started waiting for WiFi to be ready.
waitingMillis = millis();
}
} else {
waitingMillis = 0; //Reset the waiting variable if WiFi is already established.
listeningForWiFi = false; //Reset the listening variable if WiFi is already established.
}
Also, I use comments to help me try an work out code with logical blocks when I want to code something that I don’t exactly know what the code should look like. For example,
//Check if we are connected to WiFi
//if not connected, wait for 2 minutes
//after wait period, go into listening mode
From there, I start to research how to do each one of those tasks. You could add some if statements with psuedo code to get closer to the end goal like this:
//Check if we are connected to WiFi
if (!WiFi.ready) { //We are NOT connected to WiFi
//if not connected, wait for 2 minutes
if (millis() - whendidwestartlistening > 120000) {
//after wait period, go into listening mode
WiFi.listen();
}
} else { //We are connected to WiFi
}
@ScruffR gave you the check for WiFi connectivity with the WiFi.ready(). @Viscacha gave you the suggestion for a millis() based timer which you can search this forum for examples on how to accomplish. @ScruffR also gave you the piece for how to go into listening mode with WiFi.listen(). All you have to do at that point is create a few variables to track status between loops, figure out when to reset those variables and when to increment those variables. For example, when I was writing the code in the previous post, I got to a point where I said “I only want to call WiFi.listen() once. Now how do I track that I already called it?” So I had to go back and add the boolean to the header and figure out when to set/reset that boolean.