-
Notifications
You must be signed in to change notification settings - Fork 0
/
dweet_button.py
executable file
·108 lines (77 loc) · 2.85 KB
/
dweet_button.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
File: chapter02/dweet_button.py
A Python program to control an LED using the public dweet.io service
by using a Button to post a dweet.
Dependencies:
pip3 install gpiozero pigpio requests
Built and tested with Python 3.7 on Raspberry Pi 4 Model B
"""
import signal
import requests
import logging
import os
from gpiozero import Device, Button
from gpiozero.pins.pigpio import PiGPIOFactory
# Global variables
THING_NAME_FILE = 'thing_name.txt'
# Initialize Logging
logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger('main')
logger.setLevel(logging.INFO)
# Initialise GPIOZero
Device.pin_factory = PiGPIOFactory()
BUTTON_GPIO_PIN = 23
button = None
LED_STATES = ['off', 'on', 'blink']
current_led_state = 0 # off
# Make sure thing_name matches the "dweet_led thing" you want to control.
thing_name = None #'**** ADD YOUR THING NAME HERE ****'
URL = 'https://dweet.io'
def init_button():
"""Setup button"""
global button
button = Button(BUTTON_GPIO_PIN, pull_up=True, bounce_time=0.1)
button.when_pressed = button_pressed
def button_pressed():
"""Button pressed handler"""
cycle_led_state()
def cycle_led_state():
"""Send revolving dweet to change LED from off -> on -> blink -> off -> ..."""
global current_led_state
current_led_state += 1
if current_led_state >= len(LED_STATES):
current_led_state = 0
state = LED_STATES[current_led_state]
logger.info('Setting LED state %s', state)
send_dweet(thing_name, {'state': state})
def send_dweet(thing_name, values):
"""Send a dweet to a thing."""
resource = URL + '/dweet/for/' + thing_name
logger.debug('Dweeting to url %s with values %s', resource, values)
r = requests.get(resource, params=values)
if r.status_code == 200:
dweet_response = r.json()
logger.debug('Dweet response was %s', dweet_response)
return dweet_response
else:
logger.error('Dweeting dweet failed with http status %s', r.status_code)
return {}
def resolve_thing_name(thing_file):
if os.path.exists(thing_file):
with open(thing_file, 'r') as file_handle:
name = file_handle.read()
logger.info('Thing name ' + name + ' loaned from ' + thing_file)
return name.strip()
else:
logger.info('No comprendre')
# Initialise Module
thing_name = resolve_thing_name(THING_NAME_FILE)
init_button()
# Main entry point
if __name__ == '__main__':
# You could adopt the get_last_dweet() / process_dweet() from dweet_listner.py
# to initialise the led state in this file. For simplicity we're just
# starting with 'Off' (current_led_state = 0)
print("Press button to send a dweet to turn LED on, blink or off.")
# Stop Python from exiting.
signal.pause()