In this article, you will learn how to control the lights of your home (or anything you want to) using Discord, or any other message service.

During this tutorial, you will see how to connect, configure, and use several cloud services to implement this automation system. And the best part, you will do it at zero cost.

Currently, there are many PaaS (Platform as a Service) that you can use to achieve this goal.

In this case, I selected Pipedream.

Building a workflow on Pipedream

Pipedream is a platform for automating processes that use connections to APIs. In this way, you can build workflows that allow you to obtain data, process it, analyze it, and perform actions.

Let’s start by creating a new Workflow in Pipedream.

Once you are logged in to Pipedream, go to the big blue button at the top right and click on it.

Fig. 1 – New workflow

This will lead you to the following screen. Here you have to select a source to act as a trigger. Search Discord and select it.

NOTE: You must have a Discord account and own a server before you can add a Discord source to your workflow.

Fig. 2 – Create Discord source.

Then, several options will appear. Select the New Message (Instant) item.

Fig. 3 – New trigger.

Once you selected the Discord source, you have to ingress the Discord account and the channel (you have to authorize Pipedream from the Discord side). This step will create a Discord bot that will have access to the selected channel.

Fig. 4 – Create New Message.

You can test the Discord source by sending a message on the specified channel from Pipedream or from Discord itself.

Ad - Web Hosting from SiteGround - Crafted for easy site management. Click to learn more.

Then you can explore the data structure of the message received, as shown in the following figure.

Fig. 5 – Test Discord message.

Now that you have a working source in Pipedream, we will add a filter.

Click on Add a step and search for a filter. You will use this step to see if the message is relevant to your application.

In this case, we will use two types of messages: lighton and lightoff. Pretty descriptive 😉.

So, search for the filter in the dialog box and select it.

Fig. 6 – Add a filter.

This will lead to two possible options. Select the one that shows “Continue execution if a condition is met”.

This filter will process all the messages coming from the first step, but it will pass to the following only the ones that met the condition.

Fig. 7 – Selec Continue execution.

Now, configure the filter step as follows.

  1. In the first field (Initial value), enter the path to the data obtained in the previous step.
  2. Then, in “Condition”, select “Text Contains”.
  3. Finally, enter the word “light” in the “Second value” field.

This filter will process all the messages coming from the previous step and will check if they contain the string “light” on them. In case of finding the word, it will pass it to the next step. Otherwise, it will end the workflow.

Fig. 8 – Configure the filter.

Now that you have the filter, let´s go to the final step.

In this case, we will use a Python script to connect this workflow to an MQTT broker (HiveMQ).

Fig. 9 – Create Python code step.

Search for the “Run Python code” option, open the editor, and paste the code available on this link.

5 crucial factors iot project
Fig. 10 – Add Python code.

Look at the following lines of code and replace the username, password, host, and port parameters according to your MQTT instance. See the next section for obtaining these parameters.

# set username and password
client.username_pw_set("USERNAME", "PASSWORD")
# connect to HiveMQ Cloud on port 8883 (default for MQTT)
client.connect("HOST", PORT)
Specify the MQTT topic
# a single publish, this can also be done in loops, etc.
client.publish("home/light", payload=mqtt_payload, qos=0)

In the script use client.loop_start() and client.loop_stop() instead of client.loop_forever(). The last one will cause the workflow to hang.

client.loop_start()
client.loop_stop()

HIVEMQ configuration

In this tutorial, we are using HiveMQ as an MQTT broker, although you can implement this automation using any MQTT service.

After you register in HiveMQ, go to the console and create a cluster. HiveMQ offers a good free service that you can scale up to 100 MQTT simultaneous sessions.

Fig. 11 – Create an MQTT cluster.

Once you created the cluster, take note of the cluster URL and Port. You have to ingress these parameters on the Python code you created before.

Then, go to the ACCESS MANAGEMENT page and create new credentials for accessing your cluster.

Fig. 12 – Create new credentials.

Grant this user permission to publish and subscribe to MQTT topics.

Now that you have a working MQTT instance, let’s configure the Raspberry Pi to connect to it.

Configuring the Raspberry Pi

In this case, the Raspberry Pi computer uses Node-Red for managing IoT events and configurations.

The whole diagram looks like this.

Fig. 13 – Node-Red flow.

First, you need to add an MQTT input node for connecting to the HIVEMQ broker.

After you add the node, you have to configure a new MQTT broker. You can see this configuration in the following picture. Take into account that you need to enter a username and a password in the Security section.

Fig. 14 – MQTT connection configuration.

Going back to the MQTT in node, you can now configure the MQTT client, as shown in the following picture.

Fig. 15 – MQTT client configuration.

Now add a rpi-gpio-out node and select the pin where you have connected the input control of the relay. In this case, the pin is GPIO25.

Also, set the pin as digital output and initialize it to zero.

Fig. 16 – GPIO configuration.

Now add a function node and write the following code inside it. This code will check if the message coming from the MQTT broker is a lighton or a lightoff and will send the output accordingly.

Fig. 17 – Function node.

Here is the code for your convenience.

if (msg.payload == "lighton") {
    msg.payload = true;
}
else if (msg.payload == "lightoff"){
    msg.payload = false;
}
return msg;

Connect all the nodes as shown in Figure 13 and deploy. Now you should have the system running.

Check if is it working by sending a message to the Discord channel. If everything goes OK, you should see how the relay turns on or off depending on the type of message.

Resume

In this tutorial, we have built a system to control your home lights from Discord.

We followed these steps:

  • Create a new workflow on Pipedream.
  • Connect Pipedream to the Discord channel as a source.
  • Create a filter to process the messages coming from the first step.
  • Add a Python code for connecting to the HIVEMQ MQTT broker. You need a running HIVEMQ instance for completing this step.
  • Configure Node-Red in the Raspberry Pi for subscribing it to the MQTT broker.
  • Add a function code and send the result to a GPIO output block for controlling the relay.

I hope you enjoyed this tutorial!

5 crucial factors iot project

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *