Created
July 24, 2018 17:45
-
-
Save liz-miller/ad6f4a11e2f389171f292929f5c55b8c to your computer and use it in GitHub Desktop.
Sample Code for Wemos D1 Mini + MQTT (LED hint)
This file contains 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
//create a method to control the LED's | |
void ctrlLEDs(String topic, byte receivedChar){ | |
if (topic == "ledStatus/red"){ //every LED has it's own topic for individual control | |
if(receivedChar == '1'){ | |
digitalWrite(redLED, HIGH); | |
} | |
else if (receivedChar == '0'){ | |
digitalWrite(redLED, LOW); | |
} | |
else{ | |
analogWrite(redLED, int(receivedChar)); | |
} | |
} | |
if (topic == "ledStatus/yellow"){ | |
if(receivedChar == '1'){ | |
digitalWrite(yellowLED, HIGH); | |
} | |
else if (receivedChar == '0'){ | |
digitalWrite(yellowLED, LOW); | |
} | |
else{ | |
analogWrite(yellowLED, int(receivedChar)); | |
} | |
} | |
//... finish this method for the Green and White LED's | |
// There may be a better & shorter way of doing this! | |
} | |
//inside the callback() method, be sure to call the ctrlLEDs() method | |
ctrlLEDs(topic,msg); | |
//inside the reconnect() method, be sure to subscribe to all of the LED topics | |
client.subscribe("ledStatus/red"); | |
client.subscribe("ledStatus/yellow"); | |
client.subscribe("ledStatus/green"); | |
client.subscribe("ledStatus/white"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment