[photon] how can dealing with router power failure?

hello, again!

still we want to send OSC message without cloud connection.
it’s working(not perfectly. but at least we figure out what is the problem.) but. I want to photon working more robustiously.

let’s think about real environment. photon read sensing value, and broadcast that values via OSC.
then suddenly router is power off… then… what happen?

normally. with cloud connection(AUTOMATIC MODE), it’ll tyring reconnect to WIFI router.
but without cloud connection(SEMI_AUTOMATIC, MANUAL), it’s not working.

I tested it in many ways. but not work. check this code.


// SYSTEM_THREAD(ENABLED);  // nothing help

#include "simple-OSC.h"
SYSTEM_MODE(MANUAL);      // SYSTEM_MODE(SEMI_AUTOMATIC) is not different

UDP udp;
IPAddress outIP(192, 168, 100, 255);      // broadcast
unsigned int outPort = 8000;

void setup() {
	pinMode(D7, OUTPUT);
    // Put initialization like pinMode and begin functions here.
	Serial.begin(115200);

	// static IP setting
    // IPAddress myAddress(192, 168, 100, 101);           
    // IPAddress netmask(255, 255, 255, 0);
    // IPAddress gateway(192, 168, 100, 1);
    // IPAddress dns(192, 168, 100, 1);
    // WiFi.setStaticIP(myAddress, netmask, gateway, dns);

    // WiFi.useStaticIP();                                         // I tried with static IP setting but not helped.


    WiFi.connect();
    while(!WiFi.ready()){
    	Particle.process();		// 
    	delay(100);
	}
	Particle.process();			// 
	

	if(WiFi.ready()){	// LED ON
		digitalWrite(D7, HIGH);
	}
    
	udp.begin(8001);

    Serial.print("local IP: \t");
    Serial.println(WiFi.localIP());

}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
	// check ping to 
	Serial.println("OSC MESSAGE START!");
	OSCMessage message("/nemo");
	message.addFloat(1.23);
	message.send(udp, outIP, outPort);
	delay(500);
	Serial.println("OSC MESSAGE END!");

	if(WiFi.ready()){
		digitalWrite(D7, HIGH);
	} else {
		udp.endPacket();
		udp.stop();
		digitalWrite(D7, LOW);
		
		WiFi.disconnect();

	 // IPAddress myAddress(192, 168, 100, 101);           
	 //    IPAddress netmask(255, 255, 255, 0);
	 //    IPAddress gateway(192, 168, 100, 1);
	 //    IPAddress dns(192, 168, 100, 1);
	 //    WiFi.setStaticIP(myAddress, netmask, gateway, dns);

	 //    WiFi.useStaticIP();              // not helped either.

		WiFi.off();
		delay(5000);
		WiFi.on();

		WiFi.connect();
		Serial.print("connecting");
		while(WiFi.connecting()){
			Serial.print("waiting connection... ");
			delay(1000);
		}
		Serial.println();

		while(!WiFi.ready()){
			Particle.process();	
			Serial.print(".");
    		delay(100);
    	}


    	Serial.println();
    	Serial.println("WiFi fully ready.");

    	Particle.process();
    	Serial.print("PING returns : ");
    	Serial.println(WiFi.ping(WiFi.gatewayIP()));


    	Serial.println("========");
    	Serial.print("local IP: \t");
    	Serial.println(WiFi.localIP());

    	delay(5000);
       	udp.begin(8001);	//
	}
}

when router is off, photon’s LED is blinking green LED… and breathing. which means that photon is connected with router. but actually is not.

about 5 seconds from reconnect… ping is working. but after few seconds… check a screenshot.
I tried ping command from my macbook. I tried recoonect to router with my laptop as soon as possible. you can see photon react ping… but few seconds later it’s not.

any help? thanks in advance.

This code works for me:

Basically it waits for Photon to connect to wifi, and will try to reconnect whenever wifi is lost. If wifi is lost it goes into listening mode, and will keep retrying to connect.

1 Like

Your code shows some good techiques, but I guess in order to keep other tasks running properly Listening Mode is not required and may even be avoided.

But @icq4ever, I think you should stick with SYSTEM_THREAD(ENABLED).
If it’s not helping, it’s probably not used correctly - you have to think async :wink:

You may also want to look at waitUntil() and waitFor() instead of your while(WiFi.ready()) loops.

The next thing to check is how the OSC library deals with (half)closed connections and what it does with stale sockets.

BTW, you need to be aware that WiFi.useStaticIP() is a sticky setting. Once used, you need to explicitky switch back to WiFi.useDynamicIP() to undo that. Just commenting the code lines is not enough.

4 Likes