[PYTHON] Easy introduction to home hack with Raspberry Pi and discord.py

00 Introduction

This is a continuation of Adcare article. I wondered if I could do something with ** RaspberryPi ** + ** discord.py **. The Raspberry Pi Blynk remote control I made earlier died due to a malfunction on the Raspberry Pi side (probably an SD card), so I thought I should take this opportunity.

01 What to prepare

Basic parts are available at Akizuki Denshi. (Soldering irons, breadboards, and jumper wires are on the premise)

Item price(Circle)
RaspberryPiZeroW(Single unit) 1,320
Infrared remote control receiver module OSRB38C9AA (2 pieces) 100
Infrared LED OSI5FU5111C-40 (5 pieces) 100
Carbon resistance (carbon film resistance) 1 / 4W 1kΩ (100 pieces) 100
Carbon resistance (carbon film resistance) 1 / 4W 10Ω (100 pieces) 100
Transistor 2SC2120-Y (20 pieces) 110
Perseverance Market price

02 Learn infrared rays from the remote control

First, in order to receive infrared rays, connect the receiving modules as shown in this table.

Raspberry Pi side Module side(The receiving part is facing you)
GPIO for input left
GND Central
3.3V right

I tried Lirc for infrared exchange, but the environment in my room was bad and I could not set it well, so I use WiringPi.

This article was helpful. Scan and send infrared remote control signals using GPIO of Raspberry Pi

03 Make an infrared transmitter module

スクリーンショット 2020-06-12 2.38.28.png I made it as above. For the GPIO pin for transmission, select ** the pin number corresponding to PWM **.

04 Run with discord.py

Maintenance is easier if you let it be executed indirectly by a shell script. Execution from the python side uses ** subprocess **.

index.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import discord
from discord.ext import commands
import subprocess
from subprocess import PIPE

# DiscordToken
TOKEN = 'Your Token'

#Generate the objects needed for the connection
client = discord.Client()

#Processing that operates at startup
@client.event
async def on_ready():
    #When started, a login notification will be displayed in the terminal
    print('ready...')

#Processing that operates when receiving a message
@client.event
async def on_message(message):
    #Ignore if the message sender is a bot
    if message.author.bot:
        return

    #Lighting switch
    if message.content == 'on light':
        #User selection
        if (str)(message.author) == me_id:
            #ON command
            subprocess.run("[ABSOLUTE PATH]/lightOn.sh",shell=True, text=True)
            await message.channel.send('I set the lighting to ON'+rasp)
        else: #When a user other than yourself tries to operate
            await message.channel.send(f'{message.author.mention}No command right')

#Launching a bot and connecting to a Discord server
client.run(TOKEN)

05 Make it resident with systemctl

Under / etc / systemd / system /,

discordBot.service


[Unit]
Description=Discord Bot Service

[Service]
ExecStart=Absolute path of python file
Restart=no
Type=simple

[Install]
WantedBy=multi-user.target

Save,

raspberryPi$ sudo systemctl enable discordBot.service #activation
raspberryPi$ sudo systemctl start discordBot.service #start

To execute.

The bot is now resident.

When an error occurs ・ When the bot does not start

At the terminal

raspberryPi$ sudo systemctl status discordBot.service

You can check the log by executing, so let's deal with the error.

06 Results

If all goes well, you can control the room lighting with the commands you specify from the Discord talk screen. I also set up an air conditioner. Also, for the bot name, I played with my favorite character name.

スクリーンショット 2020-06-12 1.28.10.png

very good. (good)

For the time being, the source code is here (jumps to github)

It seems that discord.py has a convenient framework, so I wanted to use it, but I used it because there was a similar bot source code before.

07 Reference link

Scan and send infrared remote control signals using GPIO of Raspberry Pi schemeit discord.py Document TV Anime The Idolmaster Official

Recommended Posts

Easy introduction to home hack with Raspberry Pi and discord.py
Easy IoT to start with Raspberry Pi and MESH
Home hack to buy automatically when it gets cheaper with Amazon Dash Button and Raspberry Pi
Pet monitoring with Rekognition and Raspberry pi
Introduction to discord.py (2)
Introduction to discord.py
Note: I want to do home automation with Home Assistant + Raspberry Pi + sensor # 1
[Python] Easy introduction to machine learning with python (SVM)
MQTT RC car with Arduino and Raspberry Pi
Easy connection between Raspberry Pi and AWS IoT
Get temperature and humidity with DHT11 and Raspberry Pi
Connect to MySQL with Python on Raspberry Pi
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server --2 PHP introduction
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server ―― 1. Apache introduction
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
Record temperature and humidity with systemd on Raspberry Pi
Machine learning with Raspberry Pi 4 and Coral USB Accelerator
[Introduction to WordCloud] It's easy to use even with Jetson-nano ♬
Try to visualize the room with Raspberry Pi, part 1
Detect mask wearing status with OpenCV and Raspberry Pi
Measure temperature and humidity with Raspberry Pi3 and visualize with Ambient
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Getting Started with Yocto Project with Raspberry Pi 4 and WSL2
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Control music playback on a smartphone connected to Raspberry Pi 3 and bluetooth with AVRCP
Introduced python3-OpenCV3 to Raspberry Pi
Easy to use Nifty Cloud API with botocore and python
Mutter plants with Raspberry Pi
Update Python for Raspberry Pi to 3.7 or later with pyenv
Create a web surveillance camera with Raspberry Pi and OpenCV
Python beginner opens and closes interlocking camera with Raspberry Pi
Create an LCD (16x2) game with Raspberry Pi and Python
I tried connecting Raspberry Pi and conect + with Web API
I talked to Raspberry Pi
Introduction to RDB with sqlalchemy Ⅰ
Production of temperature control system with Raspberry Pi and ESP32 (1)
Easy to make with syntax
Introducing PyMySQL to raspberry pi3
Measure and compare temperature with Raspberry Pi and automatically generate graph
Machine learning to learn with Nogizaka46 and Keyakizaka46 Part 1 Introduction
[Raspberry Pi] How to continuously monitor directories and run arbitrary shells with hooks for changes
Introduction to discord.py (3) Using voice
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
Measure temperature, humidity, etc. with SensorTag and send it to Ambient via Raspberry Pi 3 to graph it Part 2
Log in to Raspberry PI with ssh without password (key authentication)
Let's access your Raspberry Pi from outside your home with VPN (WireGuard)
I tried to automate the watering of the planter with Raspberry Pi
I made a web server with Raspberry Pi to watch anime
Christmas classic (?) Lighting a Christmas tree with Raspberry Pi and Philips Hue
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Make a Kanji display compass with Raspberry Pi and Sense Hat
Graph display of household power consumption with 3GPI and Raspberry Pi
Link SORACOM, home appliances and LINE Bot [Python / Flask / Raspberry Pi]
Port FreeRTOS to Raspberry Pi 4B
[Raspberry Pi] Stepping motor control with Raspberry Pi
Use vl53l0x with Raspberry Pi (python)
Servo motor control with Raspberry Pi
MQTT on Raspberry Pi and Mac
[Introduction to Python3 Day 1] Programming and Python
Easy Slackbot with Docker and Errbot