Skip to content

Commit 84caa53

Browse files
authored
Merge pull request Mixiaoxiao#62 from JayFiDev/master
WS2812 / Neopixel Example
2 parents c87d600 + 8cb7e45 commit 84caa53

File tree

3 files changed

+328
-0
lines changed

3 files changed

+328
-0
lines changed
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
/*
2+
* Example05_WS2812_Neopixel.ino
3+
*
4+
* Created on: 2020-10-01
5+
* Author: Juergen Fink
6+
* Thanks to all the other helpful people commenting here.
7+
*
8+
* This example allows to change brightness and color of a connected neopixel strip/matrix
9+
*
10+
* You should:
11+
* 1. read and use the Example01_TemperatureSensor with detailed comments
12+
* to know the basic concept and usage of this library before other examples。
13+
* 2. erase the full flash or call homekit_storage_reset() in setup()
14+
* to remove the previous HomeKit pairing storage and
15+
* enable the pairing with the new accessory of this new HomeKit example.
16+
*/
17+
18+
19+
#include <Arduino.h>
20+
#include <arduino_homekit_server.h>
21+
#include <Adafruit_NeoPixel.h>
22+
#include "wifi_info.h"
23+
24+
#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__);
25+
26+
#define NEOPIN D4
27+
#define NUMPIXELS 64
28+
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, NEOPIN, NEO_GRB + NEO_KHZ800);
29+
30+
bool received_sat = false;
31+
bool received_hue = false;
32+
33+
bool is_on = false;
34+
float current_brightness = 100.0;
35+
float current_sat = 0.0;
36+
float current_hue = 0.0;
37+
int rgb_colors[3];
38+
39+
void setup() {
40+
Serial.begin(115200);
41+
wifi_connect(); // in wifi_info.h
42+
43+
pixels.begin();
44+
for(int i = 0; i < NUMPIXELS; i++)
45+
{
46+
pixels.setPixelColor(i, 0, 0, 0);
47+
}
48+
pixels.show();
49+
delay(1000);
50+
rgb_colors[0] = 255;
51+
rgb_colors[1] = 255;
52+
rgb_colors[2] = 255;
53+
my_homekit_setup();
54+
}
55+
56+
void loop() {
57+
my_homekit_loop();
58+
delay(10);
59+
}
60+
61+
//==============================
62+
// HomeKit setup and loop
63+
//==============================
64+
65+
// access your HomeKit characteristics defined in my_accessory.c
66+
67+
extern "C" homekit_server_config_t accessory_config;
68+
extern "C" homekit_characteristic_t cha_on;
69+
extern "C" homekit_characteristic_t cha_bright;
70+
extern "C" homekit_characteristic_t cha_sat;
71+
extern "C" homekit_characteristic_t cha_hue;
72+
73+
static uint32_t next_heap_millis = 0;
74+
75+
void my_homekit_setup() {
76+
77+
cha_on.setter = set_on;
78+
cha_bright.setter = set_bright;
79+
cha_sat.setter = set_sat;
80+
cha_hue.setter = set_hue;
81+
82+
arduino_homekit_setup(&accessory_config);
83+
84+
}
85+
86+
void my_homekit_loop() {
87+
arduino_homekit_loop();
88+
const uint32_t t = millis();
89+
if (t > next_heap_millis) {
90+
// show heap info every 5 seconds
91+
next_heap_millis = t + 5 * 1000;
92+
LOG_D("Free heap: %d, HomeKit clients: %d",
93+
ESP.getFreeHeap(), arduino_homekit_connected_clients_count());
94+
95+
}
96+
}
97+
98+
void set_on(const homekit_value_t v) {
99+
bool on = v.bool_value;
100+
cha_on.value.bool_value = on; //sync the value
101+
102+
if(on) {
103+
is_on = true;
104+
Serial.println("On");
105+
} else {
106+
is_on = false;
107+
Serial.println("Off");
108+
}
109+
110+
updateColor();
111+
}
112+
113+
void set_hue(const homekit_value_t v) {
114+
Serial.println("set_hue");
115+
float hue = v.float_value;
116+
cha_hue.value.float_value = hue; //sync the value
117+
118+
current_hue = hue;
119+
received_hue = true;
120+
121+
updateColor();
122+
}
123+
124+
void set_sat(const homekit_value_t v) {
125+
Serial.println("set_sat");
126+
float sat = v.float_value;
127+
cha_sat.value.float_value = sat; //sync the value
128+
129+
current_sat = sat;
130+
received_sat = true;
131+
132+
updateColor();
133+
134+
}
135+
136+
void set_bright(const homekit_value_t v) {
137+
Serial.println("set_bright");
138+
int bright = v.int_value;
139+
cha_bright.value.int_value = bright; //sync the value
140+
141+
current_brightness = bright;
142+
143+
updateColor();
144+
}
145+
146+
void updateColor()
147+
{
148+
if(is_on)
149+
{
150+
151+
if(received_hue && received_sat)
152+
{
153+
HSV2RGB(current_hue, current_sat, current_brightness);
154+
received_hue = false;
155+
received_sat = false;
156+
}
157+
158+
int b = map(current_brightness,0, 100,75, 255);
159+
Serial.println(b);
160+
161+
pixels.setBrightness(b);
162+
for(int i = 0; i < NUMPIXELS; i++)
163+
{
164+
165+
pixels.setPixelColor(i, pixels.Color(rgb_colors[0],rgb_colors[1],rgb_colors[2]));
166+
167+
}
168+
pixels.show();
169+
170+
}
171+
else if(!is_on) //lamp - switch to off
172+
{
173+
Serial.println("is_on == false");
174+
pixels.setBrightness(0);
175+
for(int i = 0; i < NUMPIXELS; i++)
176+
{
177+
pixels.setPixelColor(i, pixels.Color(0,0,0));
178+
}
179+
pixels.show();
180+
}
181+
}
182+
183+
void HSV2RGB(float h,float s,float v) {
184+
185+
int i;
186+
float m, n, f;
187+
188+
s/=100;
189+
v/=100;
190+
191+
if(s==0){
192+
rgb_colors[0]=rgb_colors[1]=rgb_colors[2]=round(v*255);
193+
return;
194+
}
195+
196+
h/=60;
197+
i=floor(h);
198+
f=h-i;
199+
200+
if(!(i&1)){
201+
f=1-f;
202+
}
203+
204+
m=v*(1-s);
205+
n=v*(1-s*f);
206+
207+
switch (i) {
208+
209+
case 0: case 6:
210+
rgb_colors[0]=round(v*255);
211+
rgb_colors[1]=round(n*255);
212+
rgb_colors[2]=round(m*255);
213+
break;
214+
215+
case 1:
216+
rgb_colors[0]=round(n*255);
217+
rgb_colors[1]=round(v*255);
218+
rgb_colors[2]=round(m*255);
219+
break;
220+
221+
case 2:
222+
rgb_colors[0]=round(m*255);
223+
rgb_colors[1]=round(v*255);
224+
rgb_colors[2]=round(n*255);
225+
break;
226+
227+
case 3:
228+
rgb_colors[0]=round(m*255);
229+
rgb_colors[1]=round(n*255);
230+
rgb_colors[2]=round(v*255);
231+
break;
232+
233+
case 4:
234+
rgb_colors[0]=round(n*255);
235+
rgb_colors[1]=round(m*255);
236+
rgb_colors[2]=round(v*255);
237+
break;
238+
239+
case 5:
240+
rgb_colors[0]=round(v*255);
241+
rgb_colors[1]=round(m*255);
242+
rgb_colors[2]=round(n*255);
243+
break;
244+
}
245+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* my_accessory.c
3+
* Define the accessory in C language using the Macro in characteristics.h
4+
*
5+
* Created on: 2020-05-15
6+
* Author: Mixiaoxiao (Wang Bin)
7+
*/
8+
9+
#include <homekit/homekit.h>
10+
#include <homekit/characteristics.h>
11+
12+
void my_accessory_identify(homekit_value_t _value) {
13+
printf("accessory identify\n");
14+
}
15+
16+
homekit_characteristic_t cha_on = HOMEKIT_CHARACTERISTIC_(ON, false);
17+
homekit_characteristic_t cha_name = HOMEKIT_CHARACTERISTIC_(NAME, "ESP8266 Lamp");
18+
homekit_characteristic_t cha_bright = HOMEKIT_CHARACTERISTIC_(BRIGHTNESS, 50);
19+
homekit_characteristic_t cha_sat = HOMEKIT_CHARACTERISTIC_(SATURATION, (float) 0);
20+
homekit_characteristic_t cha_hue = HOMEKIT_CHARACTERISTIC_(HUE, (float) 180);
21+
22+
homekit_accessory_t *accessories[] = {
23+
HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_lightbulb, .services=(homekit_service_t*[]) {
24+
HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) {
25+
HOMEKIT_CHARACTERISTIC(NAME, "ESP8266 Lamp"),
26+
HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Arduino HomeKit"),
27+
HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "0123456"),
28+
HOMEKIT_CHARACTERISTIC(MODEL, "ESP8266/ESP32"),
29+
HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"),
30+
HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify),
31+
NULL
32+
}),
33+
HOMEKIT_SERVICE(LIGHTBULB, .primary=true, .characteristics=(homekit_characteristic_t*[]) {
34+
&cha_on,
35+
&cha_name,
36+
&cha_bright,
37+
&cha_sat,
38+
&cha_hue,
39+
NULL
40+
}),
41+
NULL
42+
}),
43+
NULL
44+
};
45+
46+
homekit_server_config_t accessory_config = {
47+
.accessories = accessories,
48+
.password = "111-11-111"
49+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* wifi_info.h
3+
*
4+
* Created on: 2020-05-15
5+
* Author: Mixiaoxiao (Wang Bin)
6+
*/
7+
8+
#ifndef WIFI_INFO_H_
9+
#define WIFI_INFO_H_
10+
11+
#if defined(ESP8266)
12+
#include <ESP8266WiFi.h>
13+
#elif defined(ESP32)
14+
#include <WiFi.h>
15+
#endif
16+
17+
const char *ssid = "your-ssid";
18+
const char *password = "your-password";
19+
20+
void wifi_connect() {
21+
WiFi.persistent(false);
22+
WiFi.mode(WIFI_STA);
23+
WiFi.setAutoReconnect(true);
24+
WiFi.begin(ssid, password);
25+
Serial.println("WiFi connecting...");
26+
while (!WiFi.isConnected()) {
27+
delay(100);
28+
Serial.print(".");
29+
}
30+
Serial.print("\n");
31+
Serial.printf("WiFi connected, IP: %s\n", WiFi.localIP().toString().c_str());
32+
}
33+
34+
#endif /* WIFI_INFO_H_ */

0 commit comments

Comments
 (0)