NeoPixels WS2812B


(S.F) #1

I’m in the process of using some neopixel for under kitchen lights. I have gone with WS2812B so a lot of the example that use the fastLED system are not compatible. SO I decided to try and roll my own.

I’ve started with the Adafruit_NeoPixel example code and Jon Oxers Superhouse Sonoff code and have started to combine.

The section I don’t understand is how to call the colorwipe section of the code and the strip.show() also does not work. Any guidance would be great. I need to get the structure correct before adding other animations.

*
 * Simple example MQTT client to run on the Itead Studio Sonoff with OTA
 * update support. Subscribes to a topic and watches for a message of
 * either "0" (turn off LED and relay) or "1" (turn on LED and relay)
 *
 */

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

#define PIN 1
/*NeoPiels*/
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, PIN, NEO_RGBW + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

/* WiFi Settings */
const char* ssid     = "cc";
const char* password = "cc";

/* Sonoff Outputs */
//const int relayPin = 12;  // Active high
//const int ledPin   = 13;  // Active low

/* MQTT Settings */
const char* mqttServer = "192.168.1.cc";
const int mqttPort = 1883;
const char* mqttUser = "cc";
const char* mqttPassword = "cc"; //cc
const char* mqttTopic = "/Kichen/UNLight";   // MQTT topic
IPAddress broker(192,168,1,135);          // Address of the MQTT broker
#define CLIENT_ID "KichenNodeMU"         // Client ID to send to the broker


void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

/**
 * MQTT callback to process messages
 */
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Examine only the first character of the message
  if(payload[0] == 49)              // Message "1" in ASCII (turn outputs ON)
  {
   colorWipe(strip.Color(33, 255, 0, 255), 50);
   Serial.print("should be on Now  ");// White RGBW
    //digitalWrite(ledPin, LOW);      // LED is active-low, so this turns it on
    //digitalWrite(relayPin, HIGH)
    
  } else if(payload[0] == 48)       // Message "0" in ASCII (turn outputs OFF)
  {
    strip.show();
    (strip.Color(33, 22, 99, 0), 50);
     Serial.print("should be OFF Now  ");
    //digitalWrite(ledPin, HIGH);     // LED is active-low, so this turns it off
    //digitalWrite(relayPin, LOW)
    
  } else {
    Serial.println("Unknown value");
  }
  
}

WiFiClient wificlient;
PubSubClient client(wificlient);

/**
 * Attempt connection to MQTT broker and subscribe to command topic
 */
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(CLIENT_ID)) {
      Serial.println("connected");
      client.subscribe(mqttTopic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

/**
 * Setup
 */
void setup() {
  //NeoPixels
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  
 //Serial Etc
  Serial.begin(115200);
  Serial.println("Booting");
  strip.show();
  Serial.println("strip.show()");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("WiFi begun");
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }
  Serial.println("Proceeding");

  // Port defaults to 8266
  // ArduinoOTA.setPort(8266);

  // Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setHostname("Sonoff01");

  // No authentication by default
  // ArduinoOTA.setPassword((const char *)"123");
  
  ArduinoOTA.onStart([]() {
    Serial.println("Start");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if      (error == OTA_AUTH_ERROR   ) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR  ) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR    ) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  
  /* Set up the outputs. LED is active-low */
 // pinMode(ledPin, OUTPUT);
 // pinMode(relayPin, OUTPUT);
  //digitalWrite(ledPin, HIGH);
  //digitalWrite(relayPin, LOW);

  /* Prepare MQTT client */
  client.setServer(broker, 1883);
  client.setCallback(callback);
}

/**
 * Main
 */
void loop() {
  ArduinoOTA.handle();
  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.print("Connecting to ");
    Serial.print(ssid);
    Serial.println("...");
    WiFi.begin(ssid, password);

    if (WiFi.waitForConnectResult() != WL_CONNECTED)
      return;
    Serial.println("WiFi connected");
  }

  if (WiFi.status() == WL_CONNECTED) {
    if (!client.connected()) {
      reconnect();
    }
  }
  
  if (client.connected())
  {
    client.loop();
  }
}

(Vincent Regaud) #2

According to that code you need to send a payload of “1” on the mqtt topic “/Kichen/UNLight”

I suggest you change the topic to “Kitchen/UNLight”. Having a topic starting with “/” is not good MQTT practice.


(B K) #3

What makes you say the ws2812b isn’t compatible with FastLed? I use them with my Nodemcu LED controller with no issues at all.


(S.F) #4

I will remove the first “/” tonight thanks.

I can send and the controller receives the MQTT message (i print it to the serial).

The issue is the if statement and the colour wipe not working.


(S.F) #5

I saw your code and videos and they are great! The neo’s i have are rgbw so my understanding and searching was that fast led was only good for rgb?


(Vincent Regaud) #6

Try
if ((char)(payload[0] == '1')
and
} else if ((char)(payload[0] == '0')


(B K) #7

Ah, I didn’t realize you were using RGBW - definitely not yet supported by FastLED (though I think it’s on the list for a near term upgrade).


(S.F) #8

HI,

Back again, having thought that I needed a level changer I have installed a 74AHCT125 in the circuit so send 5V logic to the LEDs.

I’m still having problems with getting the LEDs to change. The MQTT part is working and I am receiving the topic and using the serial print function to print out a status:

Message arrived [/Kichen/UNLight] 1
should be on Now  Message arrived [/Kichen/UNLight] 0
should be OFF Now  Message arrived [/Kichen/UNLight] 1
should be on Now  Message arrived [/Kichen/UNLight] 0
should be OFF Now  Message arrived [/Kichen/UNLight] 1
should be on Now

So from this I am happy with the MQTT.

My issue appears to be the command to strip. I don’t even appear to be sending clear correctly:

void setup() {
  //NeoPixels
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

This should boot up with no LEDs on.

I have a feeling it’s the structure and layout of my code but unsure where I am going wrong. Anyone have any ideas?


(Vincent Regaud) #9

I have a feeling it could be your wiring to the LEDs.
The library works, it’s proven.
The MQTT works, you’ve proven it.
The LEDs don’t. Hence probably hardware related.


(S.F) #10

Yep agreed the parts work separately.

I have run the neopix test code and it cycles the colours chases etc. So i can confirm the wiring is good.

I think it is the instruction when a message is received that must be an issue. I’ve found another thread so will take a look and report back.

Will also strip down the code to the bare bones.

Thanks


(John Chiasson) #11

@SabreFrenzy I was able to get my SK6812 RGBW strips working with the https://github.com/coryking/FastLED fork and @bartus library