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);
}
@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.
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…
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.
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.
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.
In that library are some goto START; ( ) 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?
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.
ScruffR,
After add Particle.process() after goto START made enormous difference. Now the system go dow some times only, perhaps at a normal rate of Internet failure. I am still investigating…
I call the original code that is provided by Grove Multichannel Gas Sensor in:http://wiki.seeedstudio.com/Grove-Multichannel_Gas_Sensor/
The code in, require lib that never finish. For each lib required that I provide, other is requested…never finish
Moors7,
I am using only 4 publishes now. In fact, the difference is that; more than 4 is not published. However, this does not seem to affect the intermittent connection. This also doesn’t affect the Blynk mode.
If you add it after the goto START; line it won't actually be executed, as the code flow is sent back to START: before it actually reaches the following line.
So we have a problem:
By the wrong way, adding Particle.process(): after goto START: works
By the correct way i.e., adding Particle.process(): after START: does not work
This is really confusing
I am doing some thing wrong?