-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.ino
87 lines (56 loc) · 1.88 KB
/
Code.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "MQ135.h"
#include <WiFi.h>
#include <PubSubClient.h>
#define sensor 32 //pin for analog input
#define buzzer 12 //pin for buzzer
const char* ssid = "Network_SSID";
const char* password = "Network_Password";
const char* mqttServer = "IP_Address";
const int mqttPort = 1883;
const char* mqttUser = "MQQT_Username";
const char* mqttPassword = "MQQT_Password";
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
pinMode(sensor,INPUT); //MQ135 analog feed set for input
//pinMode(digTrigger,INPUT); //MQ135 digital feed set for input
pinMode(buzzer,OUTPUT); //led set for output
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
client.setServer(mqttServer, mqttPort);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
}
void loop() {
client.loop();
MQ135 gasSensor = MQ135(sensor);
int air_quality = gasSensor.getPPM();
Serial.print("Air Quality: ");
Serial.print(air_quality);
Serial.println(" PPM");
Serial.println();
char mqtt_payload[30] = "";
snprintf (mqtt_payload,30, "%1d", air_quality);
client.publish("esp/test", mqtt_payload);
if (air_quality>1000){
digitalWrite(buzzer,LOW);
}
else {
digitalWrite(buzzer,HIGH);
}
Serial.println("Waiting...");
delay(2000);
}