-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminiProjet.py
More file actions
32 lines (23 loc) · 894 Bytes
/
Copy pathminiProjet.py
File metadata and controls
32 lines (23 loc) · 894 Bytes
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
from flask import Flask, jsonify
import requests
app = Flask(__name__)
@app.route("/iot")
#http://127.0.0.1:5000/iot
def iot():
# ThingSpeak Channel 9: MathWorks Weather Station
url = "https://api.thingspeak.com/channels/9/feeds.json?results=10"
try:
response = requests.get(url)
# Raise an error if the HTTP status code is bad (e.g., 404 or 500)
response.raise_for_status()
data = response.json()
# Check if 'feeds' key exists to avoid KeyError
if "feeds" in data:
return jsonify(data["feeds"])
else:
return jsonify({"error": "Unexpected data format from ThingSpeak"}), 502
except requests.exceptions.RequestException as e:
# Handle connection errors (DNS, Timeout, etc.)
return jsonify({"error": str(e)}), 503
if __name__ == "__main__":
app.run(debug=True, port=5000)