-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathESP8266WS2812FastLED.ino
141 lines (121 loc) · 4.43 KB
/
ESP8266WS2812FastLED.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
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <FastLED.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define DATA_PIN D6 //pin de salida D1
#define NUM_LEDS 425 //numero de pixeles a controlar
CRGB leds[NUM_LEDS];
const char* ssid = "SSID"; //nombre del wifi
const char* password = "PASSWORD"; //clave router
IPAddress ip(192, 168, 0, 200 );
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
unsigned int localPort = 65506; // local port to listen for UDP packets
const int PACKET_SIZE = 1357;
byte packetBuffer[PACKET_SIZE]; //buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP
WiFiUDP udp;
typedef struct
{
byte r;
byte g;
byte b;
} colorpin;
colorpin led;
int led_index = 0;
void WiFiEvent(WiFiEvent_t event)
{
switch (event)
{
case WIFI_EVENT_STAMODE_GOT_IP:
break;
case WIFI_EVENT_STAMODE_DISCONNECTED:
break;
}
}
void setup()
{
// Serial.begin(115200);
WiFi.disconnect(true);
delay(1000);
WiFi.onEvent(WiFiEvent);
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
// Serial.print("x");
}
// Serial.println("");
// Serial.println("WiFi connected");
// Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Serial.println("Starting UDP");
udp.begin(localPort);
// Serial.print("Local port: ");
// Serial.println(udp.localPort());
LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
LEDS.setBrightness(255);
}
void loop()
{
LEDS.setBrightness(255);
int cb = udp.parsePacket();
if (!cb)
{
// Serial.setDebugOutput(true);
}
else
{
// We've received a packet, read the data from it
udp.read(packetBuffer, PACKET_SIZE); // read the packet into the buffer
if (cb >= 6 && packetBuffer[0] == 0x9C)
{
// header identifier (packet start)
byte blocktype = packetBuffer[1]; // block type (0xDA)
unsigned int framelength = ((unsigned int)packetBuffer[2] << 8) | (unsigned int)packetBuffer[3]; // frame length (0x0069) = 105 leds
// Serial.print("Frame.");
// Serial.println(framelength); // chan/block
byte packagenum = packetBuffer[4]; // packet number 0-255 0x00 = no frame split (0x01)
byte numpackages = packetBuffer[5]; // total packets 1-255 (0x01)
if (blocktype == 0xDA)
{
// data frame ...
// Serial.println("command");
int packetindex;
if (cb >= framelength + 7 && packetBuffer[6 + framelength] == 0x36)
{
// header end (packet stop)
//Serial.println("s:");
int i = 0;
packetindex = 6;
if (packagenum == 1)
{
led_index = 0;
}
while (packetindex < (framelength + 6))
{
led.r = ((int)packetBuffer[packetindex]);
led.g = ((int)packetBuffer[packetindex + 1]);
led.b = ((int)packetBuffer[packetindex + 2]);
//LEDS.setBrightness(255);
leds[led_index] = CRGB(led.r, led.g, led.b);
led_index++;
Serial.println(led_index);
packetindex += 3;
}
}
// Serial.print(packagenum);
// Serial.print("/");
// Serial.println(numpackages);
}
if ((packagenum == numpackages) && (led_index == NUM_LEDS))
{
FastLED.show();
led_index == 0;
}
}
}
}