This show has been flagged as Clean by the host.
I have set up some LoRaWAN temperature and humidity sensors, and
am using the Things Stack to collect the data.
This gets processed via a web-hook and rendered as a graph.
https://lora-alliance.org
https://www.amazon.com/Mastering-LoRaWAN-Comprehensive-Communication-Connectivity-ebook/dp/B0CTRH6MV6
The Things Industries - https://thethingsindustries.com
server.py
import json
import sqlite3
import logging
from http.server import BaseHTTPRequestHandler, HTTPServer
rooms = {
'eui-24e12*********07': 'living-room',
'eui-24e12*********54': 'hall',
'eui-24e12*********42': 'downstairs-office',
'eui-24e12*********35': 'kitchen',
'eui-24e12*********29': 'conservatory',
'eui-24e12*********87': 'landing',
'eui-24e12*********45': 'main-bedroom',
'eui-24e12*********89': 'upstairs-office',
'eui-24e12*********38': 'spare-bedroom',
'eui-24e12*********37': 'playroom'
};
# Configure logging
logging.basicConfig(filename="server_log.txt", level=logging.INFO, format="%(asctime)s - %(message)s")
# Define the web server handler
class MyServerHandler(BaseHTTPRequestHandler):
# Handle POST requests
def do_POST(self):
length = int(self.headers.get('Content-Length'))
data = self.rfile.read(length).decode('utf-8')
try:
# Validate and parse JSON data
json_data = json.loads(data)
logging.info(f"Received valid JSON data: {json_data}")
# Write the data to database
id = json_data["end_device_ids"]["device_id"]
room = rooms.get(id)
readat = json_data["uplink_message"]["rx_metadata"][0]["time"]
temp = json_data["uplink_message"]["decoded_payload"]["temperature"]
hum = json_data["uplink_message"]["decoded_payload"]["humidity"]
conn = sqlite3.connect('data.db')
sql = """CREATE TABLE IF NOT EXISTS data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
room TEXT,
readat DATETIME,
temp DECIMAL(4,1),
hum DECIMAL(4,1)
);"""
conn.execute(sql)
sql = "INSERT INTO data (room, readat, temp, hum) VALUES (?, ?, ?, ?)"
conn.execute(sql, (room, readat, temp, hum))
conn.commit()
conn.close()
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("Data received and logged!", "utf-8"))
except json.JSONDecodeError:
logging.error("Invalid JSON data received.")
self.send_response(400) # Bad Request
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("Invalid JSON format.", "utf-8"))
except PermissionError:
logging.error("File write permission denied.")
self.send_response(500) # Internal Server Error
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("Server error: Unable to write data to file.", "utf-8"))
# Start the server
server_address = ('0.0.0.0', 12345) # Customize host and port if needed
httpd = HTTPServer(server_address, MyServerHandler)
print("Server started on http://localhost:12345")
httpd.serve_forever()
process.php
Temperature Chart
Humidity Chart
Provide feedback on this episode.