-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfujitsu_ac_homekit.ino
236 lines (195 loc) · 6.28 KB
/
fujitsu_ac_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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Apple HomeKit remote control for my old Fujitsu AC via infrared
*
* hadware list:
* controller: NodeMCU v2
* transistor: BC547
* ir led: TSAL6100
* temperature sensor: DHT11
*
*
*/
#include <Arduino.h>
#include <arduino_homekit_server.h>
#include <homekit/characteristics.h>
#include <IRremoteESP8266.h>
#include <ir_Fujitsu.h>
#include <DHT.h>
#include "wifi_info.h"
#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__);
const int IRPin = 4; //d2.
const int DHTPin = 5; //d1.
const int LEDpin = 2; //led
const int buttonPin = 0; //flashButton
extern "C" char serial[16];
extern "C" homekit_server_config_t accessory_config;
extern "C" homekit_characteristic_t currentHeatingCoolingState;
extern "C" homekit_characteristic_t targetHeatingCoolingState;
extern "C" homekit_characteristic_t currentTemperature;
extern "C" homekit_characteristic_t targetTemperature;
extern "C" homekit_characteristic_t temperatureDisplayUnit;
extern "C" homekit_characteristic_t currentRelativeHumidity;
static long long nextNotifyTime = 0;
static long long nextWifiCheckTime = 0;
static long long nextTempNotifyTime = 0;
IRFujitsuAC AC(IRPin);
DHT DHTSensor(DHTPin, DHT11);
void ACModeSetter(const homekit_value_t value)
{
if(currentHeatingCoolingState.value.uint8_value != value.uint8_value)
{
currentHeatingCoolingState.value.uint8_value = value.uint8_value;
homekit_characteristic_notify(¤tHeatingCoolingState, currentHeatingCoolingState.value);
}
if(targetHeatingCoolingState.value.uint8_value != value.uint8_value)
{
targetHeatingCoolingState.value.uint8_value = value.uint8_value;
homekit_characteristic_notify(&targetHeatingCoolingState, targetHeatingCoolingState.value);
}
updateac();
}
void ACTemperatureSetter(const homekit_value_t value)
{
if(targetTemperature.value.float_value != value.float_value)
{
targetTemperature.value.float_value = value.float_value;
homekit_characteristic_notify(&targetTemperature, targetTemperature.value);
updateac();
}
}
void temperatureDisplayUnitSetter(const homekit_value_t value)
{
if(temperatureDisplayUnit.value.uint8_value != value.uint8_value)
{
temperatureDisplayUnit.value.uint8_value = value.uint8_value;
homekit_characteristic_notify(&temperatureDisplayUnit, temperatureDisplayUnit.value);
}
}
void updateac()
{
const int targetTemp = round(targetTemperature.value.float_value);
switch (targetHeatingCoolingState.value.uint8_value)
{
case HOMEKIT_TARGET_HEATING_COOLING_STATE_OFF:
AC.off();
AC.send();
digitalWrite(LEDpin, HIGH);
break;
case HOMEKIT_TARGET_HEATING_COOLING_STATE_HEAT:
AC.on();
AC.setSwing(kFujitsuAcSwingOff);
AC.setMode(kFujitsuAcModeHeat);
AC.setFanSpeed(kFujitsuAcFanQuiet);//kFujitsuAcFanAuto
AC.setTemp(targetTemp);
AC.setCmd(kFujitsuAcCmdTurnOn);
AC.send();
digitalWrite(LEDpin, LOW);
break;
case HOMEKIT_TARGET_HEATING_COOLING_STATE_COOL:
AC.on();
AC.setSwing(kFujitsuAcSwingOff);
AC.setMode(kFujitsuAcModeCool);
AC.setFanSpeed(kFujitsuAcFanQuiet);//kFujitsuAcFanAuto
AC.setTemp(targetTemp);
AC.setCmd(kFujitsuAcCmdTurnOn);
AC.send();
digitalWrite(LEDpin, LOW);
break;
case HOMEKIT_TARGET_HEATING_COOLING_STATE_AUTO:
AC.on();
AC.setSwing(kFujitsuAcSwingOff);
AC.setMode(kFujitsuAcModeAuto);
AC.setFanSpeed(kFujitsuAcFanAuto);//kFujitsuAcFanQuiet
AC.setTemp(targetTemp);
AC.setCmd(kFujitsuAcCmdTurnOn);
AC.send();
digitalWrite(LEDpin, LOW);
break;
}
LOG_D("AC settings: %s", AC.toString().c_str());
}
void homekitTempNotify()
{
float newTemperature = DHTSensor.readTemperature() -1;
float newHumidity = DHTSensor.readHumidity();
if (!(newTemperature > 0 && newTemperature < 100))
newTemperature = 24.0;
if (!(newHumidity > 0 && newHumidity < 100))
newHumidity = 50.0;
if (currentTemperature.value.float_value != newTemperature)
{
currentTemperature.value.float_value = newTemperature;
homekit_characteristic_notify(¤tTemperature, currentTemperature.value);
}
if (currentRelativeHumidity.value.float_value != newHumidity)
{
currentRelativeHumidity.value.float_value = newHumidity;
homekit_characteristic_notify(¤tRelativeHumidity, currentRelativeHumidity.value);
}
}
void homekitSetup()
{
targetHeatingCoolingState.setter = ACModeSetter;
targetTemperature.setter = ACTemperatureSetter;
temperatureDisplayUnit.setter = temperatureDisplayUnitSetter;
arduino_homekit_setup(&accessory_config);
}
void homekitLoop()
{
arduino_homekit_loop();
const uint32_t timer = millis();
if (timer > nextNotifyTime)
{
nextNotifyTime = timer + 2 * 1000;
LOG_D(
"Free heap: %d, HomeKit clients: %d Uptime: %d s",
ESP.getFreeHeap(),
arduino_homekit_connected_clients_count(),
int(timer/1000)
);
}
if (timer > nextTempNotifyTime)
{
nextTempNotifyTime = timer + 25 * 1000;
homekitTempNotify();
LOG_D(
"Free heap: %d, HomeKit clients: %d Uptime: %d s",
ESP.getFreeHeap(),
arduino_homekit_connected_clients_count(),
int(timer/1000)
);
}
if (timer > nextWifiCheckTime)
{
nextWifiCheckTime = timer + 60 * 1000;
if(WiFi.status() != WL_CONNECTED)
{
delay(10000);
ESP.restart();
}
}
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW)
{
homekit_storage_reset();
ESP.restart();
}
}
void setup()
{
sprintf(serial, "SN%X\0", ESP.getChipId());
wifi_connect();
AC.begin();
DHTSensor.begin();
Serial.begin(115200);
pinMode(LEDpin, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(LEDpin, HIGH);
delay(1000);
homekitSetup();
}
void loop()
{
homekitLoop();
delay(10);
}