I tried to make a trivia spring-like button that says "Hey" to Slack when pressing the tact switch connected to Raspberry Pi
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.
I wired it as follows. This is exactly what was written in the book. I drew the wiring diagram using software called fritzing.
In fact, it looks like this.
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
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.
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.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.
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:'
)
You're done.
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.
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