/*
MQUnifiedsensor Library - reading an MQ2
Demonstrates the use a MQ2 sensor.
Library originally added 01 may 2019
by Miguel A Califa, Yersson Carrillo, Ghiordy Contreras, Mario Rodriguez
Added example
modified 23 May 2019
by Miguel Califa
Updated library usage
modified 26 March 2020
by Miguel Califa
Wiring:
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
Please make sure arduino A0 pin represents the analog input configured on #define pin
This example code is in the public domain.
*/
#include <WiFi.h>
#include "ThingSpeak.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <MQUnifiedsensor.h>
//Include the library
/************************Hardware Related Macros************************************/
LiquidCrystal_I2C lcd(0x27, 16, 2);
const char* WIFI_NAME = "Wokwi-GUEST"; // WiFi SSID
const char* WIFI_PASSWORD = ""; // WiFI Password
const int myChannelNumber = 2891570; // ThingSpeak channel number
const char* myApiKey = "X01433VB0TH5RMD8"; // ThingSpeak API key
const char* server = "api.thingspeak.com"; // ThingSpeak server address
const int mq7Pin = 35; // GPIO35
const int LED_PIN = 13;
const int LED = 26;
const int buzzer = 25;
// Create an instance of the DHTesp library
// Create a WiFi client object
WiFiClient client;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(buzzer, OUTPUT);
// Initialize the LCD
lcd.init();
lcd.backlight();
// Print a message to the LCD.
lcd.setCursor(0, 0);
lcd.print("MQ7 Sensor");
delay(2000);
WiFi.begin(WIFI_NAME, WIFI_PASSWORD); // Connect to the WiFi network
while (WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Wifi not connected"); // Print a message if WiFi is not connected
}
Serial.println("Wifi connected !"); // Print a message if WiFi is connected
Serial.println("Local IP: " + String(WiFi.localIP())); // Print the local IP address
WiFi.mode(WIFI_STA); // Set the WiFi mode to station mode
ThingSpeak.begin(client); // Initialize the ThingSpeak library
}
void loop() {
int mq7Value = analogRead(mq7Pin);
mq7Value = map(mq7Value, 0, 4095, 0, 100);
Serial.println("MQ-7 Value: ");
Serial.println(mq7Value);
// Display sensor values on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MQ-7: ");
lcd.print(mq7Value);
lcd.setCursor(0, 1);
// Wait for a while before reading the values again
// Wait for a while before reading the values again
delay(1000);
// Read temperature and humidity from the DHT22 sensor
// Set the value of field 1 in the ThingSpeak channel to the temperature
// Set the value of field 2 in the ThingSpeak channel to the humidity
// Write the data to the ThingSpeak channel
ThingSpeak.setField(1, mq7Value);
ThingSpeak.setField(2, mq7Value);
if (mq7Value > 35) {
digitalWrite(buzzer, HIGH);
digitalWrite(LED_PIN, HIGH);
digitalWrite(LED, HIGH);
delay(1000);
} else {
digitalWrite(LED_PIN, LOW);
digitalWrite(LED, LOW);
}
int x = ThingSpeak.writeFields(myChannelNumber, myApiKey);
Serial.println("Gas Value: " + String(mq7Value));
if (x == 200) {
Serial.println("Data pushed successfully");
} else {
Serial.println("Push error: " + String(x));
}
Serial.println("---");
delay(10000);
}