-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
118 lines (118 loc) · 3.7 KB
/
server.py
File metadata and controls
118 lines (118 loc) · 3.7 KB
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
#!/usr/bin/env python3
# Author: Ramswaroop Soren(xtremerox)
# GitHub : github.com/xtremerox
# Simple flask app to serve Webapp to control GPIO
# and take feedback from RPi
from flask import Flask, request , jsonify, render_template
import RPi.GPIO as GPIO
import time
from time import sleep
import board
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
#Production server settings
#from gevent.pywsgi import WSGIServer
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(16,GPIO.OUT) #test_led
GPIO.setup(11,GPIO.OUT) #main_switch
GPIO.setup(10,GPIO.OUT) #volt_inc_mode
GPIO.setup(4,GPIO.OUT) #memory
GPIO.setup(9,GPIO.OUT) #ht_start
GPIO.setup(27,GPIO.OUT) #inc_volt
GPIO.setup(22,GPIO.OUT) #dec_volt
GPIO.setup(17,GPIO.OUT) #ht_stop
GPIO.setup(5,GPIO.OUT) #memory_relay
app = Flask(__name__, template_folder = 'html_code')
#Password
secretpasskey = '1234'
app.debug = True
@app.route('/')
@app.route('/home')
def home():
return render_template('index.html')
@app.route('/live', methods=['POST'])
def live():
content = request.get_json(silent=True)
if content['secret'] != secretpasskey:
return jsonify({'status':'dead'})
data = {'status':'alive'}
response = jsonify(data)
#response.headers.add('Access-Control-Allow-Origin', '*')
return response
@app.route('/status', methods=['GET'])
def status():
#Get state of all pins(11,10,4,9,27,22,17)
response = {}
pins = [11,10,4,9,27,22,17,16]
for pin in pins:
response[pin] = 'on' if GPIO.input(pin) == 1 else 'off'
return jsonify(response)
def reading():
#taking 30 reading for average
i2c = busio.I2C(board.SCL, board.SDA)
ads = ADS.ADS1115(i2c)
channel = AnalogIn(ads, ADS.P3)
sum = 0.00
for i in range(0,30):
sum+=channel.value
avg = str('%.4f'%(sum/30))
return avg
@app.route('/push', methods=['POST'])
def push():
#Get pushrequest and activate for 30 reading and set state to 0 and return reading
content = request.get_json(silent=True)
if content['secret'] != secretpasskey:
return jsonify({'error':'Not a valid Request'})
GPIO.output(5, GPIO.HIGH) #memory_relay high
read = reading()
GPIO.output(5, GPIO.LOW) #memory_relay high
return jsonify({'reading':read})
@app.route('/holdpush', methods=['POST'])
def holdpush():
#hold pin for 100ms and reset it and wait for another request
#change pin state and authentication using POST
content = request.get_json(silent=True)
if content['secret'] != secretpasskey:
return jsonify({'error':'Not a valid Request'})
pin = content['pin']
allowedpins = [9,17,27]
z = ''
if pin.isdigit():
pin = int(pin)
if pin not in allowedpins:
return jsonify({'error':'not a valid pin'})
GPIO.output(pin, GPIO.HIGH)
z = reading()
GPIO.output(pin, GPIO.LOW)
else:
return jsonify({'error':'not a valid pin'})
data = {'success':'1', 'reading':z}
response = jsonify(data)
#response.headers.add('Access-Control-Allow-Origin', '*')
return response
@app.route('/toggle', methods=['POST'])
def toggle():
#change pin state and authentication using POST
content = request.get_json(silent=True)
if content['secret'] != secretpasskey:
return jsonify({'error':'Not a valid Request'})
pin = content['pin']
allowedpins = [11,10,5,27,16]
if pin.isdigit():
pin = int(pin)
if pin not in allowedpins:
return jsonify({'error':'not a valid pin'})
GPIO.output(pin, GPIO.HIGH) if GPIO.input(pin) == 0 else GPIO.output(pin, GPIO.LOW)
else:
return jsonify({'error':'not a valid pin'})
data = {'success':'1'}
response = jsonify(data)
#response.headers.add('Access-Control-Allow-Origin', '*')
return response
if __name__ =="__main__":
#http_server = WSGIServer(('0.0.0.0', 5000), app)
print("Server Started waiting for request")
#http_server.serve_forever()
app.run('0.0.0.0',debug = True)