The locking up, might be an issue difficult to track down with this many libraries playing together (or not).
Maybe you need to step back a bit and add one component after the other, starting with the one most likely to be the foe. I’ve used the DHT sensors without pull-up resistors, but I think the recommendation is to use one.
But for your WiFi issue there definetly is a cure.
First thing is to change from default SYSTEM_MODE(AUTOMATIC)
to SYSTEM_MODE(SEMI_AUTOMATIC)
which allows your code to run even without cloud and/or WiFi connection - and possibly also use SYSTEM_THREAD(ENABLED)
.
This then brings us to the difference between WiFi.connect()
and Particle.connect()
(which actually would be explained in the docs ;-)).
The former only connects your device to your local WiFi network, so you could use TCP/UDP communication but would not have the Particle.xxxx()
cloud functions working. For these to work you also need to call Particle.connect()
(which implicitly would call the former, if there is no WiFi connection already).
So a possible use case for you might look like this
SYSTEM_MODE(SEMI_AUTOMATIC)
SYSTEM_THREAD(ENABLED)
const uint32_t msRetryDelay = 5*60000; // retry every 5min
const uint32_t msRetryTime = 30000; // stop trying after 30sec
bool retryRunning = false;
Timer retryTimer(msRetryDelay, retryConnect); // timer to retry connecting
Timer stopTimer(msRetryTime, stopConnect); // timer to stop a long running try
void setup()
{
Serial.begin(115200);
pinMode(D7, OUTPUT);
Particle.connect();
if (!waitFor(Particle.connected, msRetryTime))
WiFi.off(); // no luck, no need for WiFi
}
void loop()
{
// do your stuff
digitalWrite(D7, !digitalRead(D7));
if (!retryRunning && !Particle.connected())
{ // if we have not already scheduled a retry and are not connected
Serial.println("schedule");
stopTimer.start(); // set timeout for auto-retry by system
retryRunning = true;
retryTimer.start(); // schedula a retry
}
delay(500);
}
void retryConnect()
{
if (!Particle.connected()) // if not connected to cloud
{
Serial.println("reconnect");
stopTimer.start(); // set of the timout time
WiFi.on();
Particle.connect(); // start a reconnectino attempt
}
else // if already connected
{
Serial.println("connected");
retryTimer.stop(); // no further attempts required
retryRunning = false;
}
}
void stopConnect()
{
Serial.println("stopped");
if (!Particle.connected()) // if after retryTime no connection
WiFi.off(); // stop trying and swith off WiFi
stopTimer.stop();
}