Ive got a a particle photon that I need reliable local scheduling and control as it controls pumps valves and chemical injections. I want the cloud to be as an enhancement not something that gets in the way of normal function. I’ve created a function that I check the time of day and do stuff at scheduled times. I’m checking the hour and minute to see if they match my time and running if the minute changed from the last minute so it only runs once when the time matches that hour and minute. Problem is the photon is randomly missing some of these events and its important that it doesn’t. The only thing I can think of is internet connectivity issues occasionally causing blocking code that keeps my loop from even running. I’ve tried several things from semi automatic mode to now my most recent code which I think in manual mode should keep any cloud or mqtt call from blocking longer than 15 serconds. With this code I’m expecting It should never miss an entire minute?! It shouldn’t be hard to get a one minute resolution with this. Ill post the relevant code and hopefully you guys can see why I may be missing any events… I missed one this morning and when I went to investigate the photon is connected behaving normally and shows perfect device health and a round trip time of a few milliseconds…
STARTUP(WiFi.selectAntenna(ANT_EXTERNAL)); // Use external antenna
SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED); // enable so loop and setup run immediately as well as allows wait for to work properly for the timeout of wifi
// This #include statement was automatically added by the Particle IDE.
#include <MQTT.h>
// Time Variables for scheduling
int month;
int hour;
int minute;
int day;
int daylasttimesync;
int previousminute;
bool minutechange = false;
int rssi = 0;
void handlecloud(){
if (WiFi.ready()){
if (client.isConnected()){
client.loop();
}
else{
mqttconnect();
}
if (Particle.connected()){
Particle.process();
}
else{
Particle.connect(); // attempt to connect to the cloud
waitFor(Particle.connected, 15000); // allow 15 seconds to conncect
if (Particle.connected()){
Particle.process();
}
else{
Particle.disconnect();
}
}
}
else{
WiFi.connect(); //attempt to connect to wifi
if (!waitFor(WiFi.ready, 15000)){
WiFi.disconnect();
}
if (WiFi.ready()){
if (client.isConnected()){
client.loop();
}
else {
mqttconnect();
}
Particle.connect(); // attempt to connect to the cloud
waitFor(Particle.connected, 15000); // allow 15 seconds to conncect
if (Particle.connected()){
Particle.process();
}
else{
Particle.disconnect();
}
}
}
}
void mqttconnect(){
if (WiFi.ready()){
//** Mqtt connect
// connect to the mqtt server with your username and password here
client.connect("clientname", "username", "password");
int i = 0;
while(!client.isConnected() && i < 20) {
delay(100);
i++;
}
// subscribe to mqtt events that you wish
if (client.isConnected()) {
client.subscribe("subscriptiontopics");
}
}
}
// Runs when called upon in loop to check for a regularly scheduled event such as pump speed changes valve changes and chemical pump changes at set times of the day
void checktime() {
hour = Time.hour();
minute = Time.minute();
month = Time.month();
day = Time.weekday();
//check if the minute changed so stuff runs once
if (minute != previousminute){
minutechange = true;
previousminute = minute;
}
else {
minutechange = false;
}
int cursec = Time.local() % 86400;
if (hour == 6 && minute == 30 && minutechange == true){
//doesstuff its these scheduled events that its occaionally missing
}
// 8 am 1500 rpm main returns setting 3
if (hour == 8 && minute == 0 && minutechange == true){
// does more stuff yada yada yada more like this occasionally missing to fire
}
}
void setup(){
WiFi.connect(); //attempt to connect to wifi
if (!waitFor(WiFi.ready, 15000)){
System.reset();
}
if (WiFi.ready()){
mqttconnect();
Particle.connect(); // attempt to connect to the cloud
waitFor(Particle.connected, 15000); // allow 15 seconds to conncect
}
if (Particle.connected()){
Particle.process();
}
//set timezone
Time.zone(-7);
}
void loop() {
handlecloud();
rssi = WiFi.RSSI();
// check time for schedule based functions
checktime();
}