Hi, I've been working on a gas detection system on my assignment.
I'm trying to use a MQ2 gas sensor to detect gases and in case they over a certain amount, there will be a red light on and buzzer sound.
Moreover, I want to use ESP8266 WiFi module so that I can use Thinkspeak to monitor the data. Code as below..
#include <MQ2.h>
#include <SoftwareSerial.h>
#define _baudrate 9600
#define _rxpin 4
#define _txpin 5
SoftwareSerial debug( _rxpin, _txpin ); // RX, TX
#define SSID "xxx"
#define PASS "12345678"
#define IP "184.106.153.149" // ThingSpeak IP Address: 184.106.153.149
// GET /update?key=[THINGSPEAK_KEY]&field1=[data 1]&filed2=[data 2]...;
String GET = "GET /update?key=HU8DEWEZUG7IL3QM";
/*****************************sensors****************************/
int Analog_Input = A0;
int lpg, co, smoke;
int greenled = 9;
int blueled = 10;
int redled = 11;
int buzzer = 8;
MQ2 mq2(Analog_Input);
void setup(){
Serial.begin( _baudrate );
debug.begin( _baudrate );
sendDebug("AT");
delay(5000);
Loding("sent AT");
connectWiFi();
Serial.begin(9600);
pinMode(greenled,OUTPUT);
pinMode(blueled,OUTPUT);
pinMode(redled,OUTPUT);
pinMode(buzzer,OUTPUT);
mq2.begin();
}
void loop(){
float* values= mq2.read(true); //set it false if you don't want to print the values in the Serial
//lpg = values[0];
lpg = mq2.readLPG();
//co = values[1];
co = mq2.readCO();
//smoke = values[2];
smoke = mq2.readSmoke();
if (lpg > 2000)
{
digitalWrite (redled, HIGH);
digitalWrite (greenled, LOW);
tone(buzzer, 1000, 200);
}
else
{
digitalWrite (redled,LOW);
digitalWrite (greenled, HIGH);
noTone(buzzer);
}
if (co > 70)
{
digitalWrite (redled, HIGH);
digitalWrite (greenled, LOW);
tone(buzzer, 1000, 200);
}
else
{
digitalWrite (redled,LOW);
digitalWrite (greenled, HIGH);
noTone(buzzer);
}
if (smoke > 400)
{
digitalWrite (redled, HIGH);
digitalWrite (greenled, LOW);
tone(buzzer, 1000, 200);
}
else
{
digitalWrite (redled,LOW);
digitalWrite (greenled, HIGH);
noTone(buzzer);
delay(1000);
}
/******************************************wifi*********************/
delay(5000);
SentOnCloud( String(lpg), String(co),String(smoke) );
}
boolean connectWiFi()
{
debug.println("AT+CWMODE=1");
Wifi_connect();
}
void SentOnCloud( String lpg, String co, String smoke )
{
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
sendDebug(cmd);
if( debug.find( "Error" ) )
{
Serial.print( "RECEIVED: Error\nExit1" );
return;
}
cmd = GET + "&field1=" + lpg + "&field2=" + co +"&field3=" + smoke +"\r\n";
debug.print( "AT+CIPSEND=" );
debug.println( cmd.length() );
if(debug.find( ">" ) )
{
Serial.print(">");
Serial.print(cmd);
debug.print(cmd);
}
else
{
debug.print( "AT+CIPCLOSE" );
}
if( debug.find("OK") )
{
Serial.println( "RECEIVED: OK" );
}
else
{
Serial.println( "RECEIVED: Error\nExit2" );
}
}
void Wifi_connect()
{
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
sendDebug(cmd);
Loding("Wifi_connect");
}
void Loding(String state){
for (int timeout=0 ; timeout<10 ; timeout++)
{
if(debug.find("OK"))
{
Serial.println("RECEIVED: OK");
break;
}
else if(timeout==9){
Serial.print( state );
Serial.println(" fail...\nExit2");
}
else
{
Serial.print("Wifi Loading...");
delay(500);
}
}
}
void sendDebug(String cmd)
{
Serial.print("SEND: ");
Serial.println(cmd);
debug.println(cmd);
}
I can successfully connect to wifi and thinkspeak, but the 3 field charts keep showing "0" even the sensor detected gas. Suppose it is a floating chart?
btw..the led and buzzer works..
Can anyone help me? THX!