Photon with intermittent connection


#1

I’m working with Grove - Multichannel Gas Sensor using Photon particle. I am using the code below. The system works intermittently ie come online and go offline in periods of approximately 20 seconds. Due to this failure, the readings are never totaly complete. Some gas readings are not refreshed. In addition, because of the intermittent connection, it is difficult to redo the flash when necessary. It seems to be a bug in the code. I have already tested with 3 different units in two environments with different internet connections. The behavior is always the same. Some expert could check this code please …

Thanks for any help,

Cesar


// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>

       // This #include statement was automatically added by the Particle IDE.
       #include "MutichannelGasSensor.h"
       // Read Data from Grove - Multichannel Gas Sensor
       #include <Wire.h>
       #include "MutichannelGasSensor.h"


       char eventinfo[64];
       
       char auth[] = "";
       
       unsigned int ms;
       int publishdelay = 60 * 1000;
       // Publish CO, Humidity, Dew Point
       void PublishMutichannelGasSensorInfo(){
       float c = gas.measure_CO();
       float d = gas.measure_H2();
       float e = gas.measure_NH3();
       float f = gas.measure_NO2();
       float g = gas.measure_C3H8();
       float h = gas.measure_C4H10();
       float i = gas.measure_CH4();
       float j = gas.measure_C2H5OH();
       sprintf(eventinfo,"CO=%.2f|ppm",c);
       Publish(eventinfo);
       sprintf(eventinfo,"H2=%.2f|ppm",d);
       Publish(eventinfo);
       sprintf(eventinfo,"NH3=%.2f|ppm",e);
       Publish(eventinfo);
       sprintf(eventinfo,"NO2=%.2f|ppm",f);
       Publish(eventinfo);
       sprintf(eventinfo,"C3H8=%.2f|ppm",g);
       Publish(eventinfo);
       sprintf(eventinfo,"C4H10=%.2f|ppm",h);
       Publish(eventinfo);
       sprintf(eventinfo,"CH4=%.2f|ppm",i);
       Publish(eventinfo);
       sprintf(eventinfo,"C2H5OH=%.2f|ppm",j);
     Blynk.virtualWrite(V11, c);
     Blynk.virtualWrite(V12, d);
     Blynk.virtualWrite(V13, e);
     Blynk.virtualWrite(V14, f);
     Blynk.virtualWrite(V15, g);
     Blynk.virtualWrite(V16, h);
     Blynk.virtualWrite(V17, i);
     Blynk.virtualWrite(V18, j);
       
       
       
       }
       // Publush event
       void Publish(char* szEventInfo){
       Spark.publish("Gas Sensor", szEventInfo);
       }

      void InitializeMutichannelGasSensor(){
      unsigned char id;
      gas.begin();
      gas.powerOn();
 }

      void InitializeApplication(){
      Serial.begin(9600);
      pinMode(D7, OUTPUT);
 }

      void setup() {
      InitializeApplication();
      Serial.println("Initializing sensors...");
      InitializeMutichannelGasSensor();
      Blynk.begin(auth);
}  

      void loop() {
          
                                                        Blynk.run();
                                                        
      PublishMutichannelGasSensorInfo();
       delay(200);
}

#2

@cesar, I assume you are aware of the Particle.publish() rate limit of one per second?

NOTE 1: Currently, a device can publish at rate of about 1 event/sec, with bursts of up to 4 allowed in 1 second. Back to back burst of 4 messages will take 4 seconds to recover.

I also notice you have 2 #include "MutichannelGasSensor.h" lines and you use the deprecated form Spark.publish() instead of the correct Particle.publish() version.

Which firmware version are you using?


#4

The firmware version is 0.7.0
I corrected the fails that you have noticed. However, the device go offline and came online.
I tried several delays values, include no delay in void loop. The problem is the same…

Cesar


#5

This is the screen I see on the console.


#6

Where is the code of that MultiChannelGasSensor library?
It’s very likely that this lib has some blocking parts that keep the controller from servicing the cloud in a timely manner.
You can add SYSTEM_THREAD(ENABLED) to reduce the risk of that happening, but that’s only a workaround. Better would be to make the library non-blocking.

BTW, you don’t need #include <Wire.h>

There are also some unresolved issues with 0.7.0 around stability of connection, you could try 0.6.3.


#7

Look at your publish timestamps… 4 publishes in the same second. You should combine those into one publish so as not to exceed the publish rate limit as @peekay123 already mentioned. I’m not saying that’s the root of the problem, but it could be. It seems that there’s always a disconnect right after your publish sequence if you look at the console bar graph colors.


#8

Here is how I put multiple data points in a single Particle Publish event.


#9

Firstly thank you all for the suggestions. I’ll work with all of them. Great opportunity to learn.

ScruffR,
Libraries can be found at;
https://github.com/Seeed-Studio/Mutichannel_Gas_Sensor/archive/master.zip
The original ino code does not work with photon particle.

Ninjatill,
I’ve been using more than 4 publishes before with Photon and it always worked fine. However, I must take into account that some of the publishes did not come from sensor readings but calculations from the same readings. This can be significant, so that I will consider your suggestion and do some testing.

RWB
About The C-String I need to study a little

Thanks,
Cesar


#10

In that library are some goto START; ( :stuck_out_tongue_closed_eyes: ) instructions that may cause some connection issues.
You can add Particle.process() after the respective START labels to prevent that.

Which one - there are several examples?
For what reason, in what manner does it not work?


#11

Though you can certainly use more than 4 publishes, you must still adhere to the rate limits. If you add a 1s delay between each of them, there’s no problem. You can also burst 4, and then not send any for the following 4 seconds, and all is well too. As long as you play by the rules, you should be fine.