#include "application.h"
// Choose external antenna for WiFi
STARTUP(WiFi.selectAntenna(ANT_AUTO));
// Run application thread separately from Wifi Thread
SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);
void setup(void)
{
Serial.begin(9600);
//uart1_init();
}
void loop(void)
{
if (!Particle.connected()) {
//if (WiFi.hasCredentials()) {
Particle.connect();
for (int32_t i = 0; i < 100; i++) {
delay(20);
if (Particle.connected()) {
break;
}
}
//}
}
if (Particle.connected()) {
/*try to publish data*/
bool p_re = Particle.publish("new-data", "128degree", 10000);
Serial.printf("publish done %d\r\n", p_re);
}
/*fall back to sleep to save power*/
System.sleep(6);
}
I’d use waitFor()
instead of your delay-loop to wait for connection.
Also sleeping the WiFi module for 6 seconds and then immediately trying to reestablish the connection is bound to cause troubles.
System.sleep(x)
does not stop execution of loop()
.
BTW, the TTL parameter in your publish is not doing anything, you can just drop it and I’d also go with PRIVATE
events instead of (currently default) PUBLIC
.
I find this issue by chance.I am not going to use System.sleep().