Cannot manually connect and disconnect from particle cloud

I have been struggling for some time trying to find a way to connect to particle cloud manually at the end my program, send the events and then disconnect from particle cloud. Everything in the program works, with the exception of the wifi portion.

The code builds as is but when flashed on the photon, the LED turns red. Here are two of the several versions I have tried.

I went through the forum and there is a lot on wifi but haven’t been able to get the solved solutions to work with my code base. For example, I have viewed this solution WiFi.off(), WiFi.on(); issues and question to conserve battery

Any suggestions?

SYSTEM_MODE(SEMI_AUTOMATIC);

is above void setup()

void Program::sendEvents() {
	
	Particle.connect();

	if (Particle.connected()) {
		Serial.println("Connected to Particle Cloud!");
		Particle.publish("Connected to Particle Cloud");

		if (!WiFi.hasCredentials()) {
			Serial.println("NO WIFI CREDENTIALS");
		}

        // this works 
		while (!queue.empty()) {
			EventData* ed = queue.back();
			Particle.publish(ed->name.c_str(), ed->value.c_str());
			delete ed;
			queue.pop();
		}

		Particle.disconnect();
		Serial.println("DISCONNECTED FROM PARTICLE CLOUD, WIFI MODULE STILL ON");
	}
}

and the below doesn’t work either

SYSTEM_MODE(SEMI_AUTOMATIC);

and

STARTUP(WiFi.setListenTimeout(60));

above

void setup()

void Program::sendEvents() {

	WiFi.listen(); // Warning! This blocks application code

	WiFi.connect();

	if (!WiFi.hasCredentials()) {
		Serial.println("NO WIFI CREDENTIALS");
	}


    // this works as it should
	while (!queue.empty()) {
		EventData* ed = queue.back();
		Particle.publish(ed->name.c_str(), ed->value.c_str(), PRIVATE);
		delete ed;
		queue.pop();
	}

	if (disableTimeout) WiFi.setListenTimeout(0); 

	WiFi.disconnect();

}

Since your sendEvents() seems to be an object method, we’d need to know how the Program class is declared and how the respective object (if there is one) is instantiated.

Also your order of execution is back to front in parts.
If you expect WiFi.hasCredentials() to render false you should perform that check before you call WiFi.connect(). So I’d rather do

  WiFi.on();
  if (!WiFi.hasCredentials()) {
    Serial.println("NO WIFI CREDENTIALS");
    return;
  } 
  ...

And in order to use Particle.publish() you also need perform Particle.connect() and wait for Particle.connected() to become true - WiFi.connect() won’t be enough.

1 Like

Is the below logic correct?

    void Program::sendEvents() {

    	WiFi.on();
    	if (!WiFi.hasCredentials()) {
    		Serial.println("NO WIFI CREDENTIALS");
    		return;
    	}

    	WiFi.connect(WIFI_CONNECT_SKIP_LISTEN);

    	Particle.connect();

    	if (Particle.connected()) {
    		Serial.println("Connected to Particle Cloud!");
    		Particle.publish("Connected to Particle Cloud");
    	}

    	while (!queue.empty()) {
    		EventData* ed = queue.back();
    		Particle.publish(ed->name.c_str(), ed->value.c_str(), PRIVATE);
    		delete ed;
    		queue.pop();
    	}

    	Particle.disconnect();
    	Serial.println("DISCONNECTED FROM PARTICLE CLOUD, WIFI MODULE STILL ON");
    }

Below is a video of the red flashing sequence of the Photon.

https://drive.google.com/file/d/0B-2fTPnJmH1JakpkazM1RElqZWs/view?usp=sharing

I have multiple times flashed tinker to it and used the desktop Particle firmware manager to start over and then flashed sample Particle maker code and the above flashing sequence still occurs.

I still repeat this point

The rest of your code may provide some information we are currently lacking

But the logic is also not quite right.
Particle.connect() won't be blocking (nor is WiFi.connect()), so the immediate Particle.connected() check will hardly ever give you a true result and even worse, you are trying to publish inside your while() loop irrespective whether the connection has been established or not.
And for nit-picking, Particle.publish() can work with String objects just the same, so the c_str() call won't be needed.

1 Like

The entire code base is for a project so I cannot share it. I did share your statement with a C++ developer on the project and his feedback was that there are not many ways to declare a class or how an object is instantiated. Please clarify how this would impact using the Particle semi automatic mode.

The reason I am using semi-automatic is so the application code can run immediately without it being hung up on connecting to Wi-Fi.

It doesn't work without, so I will need to keep it.

There might not be many ways how to instantiate an object, but there are several ways where and when to do so and "where" the code will be running.
Is it a local instance, or is it even an instance created in an independent thread?
What does the constructor do?
Does it rely on other objects being readily available?
What if they weren't?

That would need examination, since it should. What is the error message you get?
https://docs.particle.io/reference/firmware/photon/#particle-publish-

1 Like

@KyleG - aside from the semi automatic issues. I cannot "reset" the photon. Meaning DFU particle update and particle flash --usb tinker, Particle Firmware Manager, Tinker App does nothing. The red flashing is persistent. Whom from Particle can I tag to this post :slight_smile:. I don't understand what else I can do to bring back the Photon to working again.

Let me ping someone that might be able to help, @rickkas7 are you able to assist?

Kyle

@kennethlimcp, I am trying to do a factory reset of the firmware, i.e.

dfu-util -d 1d50:607f -a 1 -s 0x00020000 -D factory-firmware.bin

I tried searching github and I do not know where to find the .bin I need. GitHub - particle-iot/device-os: Device OS (Firmware) for Particle Devices. Can you please send me the link?

@dancingpearl, with a photon, you can do the following in DFU mode using CLI:

  • particle flash --usb tinker
  • particle update

I have tried both of those, while it has no errors in command line. My firmware is still on it. I can tell because the SSID is my custom name not Photon-XXXX. And it continues to flash red after it restarts.

Can you place the Photon in safe mode?

It wont stay in safe mode. When I attempt to do it, it immediately flashes once magenta, once white and then forever red.

Interesting… What does particle --version say?

1.23.1

Ah… i guess we will have to wait and see what tech support says then.

@dancingpearl, the SSID is stored in a memory location which is not touched by a system or application firmware update. So this is no indication that your old firmware is still present.
If you want to revert the custom SSID you need to explicitly set it back to the original value.