[PYTHON] Run LEDmatrix interactively with Raspberry Pi 3B + on Slackbot

This is a continuation of Previous article.

environment

Raspberry Pi 3B + (including SD card) RGB LED matrix (6mm pitch 32 x 32) 6 sheets Connector for LED matrix MAXWELL switching power supply 3 power cords for LED panel 6 IDC flat cables 50 jump wires, male and female, and 50 male and female bread board

What you want to achieve

I want to switch the display of the electric bulletin board from Slack on my smartphone and create an environment where I can easily operate it without having to operate the screen of Raspberry Pi.

How to use Slackbot

Introduce Slackbot to operate Raspberry Pi from Slack. With Slackbot, you can make it work according to the message sent to Slack.

Installation

First, install the library to handle Slackbot with python.

$ sudo pip install slackbot

Create a bot account

First, create a new workspace in Slack. After that, please create a bot from this site. When you press "Add bot integration", an API token will be issued. Use this API token.

Invite your bot account to a channel

Once the bot account is created, it will be added to the app as shown below. Invite this app to your channel. 68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3535373637392f62316638386465342d303837332d626231332d346630642d63316636303166313830.png

Slackbot directory structure

Set the Slackbot directory structure as follows. Make only slackbot_settings.py with this name. Any name is fine for the other two.

slackbot
    ├ bot.py #Run slackbot
    ├ slackbot_settings.py #Slackbot settings
    └ botmodule.py #Description of operation content

Write a program

slackbot_settings.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 「API_Please describe the API token obtained from Slack in "TOKEN"
API_TOKEN = "Obtained API token"

#Default response when mentioning unknown words
DEFAULT_REPLY = "There is no such command."
 
#Read an external file. botmodule.Load py
PLUGINS = [
    'slackbot.plugins',
    'botmodule',
]

In slackbot_settings.py, write the API token you got. Also, describe the file that describes the operation as a plug-in. In DEFAULT_REPLY, you can set a response when an unregistered word arrives by mentioning the bot.

bot.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from slackbot.bot import Bot
from slacker import Slacker
import slackbot_settings

#Launch the bot
def main():

    #from here
    text="Send a command to this channel to operate the scoreboard.\n"\
        "You can operate it by sending the following command.\n"\
            "<Command name>\n → command"
    sla=Slacker("Obtained API token")
    sla.chat.post_message('The name of the channel you want to post', text, as_user=True)
    #Up to this point, describe only if you want to post at startup

    bot = Bot()
    bot.run()
 
if __name__ == "__main__":
    main()

The bot works by executing bot.py. If you want to send a message when the bot starts, use Slacker. Slacker also works with the acquired API token. Please specify the channel you want to post.

botmodule.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from slackbot.bot import respond_to
from slackbot.bot import listen_to
import io,sys
import subprocess
import text_image_ledmatrix
#sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
import requests
import slackbot_settings
import time

def processmatrix():
    f=open('clear.txt')
    line=f.readline()
    while line:
        ans=line
        line=f.readline()			
    f.close()
    ans=ans.strip()
    if ans=='on':
        return True
    else:
        return False

@listen_to('-print(.*)')
def print(message, something):
        if processmatrix():
                text_image_ledmatrix.add_text('clear')
                time.sleep(3)
        message.react('ok')
        message.reply('[{0}]It is displayed'.format(something))
        text_image_ledmatrix.ledprint(something)

@listen_to('-scroll(.*)')
def print(message,something):
        if processmatrix():
                text_image_ledmatrix.add_text('clear')
                time.sleep(3)
        message.react('ok')
        message.reply('[{0}]Scroll'.format(something))
        text_image_ledmatrix.textscroll(something)

text_image_ledmatrix.py can be found in Previous article. Listen_to receives words other than mentions posted in the channel. It works when it receives the words in (). I'm using a regular expression later in (), which is the second argument defined in the next line. If you don't need any arguments, you don't need a regular expression. Stamp with message.react (). You can also mention the post with message.reply (). If you post at once, a bug will occur, so please do not post the next post until there is a reaction.

Try it on Slack

I actually moved it.

python3 bot.py

Let's talk to LEDmatrix in Slack. ezgif.com-video-to-gif (2).gif

It went well! Now you can easily switch the display of LED matrix.

What I was allowed to refer to

This time, I referred to the following site. https://qiita.com/undo0530/items/2139a1e8b73b3eee6e00/ http://www.denzow.me/entry/2017/12/16/225241/ https://qiita.com/minase_tetsuya/items/dba79cfe12db4557cefc https://qiita.com/o_s_t/items/f05057aefb297393a69a

in conclusion

This time, we succeeded in combining LED matrix with Slack. From the next time, we will summarize the diversification of display functions and program startup when the power is turned on.

Recommended Posts

Run LEDmatrix interactively with Raspberry Pi 3B + on Slackbot
Run BNO055 python sample code with I2C (Raspberry Pi 3B)
USB boot on Raspberry Pi 4 Model B
Programming normally with Node-RED programming on Raspberry Pi 3
Run servomotor on Raspberry Pi 3 using python
Working with sensors on Mathematica on Raspberry Pi
Build OpenCV-Python environment on Raspberry Pi B +
Working with GPS on Raspberry Pi 3 Python
Why detectMultiScale () is slow on Raspberry Pi B +
Enjoy electronic work with GPIO on Raspberry Pi
Power on / off your PC with raspberry pi
Play with your Ubuntu desktop on your Raspberry Pi 4
Introduced Ceph on Kubernetes on Raspberry Pi 4B (ARM64)
Connect to MySQL with Python on Raspberry Pi
GPS tracking with Raspberry Pi 4B + BU-353S4 (Python)
GPGPU with Raspberry Pi
pigpio on Raspberry pi
DigitalSignage with Raspberry Pi
Cython on Raspberry Pi
Record temperature and humidity with systemd on Raspberry Pi
Try debugging Python on Raspberry Pi with Visual Studio.
Control brushless motors with GPIOs on Raspberry Pi Zero
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
USB boot with Raspberry Pi 4 Model B (3) LVM edition
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Raspberry Pi 4B initial setting
Mutter plants with Raspberry Pi
raspberry pi 1 model b, python
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Run AWS IoT Device SDK for Python on Raspberry Pi
[Note] Installing vmware ESXi on Arm Fling on Raspberry Pi 4B
getrpimodel: Recognize Raspberry Pi model (A, B, B +, B2, B3, etc) with python
Port FreeRTOS to Raspberry Pi 4B
[Raspberry Pi] Stepping motor control with Raspberry Pi
Testing uart communication on Raspberry Pi
Control the motor with a motor driver using python on Raspberry Pi 3!
Use vl53l0x with Raspberry Pi (python)
raspberry pi 1 model b, node-red part 17
Servo motor control with Raspberry Pi
MQTT on Raspberry Pi and Mac
raspberry pi 4 centos7 install on docker
Serial communication with Raspberry Pi + PySerial
Install ghoto2 on Raspberry Pi (memo)
OS setup with Raspberry Pi Imager
Run SwitchBot on Windows 10 with Bleak
Try using ArUco on Raspberry Pi
Try L Chika with raspberry pi
OpenCV installation procedure on Raspberry Pi
Programd automatic start at startup with Raspberry Pi 3B + systemd Summary
VPN server construction with Raspberry Pi
Try moving 3 servos with Raspberry Pi
Power on / off Raspberry pi on Arduino
Detect switch status on Raspberry Pi 3
Install OpenMedia Vault 5 on Raspberry Pi 4
Using a webcam with Raspberry Pi
Run Polyglot on Raspberry Pi to perform morphological analysis in English
L Chika on Raspberry Pi C #
Build wxPython on Ubuntu 20.04 on raspberry pi 4