Hello !
Let me introduce first, I'm Romain, begining in arduino programming.
I'm having an issue while trying to program an ESP8266 module.
I'm trying to use it to trigger differents light modes on my WS2811 led strip with Adafruit IO.
Here is the code:
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define WLAN_SSID "Freebox-1CE573"
#define WLAN_PASS "arrhis-adsumetur38-venenose.@-adpingit"
#define AIO_SERVER "io.adafruit.com" //Definition des paramètres IO
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "Kdcius"
#define AIO_KEY "aio_WvIm53NbJpTXfvuJXcUfRkbSjlVM"
#define LED_animations D0
#include <FastLED.h>
FASTLED_USING_NAMESPACE
#define DATA_PIN 0
#define LED_TYPE WS2811
#define COLOR_ORDER GRB //Definition des paramètres FastLED
#define NUM_LEDS 245
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 150
#define FRAMES_PER_SECOND 120
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); //Connecetion au serv mqtt
Adafruit_MQTT_Subscribe LED_animations= Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/LED_animations"); //Ajout du feed
Adafruit_MQTT_Subscribe LED_off= Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/LED_off");
void MQTT_connect();
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(F("Adafruit MQTT demo"));
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
//Connection au WiFi
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
mqtt.subscribe(&LED_animations);
mqtt.subscribe(&LED_off);
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
//FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
//SetUp FastLED
// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
}
uint32_t x=0; //J'ai pas compris mais truc IO
// List of patterns to cycle through. Each is defined as a separate function below.
typedef void (*SimplePatternList[])();
SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm };
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
void loop() {
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &LED_animations) {
Serial.print(F("Got: "));
Serial.println((char *)LED_animations.lastread); //Differentes action à réaliser
uint16_t state0 = atoi((char *)LED_animations.lastread);
while (state0 ==1)
{
// Call the current pattern function once, updating the 'leds' array
gPatterns[gCurrentPatternNumber]();
// send the 'leds' array out to the actual LED strip
FastLED.show(); //My issue is here !
// insert a delay to keep the framerate modest
FastLED.delay(1000/FRAMES_PER_SECOND);
// do some periodic updates
EVERY_N_MILLISECONDS( 10 ) { gHue++; } // slowly cycle the "base color" through the rainbow
EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
}
}
if (subscription == &LED_off) {
Serial.print(F("Got: "));
Serial.println((char *)LED_off.lastread);
uint16_t state1 = atoi((char *)LED_off.lastread);
if (state1 ==1)
{
for(int led = 0; led < 245; led++) {
leds[led] = CRGB::Black ;
FastLED.show();
delay (8);
}
}
}
}
}
void MQTT_connect() {
int8_t ret;
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) {
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect(); //Boucle de connection au serv mqtt
delay(5000);
retries--;
if (retries == 0) {
while (1);
}
}
Serial.println("MQTT Connected!");
}
//Là c'est les differents paterns des animations
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
void nextPattern()
{
// add one to the current pattern number, and wrap around at the end
gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
}
void rainbow()
{
// FastLED's built-in rainbow generator
fill_rainbow( leds, NUM_LEDS, gHue, 7);
}
void rainbowWithGlitter()
{
// built-in FastLED rainbow, plus some random sparkly glitter
rainbow();
addGlitter(80);
}
void addGlitter( fract8 chanceOfGlitter)
{
if( random8() < chanceOfGlitter) {
leds[ random16(NUM_LEDS) ] += CRGB::White;
}
}
void confetti()
{
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds, NUM_LEDS, 10);
int pos = random16(NUM_LEDS);
leds[pos] += CHSV( gHue + random8(64), 200, 255);
}
void sinelon()
{
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = beatsin16( 13, 0, NUM_LEDS-1 );
leds[pos] += CHSV( gHue, 255, 192);
}
void bpm()
{
// colored stripes pulsing at a defined Beats-Per-Minute (BPM)
uint8_t BeatsPerMinute = 62;
CRGBPalette16 palette = PartyColors_p;
uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
for( int i = 0; i < NUM_LEDS; i++) { //9948
leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
}
}
void juggle() {
// eight colored dots, weaving in and out of sync with each other
fadeToBlackBy( leds, NUM_LEDS, 20);
byte dothue = 0;
for( int i = 0; i < 8; i++) {
leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
dothue += 32;
}
}
//Et là c'est la fin de la liste des paterns
The issue I have is marked with comment it's in the middle of the code.
Let me explain.
I'm trying to trigger the first mode which contains 6 animations.
But with the while control structure, the state of the IO feed won't be checked so I will be stuck in the animation.
Unable to trigger any other mode.
I appologize if my English isn't perfect ^^'
So if you mind a good way of doing what I'm trying to do since hours and hours, let me know !
PS: The code I used isn't mine, It's based on an other maker one.