Skip to content

Instantly share code, notes, and snippets.

@liz-miller
Created July 24, 2018 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liz-miller/ad6f4a11e2f389171f292929f5c55b8c to your computer and use it in GitHub Desktop.
Save liz-miller/ad6f4a11e2f389171f292929f5c55b8c to your computer and use it in GitHub Desktop.
Sample Code for Wemos D1 Mini + MQTT (LED hint)
//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