Added temperature sensor
This commit is contained in:
parent
b3dae8107b
commit
670bcda19e
4 changed files with 68 additions and 5 deletions
|
@ -2,4 +2,5 @@ gpiozero
|
||||||
luma.led_matrix
|
luma.led_matrix
|
||||||
fastapi
|
fastapi
|
||||||
uvicorn
|
uvicorn
|
||||||
fastapi-cors
|
fastapi-cors
|
||||||
|
Adafruit_DHT
|
13
src/climate.py
Normal file
13
src/climate.py
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import Adafruit_DHT
|
||||||
|
|
||||||
|
class Dht22Sensor:
|
||||||
|
def __init__(self, pin):
|
||||||
|
self.sensor = Adafruit_DHT.DHT22
|
||||||
|
self.pin = pin
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
humidity, temperature = Adafruit_DHT.read_retry(self.sensor, self.pin)
|
||||||
|
if humidity is not None and temperature is not None:
|
||||||
|
return {'temperature': temperature, 'humidity': humidity}
|
||||||
|
else:
|
||||||
|
return None
|
|
@ -20,7 +20,9 @@
|
||||||
<button type="button" onclick="flash()">Flash</button>
|
<button type="button" onclick="flash()">Flash</button>
|
||||||
</form>
|
</form>
|
||||||
<button type="button" onclick="displayTime()">Display Time</button>
|
<button type="button" onclick="displayTime()">Display Time</button>
|
||||||
<button type="button" onclick="off()">Turn Off</button>
|
<button type="button" onclick="generic('off')">Turn Off</button>
|
||||||
|
<button type="button" onclick="generic('temperature')">Display Temperature</button>
|
||||||
|
<button type="button" onclick="generic('humidity')">Display Humidity</button>
|
||||||
<script>
|
<script>
|
||||||
const targetHost = "http://192.168.178.54:8000";
|
const targetHost = "http://192.168.178.54:8000";
|
||||||
|
|
||||||
|
@ -50,8 +52,8 @@
|
||||||
.catch((error) => console.error(error));
|
.catch((error) => console.error(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
function off() {
|
function generic(endpoint) {
|
||||||
const url = targetHost + "/off";
|
const url = targetHost + "/" + endpoint;
|
||||||
fetch(url, {
|
fetch(url, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
|
|
49
src/main.py
49
src/main.py
|
@ -4,6 +4,7 @@ import requests
|
||||||
from matrix import MatrixDisplay
|
from matrix import MatrixDisplay
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from climate import Dht22Sensor
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
|
@ -22,8 +23,11 @@ app.add_middleware(
|
||||||
allow_headers=["*"],
|
allow_headers=["*"],
|
||||||
)
|
)
|
||||||
|
|
||||||
matrix_display = MatrixDisplay()
|
|
||||||
should_run_time_loop = True
|
should_run_time_loop = True
|
||||||
|
dht22_pin = 27
|
||||||
|
|
||||||
|
matrix_display = MatrixDisplay()
|
||||||
|
dht22_sensor = Dht22Sensor(dht22_pin)
|
||||||
|
|
||||||
|
|
||||||
async def display_time():
|
async def display_time():
|
||||||
|
@ -61,6 +65,49 @@ async def turn_off():
|
||||||
return {"message": "Display turned off"}
|
return {"message": "Display turned off"}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/temperature")
|
||||||
|
async def temperature():
|
||||||
|
measurements = dht22_sensor.read()
|
||||||
|
if measurements is None:
|
||||||
|
return {"message": "Failed to read temperature"}
|
||||||
|
|
||||||
|
|
||||||
|
global should_run_time_loop
|
||||||
|
was_clock_runnign = should_run_time_loop
|
||||||
|
should_run_time_loop = False
|
||||||
|
|
||||||
|
|
||||||
|
matrix_display.show_text(measurements["temperature"] + "°C")
|
||||||
|
|
||||||
|
if was_clock_runnign:
|
||||||
|
should_run_time_loop = True
|
||||||
|
asyncio.create_task(display_time())
|
||||||
|
|
||||||
|
return measurements
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/humidity")
|
||||||
|
async def humidity():
|
||||||
|
measurements = dht22_sensor.read()
|
||||||
|
if measurements is None:
|
||||||
|
return {"message": "Failed to read humidity"}
|
||||||
|
|
||||||
|
|
||||||
|
global should_run_time_loop
|
||||||
|
was_clock_runnign = should_run_time_loop
|
||||||
|
should_run_time_loop = False
|
||||||
|
|
||||||
|
|
||||||
|
matrix_display.show_text(measurements["humidity"] + "%")
|
||||||
|
|
||||||
|
if was_clock_runnign:
|
||||||
|
should_run_time_loop = True
|
||||||
|
asyncio.create_task(display_time())
|
||||||
|
|
||||||
|
return measurements
|
||||||
|
|
||||||
|
|
||||||
@app.post("/flash")
|
@app.post("/flash")
|
||||||
async def flash(count: int = 1):
|
async def flash(count: int = 1):
|
||||||
global should_run_time_loop
|
global should_run_time_loop
|
||||||
|
|
Loading…
Reference in a new issue