MQ-2 slow responses | Arduino | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
MQ-2 slow responses
No permission to create posts
October 27, 2015
4:21 pm
Avatar
BSB

Silver
Members
Forum Posts: 19
Member Since:
October 10, 2015
sp_UserOfflineSmall Offline

Looking for a little help if I may.

I have a couple of Arduinos running on Thingspeak but I want to try and speed up one of them.
Managed to graft some MQ-2 code into the basic Thingspeak sketch and it is working, but its very slow.

https://thingspeak.com/channels/62674

Tried two versions. One with all comments and serial print statements removed apart from the few that are really needed for debug. and one that contains items from the original MQ source, but both are the same.

Managed to reduce some delays and timers but I suspect I may be getting close to corrupting the outputs to Thingspeak.
Thinking maybe I might have grafted a section of code into the wrong spot and its doing too many readings on the sensor before sending to Thingspeak ?
I understand there is a 15/16 second delay between readings but currently it is way more than that and I think some brief readings will probably get missed.
Below is the sketch I currently use for the MQ-2. Not sure how much the forum munges code so gonna try it as is.
Forgive me if I get this wrong

--------------MQ-2 Sketch below-----------------

/*
Arduino --> ThingSpeak Channel via Ethernet

The ThingSpeak Client sketch is designed for the Arduino and Ethernet.
This sketch updates a channel feed with an analog input reading via the
ThingSpeak API (https://thingspeak.com/docs)
using HTTP POST. The Arduino uses DHCP and DNS for a simpler network setup.
The sketch also includes a Watchdog / Reset function to make sure the
Arduino stays connected and/or regains connectivity after a network outage.
Use the Serial Monitor on the Arduino IDE to see verbose network feedback
and ThingSpeak connectivity status.

Getting Started with ThingSpeak:

* Sign Up for New User Account - https://thingspeak.com/users/new
* Create a new Channel by selecting Channels and then Create New Channel
* Enter the Write API Key in this sketch under "ThingSpeak Settings"

Arduino Requirements:

* Arduino with Ethernet Shield or Arduino Ethernet
* Arduino 1.0+ IDE

Network Requirements:
* Ethernet port on Router
* DHCP enabled on Router
* Unique MAC Address for Arduino

Created: October 17, 2011 by Hans Scharler (http://www.nothans.com)

Additional Credits:
Example sketches from Arduino team, Ethernet by Adrian McEwen
MQ2 grafted from https://github.com/iot-playground/Arduino/blob/master/external_libraries/MySensors/examples/AirQualitySensor/AirQualitySensor.ino
By R. Humpleby (2015)

// MQ2 ADDITIONS START
/************************Hardware Related Macros************************************/
#define MQ_PIN (0) //define which analog input channel you are going to use
#define RL_VALUE (5) //define the load resistance on the board, in kilo ohms
#define RO_CLEAN_AIR_FACTOR (9.83) //RO_CLEAR_AIR_FACTOR=(Sensor resistance in clean air)/RO,
//which is derived from the chart in datasheet
/***********************Software Related Macros************************************/
#define CALIBARAION_SAMPLE_TIMES (40) // was 50 //define how many samples you are going to take in the calibration phase
#define CALIBRATION_SAMPLE_INTERVAL (400) // was 500 //define the time interal(in milisecond) between each samples in the
//cablibration phase
#define READ_SAMPLE_INTERVAL (25) // was 50 //define how many samples you are going to take in normal operation
#define READ_SAMPLE_TIMES (5) //define the time interal(in milisecond) between each samples in
//normal operation
/**********************Application Related Macros**********************************/
#define GAS_LPG (0)
#define GAS_CO (1)
#define GAS_SMOKE (2)
/*****************************Globals***********************************************/
float LPGCurve[3] = {2.3,0.21,-0.47}; //two points are taken from the curve.
//with these two points, a line is formed which is "approximately equivalent"
//to the original curve.
//data format:{ x, y, slope}; point1: (lg200, 0.21), point2: (lg10000, -0.59)
float COCurve[3] = {2.3,0.72,-0.34}; //two points are taken from the curve.
//with these two points, a line is formed which is "approximately equivalent"
//to the original curve.
//data format:{ x, y, slope}; point1: (lg200, 0.72), point2: (lg10000, 0.15)
float SmokeCurve[3] ={2.3,0.53,-0.44}; //two points are taken from the curve.
//with these two points, a line is formed which is "approximately equivalent"
//to the original curve.
//data format:{ x, y, slope}; point1: (lg200, 0.53), point2: (lg10000, -0.22)
float Ro = 10; //Ro is initialized to 10 kilo ohms
float LPG = 0;
float CO = 0;
float SMOKE = 0;
// MQ2 ADDITIONS END
#include
#include
// Local Network Settings
byte mac[] = { 0xDA, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "xxxxxxxxxxxxxxxxxxxxxx";
const int updateThingSpeakInterval = 15 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
float AV = 0;
// Initialize Arduino Ethernet Client
EthernetClient client;
void setup()
{
// Start Serial for debugging on the Serial Monitor
Serial.begin(9600);
// Start Ethernet on Arduino
startEthernet();
}
void loop()
{
// Read value from Analog Input Pin 1
AV = analogRead(A1);
AV = AV / 100; // get true decimal value
String analogValue0 = String((AV), DEC);
// String analogValue0 = String(analogRead(A1), DEC); // original code
// MQ2 ADDITIONS START
// Serial.print("LPG:");
// Serial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_LPG) );
char lpg2_buffer[10];
float lpg2 = (MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_LPG) );
String LPGONE=dtostrf(lpg2,0,5,lpg2_buffer);
// Serial.print( "ppm" );
// Serial.print(" ");
// Serial.print("CO:");
// Serial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_CO) );
char co_buffer[10];
float co = (MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_CO) );
String COTWO=dtostrf(co,0,5,co_buffer);
// CO = (MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_CO) );
// String CO2 = String(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_CO), DEC);
// Serial.print( "ppm" );
// Serial.print(" ");
// Serial.print("SMOKE:");
// Serial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_SMOKE) );
char smoke_buffer[10];
float smoke = (MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_SMOKE) );
String SMOKEY=dtostrf(smoke,0,5,smoke_buffer);
// SMOKE = (MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_SMOKE) );
// String SMOKE3 = String(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_SMOKE), DEC);
// Serial.print( "ppm" );
// Serial.print("
");
// delay(200); // Original code not req. here
// MQ2 ADDITIONS END
// Print Update Response to Serial Monitor
if (client.available())
{
char c = client.read();
Serial.print(c);
}
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println("...disconnected");
Serial.println();
client.stop();
}
// Update ThingSpeak
if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
{
updateThingSpeak("field1="+analogValue0+"&field2="+LPGONE+"&field3="+COTWO+"&field4="+SMOKEY);
}
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 ) {startEthernet();}
lastConnected = client.connected();
}
void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
client.print("POST /update HTTP/1.1
");
client.print("Host: api.thingspeak.com
");
client.print("Connection: close
");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"
");
client.print("Content-Type: application/x-www-form-urlencoded
");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("
");
client.print(tsData);
lastConnectionTime = millis();
if (client.connected())
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();
failedCounter = 0;
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();
lastConnectionTime = millis();
}
}
void startEthernet()
{
client.stop();
Serial.println("Connecting Arduino to network...");
Serial.println();
delay(900); // was 1000
// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
else
{
Serial.println("Arduino connected to network using DHCP");
Serial.println();
}
delay(900); // was 1000
}
// MQ2 ADDITIONS START
/****************** MQResistanceCalculation ****************************************
Input: raw_adc - raw value read from adc, which represents the voltage
Output: the calculated sensor resistance
Remarks: The sensor and the load resistor forms a voltage divider. Given the voltage
across the load resistor and its resistance, the resistance of the sensor
could be derived.
************************************************************************************/
float MQResistanceCalculation(int raw_adc)
{
return ( ((float)RL_VALUE*(1023-raw_adc)/raw_adc));
}
/***************************** MQCalibration ****************************************
Input: mq_pin - analog channel
Output: Ro of the sensor
Remarks: This function assumes that the sensor is in clean air. It use
MQResistanceCalculation to calculates the sensor resistance in clean air
and then divides it with RO_CLEAN_AIR_FACTOR. RO_CLEAN_AIR_FACTOR is about
10, which differs slightly between different sensors.
************************************************************************************/
float MQCalibration(int mq_pin)
{
int i;
float val=0;
for (i=0;i<CALIBARAION_SAMPLE_TIMES;i++) { //take multiple samples
val += MQResistanceCalculation(analogRead(mq_pin));
delay(CALIBRATION_SAMPLE_INTERVAL);
}
val = val/CALIBARAION_SAMPLE_TIMES; //calculate the average value
val = val/RO_CLEAN_AIR_FACTOR; //divided by RO_CLEAN_AIR_FACTOR yields the Ro
//according to the chart in the datasheet
return val;
}
/***************************** MQRead *********************************************
Input: mq_pin - analog channel
Output: Rs of the sensor
Remarks: This function use MQResistanceCalculation to caculate the sensor resistenc (Rs).
The Rs changes as the sensor is in the different consentration of the target
gas. The sample times and the time interval between samples could be configured
by changing the definition of the macros.
************************************************************************************/
float MQRead(int mq_pin)
{
int i;
float rs=0;
for (i=0;i<READ_SAMPLE_TIMES;i++) {
rs += MQResistanceCalculation(analogRead(mq_pin));
delay(READ_SAMPLE_INTERVAL);
}
rs = rs/READ_SAMPLE_TIMES;
return rs;
}
/***************************** MQGetGasPercentage **********************************
Input: rs_ro_ratio - Rs divided by Ro
gas_id - target gas type
Output: ppm of the target gas
Remarks: This function passes different curves to the MQGetPercentage function which
calculates the ppm (parts per million) of the target gas.
************************************************************************************/
int MQGetGasPercentage(float rs_ro_ratio, int gas_id)
{
if ( gas_id == GAS_LPG ) {
return MQGetPercentage(rs_ro_ratio,LPGCurve);
} else if ( gas_id == GAS_CO ) {
return MQGetPercentage(rs_ro_ratio,COCurve);
} else if ( gas_id == GAS_SMOKE ) {
return MQGetPercentage(rs_ro_ratio,SmokeCurve);
}
return 0;
}
/***************************** MQGetPercentage **********************************
Input: rs_ro_ratio - Rs divided by Ro
pcurve - pointer to the curve of the target gas
Output: ppm of the target gas
Remarks: By using the slope and a point of the line. The x(logarithmic value of ppm)
of the line could be derived if y(rs_ro_ratio) is provided. As it is a
logarithmic coordinate, power of 10 is used to convert the result to non-logarithmic
value.
************************************************************************************/
int MQGetPercentage(float rs_ro_ratio, float *pcurve)
{
return (pow(10,( ((log(rs_ro_ratio)-pcurve[1])/pcurve[2]) + pcurve[0])));
}
// MQ2 ADDITIONS END

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
20 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

Team_mafia, icXu, emorphistechno, VTVMART, manimozhi, adamhebrew

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457