Skip to content

Instantly share code, notes, and snippets.

@makoto
Created October 4, 2017 10:54
Show Gist options
  • Save makoto/3962daf3a25bc060739d795f8b83e153 to your computer and use it in GitHub Desktop.
Save makoto/3962daf3a25bc060739d795f8b83e153 to your computer and use it in GitHub Desktop.
import time
import sys
import os
from time import sleep
from gpiozero import LED
red_led = LED(17)
import json
import urllib2
ip = '10.0.110.129'
host = 'http://' + ip + ':3000'
req = urllib2.Request(host + '/api/v1/usage')
req.add_header('Content-Type', 'application/json')
data = {'in_use':True}
response = urllib2.urlopen(req, json.dumps(data))
#if command == "start":
# red_led.on()
#else:
# red_led.off()
#sleep(3)
path = '/home/pi/mmal/event.log'
def alive(threshold):
current_time = time.time()
file_time = os.stat(path).st_mtime
file_age = current_time - file_time
print(file_age, threshold)
if file_age > threshold:
return False
else:
return True
while True:
sleep(1)
status = open(path).read().rstrip()
print("status", status, red_led.is_lit)
if status == "start" and red_led.is_lit == False:
red_led.on()
elif status == "end" and red_led.is_lit == True:
red_led.off()
else:
print("ok", status, red_led.is_lit)
data = {'in_use':(status == 'start')}
try:
response = urllib2.urlopen(req, json.dumps(data))
except Exception:
print("Cannot connect to URL endpoint")
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment