From d3a9a3e4675a88b0a36d1d0c779241bb029d90df Mon Sep 17 00:00:00 2001
From: eljamm <83901271+eljamm@users.noreply.github.com>
Date: Mon, 13 Nov 2023 03:13:21 +0100
Subject: [PATCH] Fixes and Improvements
---
app/asgi.py | 8 +-
raspberry/raspi/sensors/__init__.py | 3 +-
raspberry/raspi/sensors/distance.py | 37 +-
raspberry/raspi/utils/utils.py | 15 +-
raspberry/ws_control.py | 17 +-
sensor/consumers.py | 60 +-
.../static/sensor/css/sensor/ultrasonic.css | 106 +++
.../sensor/css/sensor/ultrasonic.css.map | 1 +
.../sensor/images/wiring/pins/ultrasonic.svg | 828 +++++++++---------
sensor/static/sensor/js/sensors/ultrasonic.js | 78 ++
.../static/sensor/scss/sensor/ultrasonic.scss | 82 ++
.../templates/sensor/sensors/ultrasonic.html | 13 +
sensor/views.py | 4 +-
13 files changed, 793 insertions(+), 459 deletions(-)
create mode 100644 sensor/static/sensor/css/sensor/ultrasonic.css
create mode 100644 sensor/static/sensor/css/sensor/ultrasonic.css.map
create mode 100644 sensor/static/sensor/js/sensors/ultrasonic.js
create mode 100644 sensor/static/sensor/scss/sensor/ultrasonic.scss
diff --git a/app/asgi.py b/app/asgi.py
index 096a334..a9daff9 100644
--- a/app/asgi.py
+++ b/app/asgi.py
@@ -7,6 +7,7 @@
https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
"""
+import sensor.routing
import os
from channels.auth import AuthMiddlewareStack
@@ -19,15 +20,14 @@
django_asgi_app = get_asgi_application()
-import sensor.routing
application = ProtocolTypeRouter({
- "http": django_asgi_app,
- "websocket": AllowedHostsOriginValidator(
+ "http": django_asgi_app,
+ "websocket": AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter(
sensor.routing.websocket_urlpatterns
)
)
),
-})
\ No newline at end of file
+})
diff --git a/raspberry/raspi/sensors/__init__.py b/raspberry/raspi/sensors/__init__.py
index 1e99560..6df2210 100644
--- a/raspberry/raspi/sensors/__init__.py
+++ b/raspberry/raspi/sensors/__init__.py
@@ -6,4 +6,5 @@
from .ledmatrix import Matrix
from .buzzer import Buzzer
from .segment import Segment
-from .relay import Relay
\ No newline at end of file
+from .relay import Relay
+from .distance import Ultrasonic
diff --git a/raspberry/raspi/sensors/distance.py b/raspberry/raspi/sensors/distance.py
index fb2e37b..a72f055 100644
--- a/raspberry/raspi/sensors/distance.py
+++ b/raspberry/raspi/sensors/distance.py
@@ -1,34 +1,34 @@
-# WORK IN PROGRESS
-
import json
import logging
from time import sleep
from gpiozero import DistanceSensor
-from gpiozero.pins.pigpio import PiGPIOFactory
-class Ultrasound:
- def __init__(self, pins, pin_factory=PiGPIOFactory(),
- setup=False):
+class Ultrasonic:
+ def __init__(self, pins, setup=False):
self.echo = pins["E"]
self.trigger = pins["T"]
- self.pin_factory = pin_factory
if setup:
self.setup()
def setup(self):
- self.device = DistanceSensor(
- echo=self.echo, trigger=self.trigger, pin_factory=self.pin_factory)
+ self.device = DistanceSensor(echo=self.echo, trigger=self.trigger)
+
+ def cleanup(self):
+ self.device.close()
def read(self):
- data = sensor.distance*100
+ distance = self.device.distance*100
+
+ message = "Distance: {:.1f} cm".format(distance)
json_file = {
"sensor": "ultrasonic",
- "message": data,
+ "message": message,
"message_type": "data",
+ "distance": distance
}
return json_file
@@ -54,10 +54,13 @@ def process(self, ws, delay):
"E": 17
}
- sensor = Ultrasound(pins)
-
- sensor.wait_for_active()
+ sensor = Ultrasonic(pins, setup=True)
- while True:
- print('Distance: ', sensor.distance * 100)
- sleep(1)
+ try:
+ while True:
+ distance = sensor.device.distance*100
+ print('Distance: ', "Distance: {:.1f} cm".format(distance))
+ sleep(1)
+ except KeyboardInterrupt:
+ sensor.cleanup()
+ print("\nQuitting Program")
diff --git a/raspberry/raspi/utils/utils.py b/raspberry/raspi/utils/utils.py
index ffda60f..01794bf 100644
--- a/raspberry/raspi/utils/utils.py
+++ b/raspberry/raspi/utils/utils.py
@@ -13,7 +13,7 @@ async def async_receive(ws):
def run(sensor, pi):
(logger, dht11, mat8x8, dpad, array_led,
- lcd, joystick, buzzer, segment, relay) = pi.sensors
+ lcd, joystick, buzzer, segment, relay, ultra) = pi.sensors
# --- Sensor Regex --- #
sensor_is_dht = re.search(".*dht11-.*", sensor)
@@ -24,6 +24,7 @@ def run(sensor, pi):
sensor_is_buz = re.search(".*buzzer-.*", sensor)
sensor_is_seg = re.search(".*7segment.*", sensor)
sensor_is_rel = re.search(".*relay-.*", sensor)
+ sensor_is_ult = re.search(".*ultrasonic.*", sensor)
# --- Extra Regex --- #
extra_is_gauge = re.search(".*gauge.*", sensor)
@@ -86,6 +87,10 @@ def run(sensor, pi):
relay.setup()
relay.com.on() # Power COM on from GPIO
+ # Ultrasonic
+ elif sensor_is_ult:
+ ultra.setup()
+
while True:
message = pi.message
message_type = pi.message_type
@@ -149,6 +154,10 @@ def run(sensor, pi):
relay.com.off() # Power COM off from GPIO
relay.cleanup()
+ # Ultrasonic
+ elif sensor_is_ult:
+ ultra.cleanup()
+
break
# --- Process --- #
@@ -228,6 +237,10 @@ def run_scroll(stop_event: threading.Event):
if extra_is_alarm:
relay.toggle_switch(1.0)
+ # Ultrasonic
+ elif sensor_is_ult:
+ ultra.process(pi.ws, 1.5)
+
except RuntimeError as error:
logger.warning(error.args[0])
sleep(2.0)
diff --git a/raspberry/ws_control.py b/raspberry/ws_control.py
index 0dd49c7..a6984d4 100644
--- a/raspberry/ws_control.py
+++ b/raspberry/ws_control.py
@@ -11,7 +11,7 @@
from raspi import (
DHT11, LCD, ArrayLED, DPad, Joystick, Matrix, Buzzer, Segment, Relay,
- async_receive, run
+ Ultrasonic, async_receive, run
)
@@ -118,19 +118,28 @@ def __init__(self, server, port, pi_name):
self.segment = Segment(seg_pins, multi=seg_multi)
# --- Relay --- #
- pins = {
+ relay_pins = {
"COM": 21, # Common pin
"SWITCH": 20 # Relay coil switch
}
- self.relay = Relay(pins)
+ self.relay = Relay(relay_pins)
+
+ # --- Ultrasonic --- #
+
+ ultra_pins = {
+ "T": 18,
+ "E": 17
+ }
+
+ self.ultra = Ultrasonic(ultra_pins)
# --- Misc --- #
self.sensor = ""
self.sensors = [self.logger, self.dht11, self.mat8x8, self.dpad,
self.array_led, self.lcd, self.joystick, self.buzzer,
- self.segment, self.relay]
+ self.segment, self.relay, self.ultra]
if __name__ == "__main__":
diff --git a/sensor/consumers.py b/sensor/consumers.py
index e338b5d..8d2c62f 100644
--- a/sensor/consumers.py
+++ b/sensor/consumers.py
@@ -50,19 +50,28 @@ async def receive(self, text_data):
'time': now.isoformat()
}
- if (sensor == 'dht11'):
- temp = text_data_json['temp']
- hum = text_data_json['hum']
+ # Write data to database
+ if message_type != "command":
+ if (sensor == 'dht11'):
+ temp = text_data_json['temp']
+ hum = text_data_json['hum']
- dht11_json = {
- 'temp': temp,
- 'hum': hum,
- }
+ tmp_json = {
+ 'temp': temp,
+ 'hum': hum,
+ }
- json_file = {**json_file, **dht11_json}
+ json_file = {**json_file, **tmp_json}
+
+ if (sensor == 'ultrasonic'):
+ distance = text_data_json['distance']
+
+ tmp_json = {
+ 'distance': distance,
+ }
+
+ json_file = {**json_file, **tmp_json}
- # Write data to database
- if message_type != "control":
await self.register_data(self.pi_name, sensor, now, message)
# Send message to group
@@ -84,16 +93,26 @@ async def sensor_message(self, event):
'time': now
}
- if (sensor == 'dht11'):
- temp = event['temp']
- hum = event['hum']
+ if message_type != "command":
+ if (sensor == 'dht11'):
+ temp = event['temp']
+ hum = event['hum']
+
+ tmp_json = {
+ 'temp': temp,
+ 'hum': hum,
+ }
+
+ json_file = {**json_file, **tmp_json}
+
+ if (sensor == 'ultrasonic'):
+ distance = event['distance']
- dht11_json = {
- 'temp': temp,
- 'hum': hum,
- }
+ tmp_json = {
+ 'distance': distance,
+ }
- json_file = {**json_file, **dht11_json}
+ json_file = {**json_file, **tmp_json}
# Send message to WebSocket
await self.send(text_data=json.dumps(json_file))
@@ -104,6 +123,7 @@ def register_data(self, raspi, sensor, time, data):
pi = Raspi.objects.get(name=raspi)
se = pi.sensor_set.get(name=sensor)
se.item_set.create(datetime=time, data=data)
- except:
- #logging.debug("Raspi: %s | Sensor: %s | Time: %s | Data: %s" % (raspi, sensor, time, data))
+ except Exception:
+ # logging.debug("Raspi: %s | Sensor: %s | Time: %s | Data: %s" %
+ # (raspi, sensor, time, data))
logging.debug("Sensor does not exist in database")
diff --git a/sensor/static/sensor/css/sensor/ultrasonic.css b/sensor/static/sensor/css/sensor/ultrasonic.css
new file mode 100644
index 0000000..0cd9a5a
--- /dev/null
+++ b/sensor/static/sensor/css/sensor/ultrasonic.css
@@ -0,0 +1,106 @@
+.ultra {
+ width: 30vw;
+}
+
+.ultra-container {
+ display: flex;
+ flex-direction: row;
+ align-items: center;
+ justify-content: center;
+ margin-bottom: 5%;
+}
+@media (min-width: 1200px) {
+ .ultra-container {
+ padding: 2% 10%;
+ }
+}
+@media (max-width: 1199.98px) {
+ .ultra-container {
+ padding: 2% 8%;
+ }
+}
+@media (max-width: 991.98px) {
+ .ultra-container {
+ padding: 2% 6%;
+ }
+}
+@media (max-width: 767.98px) {
+ .ultra-container {
+ flex-direction: column;
+ padding: 2% 4%;
+ }
+}
+@media (max-width: 575.98px) {
+ .ultra-container {
+ flex-direction: column;
+ padding: 2% 2%;
+ }
+}
+
+.ultra-box {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ justify-content: center;
+ object-fit: fill;
+ font-style: bold;
+ text-align: center;
+ font-size: 24px;
+}
+.ultra-box p {
+ margin: 0%;
+}
+@media (min-width: 1200px) {
+ .ultra-box p {
+ font-size: 2.2rem;
+ }
+}
+@media (max-width: 1199.98px) {
+ .ultra-box p {
+ font-size: 2rem;
+ }
+}
+@media (max-width: 991.98px) {
+ .ultra-box p {
+ font-size: 1.6rem;
+ }
+}
+@media (max-width: 767.98px) {
+ .ultra-box p {
+ font-size: 1.5rem;
+ }
+}
+@media (max-width: 575.98px) {
+ .ultra-box p {
+ font-size: 1.2rem;
+ }
+}
+@media (min-width: 1200px) {
+ .ultra-box div {
+ font-size: 2rem;
+ }
+}
+@media (max-width: 1199.98px) {
+ .ultra-box div {
+ font-size: 1.6rem;
+ }
+}
+@media (max-width: 991.98px) {
+ .ultra-box div {
+ font-size: 1.4rem;
+ }
+}
+@media (max-width: 767.98px) {
+ .ultra-box div {
+ font-size: 1.2rem;
+ margin-bottom: 15%;
+ }
+}
+@media (max-width: 575.98px) {
+ .ultra-box div {
+ font-size: 1.1rem;
+ margin-bottom: 10%;
+ }
+}
+
+/*# sourceMappingURL=ultrasonic.css.map */
diff --git a/sensor/static/sensor/css/sensor/ultrasonic.css.map b/sensor/static/sensor/css/sensor/ultrasonic.css.map
new file mode 100644
index 0000000..9905637
--- /dev/null
+++ b/sensor/static/sensor/css/sensor/ultrasonic.css.map
@@ -0,0 +1 @@
+{"version":3,"sourceRoot":"","sources":["../../scss/sensor/ultrasonic.scss","../../scss/utils/_common.scss","../../scss/third-party/bootstrap/mixins/_breakpoints.scss"],"names":[],"mappings":"AAGA;EACI;;;AAGF;ECDA;EACA;EAIA;EACA;EDiBE;;AEkCA;EFxDF;IAKI;;;AEgEF;EFrEF;IAQI;;;AE6DF;EFrEF;IAWI;;;AE0DF;EFrEF;IAcI;IACA;;;AEsDF;EFrEF;IAkBI;IACA;;;;AAMN;EC/BE;EACA;EASA;EACA;EDwBE;EAEA;EACA;EACA;;AAEA;EACE;;AEoBF;EFrBA;IAII;;;AE8BJ;EFlCA;IAOI;;;AE2BJ;EFlCA;IAUI;;;AEwBJ;EFlCA;IAaI;;;AEqBJ;EFlCA;IAgBI;;;AEKJ;EFDA;IAEI;;;AEYJ;EFdA;IAKI;;;AESJ;EFdA;IAQI;;;AEMJ;EFdA;IAWI;IACA;;;AEEJ;EFdA;IAeI;IACA","file":"ultrasonic.css"}
\ No newline at end of file
diff --git a/sensor/static/sensor/images/wiring/pins/ultrasonic.svg b/sensor/static/sensor/images/wiring/pins/ultrasonic.svg
index a2d6d4b..bd3fb2f 100644
--- a/sensor/static/sensor/images/wiring/pins/ultrasonic.svg
+++ b/sensor/static/sensor/images/wiring/pins/ultrasonic.svg
@@ -4,7 +4,7 @@
height="136.828346pt"
viewBox="0 0 561.826772 136.828346"
version="1.2"
- id="svg1005"
+ id="svg1008"
sodipodi:docname="ultrasonic.svg"
inkscape:version="1.1.1 (3bf5ae0d25, 2021-09-20)"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
@@ -13,7 +13,7 @@
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
Distance
+ +