The timeout devices are now throwing a different odd behaviour. I’m trying to deploy a new version of my app to them, and disconnect from Particle. However they keep trying to connect to Particle regardless. I’ve tried flashing a blank app & moving Particle.disconnect()
to the top of my Setup.
Any ideas? Wonder if theres a connection with the Timeouts?
I’ve commented out some areas of the code to run the disconnect at the top of Setup but three of the four Xenons still go right back to trying to connect to Particle.
Update: I have now connected them over USB to flash that way, one of my devices, the flash went through still didn’t fully complete setup. A second time, it did complete setup. Very odd…
// enable retained memory
SYSTEM_THREAD(ENABLED);
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
// Settings
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#define updatePeriod 10 // update every 10 seconds
// pins
int WATER_SENSOR_PIN = D3;
int boardLed = D7;
// general vars
retained int pulseCount = 0;
retained int lastUpdate = 0;
char *version = "0.1.4";
char deviceInfo[120]; //adjust size as required
char json[256];
// Variables for the reset
const uint32_t connectivity_message_interval = 5000; // 1 sec
uint32_t last_connectivity_message_time = 0; // controls how often we send the offline message
const uint32_t connectivity_timeout = 180000UL; // 3 min b/c 30sec is pretty short, but up to you.
uint32_t last_connectivity_change_time = 0;
bool is_particle_connected; // flag to handle the moment of connectivity change
// This is when there is an issue and to reset the Xenon
void handlePulse(const char *event, const char *data) {
Serial.println("Heard");
}
// Function to find a device
void handleHello(const char *event, const char *data) {
if (strcmp(data, System.deviceID()) == 0) {
digitalWrite(boardLed,HIGH);
delay(2000);
digitalWrite(boardLed,LOW);
}
}
// This is when there is an issue and to reset the Xenon
void handleNeedReset(const char *event, const char *data) {
System.reset();
}
void handleParticleConnect(const char *event, const char *data) {
if (strcmp(data, System.deviceID()) == 0) {
Particle.connect();
}
}
void handleParticleDisconnect(const char *event, const char *data) {
Particle.disconnect();
}
// setup() runs once, when the device is first turned on.
void setup() {
Particle.disconnect();
Serial.begin(9600);
pinMode(WATER_SENSOR_PIN, INPUT);
attachInterrupt(WATER_SENSOR_PIN, pulse, CHANGE);
Mesh.on(); // potentially needed due to bug when mesh module is not already powered up.
Mesh.connect();
// Particle.connect();
// Particle.variable("version", version);
// Particle.variable("deviceInfo", deviceInfo);
// snprintf(deviceInfo, sizeof(deviceInfo)
// ,"App: %s, Date: %s, Time: %s, Sysver: %s"
// ,__FILENAME__
// ,__DATE__
// ,__TIME__
// ,(const char*)System.version());
// is_particle_connected = Particle.connected();
//
// Particle.syncTime();
// Particle.publishVitals(3600);
// Particle.disconnect();
// // turn off core LED
// RGB.control(true);
// RGB.color(0, 0, 0);
pulseCount = 0;
lastUpdate = Time.now();
// Mesh
Mesh.subscribe("hello", handleHello);
Mesh.subscribe("particle-connect", handleParticleConnect);
Mesh.subscribe("particle-disconnect", handleParticleDisconnect);
Mesh.subscribe("pulse", handlePulse);
Mesh.subscribe("reset", handleNeedReset);
Serial.println("Finished setup");}
void handle_reset() {
// I the device is in a bad state, resetting it
Particle.process();
if (!Particle.connected()) {
if (is_particle_connected) {
// handle moment of disconnection
is_particle_connected = false;
last_connectivity_change_time = millis();
Serial.println("Just Went Offline");
}
if ((millis() - last_connectivity_change_time) > connectivity_timeout) {
// probably an issue connecting, we'll try to fix by resetting
Serial.println("Resetting due to connectivity timeout...");
delay(1000); // allow serial message to get read out
System.reset();
}
if ((millis() - last_connectivity_message_time) > connectivity_message_interval) {
last_connectivity_message_time = millis();
Serial.println("Still Offline");
}
}
else {
if (!is_particle_connected) {
// handle moment of connection
is_particle_connected = true;
last_connectivity_change_time = millis();
Serial.println("Just Came Online");
}
// We are connected! Time for normal connected stuff...
if ((millis() - last_connectivity_message_time) > connectivity_message_interval) {
last_connectivity_message_time = millis();
Serial.println("Still Online");
}
}
}
// loop() runs over and over again, as quickly as it can execute.
void loop() {
// handle_reset();
// Loop to see if there should be any information published
if (Time.now() >= (lastUpdate + updatePeriod)) {
noInterrupts(); // Disables interrupts while publishing data
publish();
lastUpdate = Time.now();
interrupts(); // Re-enables interrupts when data is published
}
}
void pulse(void) {
// increment pulse count
pulseCount++;
}
void publish() {
// Try to push data to the rest of the Mesh Network
bool success;
snprintf(json, sizeof(json), "{\"time\":%d,\"device\":\"%s\",\"count\":%d,\"version\":%s}", Time.now(), System.deviceID().c_str(), pulseCount, version);
success = Mesh.publish("meter-data", json);
if (success == 0) {
pulseCount = 0;
lastUpdate = Time.now();
Serial.println(json);
} else {
Serial.println("Not pushed to Mesh");
}
}