# Controlling LEDs on a Wemos D1 Mini (Arduino code) via MQTT

**URL:** <https://discourse.nodered.org/t/controlling-leds-on-a-wemos-d1-mini-arduino-code-via-mqtt/62523>\
**Category:** Share Your Projects\
**Created:** [11 May 2022 16:30 UTC](https://discourse.nodered.org/t/controlling-leds-on-a-wemos-d1-mini-arduino-code-via-mqtt/62523 "2022-05-11T16:30:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [11 May 2022 16:30 UTC](https://discourse.nodered.org/t/controlling-leds-on-a-wemos-d1-mini-arduino-code-via-mqtt/62523/1 "2022-05-11T16:30:37Z")

</div>

One of my IoT students was asking how to control a set of LEDs on a Wemos D1 Mini, that had been programmed using Arduino code, via MQTT commands from Node-RED.

This is what the breadboard lash-up looks like...

 ![breadboard](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/0/a01418b6ee85662c7f0b5604f2b0146c8db36aa5.jpeg)  
The red LED is connected to D8 (GPIO-15).  
The green LED is connected to D7 (GPIO-13)  
The yellow LED is connected to D6 (GPIO-12)

This is the very simple test-flow we created in Node-RED.  
 ![Screen Shot 05-11-22 at 06.35 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/2/82959161f2941bad31e9c4e7cc9c8baab776baf6.png)

```auto
[{"id":"bb2bbcc236726f09","type":"tab","label":"Wemos Controller","disabled":false,"info":"","env":[]},{"id":"a88767b42282a67e","type":"inject","z":"bb2bbcc236726f09","name":"redLED = \"on\" ","props":[{"p":"payload.state","v":"on","vt":"str"},{"p":"payload.color","v":"red","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":230,"y":260,"wires":[["de011f97521ea82a"]]},{"id":"de011f97521ea82a","type":"mqtt out","z":"bb2bbcc236726f09","name":"","topic":"wemos/test","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"b80c36f2.044588","x":450,"y":460,"wires":[]},{"id":"d7aff05bf89bba23","type":"inject","z":"bb2bbcc236726f09","name":"redLED = \"off\" ","props":[{"p":"payload.state","v":"off","vt":"str"},{"p":"payload.color","v":"red","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":230,"y":300,"wires":[["de011f97521ea82a"]]},{"id":"1ef4446fccad4b63","type":"inject","z":"bb2bbcc236726f09","name":"grnLED = \"on\" ","props":[{"p":"payload.state","v":"on","vt":"str"},{"p":"payload.color","v":"green","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":230,"y":380,"wires":[["de011f97521ea82a"]]},{"id":"d38f12d896cc2eee","type":"inject","z":"bb2bbcc236726f09","name":"grnED = \"off\" ","props":[{"p":"payload.state","v":"off","vt":"str"},{"p":"payload.color","v":"green","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":230,"y":420,"wires":[["de011f97521ea82a"]]},{"id":"d03d94b53eaa94d9","type":"inject","z":"bb2bbcc236726f09","name":"yelED = \"on\" ","props":[{"p":"payload.state","v":"on","vt":"str"},{"p":"payload.color","v":"yellow","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":230,"y":500,"wires":[["de011f97521ea82a"]]},{"id":"e5d4bc03aa7bec28","type":"inject","z":"bb2bbcc236726f09","name":"yelLED = \"off\" ","props":[{"p":"payload.state","v":"off","vt":"str"},{"p":"payload.color","v":"yellow","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":230,"y":540,"wires":[["de011f97521ea82a"]]},{"id":"087bd5bce959a671","type":"inject","z":"bb2bbcc236726f09","name":"allLEDs = \"on\" ","props":[{"p":"payload.state","v":"on","vt":"str"},{"p":"payload.color","v":"all","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":230,"y":620,"wires":[["de011f97521ea82a"]]},{"id":"a2773f1bbca9f31c","type":"inject","z":"bb2bbcc236726f09","name":"allLED = \"off\" ","props":[{"p":"payload.state","v":"off","vt":"str"},{"p":"payload.color","v":"all","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":230,"y":660,"wires":[["de011f97521ea82a"]]},{"id":"b80c36f2.044588","type":"mqtt-broker","name":"Broker_156","broker":"192.168.1.156","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"120","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

```

Basically, a command is sent (from Node-RED) via MQTT to the Wemos with this format...  
"color": "red" or "green" or "yellow" or "all"  
"state": "on" or "off"

Together we worked out these [Arduino files](http://www.resources-area.co.uk/node-red-flows/mqtt_relay_controller/) that need to go in the Wemos. You will need to place them in a folder, with the same name as the .ino file, so Arduino can compile them correctly.

Most of the clever stuff is performed in the function...  
`void callback(char *mqtt_topic, byte *message, unsigned int length)`

You will need to enter your WiFi credentials in the 'config.h' file.  
You will also need to install these Arduino libraries...

- PubSubClient
- ArduinoJson

My student and I hope you find this write-up useful?

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [11 May 2022 18:01 UTC](https://discourse.nodered.org/t/controlling-leds-on-a-wemos-d1-mini-arduino-code-via-mqtt/62523/2 "2022-05-11T18:01:14Z")

</div>

Can't download the sketch Dave.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [11 May 2022 18:52 UTC](https://discourse.nodered.org/t/controlling-leds-on-a-wemos-d1-mini-arduino-code-via-mqtt/62523/3 "2022-05-11T18:52:24Z")

</div>

It's a zip file, you might need to accept a security flag before you can download it.  
Let me know if you still have problems and I'll upload separate files.

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [11 May 2022 18:54 UTC](https://discourse.nodered.org/t/controlling-leds-on-a-wemos-d1-mini-arduino-code-via-mqtt/62523/4 "2022-05-11T18:54:17Z")

</div>

Got it Dave, it was a security problem.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [11 May 2022 18:56 UTC](https://discourse.nodered.org/t/controlling-leds-on-a-wemos-d1-mini-arduino-code-via-mqtt/62523/5 "2022-05-11T18:56:06Z")

</div>

I've put the two files in this directory so it might be easier for people to download.  
[Arduino files](http://www.resources-area.co.uk/node-red-flows/mqtt_relay_controller/)

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [12 May 2022 06:28 UTC](https://discourse.nodered.org/t/controlling-leds-on-a-wemos-d1-mini-arduino-code-via-mqtt/62523/6 "2022-05-12T06:28:15Z")

</div>

Small edit to the .ino file to handle the contents of the "color" and "state" commands with any case.  
e.g. "color": "red" or "Red" or "RED" or any combination.

---

<div class="post-metadata">

**Author:** ![Flixm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/flixm/32/17122_2.png) [@Flixm](https://discourse.nodered.org/u/Flixm)\
**Post date:** [2 September 2022 19:32 UTC](https://discourse.nodered.org/t/controlling-leds-on-a-wemos-d1-mini-arduino-code-via-mqtt/62523/7 "2022-09-02T19:32:42Z")

</div>

That's a great solution for me to show the power of the Photovoltaics system at home.

Green for more power as actually in use or red to save energy of possible.

👍 Great

---

<div class="post-metadata">

**Author:** ![Telesistemasca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/telesistemasca/32/91552_2.png) [@Telesistemasca](https://discourse.nodered.org/u/Telesistemasca)\
**Post date:** [30 May 2024 17:07 UTC](https://discourse.nodered.org/t/controlling-leds-on-a-wemos-d1-mini-arduino-code-via-mqtt/62523/8 "2024-05-30T17:07:32Z")

</div>

Hi Dave, can you authorize me to download the .zip?

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [30 May 2024 18:48 UTC](https://discourse.nodered.org/t/controlling-leds-on-a-wemos-d1-mini-arduino-code-via-mqtt/62523/9 "2024-05-30T18:48:56Z")

</div>

I've updated the links on my server - please can you try again.
