-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver (1).py
35 lines (24 loc) · 962 Bytes
/
server (1).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
from flask import Flask, request, jsonify
app = Flask(__name__)
# Variabel global untuk menyimpan data terakhir
last_data = {}
@app.route('/api/data', methods=['POST'])
def receive_data():
global last_data
data = request.json
if not data:
return jsonify({'error': 'No data received'}), 400
temp = data.get('temp')
humidity = data.get('humidity')
if temp is None or humidity is None:
return jsonify({'error': 'Invalid data'}), 400
last_data = data # Simpan data terakhir yang diterima
print(f"Received temperature: {temp} and humidity: {humidity}")
return jsonify({'message': 'Data received successfully'}), 200
@app.route('/api/last_data', methods=['GET'])
def get_last_data():
if not last_data:
return jsonify({'error': 'No data available'}), 404
return jsonify(last_data), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)