-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGas.ino
109 lines (84 loc) · 2.64 KB
/
Gas.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <AsyncMqttClient.h>
#define WIFI_SSID "AndroidAP"
#define WIFI_PASSWORD "rnq20cck"
#define MQTT_HOST IPAddress(192, 168, 43, 224)
#define MQTT_PORT 1883
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
int Buzzer = D2; // used for ESP8266
int Gas_analog = A0; // used for ESP8266
int Gas_digital = D1; // used for ESP8266
AsyncMqttClient mqttClient;
Ticker mqttReconnectTimer;
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
Ticker wifiReconnectTimer;
void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
mqttClient.setWill("system/status", 1, true, "ende");
}
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
Serial.println("Connected to Wi-Fi.");
connectToMqtt();
}
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
Serial.println("Disconnected from Wi-Fi.");
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
wifiReconnectTimer.once(2, connectToWifi);
}
void onMqttConnect(bool sessionPresent) {
Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected()) {
mqttReconnectTimer.once(2, connectToMqtt);
}
}
void setup() {
Serial.begin(115200);
Serial.println();
pinMode(Buzzer, OUTPUT);
pinMode(Gas_digital, INPUT);
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
connectToWifi();
}
void handleGas() {
int gassensorAnalog = analogRead(Gas_analog);
int gassensorDigital = digitalRead(Gas_digital);
Serial.print("Gas Sensor: ");
Serial.print(gassensorAnalog);
Serial.print("\t");
Serial.print("Gas Class: ");
Serial.print(gassensorDigital);
Serial.print("\t");
Serial.print("\t");
if (gassensorAnalog > 100) {
Serial.println("Gas");
digitalWrite (Buzzer, HIGH) ; //send tone
delay(1000);
digitalWrite (Buzzer, LOW) ; //no tone
uint16_t packetIdPub2 = mqttClient.publish("presence_detection_infrared", 1, true, String("Gas erkannt").c_str());
Serial.println(packetIdPub2);
}
else {
Serial.println("No Gas");
}
delay(100);
}
void loop() {
handleGas();
}