Created
April 22, 2015 04:02
-
-
Save bazilio91/f4af833a0d83808616af to your computer and use it in GitHub Desktop.
arduino mqtt led stripe
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 <PubSubClient.h> | |
#include <SPI.h> | |
#include <Ethernet.h> | |
// don't futz with these, illicit sums later | |
#define RED 7// pin for red LED | |
#define GREEN 8 // pin for green - never explicitly referenced | |
#define BLUE 2 // pin for blue - never explicitly referenced | |
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; | |
byte server[] = { 192, 168, 88, 158 }; | |
EthernetClient ethClient; | |
PubSubClient client(server, 1883, callback, ethClient); | |
byte brightness[3] = {200, 255, 245}; | |
void setColor(byte r, byte g, byte b) { | |
analogWrite(RED, r * brightness[0] / 255); | |
analogWrite(GREEN, g * brightness[1] / 255); | |
analogWrite(BLUE, b * brightness[2] / 255); | |
Serial.print("Processed RGB: "); | |
Serial.print(r * brightness[0] / 255); | |
Serial.print(g * brightness[1] / 255); | |
Serial.println(b * brightness[2] / 255); | |
} | |
void setup() { | |
pinMode(RED, OUTPUT); | |
pinMode(GREEN, OUTPUT); | |
pinMode(BLUE, OUTPUT); | |
setColor(100,100,100); | |
Serial.begin ( 9600 ); | |
Serial.println("connecting...to ethernet"); | |
// Start the Ethernet connection | |
Ethernet.begin(mac); | |
Serial.println("connecting to openHab"); | |
if (client.connect("arduinoClient")) { | |
Serial.println("ok"); | |
client.subscribe("\/room\/ambilight\/color"); | |
} else { | |
Serial.println("fail"); | |
while(1) {}; | |
} | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
// check for messages on subscribed topics | |
Serial.print("Topic: "); | |
Serial.println(String(topic)); | |
// check topic to identify type of content | |
if(String(topic) == "\/room\/ambilight\/color") { | |
// convert payload to String | |
String value = String((char*)payload).substring(0, length); | |
Serial.println(value); | |
Serial.println(length); | |
// split string at every "," and store in proper variable | |
// convert final result to integer | |
byte TopperR = value.substring(0,value.indexOf(',')).toInt(); | |
byte TopperG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt(); | |
byte TopperB = value.substring(value.lastIndexOf(',')+1).toInt(); | |
Serial.print("Unprocessed RGB: "); | |
Serial.print(TopperR); | |
Serial.print(TopperG); | |
Serial.println(TopperB); | |
setColor(TopperR, TopperG, TopperB); | |
} | |
payload = 0; | |
} | |
void loop() { | |
client.loop(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment