Created
May 7, 2020 11:05
-
-
Save suadanwar/9e076c85895ecf9a51b3bd996027fcec to your computer and use it in GitHub Desktop.
This sample code is for MQ2 Gas Detector Using ESP32 and Blynk App.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define BLYNK_PRINT Serial | |
#include <WiFi.h> | |
#include <WiFiClient.h> | |
#include <BlynkSimpleEsp32.h> | |
BlynkTimer timer; | |
// You should get Auth Token in the Blynk App. | |
// Go to the Project Settings (nut icon). | |
char auth[] = "YourAuthToken"; | |
// Your WiFi credentials. | |
// Set password to "" for open networks. | |
char ssid[] = "YourNetworkName"; | |
char pass[] = "YourPassword"; | |
#define MQ2 34 | |
#define GREEN 16 | |
#define RED 17 | |
int sensorValue = 0; | |
boolean state = false; | |
void setup() | |
{ | |
// Debug console | |
Serial.begin(115200); | |
Blynk.begin(auth, ssid, pass); | |
pinMode(MQ2, INPUT); | |
pinMode(GREEN, OUTPUT); | |
pinMode(RED, OUTPUT); | |
timer.setInterval(1000L, sendUptime); | |
} | |
void sendUptime() | |
{ | |
sensorValue = analogRead(MQ2); | |
Blynk.virtualWrite(V1, sensorValue); | |
Serial.println(sensorValue); | |
if (sensorValue > 600) | |
{ | |
Blynk.notify("Gas Detected!"); | |
digitalWrite(GREEN, LOW); | |
digitalWrite(RED, HIGH); | |
} | |
else | |
{ | |
digitalWrite(GREEN, HIGH); | |
digitalWrite(RED, LOW); | |
} | |
} | |
void loop() | |
{ | |
Blynk.run(); | |
timer.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment