
Here is my coding:
#include <CytronWiFiShield.h>
#include <CytronWiFiClient.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <ThingSpeak.h>
const char *ssid = "A6-L3-2";
const char *pass = "a6l3pass2";
ESP8266Client client;
unsigned long GasLeakageDataChannelNumber = 461523;
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
int pin9 = 9; //led
int buzzer = 6; //buzzer
// Analog pin 0 will be called 'sensor'
int sensor = A0;
// Set the initial sensorValue to 0
int sensorValue = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.begin(16, 2); //set up the LCD's number of columns and rows
lcd.clear();
lcd.print("GAS SENSOR");// print a message to the LCD
Serial.begin(9600); // Initialize serial communication at 9600 bits per second
pinMode(pin9, OUTPUT);
pinMode(pin9, LOW);
pinMode(buzzer, OUTPUT);
beep(50);
beep(200) ;
beep(50) ;
beep(100) ;
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
if (!wifi.begin(2, 3))
{
Serial.println(F("Error talking to shield"));
while (1);
}
Serial.println(F("Start wifi connection"));
if (!wifi.connectAP(ssid, pass))
{
Serial.println(F("Error connecting to WiFi"));
while (1);
}
Serial.print(F("Connected to ")); Serial.println(wifi.SSID());
Serial.println(F("IP address: "));
Serial.println(wifi.localIP());
ThingSpeak.begin(client);
delay(1000);
}
void loop() {
// put your main code here, to run repeatedly:
// Read the input on analog pin 0 (named 'sensor')
sensorValue = analogRead(sensor);
int outputvalue = map(sensorValue, 0, 1023, 0, 255);
lcd.setCursor(0, 1);
lcd.print("Value = ");
lcd.print(sensorValue);
delay(1000);
lcd.print(" ");
//Scroll
int outputMap2 = 0;
int outputBuzzer = 0;
if (sensorValue > 280) {
// Activate digital output pin 9 - the LED will light up
//Notice the value of the experiment. Minimum =70 , maximum= 110 for my mq2 device
outputMap2 = map(outputvalue, 70, 110, 0, 255); // for led,'map' func from 70-110 become 0-255 so that be value for pwm made fade with led.
//map อีกครั้ง 29-110 ไป 0-255 so that pwm ทำ fade กับ led
outputMap2 = constrain(outputMap2, 0, 255); //value for led , make 'map' func again,make a wider range of led or brightness of the led.
//ทำให้ เห็น ช่วงที่กว้างขึ้นของ แสง led หรือความสว่างของ led ให้ชัดเจนขึ้น
lcd.print("DANGER!!");
outputBuzzer = 254 - map(outputvalue, 70, 110, 50, 254); //value for buzzer
analogWrite(pin9, outputMap2);
beep(outputBuzzer);
mySerial.print(sensorValue);
}
else {
analogWrite(pin9, 0);
mySerial.print(sensorValue);
lcd.print("SAFE!!");
}
//for debug mq2 sensor value
Serial.print("sensorValue : ");
Serial.print(sensorValue, DEC);
Serial.print("\t mapValue : ");
Serial.print(outputvalue, DEC);
Serial.print("\t outputMap2 : ");
Serial.println(outputMap2, DEC);
delay(250);
}
void beep(unsigned char delayms) {
analogWrite(buzzer, 150); // Almost any value can be used except 0 and 255
delay(delayms); // wait for a delayms ms
analogWrite(buzzer, 0); // 0 turns it off
delay(delayms); // wait for a delayms ms
}
String GasValue = parseString(ThingSpeak.readStringField(GasValueChannelNumber, 1));
Serial(F("Gas value is"));
Serial.print(GasValue);
Serial.println();
delay(60000); // Note that the weather station only updates once a minute
}
//workaround for Cytron WiFi Shield leftover content when client closes
String parseString(String str)
{
int index = str.indexOf(",CLOSED");
if (index == -1) return str;
return str.substring(0, index - 1);
}