In this post I will use a Discord to turn on/off a led that is attached to a Raspberry Pi Zero. This is going to show how easy is to control something from your house using the Discord. And will demonstrate that even if the Discord is designed for gaming communities it can be used for many more.

How do I do this?

To control the led using Discord we need to run a Bot on Rasbperry Pi Zero. To create a Discord Bot use the following steps that can be found here. After you create the application don't forget to mark it as a Bot (be aware, the action is irreversible). Save the token provided by the application and store it in a json file in order to be used.

In this mini project I named the file auth.json , don't forget to use the token of the Bot you created.

{

"token": "PUT_YOUR_TOKEN_HERE"

}

The package.json file for the this project. I may use some old dependencies of rpio (at the moment of writting this), but for this project is enough. Also the discord.js module to interact with Discord, make sure you have the node version 6 or higher. If you don't there are many tutorials on internet that help you achieve this.

{ "name": "home-manager-led", "version": "0.0.1", "description": "A bot that helps me to automize my home.", "main": "bot.js", "author": "TheHumbleCode", "dependencies": { "rpio": "0.9.12", "discord.js": "^11.3.2" } }

The code below allows us to use the keywords !on, !off and !blink in the discord chat to control the led.

First, we import the auth token in order to login to Discord. In this project I have chose to use the PIN 18 from Raspberry Pi Zero. After the led is turned on or off a message is sent to the Discord chat in order to have a feedback when we are not close to the led.

var auth = require('./auth.json'); var Discord = require('discord.js'); var rpio = require('rpio'); // Initialize Discord Bot var bot = new Discord.Client(); bot.on('ready', (evt) => { console.log('Connected'); }); const LED_PIN = 18; /* * Set the initial state to low. The state is set prior to the pin becoming * active, so is safe for devices which require a stable setup. */ rpio.open(LED_PIN, rpio.OUTPUT, rpio.LOW); turnOn = (channel) => { rpio.write(LED_PIN, rpio.HIGH); channel.send('The led is ON now!'); }; turnOff = (channel) => { rpio.write(LED_PIN, rpio.LOW); channel.send('The led is OFF now!'); } bot.on('message', message => { // Our bot needs to know if it will execute a command // It will listen for messages that will start with `!` var text = message.content; var channel = message.channel; if (text.substring(0, 1) == '!') { var args = text.substring(1).split(' '); var cmd = args[0]; args = args.splice(1); switch(cmd) { // !open case 'on': turnOn(channel); break; case 'off': turnOff(channel); break; case 'blink': turnOn(channel); setTimeout(() => { turnOff(channel); }, 3000); break; // Just add any case commands if you want to.. } } });



bot.login(auth.token);

I put the above code in a file named bot.js and used the command node bot.js to start it.

You can add any custom commands or replace the led with something else, like a matrix of leds or a temperature sensor that will tell you how many oC (or fahrenheit) are in the room.

Thank you for reading!