-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
119 lines (110 loc) · 3.66 KB
/
main.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
from flask import Flask, url_for, render_template, request, flash, redirect, session, abort, jsonify
import RPi.GPIO as GPIO
import subprocess, os, logging
import ipdb
from config import Config
from time import sleep
'''initial VAR'''
# Light GPIO
RELAIS_4_GPIO = 2
# Water GPIO
RELAIS_WATER_GPIO = 22
logging.basicConfig(
filename='server.log',
level=logging.DEBUG,
format='[%(asctime)s] %(levelname)s:%(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p'
)
app = Flask(__name__)
app.config.from_object(Config)
TOKEN = app.config['TOKEN']
'''functions'''
# Turn the light on
@app.route('/accendilucicortile', methods=['POST'])
def lights_on():
token = request.json.get('token', None)
if token != TOKEN:
logging.debug('not authorized access')
return jsonify({"msg": "Unauthorized"}), 400
elif token == TOKEN:
logging.debug('Turn the lights on')
GPIO.output(RELAIS_4_GPIO, GPIO.LOW)
logging.debug('Lights are on')
return jsonify({"msg": "Lights on"}), 200
else:
return jsonify({"msg": "This should never happen"}), 200
# lights off
@app.route('/spegnilucicortile', methods=['POST'])
def lights_off():
token = request.json.get('token', None)
if token != TOKEN:
logging.debug('not authorized access')
return jsonify({"msg": "Unauthorized"}), 400
elif token == TOKEN:
logging.debug('Turn the lights off')
GPIO.output(RELAIS_4_GPIO, GPIO.HIGH)
logging.debug('Lights are off')
return jsonify({"msg": "Lights off"}), 200
else:
return jsonify({"msg": "This should never happen"}), 200
# water on
@app.route('/accendiacqua', methods=['POST'])
def water_on():
token = request.json.get('token', None)
if token != TOKEN:
logging.debug('not authorized access')
return jsonify({"msg": "Unauthorized"}), 400
elif token == TOKEN:
GPIO.output(RELAIS_WATER_GPIO, GPIO.LOW)
logging.debug('Starting irrigation')
sleep(5)
if GPIO.input(RELAIS_WATER_GPIO):
logging.error('Irrigation not started')
else:
logging.debug('Irrigation correctly started')
return "<h1>Irrigation is on</h1>"
else:
return jsonify({"msg": "This should never happen"}), 200
# water off
@app.route('/spegniacqua', methods=['POST'])
def water_off():
token = request.json.get('token', None)
if token != TOKEN:
logging.debug('not authorized access')
return jsonify({"msg": "Unauthorized"}), 400
elif token == TOKEN:
GPIO.output(RELAIS_WATER_GPIO, GPIO.HIGH)
logging.debug('Stopping Irrigation')
sleep(5)
if GPIO.input(RELAIS_WATER_GPIO):
logging.debug('Irrigation correctly stopped')
else:
logging.error('Irrigation not stopped')
return "<h1>Irrigation is off</h1>"
else:
return jsonify({"msg": "This should never happen"}), 200
if __name__ == '__main__':
logging.info('starting up')
GPIO.setmode(GPIO.BCM)
GPIO.setup(RELAIS_4_GPIO,GPIO.OUT, initial=GPIO.HIGH) #lights off
if GPIO.input(RELAIS_4_GPIO):
logging.debug('Luce spenta')
else:
logging.debug('Luce accesa')
GPIO.setup(RELAIS_WATER_GPIO, GPIO.OUT, initial=GPIO.HIGH) #water off
if GPIO.input(RELAIS_WATER_GPIO):
logging.debug('Irrigazione spenta')
else:
logging.debug('Irrigazione accesa')
app.secret_key = os.urandom(12)
try:
app.run(
debug=True,
host='0.0.0.0',
port=5000
)
except:
logging.info('exception')
finally:
GPIO.output(RELAIS_WATER_GPIO, GPIO.HIGH)
GPIO.cleanup()