Skip to content

Instantly share code, notes, and snippets.

@dproject21
Last active December 11, 2015 17:57
Show Gist options
  • Save dproject21/07e6d98c7636ab5f29b1 to your computer and use it in GitHub Desktop.
Save dproject21/07e6d98c7636ab5f29b1 to your computer and use it in GitHub Desktop.
こちらの記事(http://yoshiori.hatenablog.com/entry/2015/12/03/191621 )を読んで、Raspberry piを使って http://haka.yamashi.ro/ に線香上げる仕組みを作ってみた。(コードもうちょっと綺麗にする。)
import RPi.GPIO as GPIO
import time
import requests
import json
BUTTON = 7
LED_B = 11
LED_G = 13
LED_Y = 15
LED_R = 12
def led_setup(PIN):
GPIO.setup(PIN, GPIO.OUT)
def led_on(PIN):
GPIO.output(PIN, GPIO.HIGH)
def led_off(PIN):
GPIO.output(PIN, GPIO.LOW)
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(BUTTON, GPIO.IN,pull_up_down=GPIO.PUD_UP)
led_setup(LED_B)
led_setup(LED_G)
led_setup(LED_Y)
led_setup(LED_R)
def led_all_off():
led_off(LED_B)
led_off(LED_G)
led_off(LED_Y)
led_off(LED_R)
def apicall():
s = requests.Session()
params = {
'token': 'XXXXX',
}
resp = s.post('http://haka.yamashi.ro/api/v1/incenses',params=params)
return resp.status_code
def lighting(status):
if status == 201:
led_on(LED_B)
led_on(LED_G)
led_on(LED_Y)
led_on(LED_R)
elif status == 400:
led_on(LED_Y)
elif status == 401:
led_on(LED_G)
elif status == 409:
led_on(LED_B)
else:
led_on(LED_R)
time.sleep(5)
setup()
led_all_off()
prev = 0
curr = 0
press = False
for i in range(15):
curr = GPIO.input(BUTTON)
press = prev + curr
if (not(press)):
lighting(apicall())
break
else:
led_all_off()
time.sleep(1)
led_all_off()
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment