-
Notifications
You must be signed in to change notification settings - Fork 2
/
coinslot.py
executable file
·126 lines (102 loc) · 4.17 KB
/
coinslot.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
import argparse
import configparser
import board
import time
import digitalio
import adafruit_character_lcd.character_lcd as characterlcd
import os
from pyunifi.controller import Controller
from gpiozero import Button
from gpiozero import OutputDevice
lcd_rs = digitalio.DigitalInOut(board.D26)
lcd_en = digitalio.DigitalInOut(board.D19)
lcd_d7 = digitalio.DigitalInOut(board.D27)
lcd_d6 = digitalio.DigitalInOut(board.D22)
lcd_d5 = digitalio.DigitalInOut(board.D24)
lcd_d4 = digitalio.DigitalInOut(board.D25)
lcd_columns = 16
lcd_rows = 2
lcd = characterlcd.Character_LCD_Mono(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows)
coinslot = Button(17)
confirm = Button(2,bounce_time=5)
reset = Button(3,bounce_time=5)
coinslotpower = OutputDevice(4)
config = configparser.ConfigParser()
config.read('config.ini')
controller = config.get('config', 'controller')
username = config.get('config', 'username')
password = config.get('config', 'password')
port = config.get('config', 'port')
version = config.get('config', 'version')
siteid = config.get('config', 'siteid')
nosslverify = config.get('config', 'nosslverify')
certificate = config.get('config', 'certificate')
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--controller', default=controller)
parser.add_argument('-u', '--username', default=username)
parser.add_argument('-p', '--password', default=password)
parser.add_argument('-b', '--port', default=port)
parser.add_argument('-v', '--version', default=version)
parser.add_argument('-s', '--siteid', default=siteid)
parser.add_argument('-V', '--no-ssl-verify', default=nosslverify, action='store_true')
parser.add_argument('-C', '--certificate', default=certificate)
args = parser.parse_args()
ssl_verify = (not args.no_ssl_verify)
if ssl_verify and len(args.certificate) > 0:
ssl_verify = args.certificate
c = Controller(args.controller, args.username, args.password, args.port, args.version, args.siteid, ssl_verify=ssl_verify)
# https://stackoverflow.com/questions/52126586/python-scripting-for-coins-slot-raspberry-pi
while True:
total = 0
state = True
counter = 0
lcd.clear()
lcd.message = 'Insert coin and\npress confirm'
while state:
if coinslot.is_pressed:
counter+=1
time.sleep(.05)
lcd.clear()
if counter == 1:
lcd.message = '{} Peso\ninserted'.format(counter)
else:
lcd.message = '{} Pesos\ninserted'.format(counter)
# https://gist.github.com/alaudet/9e280d190bff83830dc7
if reset.is_pressed:
time.sleep(5)
if reset.is_pressed:
lcd.clear()
lcd.message = "Shutting down"
cmd = "sudo shutdown -h now"
os.system(cmd)
if confirm.is_pressed:
state = False
total = counter * 4 # 1 peso = 4 minutes
if total == 0:
lcd.clear()
lcd.message = 'No coin inserted\nplease wait'
coinslotpower.off()
time.sleep(5)
coinslotpower.on()
break
def generate_guest():
voucher = c.create_voucher(1, 1, total, up_bandwidth=4098, down_bandwidth=4098, note="unipi guest") # remove quota
code = voucher[0].get('code')
def format_code(string):
length_string = len(string)
first_length = round(length_string / 2)
first_half = string[0:first_length].lower()
second_half = string[first_length:].upper()
return first_half + '-' + second_half
voucher_code = format_code(code)
lcd.clear()
# https://stackoverflow.com/a/18175488/10025507
timeout = 90
while timeout != 0:
if reset.is_pressed:
break
lcd.message = 'Guest voucher {:2d}\n{}'.format(timeout, voucher_code)
time.sleep(1)
timeout = timeout-1
generate_guest()