2016-10-12 23:50:00 +02:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <WebSocketsServer.h>
|
|
|
|
#include <Hash.h>
|
|
|
|
#include <WiFiUdp.h>
|
2016-12-31 06:31:27 +01:00
|
|
|
#include "ws2812_i2s.h"
|
2016-10-12 23:50:00 +02:00
|
|
|
|
2017-01-05 09:02:43 +01:00
|
|
|
// Set to the number of LEDs in your LED strip
|
2016-12-31 06:31:27 +01:00
|
|
|
#define NUM_LEDS 60
|
|
|
|
// Maximum number of packets to hold in the buffer. Don't change this.
|
2016-10-12 23:50:00 +02:00
|
|
|
#define BUFFER_LEN 1024
|
2017-01-05 09:02:43 +01:00
|
|
|
// Toggles FPS output (1 = print FPS over serial, 0 = disable output)
|
2016-12-30 01:40:51 +01:00
|
|
|
#define PRINT_FPS 1
|
2016-10-12 23:50:00 +02:00
|
|
|
|
|
|
|
// Wifi and socket settings
|
2016-12-28 05:09:06 +01:00
|
|
|
const char* ssid = "YOUR_WIFI_SSID";
|
|
|
|
const char* password = "YOUR_WIFI_PASSWORD";
|
2016-10-12 23:50:00 +02:00
|
|
|
unsigned int localPort = 7777;
|
|
|
|
char packetBuffer[BUFFER_LEN];
|
|
|
|
|
|
|
|
// LED strip
|
|
|
|
static WS2812 ledstrip;
|
|
|
|
static Pixel_t pixels[NUM_LEDS];
|
|
|
|
WiFiUDP port;
|
|
|
|
|
2016-10-23 06:55:22 +02:00
|
|
|
// Network information
|
2016-12-28 05:09:06 +01:00
|
|
|
// IP must match the IP in config.py
|
|
|
|
IPAddress ip(192, 168, 0, 150);
|
|
|
|
// Set gateway to your router's gateway
|
|
|
|
IPAddress gateway(192, 168, 0, 1);
|
2016-10-23 06:55:22 +02:00
|
|
|
IPAddress subnet(255, 255, 255, 0);
|
|
|
|
|
2016-10-12 23:50:00 +02:00
|
|
|
void setup() {
|
|
|
|
Serial.begin(115200);
|
2016-10-23 06:55:22 +02:00
|
|
|
WiFi.config(ip, gateway, subnet);
|
2016-10-12 23:50:00 +02:00
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
Serial.println("");
|
|
|
|
// Connect to wifi and print the IP address over serial
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
|
|
delay(500);
|
|
|
|
Serial.print(".");
|
|
|
|
}
|
|
|
|
Serial.println("");
|
|
|
|
Serial.print("Connected to ");
|
|
|
|
Serial.println(ssid);
|
|
|
|
Serial.print("IP address: ");
|
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
port.begin(localPort);
|
|
|
|
ledstrip.init(NUM_LEDS);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t N = 0;
|
2017-01-05 09:02:43 +01:00
|
|
|
#if PRINT_FPS
|
|
|
|
uint16_t fpsCounter = 0;
|
|
|
|
uint32_t secondTimer = 0;
|
2016-12-30 23:28:32 +01:00
|
|
|
#endif
|
|
|
|
|
2016-10-12 23:50:00 +02:00
|
|
|
void loop() {
|
|
|
|
// Read data over socket
|
|
|
|
int packetSize = port.parsePacket();
|
|
|
|
// If packets have been received, interpret the command
|
|
|
|
if (packetSize) {
|
2017-01-05 09:02:43 +01:00
|
|
|
int len = port.read(packetBuffer, BUFFER_LEN);
|
|
|
|
for(int i = 0; i < len; i+=4) {
|
2016-10-12 23:50:00 +02:00
|
|
|
packetBuffer[len] = 0;
|
|
|
|
N = packetBuffer[i];
|
|
|
|
pixels[N].R = (uint8_t)packetBuffer[i+1];
|
|
|
|
pixels[N].G = (uint8_t)packetBuffer[i+2];
|
|
|
|
pixels[N].B = (uint8_t)packetBuffer[i+3];
|
2016-12-25 04:37:43 +01:00
|
|
|
}
|
2017-01-05 09:02:43 +01:00
|
|
|
ledstrip.show(pixels);
|
|
|
|
#if PRINT_FPS
|
|
|
|
fpsCounter++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#if PRINT_FPS
|
|
|
|
if (millis() - secondTimer >= 1000U) {
|
|
|
|
secondTimer = millis();
|
|
|
|
Serial.printf("FPS: %d\n", fpsCounter);
|
|
|
|
fpsCounter = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2016-10-23 06:55:22 +02:00
|
|
|
}
|