-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
97 lines (82 loc) · 3.17 KB
/
app.py
File metadata and controls
97 lines (82 loc) · 3.17 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
from flask import Flask, request, jsonify, send_from_directory
import pymysql
import json
import os
app = Flask(__name__)
port = 3000
# Configuración de la base de datos
db = pymysql.connect(
host='localhost',
user='root',
password='admin',
database='sensores_db'
)
# Ruta para recibir los datos del ESP32
@app.route('/insertar_datos', methods=['POST'])
def insertar_datos():
data = request.json
temperatura = data.get('temperatura')
humedad = data.get('humedad')
humedad_suelo = data.get('humedad_suelo')
if temperatura is not None and humedad is not None and humedad_suelo is not None:
try:
with db.cursor() as cursor:
query = 'INSERT INTO datos_sensores (temperatura, humedad, humedad_suelo) VALUES (%s, %s, %s)'
cursor.execute(query, (temperatura, humedad, humedad_suelo))
db.commit()
return 'Datos insertados correctamente', 200
except Exception as e:
return f'Error al insertar datos: {str(e)}', 500
else:
return 'Datos incompletos', 400
# Ruta para actualizar y servir data.json
@app.route('/update-data', methods=['GET'])
def update_data():
try:
with db.cursor(pymysql.cursors.DictCursor) as cursor:
query = '''
SELECT id, temperatura, humedad, humedad_suelo,
DATE_FORMAT(timestamp, '%Y-%m-%d %H:%i:%s') as timestamp
FROM datos_sensores
ORDER BY id DESC
LIMIT 1
'''
cursor.execute(query)
results = cursor.fetchall()
with open('data.json', 'w') as json_file:
json.dump(results, json_file, indent=2)
return jsonify({'status': 'success', 'message': 'Data updated'}), 200
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)}), 500
@app.route('/update-last-15-data', methods=['GET'])
def update_last_15_data():
try:
with db.cursor(pymysql.cursors.DictCursor) as cursor:
query = '''
SELECT id, temperatura, humedad, humedad_suelo,
DATE_FORMAT(timestamp, '%Y-%m-%d %H:%i:%s') as timestamp
FROM datos_sensores
ORDER BY id DESC
LIMIT 15
'''
cursor.execute(query)
results = cursor.fetchall()
with open('last_15_data.json', 'w') as json_file:
json.dump(results, json_file, indent=2)
return jsonify({'status': 'success', 'message': 'Last 15 records updated'}), 200
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)}), 500
@app.route('/data.json', methods=['GET'])
def get_data():
return send_from_directory(os.getcwd(), 'data.json')
@app.route('/last_15_data.json', methods=['GET'])
def get_last_15_data():
return send_from_directory(os.getcwd(), 'last_15_data.json')
@app.route('/')
def index():
return send_from_directory(os.getcwd(), 'index.html')
@app.route('/<path:filename>')
def static_files(filename):
return send_from_directory(os.getcwd(), filename)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=port)