-
Notifications
You must be signed in to change notification settings - Fork 0
/
XiaomiGatewayAqaraAlarm.ino
105 lines (94 loc) · 3.23 KB
/
XiaomiGatewayAqaraAlarm.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
/*
* Active an Alarm on Xiaomi Gateway
*
* Code for ESP8266 that :
* - Connect to a wifi or create an Access Point
* - Connect to a Xiaomi Gateway (with IP only)
* - Read all messages from this Xiaomi Gateway
* - Send order to change ring light color and intensity of the Xiaomi Gateway
* - Send order to change sound and volume of the Xiaomi Gateway
* (Send order need to encrypt the last token from heartbeat - with AES-128-CBC encryption)
*
* - Active an alarm with blinking light at the first time with receive a heartbeat from our Xiaomi Gateway
* - Active a blinking otherwise
*
* Developped by https://github.com/anttbd
*
* Encryptage code from official Xiaomi Gateway API https://forrestfung.gitbooks.io/lumi-gateway-local-api/content/
*
* Tested configuration:
* - Board : Node MCU ESP8266 / WEMOS D1 mini
* - Upload speed : 921600
* - CPU Frequency : 80 Hz
* - Flash Size : 4MB (FS:2MB, OTA:~1019KB)
* - SSL support : All SSL cipher (most compatible)
* - Xiaomi Gateway version : 1.1.2
*
* TODO:
* - Alarm ring when input gpio change state
* - More decode ack messages from Gateway and its children devices connected in zigbee
* - Possible web server to display stats and manage alarm (optional)
*
*/
#include "Declarations.h"
void setup() {
// Start serial at 115200 bauds
Serial.begin(115200);
// Waiting for the serial port to open
for (uint8_t t = 5; t > 0; t--) {
Serial.print(F("BOOT WAIT "));
Serial.print(t);
Serial.println(F("..."));
Serial.flush();
delay(1000);
}
initWifi(wifiMode, true);
initGatewayEncryptage(gateway_password);
initUdp();
}
void loop() {
if(toggleGatewayLight && gateway_isRinging == false){
colorBlink(gateway_sid, gateway_lightToggleDelay, gateway_lightColor);
}
if(useGatewayAlarm){
alarmRing(gateway_sid, gateway_alarmDuration, gateway_alarmSound, gateway_alarmSoundVol);
}
}
// toggle color on with custom color in format aarrggbb and off after a delay
void colorBlink(String sid, uint32_t delay, String color){//aarrggbb
if((millis() - gateway_lightTogglePreviousTime) > delay)
{
gateway_lightTogglePreviousTime = millis();
if(gateway_lightOn){
gateway_lightOn = false;
changeColor(sid, F("0"));
}else{
gateway_lightOn = true;
changeColor(sid, color);//aarrggbb
}
}
}
// ring alarm with custom sound and volume during spécific time, then stop for the same time and if ring 1 time, stop repetitions
void alarmRing(String sid, uint32_t duration, Sound sound, int vol){
if(gateway_isRinging == false && (millis() - gateway_alarmPreviousTime) > duration){
if(changeSoundAlarm(sid, sound, vol)){
gateway_isRinging = true;
gateway_alarmPreviousTime = millis();
}
}
if(gateway_isRinging){
colorBlink(sid, gateway_alarmLightToggleDelay, gateway_alarmLightColor);
}
if(gateway_isRinging && (millis() - gateway_alarmPreviousTime) > duration)
{
// reset values
gateway_isRinging = false;
gateway_alarmPreviousTime = millis();
changeSoundAlarm(sid, Sound::Off, 0);
gateway_lightOn = false;
changeColor(sid, F("0"));
if(gateway_alarmRing1time){
useGatewayAlarm = false;
}
}
}