-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathesp8266-vindriktning-particle-sensor-homekit.ino
131 lines (113 loc) · 3.54 KB
/
esp8266-vindriktning-particle-sensor-homekit.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <Arduino.h>
#include <arduino_homekit_server.h>
#include <ArduinoOTA.h>
#include <DNSServer.h>
#include <WiFiManager.h>
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include "SerialCom.h"
#include "Types.h"
#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__);
particleSensorState_t state;
WiFiManager wifiManager;
WiFiClient wifiClient;
char identifier[24];
void setup() {
Serial.begin(115200);
my_homekit_setup();
}
void loop() {
my_homekit_loop();
delay(10);
}
//==============================
// Homekit setup and loop
//==============================
// access your homekit characteristics defined in my_accessory.c
extern "C" homekit_server_config_t config;
extern "C" homekit_characteristic_t cha_pm25_density;
extern "C" homekit_characteristic_t cha_air_quality;
static uint32_t next_heap_millis = 0;
static uint32_t next_report_millis = 0;
void my_homekit_setup() {
arduino_homekit_setup(&config);
Serial.begin(115200);
SerialCom::setup();
delay(3000);
snprintf(identifier, sizeof(identifier), "VINDRIKTNING-%X", ESP.getChipId());
WiFi.hostname(identifier);
setupWifi();
setupOTA();
}
void setupOTA() {
ArduinoOTA.onStart([]() { Serial.println("Start"); });
ArduinoOTA.onEnd([]() { Serial.println("\nEnd"); });
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.setHostname(identifier);
// This is less of a security measure and more a accidential flash prevention
ArduinoOTA.setPassword(identifier);
ArduinoOTA.begin();
}
void my_homekit_loop() {
arduino_homekit_loop();
ArduinoOTA.handle();
SerialCom::handleUart(state);
const uint32_t t = millis();
if (t > next_report_millis) {
// report sensor values every 10 seconds
next_report_millis = t + 10 * 1000;
if (state.valid) {
my_homekit_report();
}
}
if (t > next_heap_millis) {
// show heap info every 5 seconds
next_heap_millis = t + 5 * 1000;
LOG_D("Free heap: %d, HomeKit clients: %d",
ESP.getFreeHeap(), arduino_homekit_connected_clients_count());
}
}
void setupWifi() {
wifiManager.setDebugOutput(false);
WiFi.hostname(identifier);
wifiManager.autoConnect(identifier);
}
void resetWifiSettingsAndReboot() {
wifiManager.resetSettings();
delay(3000);
ESP.restart();
}
void my_homekit_report() {
cha_pm25_density.value.float_value = state.avgPM25;
int air_quality_val = 0;
if (state.avgPM25 >= 150){
air_quality_val = 5;
} else if (state.avgPM25 >= 56){
air_quality_val = 4;
} else if (state.avgPM25 >= 36){
air_quality_val = 3;
} else if (state.avgPM25 >= 13){
air_quality_val = 2;
} else if (state.avgPM25 <= 12){
air_quality_val = 1;
}
cha_air_quality.value.int_value = air_quality_val;
homekit_characteristic_notify(&cha_pm25_density, cha_pm25_density.value);
homekit_characteristic_notify(&cha_air_quality, cha_air_quality.value);
}