-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.cpp
More file actions
143 lines (125 loc) · 4.06 KB
/
main.cpp
File metadata and controls
143 lines (125 loc) · 4.06 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <Arduino.h>
#include <DHT.h>
#include <DHT_U.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <FirebaseArduino.h>
#include "creds.h"
// #define DONT_HAVE_SENSORS
#define MQ5PIN D6
#define DHTPIN D2
#define DHTTYPE DHT11
#define BAUDRATE 115200
struct readings {
bool gas; // Gas status: true if gas detected and false if no gas detected
float hum; // Humidity in Percent ( %)
float temp; // Temperature in Celsius (°C)
} readings;
DHT dht(DHTPIN, DHTTYPE);
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
// will store last time data was sent to Firebase
unsigned long previousMillis = 0;
const long interval = 2000;
void connectToWiFi(char const *ssid, char const *password);
void readSensors(struct readings *r);
void displaySensors(struct readings r);
void sendDataToFirebase(struct readings r);
void setup() {
pinMode(MQ5PIN, INPUT);
Serial.begin(BAUDRATE);
connectToWiFi(SSID, PASSWORD);
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
dht.begin();
}
void loop() {
// check to see if it's time to send data to Firebase; that is, if the difference
// between the current time and last time we sent data is bigger than
// the interval at which we want to send data.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time we sent data to Fireabase
previousMillis = currentMillis;
readSensors(&readings);
displaySensors(readings);
sendDataToFirebase(readings);
}
}
void connectToWiFi(char const *ssid, char const *password) {
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.print(ssid);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.disconnect();
//start connecting to WiFi
WiFi.begin(ssid, password);
//while client is not connected to WiFi keep loading
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("");
}
void readSensors(struct readings *r) {
#ifdef DONT_HAVE_SENSORS
readings.gas = !readings.gas;
readings.temp = random(0, 80);
readings.hum = random(0, 80);
#else
// Read Gas status
r->gas = digitalRead(MQ5PIN);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
r->hum = dht.readHumidity();
// Read temperature as Celsius (the default)
r->temp = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(r->hum) || isnan(r->temp)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
#endif
}
void displaySensors(struct readings r) {
if (r.gas == true) {
Serial.println("[INFO] Gas DETECTED!!!");
}
Serial.print("[INFO] Gas status: ");
Serial.println(r.gas);
Serial.print("[INFO] Humidity: ");
Serial.print(r.hum);
Serial.println("%");
Serial.print("[INFO] Temperature: ");
Serial.print(r.temp);
Serial.print("°C ");
}
void sendDataToFirebase(struct readings r) {
String gasStatusID = Firebase.pushInt("mq5/gas", r.gas);
if (Firebase.failed()) {
Serial.print("[ERROR] pushing mq5/gasStatus failed:");
Serial.println(Firebase.error());
return;
}
String humValueID = Firebase.pushFloat("dht11/humidity", r.hum);
if (Firebase.failed()) {
Serial.print("[ERROR] pushing /dht11/humidity failed:");
Serial.println(Firebase.error());
return;
}
String tempValueID = Firebase.pushFloat("dht11/temperature", r.temp);
if (Firebase.failed()) {
Serial.print("[ERROR] pushing /dht11/temperature failed:");
Serial.println(Firebase.error());
return;
}
}