-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ledstrip_ESP8266.ino
263 lines (207 loc) · 5.29 KB
/
Ledstrip_ESP8266.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <SoftwareSerial.h>
#include <Arduino.h>
#include <arduino_homekit_server.h>
#include <math.h>
#include "wifi_info.h"
#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n"), ##__VA_ARGS__);
// varibles for light strip
bool received_sat = false;
bool received_hue = false;
bool is_on = false;
float current_brightness = 100;
float current_sat = 0.0;
float current_hue = 0.0;
float h;
float s;
float v;
//defining the counter for wifi
int timepassed = 0;
#define RED_PIN 5
#define BLUE_PIN 4
#define GREEN_PIN 0
//Runs once
void setup() {
Serial.begin(115200);
LOG_D("Set Lights to Off");
TurnOff();
LOG_D("Wifi starting up");
wifi_connect(); // in wifi_info.h
LOG_D("Homekit Starting");
my_homekit_setup();
}
//Runs always
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 accessory_config;
extern "C" homekit_characteristic_t cha_on;
extern "C" homekit_characteristic_t cha_bright;
extern "C" homekit_characteristic_t cha_sat;
extern "C" homekit_characteristic_t cha_hue;
static uint32_t next_heap_millis = 0;
//THIS IS HOMEKIT SOFTWARE (DO NOT CHANGE)
// Homekit setup
void my_homekit_setup() {
cha_on.setter = set_on;
cha_bright.setter = set_bright;
cha_sat.setter = set_sat;
cha_hue.setter = set_hue;
arduino_homekit_setup(&accessory_config);
}
//THIS IS HOMEKIT SOFTWARE (DO NOT CHANGE)
// Homekit Loop
void my_homekit_loop() {
arduino_homekit_loop();
const uint32_t t = millis();
if (t > next_heap_millis) {
timepassed++;
// 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());
}
//calling the counter and hosts logic
if (arduino_homekit_connected_clients_count() == 0 && timepassed > 10) {
delay(10000);
LOG_D("Wifi or Homekit not connected.");
LOG_D("Trying to reconnect to wifi.");
wifi_connect();
//Resetting the counter to give it some time for the wifi to start up
timepassed = 0;
}
}
//THIS IS HOMEKIT SOFTWARE (DO NOT CHANGE)
//Sets the lights to be on or off
void set_on(const homekit_value_t v) {
bool on = v.bool_value;
cha_on.value.bool_value = on; // sync the value
if (on) {
is_on = true;
Serial.println("Light On");
TurnOn();
} else {
is_on = false;
Serial.println("Light Off");
TurnOff();
}
}
//THIS IS HOMEKIT SOFTWARE (DO NOT CHANGE)
//sets and receives LED Brightness
void set_bright(const homekit_value_t v) {
Serial.println("set_bright:");
float bright = v.int_value;
Serial.println(bright);
cha_bright.value.int_value = bright; // sync the value
current_brightness = bright / 100;
updateColor();
}
//THIS IS HOMEKIT SOFTWARE (DO NOT CHANGE)
//sets and receives LED Sat
void set_sat(const homekit_value_t v) {
Serial.println("set_sat:");
float sat = v.float_value;
Serial.println(sat);
cha_sat.value.float_value = sat; // sync the value
current_sat = sat / 100;
received_sat = true;
updateColor();
}
//THIS IS HOMEKIT SOFTWARE (DO NOT CHANGE)
//sets and receives LED Hue
void set_hue(const homekit_value_t v) {
Serial.println("set_hue:");
float hue = v.float_value;
Serial.println(hue);
cha_hue.value.float_value = hue; // sync the value
current_hue = hue;
received_hue = true;
updateColor();
}
//MY CODE STARTS HERE//
//Turn on
void TurnOn() {
setLEDHSV(current_hue, current_sat, current_brightness);
}
//Turn off
void TurnOff() {
setLEDHSV(0, 0, 0);
}
//updates homekit and lights etc
void updateColor() {
if (is_on) {
if (received_hue && received_sat) {
Serial.println("Updating Colour");
TurnOn();
received_hue = false;
received_sat = false;
}
} else if (!is_on) //lamp - switch to off
{
Serial.println("is_on == false");
TurnOff();
}
}
//tells voltage to circut
void setLEDRGB(byte r, byte g, byte b) {
analogWrite(RED_PIN, r);
analogWrite(GREEN_PIN, g);
analogWrite(BLUE_PIN, b);
}
//creates that voltage
void setLEDHSV(float h, float s, float v) {
int i;
float f, p, q, t;
float r, g, b;
if (s == 0) {
// achromatic (grey)
r = g = b = v * 255;
setLEDRGB(r, g, b);
return;
} else {
h = h / 60; // sector 0 to 5
i = (int)trunc(h);
f = h - i; // factorial part of h
p = v * (1.0 - s);
q = v * (1.0 - s * f);
t = v * (1.0 - s * (1.0 - f));
switch (i) {
case 0:
r = v;
g = t;
b = p;
break;
case 1:
r = q;
g = v;
b = p;
break;
case 2:
r = p;
g = v;
b = t;
break;
case 3:
r = p;
g = q;
b = v;
break;
case 4:
r = t;
g = p;
b = v;
break;
default: // case 5:
r = v;
g = p;
b = q;
break;
}
setLEDRGB(r * 255, g * 255, b * 255);
return;
}
}