-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAirGradientHomeAssistant.ino
More file actions
197 lines (158 loc) · 4.47 KB
/
AirGradientHomeAssistant.ino
File metadata and controls
197 lines (158 loc) · 4.47 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Based on https://www.airgradient.com/diy/ and https://github.com/ChaseCares/AirGradient-InfluxDB
For build instructions please visit https://www.airgradient.com/diy/
Please install ESP8266 board manager (tested with version 3.0.0)
The following libraries installed:
"ESP8266 and ESP32 OLED driver for SSD1306 displays" by ThingPulse, Fabrice Weinberg (tested with Version 4.2.1)
"arduino-home-assistant" by Dawid Chyrzynski (tested with Version 1.3.0)
MIT License
---modifications---
The original example code was modified to add additional features.
Changes:
- removed captive portal, Wi-Fi credentials are now defined below
- added a calibration offset for temperature (as Jeff Gerling noted temperatures are usually a bit high)
the calibration value is subtracted from the reading
*/
#include <SSD1306Wire.h>
#include <AirGradient.h>
#include <Wire.h>
#include "DeviceConfig.hpp"
// Sanity check
#if ENABLE_MQTT && !ENABLE_WI_FI
#error Wi-Fi must be enabled for MQTT to work properly
#endif
#if !HAS_SHT && !HAS_PM2_5 && !HAS_CO2
#error Must have at least one sensor enabled
#endif
#if ENABLE_WI_FI
#include <ESP8266WiFi.h>
#endif
#if ENABLE_MQTT
#include <ArduinoHA.h>
HADevice device;
WiFiClient client;
HAMqtt mqtt(client, device);
#if HAS_PM2_5
HASensor MQTT_PM2_5("PM2_5");
#endif
#if HAS_CO2
HASensor MQTT_CO2("CO2");
#endif
#if HAS_SHT
HASensor MQTT_temperature("Temperature");
HASensor MQTT_humidity("Humidity");
#endif
#endif
AirGradient ag = AirGradient();
SSD1306Wire display(0x3c, SDA, SCL);
void setup() {
Serial.begin(9600);
display.init();
display.flipScreenVertically();
showTextRectangle(DEVICE_NAME, "Starting");
#if (HAS_PM2_5)
ag.PMS_Init();
#endif
#if (HAS_CO2)
ag.CO2_Init();
#endif
#if (HAS_SHT)
ag.TMP_RH_Init(0x44);
#endif
#if ENABLE_WI_FI
// Setup and wait for Wi-Fi
WiFi.begin(WI_FI_SSID, WI_FI_PASSWORD);
Serial.println("");
showTextRectangle("Setting", "up WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(WI_FI_SSID);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("MAC address: ");
Serial.println(WiFi.macAddress());
Serial.print("Hostname: ");
Serial.println(String(WiFi.hostname()));
#endif
Serial.println("");
#if ENABLE_MQTT
// Unique ID must be set
byte mac[WL_MAC_ADDR_LENGTH];
WiFi.macAddress(mac);
device.setUniqueId(mac, sizeof(mac));
device.setName(DEVICE_NAME);
#if HAS_PM2_5
MQTT_PM2_5.setUnitOfMeasurement("µg/m3");
MQTT_PM2_5.setDeviceClass("pm25");
MQTT_PM2_5.setIcon("mdi:air-filter");
MQTT_PM2_5.setName(DEVICE_NAME " Particulate");
#endif
#if HAS_CO2
MQTT_CO2.setUnitOfMeasurement("PPM");
MQTT_CO2.setDeviceClass("carbon_dioxide");
MQTT_CO2.setIcon("mdi:molecule-co2");
MQTT_CO2.setName(DEVICE_NAME " Carbon Dioxide");
#endif
#if HAS_SHT
MQTT_temperature.setUnitOfMeasurement("°C");
MQTT_temperature.setDeviceClass("temperature");
MQTT_temperature.setIcon("mdi:thermometer");
MQTT_temperature.setName(DEVICE_NAME " Temperature");
MQTT_humidity.setUnitOfMeasurement("%");
MQTT_humidity.setDeviceClass("humidity");
MQTT_humidity.setIcon("mdi:water-percent");
MQTT_humidity.setName(DEVICE_NAME " Humidity");
#endif
if (MQTT_AUTHENTICATION) {
mqtt.begin(MQTT_ADDR, MQTT_USERNAME, MQTT_PASSWORD);
} else {
mqtt.begin(MQTT_ADDR);
}
#endif
}
void loop() {
#if HAS_PM2_5
int PM2 = ag.getPM2_Raw();
showTextRectangle("PM2",String(PM2));
delay(DISPLAY_INTERVAL * 1000);
#endif
#if HAS_CO2
int CO2 = ag.getCO2_Raw();
showTextRectangle("CO2",String(CO2));
delay(DISPLAY_INTERVAL * 1000);
#endif
#if HAS_SHT
TMP_RH result = ag.periodicFetchData();
float caliTemp = (result.t - TEMP_OFFSET);
showTextRectangle(String(caliTemp)+"°C",String(result.rh)+"%");
delay(DISPLAY_INTERVAL * 1000);
#endif
#if ENABLE_MQTT
mqtt.loop();
#if HAS_PM2_5
MQTT_PM2_5.setValue(PM2);
#endif
#if HAS_CO2
MQTT_CO2.setValue(CO2);
#endif
#if HAS_SHT
MQTT_temperature.setValue(caliTemp);
MQTT_humidity.setValue(result.rh);
#endif
#endif
}
void showTextRectangle(String line1, String line2) {
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(32, 16, line1);
display.drawString(32, 36, line2);
display.display();
}
void showTextRectangle(const char *line1, const char *line2) {
showTextRectangle(String(line1), String(line2));
}