Skip to content

Instantly share code, notes, and snippets.

@Lokno
Last active August 18, 2018 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lokno/402339d46de8908270af40a59a450627 to your computer and use it in GitHub Desktop.
Save Lokno/402339d46de8908270af40a59a450627 to your computer and use it in GitHub Desktop.
A discord bot that reports changes to a pin on the RaspberryPi GPIO (used for detecting door knocks via a SW-420 Vibration Switch). Python 3.7.0
import discord
import asyncio
import RPi.GPIO as GPIO
import traceback
from discord.ext import commands
TOKEN = 'BOT_TOKEN_HERE'
description = '''A bot that reports changes to a pin on the GPIO'''
client = discord.Client()
sensor_pin = 14
GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor_pin,GPIO.IN)
async def check_door(channel):
await client.wait_until_ready()
last_value = GPIO.input(sensor_pin)
while True:
await asyncio.sleep(3)
curr_value = GPIO.input(sensor_pin)
if last_value != curr_value:
last_value = curr_value
try:
await client.send_message(channel, 'Squirtle squirt! (There might be a knock on the door!)')
except:
print('Squirtle failed to deliver the message')
traceback.format_exc()
@client.event
async def on_ready():
# get the first channel in the first server
channel = None
for server in client.servers:
for c in server.channels:
channel = c
break
try:
client.loop.create_task(check_door(channel))
except:
print('Could not schedule task.')
traceback.format_exc()
print('Squirtle is checking the door...')
client.run(TOKEN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment