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();
}
}