I am using a ESP8266 with a own modified version of the following
https://github.com/cnlohr/ws2812esp8266
This is not arduino code but with the own SDK
I don´t know what you want to do with a ws2812 ledstrip.
I use it with static colors.
At the moment I send a string of codes with UDP to the ESP8266, this string is also stored in flash making it possible to power on and off the ESP8266 and not losing the configuration.
The string I send is an array of max 64 times the following:
char color R(red),G(green),B(Blue),W(White),U(Off),A(On) White is RGB same level (char uses 2 bytes)
int16 startpos (lednumber to start this command)
int16 endpos (lednumber to end this command)
uint8 startval (starting value of the color specified at color)
int8 change (value to increment or decrement the value for the next led)
for example if I send the following string RR 10 15 0 10 will have the following result:
10th LED red value 0
11th LED red value 10
12th LED red value 20
13th LED red value 30
14th LED red value 40
15th LED red value 50
I would like to implement this with espeasy using a webpage to create the configuration, but I also need a place to store the configuration (512 bytes)
chrismelba on github.com is using a ws2812 led strip with arduino code for a clock
below is the changed code
#include "mem.h"
#include "c_types.h"
#include "user_interface.h"
#include "ets_sys.h"
#include "driver/uart.h"
#include "osapi.h"
#include "espconn.h"
#include "mystuff.h"
#include "spi_flash.h"
#define PORT 7777
#define SERVER_TIMEOUT 1000
#define MAX_CONNS 5
#define MAX_FRAME 2000
#define procTaskPrio 0
#define procTaskQueueLen 1
static volatile os_timer_t some_timer;
static struct espconn *pUdpServer;
typedef struct {
char color;
int16_t start_pos;
int16_t end_pos;
uint8_t start_val;
int8_t change_val;
} ledstrip_type;
typedef struct {
ledstrip_type L[63];
} led_type;
led_type led;
char outbuffer[900];
int16_t buflen = 0;
os_event_t procTaskQueue[procTaskQueueLen];
static void ICACHE_FLASH_ATTR procTask(os_event_t *events){
system_os_post(procTaskPrio, 0, 0 );
if( events->sig == 0 && events->par == 0 ){
//Idle Event.
}
}
//Timer event.
static void ICACHE_FLASH_ATTR myTimer(void *arg){
uart0_sendStr(".");
}
void ICACHE_FLASH_ATTR led_read(){
spi_flash_read(0x3D000, &led, sizeof(led_type));
}
//char color R G B W;
//int16_t start_pos;
//int16_t end_pos;
//uint8_t start_val;
//int8_t change_val;
static void ICACHE_FLASH_ATTR schrijfbuffer(){
ets_wdt_disable();
os_intr_lock();
WS2812OutBuffer( outbuffer, buflen );
os_intr_unlock();
}
static void ICACHE_FLASH_ATTR fillbuffer(int16_t start_pos, int16_t end_pos, uint8_t start_val, int8_t change_val, int8_t RGB_offset ){
uint16_t pos;
uint16_t buf;
int8_t waarde;
int16_t waarde16;
waarde16 = start_val;
if (end_pos > 300){
end_pos = 300;}
if (start_pos > 300){
start_pos = 300; }
if (end_pos < 1){
end_pos = 1;}
if (start_pos < 1){
start_pos = 1; }
if (end_pos < start_pos) {
for( pos = start_pos; pos >= end_pos; pos-- ) {
buf = pos + pos + pos + RGB_offset - 3;
if (waarde16 > 255){
waarde16 = 255;}
if (waarde16 < 0){
waarde16 = 0;}
waarde = waarde16;
outbuffer[buf] = waarde;
if (buf > buflen){
buflen = buf;}
waarde16 = waarde16 + change_val;
}
}
if (end_pos > start_pos){
for( pos = start_pos; pos <= end_pos; pos++ ){
buf = pos + pos + pos + RGB_offset - 3;
if (waarde16 > 255){
waarde16 = 255;}
if (waarde16 < 0){
waarde16 = 0;}
waarde = waarde16;
outbuffer[buf] = waarde;
if (buf > buflen){
buflen = buf;}
waarde16 = waarde16 + change_val;
}
}
if (end_pos == start_pos){
pos = start_pos;
buf = pos + pos + pos + RGB_offset - 3;
waarde = waarde16;
outbuffer[buf] = waarde;
if (buf > buflen){
buflen = buf;}
}
}
static void ICACHE_FLASH_ATTR showled(void){
uint8_t buffer[MAX_FRAME];
uint8_t L;
for (L = 0; L < 63; L++ ){
if (led.L[L].color == 'U'){
uart0_sendStr("Uit");
fillbuffer( 1, 300, 0, 0, 0);
fillbuffer( 1, 300, 0, 0, 1);
fillbuffer( 1, 300, 0, 0, 2);}
if (led.L[L].color == 'R'){
fillbuffer( led.L[L].start_pos, led.L[L].end_pos, led.L[L].start_val, led.L[L].change_val, 1);}
if (led.L[L].color == 'G'){
fillbuffer( led.L[L].start_pos, led.L[L].end_pos, led.L[L].start_val, led.L[L].change_val, 0);}
if (led.L[L].color == 'B'){
fillbuffer( led.L[L].start_pos, led.L[L].end_pos, led.L[L].start_val, led.L[L].change_val, 2);}
if (led.L[L].color == 'W'){
fillbuffer( led.L[L].start_pos, led.L[L].end_pos, led.L[L].start_val, led.L[L].change_val, 0);
fillbuffer( led.L[L].start_pos, led.L[L].end_pos, led.L[L].start_val, led.L[L].change_val, 1);
fillbuffer( led.L[L].start_pos, led.L[L].end_pos, led.L[L].start_val, led.L[L].change_val, 2);}
}
schrijfbuffer();
}
//Called when new packet comes in.
//udpserver_recv(void *arg, char *pusrdata, unsigned short len)
static void ICACHE_FLASH_ATTR udpserver_recv(void *arg, char *pusrdata, unsigned short len){
struct espconn *pespconn = (struct espconn *)arg;
//Make sure watchdog is disabled. WS2812's take a while and can mess it up.
// WS2812OutBuffer( pusrdata, len )
uint8_t buffer[MAX_FRAME];
char uit;
uit = *pusrdata;
switch (uit){
case 'A':{
uart0_sendStr("A Aan");
led_read();
showled();
break;}
case 'U':{
uart0_sendStr("U Uit");
fillbuffer( 1, 300, 0, 0, 0);
fillbuffer( 1, 300, 0, 0, 1);
fillbuffer( 1, 300, 0, 0, 2);
schrijfbuffer();
break;}
default:{
uart0_sendStr("Default");
ETS_UART_INTR_DISABLE();
spi_flash_erase_sector(0x3D);
spi_flash_write(0x3D000, pusrdata, len);
ETS_UART_INTR_ENABLE();
led_read();
showled();
break;}
}
}
void ICACHE_FLASH_ATTR at_recvTask(){
//Called from UART.
}
static volatile os_timer_t client_timer;
static void ICACHE_FLASH_ATTR wait_for_ip(uint8 flag) {
LOCAL struct ip_info ipconfig;
LOCAL int status;
os_timer_disarm(&client_timer);
status = wifi_station_get_connect_status();
if (status == STATION_GOT_IP) {
wifi_get_ip_info(STATION_IF, &ipconfig);
if( ipconfig.ip.addr != 0) {
//Start UDP server
httpd_init();
} else {
os_timer_setfn(&client_timer, (os_timer_func_t *)wait_for_ip, NULL);
os_timer_arm(&client_timer, 100, 0);
}
} else if (status == STATION_CONNECTING) {
os_timer_setfn(&client_timer, (os_timer_func_t *)wait_for_ip, NULL);
os_timer_arm(&client_timer, 100, 0);
} else { //STATION_NO_AP_FOUND||STATION_CONNECT_FAIL||STATION_WRONG_PASSWORD
//Connection failed, somehow

system_restart();
//Bring up SOFTAP here
}
}
static void ICACHE_FLASH_ATTR system_is_done(void){
//Bringing up WLAN
wifi_station_connect();
//Wait for connection
os_timer_disarm(&client_timer);
os_timer_setfn(&client_timer, (os_timer_func_t *)wait_for_ip, NULL);
os_timer_arm(&client_timer, 100, 0);
}
void user_init(void){
uart_init(BIT_RATE_115200, BIT_RATE_115200);
int wifiMode = wifi_get_opmode();
uart0_sendStr("\r\nCustom Server\r\n");
struct station_config stconf;
memset(&stconf, 0, sizeof(stconf));
strcpy((char *)stconf.ssid,"SSID");
strcpy((char *)stconf.password,"PASSWORD");
wifi_station_set_auto_connect(0);
if (wifi_get_opmode() != STATION_MODE) {
wifi_set_opmode(STATION_MODE); //station
os_delay_us(500);
system_restart(); }
wifi_station_set_config(&stconf);
wifi_station_set_auto_connect(1);
led_read();
pUdpServer = (struct espconn *)os_zalloc(sizeof(struct espconn));
ets_memset( pUdpServer, 0, sizeof( struct espconn ) );
espconn_create( pUdpServer );
pUdpServer->type = ESPCONN_UDP;
pUdpServer->proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp));
pUdpServer->proto.udp->local_port = 7777;
espconn_regist_recvcb(pUdpServer, udpserver_recv);
if( espconn_create( pUdpServer )){
while(1) {
uart0_sendStr( "\r\nFAULT\r\n" ); }
}
ets_wdt_disable();
int16_t i;
for (i = 0; i < 900; i++){
outbuffer
= 0;}
WS2812OutBuffer( outbuffer, 900 ); //Initialize the output.
system_os_task(procTask, procTaskPrio, procTaskQueue, procTaskQueueLen);
showled();
//Timer example
os_timer_disarm(&some_timer);
os_timer_setfn(&some_timer, (os_timer_func_t *)myTimer, NULL);
os_timer_arm(&some_timer, 500, 1);
system_os_post(procTaskPrio, 0, 0 );
}