-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathairqualitysensor.ino
More file actions
95 lines (81 loc) · 2.66 KB
/
airqualitysensor.ino
File metadata and controls
95 lines (81 loc) · 2.66 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
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <Wire.h>
#include "SparkFun_SCD30_Arduino_Library.h"
#include "env.h"
SCD30 airSensor;
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
void setup()
{
Serial.begin(115200);
Serial.println();
Wire.begin();
// attempt to connect to Wifi network:
Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setTrustAnchors(&cert);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.print("\nWiFi connected. IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Retrieving time…\n");
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
botSetup();
if (airSensor.begin() == false)
{
Serial.println("Air sensor not detected. Please check wiring. Freezing...");
bot.sendMessage(CHATID, "Air sensor not detected. Please check wiring.\n", "");
while (1);
}
Serial.print("Setup end\n");
}
void loop()
{
Serial.print("Begin Loop\n");
if ((airSensor.dataAvailable())) {
if (airSensor.getCO2() > 1000) {
Serial.print("CO2 too high\n");
String message = translateActualValue + String(airSensor.getCO2()) + "ppm.\n" + translateOpen;
bot.sendMessage(CHATID, message, "");
} else {
Serial.print("Air Quality is Ok\n");
}
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages)
{
Serial.println("User wants something!\n");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
Serial.print("Begin Sleep\n");
WiFi.disconnect();
ESP.deepSleep(time_between * 1000);
delay(100);
}
}
void handleNewMessages(int numNewMessages)
{
for (int i = 0; i < numNewMessages; i++) {
String text = bot.messages[i].text;
if (text == "/getStatus")
{
String message = "CO2: " + String(airSensor.getCO2()) + "ppm.\n" + "Temperature: " + airSensor.getTemperature() + "C\n" + "Humidity: " + airSensor.getHumidity() + "%";
bot.sendMessage(CHATID, message, "");
}
}
}
void botSetup()
{
const String commands = F("["
"{\"command\":\"start\", \"description\":\"Hello, from now on I will always inform you when it is time to open the window\"},"
"{\"command\":\"getStatus\",\"description\":\"Responds with the sensor data the next time it wakes up\"}" // no comma on last command
"]");
bot.setMyCommands(commands);
}