Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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