Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from eljamm/develop
Browse files Browse the repository at this point in the history
Fixes and Improvements
  • Loading branch information
eljamm authored Nov 13, 2023
2 parents 81c8a7e + d3a9a3e commit 4e94d21
Show file tree
Hide file tree
Showing 13 changed files with 793 additions and 459 deletions.
8 changes: 4 additions & 4 deletions app/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
"""

import sensor.routing
import os

from channels.auth import AuthMiddlewareStack
Expand All @@ -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
)
)
),
})
})
3 changes: 2 additions & 1 deletion raspberry/raspi/sensors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
from .ledmatrix import Matrix
from .buzzer import Buzzer
from .segment import Segment
from .relay import Relay
from .relay import Relay
from .distance import Ultrasonic
37 changes: 20 additions & 17 deletions raspberry/raspi/sensors/distance.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")
15 changes: 14 additions & 1 deletion raspberry/raspi/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 --- #
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 13 additions & 4 deletions raspberry/ws_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from raspi import (
DHT11, LCD, ArrayLED, DPad, Joystick, Matrix, Buzzer, Segment, Relay,
async_receive, run
Ultrasonic, async_receive, run
)


Expand Down Expand Up @@ -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__":
Expand Down
60 changes: 40 additions & 20 deletions sensor/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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")
106 changes: 106 additions & 0 deletions sensor/static/sensor/css/sensor/ultrasonic.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sensor/static/sensor/css/sensor/ultrasonic.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4e94d21

Please sign in to comment.