Created
September 17, 2018 22:40
-
-
Save developersteve/e2e9a8b8a1d8c0140d734abac6933ad4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************** | |
* Include Libraries | |
****************************************/ | |
#include <Adafruit_NeoPixel.h> | |
#include <WiFi.h> | |
#include <PubSubClient.h> | |
#define PIN 25 | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800); | |
/**************************************** | |
* Define Constants | |
****************************************/ | |
#define WIFISSID "" // Put your WifiSSID here | |
#define PASSWORD "" // Put your wifi password here | |
#define TOKEN "" | |
#define MQTT_CLIENT_NAME "bulb" | |
#define VARIABLE_LABEL_SUBSCRIBE "colour" // Assing the variable label | |
#define DEVICE_LABEL "badge" // Assig the device label | |
#define RELAY 16 // Set the GPIO16 as RELAY | |
char mqttBroker[] = "iot.broker.com"; | |
char payload[100]; | |
char topic[150]; | |
char topicSubscribe[100]; | |
/**************************************** | |
* Auxiliar Functions | |
****************************************/ | |
WiFiClient wifi; | |
PubSubClient client(wifi); | |
void callback(char* topic, byte* payload, unsigned int length) { | |
char p[length + 1]; | |
memcpy(p, payload, length); | |
p[length] = NULL; | |
String message(p); | |
if (message == "red") { | |
colorWipe(strip.Color(255, 0, 0), 50); // Red | |
} else if(message == "green"){ | |
colorWipe(strip.Color(0, 255, 0), 50); // Green | |
} else if(message == "blue"){ | |
colorWipe(strip.Color(0, 0, 255), 50); // blue | |
} else if(message == "white"){ | |
colorWipe(strip.Color(255, 255, 255), 50); // white | |
} else if(message == "pink"){ | |
colorWipe(strip.Color(255, 192, 203), 50); // white | |
} else if(message == "random"){ | |
colorWipe(strip.Color(random(0,255), random(0,255), random(0,255)), 50); // random | |
} else if(message == "rainbow"){ | |
rainbowCycle(20); | |
} else { | |
rainbow(20); | |
} | |
Serial.write(payload, length); | |
Serial.println(); | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.println("Attempting MQTT connection..."); | |
// Attemp to connect | |
if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) { | |
Serial.println("Connected"); | |
client.subscribe(topicSubscribe); | |
} else { | |
Serial.print("Failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 2 seconds"); | |
// Wait 2 seconds before retrying | |
delay(2000); | |
} | |
} | |
} | |
/**************************************** | |
* Main Functions | |
****************************************/ | |
void setup() { | |
strip.begin(); | |
strip.show(); | |
strip.setBrightness(150); | |
colorWipe(strip.Color(255, 0, 0), 10); // Red | |
colorWipe(strip.Color(0, 255, 0), 10); // Blue | |
colorWipe(strip.Color(0, 0, 255), 10); // Green | |
colorWipe(strip.Color(50, 50, 50), 10); // Green | |
Serial.begin(115200); | |
WiFi.begin(WIFISSID, PASSWORD); | |
// Assign the pin as OUTPUT | |
Serial.println(); | |
Serial.print("Wait for WiFi..."); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print("."); | |
delay(500); | |
} | |
Serial.println(""); | |
Serial.println("WiFi Connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
client.setServer(mqttBroker, 1883); | |
client.setCallback(callback); | |
sprintf(topicSubscribe, "lights"); | |
client.subscribe(topicSubscribe); | |
} | |
void loop() { | |
// rainbowCycle(20); | |
if (!client.connected()) { | |
reconnect(); | |
client.subscribe(topicSubscribe); | |
} | |
client.loop(); | |
} | |
// Fill the dots one after the other with a color | |
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); | |
} | |
} | |
void rainbow(uint8_t wait) { | |
uint16_t i, j; | |
for(j=0; j<256; j++) { | |
for(i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, Wheel((i+j) & 255)); | |
} | |
strip.show(); | |
delay(wait); | |
} | |
} | |
// Slightly different, this makes the rainbow equally distributed throughout | |
void rainbowCycle(uint8_t wait) { | |
uint16_t i, j; | |
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel | |
for(i=0; i< strip.numPixels(); i++) { | |
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 127)); | |
} | |
strip.show(); | |
delay(wait); | |
} | |
} | |
// Input a value 0 to 255 to get a color value. | |
// The colours are a transition r - g - b - back to r. | |
uint32_t Wheel(byte WheelPos) { | |
WheelPos = 255 - WheelPos; | |
if(WheelPos < 85) { | |
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); | |
} | |
if(WheelPos < 170) { | |
WheelPos -= 85; | |
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); | |
} | |
WheelPos -= 170; | |
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment