I tried to create a button for Slack with Raspberry Pi + Tact Switch

I tried to make a trivia spring-like button that says "Hey" to Slack when pressing the tact switch connected to Raspberry Pi

What was made

When you press the button connected to the breadboard, it will say "Hey" to a specific channel in Slack as shown below. If you press it multiple times, it will say "Hey" that number of times.

Screen Shot 2016-03-31 at 11.43.22 AM.png

environment

Wire

I wired it as follows. This is exactly what was written in the book. I drew the wiring diagram using software called fritzing.

qiitatest_bb.jpg

In fact, it looks like this.

reduce.jpg

Get if a button was pressed in Python

Write the following program.

inputsw.py


#Magic
import RPi.GPIO as GPIO
import time
import signal
import sys

# Ctrl+The handler when the SIGINT signal is sent by C. GPIO before the end.Call cleanup
def handler(signum, frame):
  print 'Signal handler called with signal', signum
  GPIO.cleanup()
  sys.exit(0)

#Registering a handler
signal.signal(signal.SIGINT, handler)

#Use GPIO9 as input
GPIO.setmode(GPIO.BCM)
GPIO.setup(9, GPIO.IN)
before = 0

#infinite loop
while True:
  #Returns 1 if pressed, 0 if not pressed
  now = GPIO.input(9)
  if before == 0 and now == 1:
    print("Push!!!")
  time.sleep(0.1)
  before = now

Now, when the switch is pressed during program execution, ** Push !!! ** will be displayed on the standard output. If you want to interrupt, press Ctrl + C to send a signal and interrupt.

$python inputsw.py
Push!!!
Push!!!
Push!!!
^CSignal handler called with signal 2

Mumble to a specific channel in Slack

It's okay to include it in the above from the beginning, but since it was the first time for Python to notify Slack, I implemented only this one at first.

When I checked, there was a library for Slack maintained by Slack, so I will use this.

slackhq/python-slackclient

First, install it.

$sudo pip install slackclient

Let's check if it works with the contents of the official Readme. For the token part, access the following URL in advance, issue a token, and set the issued token.

slack-token

slack.py


import time
from slackclient import SlackClient

token = "xoxp-xxxxx"
sc = SlackClient(token)
print sc.api_call(
  "chat.postMessage", channel="#general", text="Hello from Python! :tada:",
  username='pybot', icon_emoji=':robot_face:'
)

I will try it.

$python slack.py

If there is a setting, it will be displayed in Slack as follows.

Screen Shot 2016-03-31 at 10.56.07 AM.png

Let's change the message. Since I am writing in Japanese, I also added that it is UTF-8.

slack.py


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

import time
from slackclient import SlackClient

token = "xoxp-xxxx"
sc = SlackClient(token)
print sc.api_call(
  "chat.postMessage", channel="#general", text="Hey:neutral_face:",
  username='pybot', icon_emoji=':robot_face:'
)

Screen Shot 2016-03-31 at 11.21.28 AM.png

You're done.

Make Slack say something when you press the button

Simply take the programming content to ʻinputsw.py` that you created first.

inputsw.py


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

import RPi.GPIO as GPIO
import time
import signal
import sys
from slackclient import SlackClient

def post(sc):
  print sc.api_call(
    "chat.postMessage", channel="#general", text="Hey:neutral_face:",
    username='pybot', icon_emoji=':robot_face:'
  )

def handler(signum, frame):
  print 'Signal handler called with signal', signum
  GPIO.cleanup()
  sys.exit(0)

signal.signal(signal.SIGINT, handler)

GPIO.setmode(GPIO.BCM)
GPIO.setup(9, GPIO.IN)
before = 0
token = "xoxp-xxxxx"
sc = SlackClient(token)

while True:
  now = GPIO.input(9)
  if before == 0 and now == 1:
    print("Push!!!")
    post(sc)
  time.sleep(0.1)
  before = now

This time I will run it in the background.

$python inputsw.py &

If you press the button in this state, Slack will speak. If you press more than one, you will be spoken for the number of times you press.

Screen Shot 2016-03-31 at 11.43.22 AM.png

annoying.

Kill the process when you're done.

#Check process ID
$ps aux |grep python
pi        2630  0.4  1.4  18940 13872 pts/0    S    17:41   0:00 python inputsw.py
pi        2639  0.0  0.1   5724  1852 pts/0    S+   17:44   0:00 grep --color=auto python

#Send a signal
$kill -KILL 2630

#Confirm the end
$ps aux |grep python
pi        2641  0.0  0.1   5724  1832 pts/0    S+   17:44   0:00 grep --color=auto python
[1]+Forced termination python inputsw.py

#Nothing is said when the button is pressed

I would like to try various things in combination with LEDs and sensors!

Recommended Posts

I tried to create a button for Slack with Raspberry Pi + Tact Switch
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to create a table only with Django
I tried to automatically create a report with Markov chain
I made a resource monitor for Raspberry Pi with a spreadsheet
I tried to automate [a certain task] using Raspberry Pi
I tried to create a bot for PES event notification
I tried to create a reinforcement learning environment for Othello with Open AI gym
I tried to create a linebot (implementation)
I tried to create a linebot (preparation)
I tried to automate the watering of the planter with Raspberry Pi
I made a web server with Raspberry Pi to watch anime
I tried to create a list of prime numbers with python
I tried to make a motion detection surveillance camera with OpenCV using a WEB camera with Raspberry Pi
Create a car meter with raspberry pi
I tried to make a strange quote for Jojo with LSTM
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
I tried to create a plug-in with HULFT IoT Edge Streaming [Development] (2/3)
I tried to create a plug-in with HULFT IoT Edge Streaming [Execution] (3/3)
[Outlook] I tried to automatically create a daily report email with Python
I tried to create a plug-in with HULFT IoT Edge Streaming [Setup] (1/3)
I tried L-Chika with Raspberry Pi 4 (Python edition)
I tried to build an environment of Ubuntu 20.04 LTS + ROS2 with Raspberry Pi 4
When I tried to create a virtual environment with Python, it didn't work
I tried to easily create a fully automatic attendance system with Selenium + Python
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
I tried to create a model with the sample of Amazon SageMaker Autopilot
I tried to draw a route map with Python
I tried to automatically generate a password with Python3
I want to manually create a legend with matplotlib
I tried a simple RPA for login with selenium
When I tried to do socket communication with Raspberry Pi, the protocol was different
[Python] I tried to automatically create a daily report of YWT with Outlook mail
I tried to create a class to search files with Python's Glob method in VBA
I tried scraping food recall information with Python to create a pandas data frame
Create a web surveillance camera with Raspberry Pi and OpenCV
I tried running Movidius NCS with python of Raspberry Pi3
I tried connecting Raspberry Pi and conect + with Web API
I tried to get started with Hy ・ Define a class
I tried to sort a random FizzBuzz column with bubble sort.
I made a surveillance camera with my first Raspberry PI.
I tried to divide with a deep learning language model
I want to create a Dockerfile for the time being.
I tried to create an article in Wiki.js with SQLAlchemy
I wanted to run the motor with Raspberry Pi, so I tried using Waveshare's Motor Driver Board
I tried to move ROS (Melodic) with the first Raspberry Pi (Stretch) at the beginning of 2021
I tried to create Quip API
Using a webcam with Raspberry Pi
[5th] I tried to make a certain authenticator-like tool with python
I tried to create a server environment that runs on Windows 10
I tried to create a simple credit score by logistic regression.
[2nd] I tried to make a certain authenticator-like tool with python
I tried to visualize bookmarks flying to Slack with Doc2Vec and PCA
I wanted to create a smart presentation with Jupyter Notebook + nbpresent
[3rd] I tried to make a certain authenticator-like tool with python
[Python] A memo that I tried to get started with asyncio
How to create a label (mask) for segmentation with labelme (semantic segmentation mask)
[Pandas] I tried to analyze sales data with Python [For beginners]
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to summarize everyone's remarks on slack with wordcloud (Python)