Take a look at the code below (sorry if it is too long). This code works fine until I add the line "Spark.subscribe(“hook-response/send_power_reading”, gotData, MY_DEVICES); " and flashing the device. After that, the device starts flashing red with the occasional single cyan then blue flash. I would then need to apply a factory reset. What am I doing wrong?
SYSTEM_MODE(SEMI_AUTOMATIC);
uint8_t retry_count = 0;
unsigned long old_time = millis();
int val = 0;
int inPin = D0;
int outPin = D7;
int PreviousSecond = -1;
int ThisSecond = -1;
int PreviousMinute = -1;
int ThisMinute = -1;
int LoopCount = 0;
bool SensorState = false;
bool PreviousSensorState = false;
int PulseCount = 0;
void setup() {
WiFi.on();
pinMode(outPin, OUTPUT);
pinMode(inPin, INPUT);
Time.zone(2);
PreviousSecond = Time.second();
Spark.subscribe("hook-response/send_power_reading", gotData, MY_DEVICES); //PROBLEMATIC LINE
}
void loop() {
if(millis() - old_time >= 2000){
if(retry_count < 10){
if(!WiFi.ready()){
WiFi.connect();
retry_count++;
}
else if (!Spark.connected()){
Spark.connect();
retry_count++;
}
}
else{
WiFi.off();
retry_count = 0;
WiFi.on();
}
old_time = millis();
}
LoopCount+=1;
val = digitalRead(inPin);
SensorState = !val;
digitalWrite(outPin, SensorState); //show sensor state on little blue led
//Count Pulses//
if(SensorState != PreviousSensorState) {
//Detect Leading Edge//
if(SensorState==true) {
PulseCount += 1;
}
///////////////////////
PreviousSensorState = SensorState;
}
////////////////
//Transmit Pulsecount at the turn of the minute//
ThisMinute = Time.minute();
if(ThisMinute != PreviousMinute) {
//Spark.publish("send_power_reading");
PreviousMinute = ThisMinute;
PulseCount = 0;
}
/////////////////////////////////////////////////
ThisSecond = Time.second();
if(ThisSecond != PreviousSecond){
PreviousSecond = ThisSecond;
LoopCount = 0;
}
}
void gotData(const char *name, const char *data) {
String str = String(data);
}
No luck. I am not sure it is because of the Spark being offline, since the device is breathing cyan before I flash the program and it starts blinking red as soon as the flashing is ready.
Yes. I have now removed all the code related to offline usage and it is working fine. Is it possible that the problem only happens when in SEMI_AUTOMATIC?
I've made the observation that one last call to SPARK_WLAN_Loop() / Spark.process() after the while() and before subscribing does cure that problem (in my case at least it did in SEMI_AUTOMATIC too).
BTW @mdma:
Did you really mean to Spark.connect() when Spark.connected() already is TRUE?
On the Core (0,3.4) Spark.process() only runs the Cloud event loop - SPARK_WLAN_Loop() is needed for background connection management. It’s only with 0.4.x that SPARK_WLAN_Loop() is replaced by Spark.process() which also takes care of the connection management.
Since I don’t actually know your use cases any definite answer might still not help you
I’m not too sure what your program (especially the disconnect/connect part) should do.
But to get rid of the red flash you can simply do this:
void setup()
{
// WiFi.on(); // forget this, it will be done implicitly if needed
Spark.connect();
pinMode(outPin, OUTPUT);
pinMode(inPin, INPUT);
Time.zone(2);
PreviousSecond = Time.second();
old_time = millis();
while(!Spark.connected() && millis() - old_time < 30000)
{
SPARK_WLAN_Loop();
delay(100);
}
SPARK_WLAN_Loop(); // once more
if (Spark.connected()) // just to make sure we didn't timeout
Spark.subscribe("hook-response/send_power_reading", gotData, MY_DEVICES);
}
But as I see it, your event handler has no useful effect anyway
If you could also clarify what you want to achieve with this if(millis() - old_time >= 2000) { ... }-block we might be able to come up with an alternative solution.
I’m not sure if WiFi.off() would kill your subscription or not (could try myself - but you know … ).
But if it does, you’d have to resubscribe, but then I’m not sure how often you can, before running into troubles again.
This might be worth asking @Dave or @mdma, since his post suggests to do so ;-).
This is the part I've coded out above to get rid of the red flash
After that you should be good to use your original code.
But my suspicion still stands
I doubt WiFi.off() kills the suscription, but if it did, I'd expect resubscribing too often might bring in a problem.
I recall that there was some discussion about this, but I'd have to find that thread again.