mirror of
https://github.com/supleed2/ELEC60013-ES-CW1.git
synced 2024-11-10 01:35:50 +00:00
Update to final program design
Accelerometer setup and class code to be added
This commit is contained in:
parent
69d2e7e2a1
commit
8a7951993e
72
main.py
72
main.py
|
@ -1,37 +1,61 @@
|
||||||
|
import os, sys
|
||||||
from time import sleep
|
from time import sleep
|
||||||
import yaml
|
from strictyaml import load, Map, Str, YAMLError
|
||||||
import smbus2
|
import paho.mqtt.client as mqtt
|
||||||
import si7201
|
import json, smbus2, si7201, tmp006, hci, gpiozero
|
||||||
import tmp006
|
|
||||||
import hci
|
|
||||||
|
|
||||||
|
# Global Sensor Data Variables
|
||||||
|
dailysteps = 0
|
||||||
|
fallen = False
|
||||||
|
|
||||||
|
def incrementStepCount() -> None:
|
||||||
|
global dailysteps
|
||||||
|
dailysteps += 1
|
||||||
|
|
||||||
|
def setFallen() -> None:
|
||||||
|
global fallen
|
||||||
|
fallen = True
|
||||||
|
|
||||||
|
# Setup
|
||||||
bus = smbus2.SMBus(1) # set up I2C bus 1
|
bus = smbus2.SMBus(1) # set up I2C bus 1
|
||||||
|
|
||||||
print("========== Testing Si7201 ==========")
|
temphum = si7201.Si7201(bus, 0x40) # set up Si7201 sensor
|
||||||
temphumsensor = si7201.Si7201(bus) # set up Si7201 sensor
|
temphum.reset() # reset Si7201
|
||||||
temphumsensor.reset() # reset the sensor
|
|
||||||
sleep(1) # wait for sensor to reset
|
|
||||||
print(temphumsensor.temperature) # read the temperature and print
|
|
||||||
print(temphumsensor.humidity) # read the humidity and print
|
|
||||||
|
|
||||||
print("========== Testing TMP006 ==========")
|
irtemp = tmp006.TMP006(bus, 0x41, tmp006.SAMPLERATE_4HZ) # set up TMP006 sensor
|
||||||
irtempsensor = tmp006.TMP006(bus, 0x41, tmp006.SAMPLERATE_4HZ) # set up TMP006 sensor
|
irtemp.active = 1 # turn on TMP006
|
||||||
irtempsensor.active = 1 # turn on the sensor
|
|
||||||
sleep(1) # wait for sensor to turn on
|
# accel = lis3dh.LIS3DH(bus, 0x18, otherconfigflags) # set up LIS3DH sensor
|
||||||
print(irtempsensor.manID) # read the manufacturer ID and print
|
# TODO: other LIS3DH Setup
|
||||||
print(irtempsensor.devID) # read the device ID and print
|
step = gpiozero.Button(18, pull_up = False) # GPIO18: Step Counter Interrupt (INT1)
|
||||||
print(irtempsensor.temperature) # read the temperature and print
|
step.when_activated = incrementStepCount # increment step count when Step Counter Interrupt (INT1) is triggered
|
||||||
|
fall = gpiozero.Button(17, pull_up = False) # GPIO17: Freefall Interrupt (INT2)
|
||||||
|
fall.when_activated = setFallen # set fallen to True when Freefall Interrupt (INT2) is triggered
|
||||||
|
|
||||||
print("========= Testing HCI Cast =========")
|
|
||||||
with open(".secrets.yml", "r") as secrets:
|
with open(".secrets.yml", "r") as secrets:
|
||||||
try:
|
try:
|
||||||
secrets = yaml.safe_load(secrets)
|
secrets = load(secrets, schema = Map({"key": Str()}))
|
||||||
key = secrets["key"] # Get Base64 encoded device public key from secrets file
|
key = secrets["key"] # Get Base64 encoded device public key from secrets file
|
||||||
except yaml.YAMLError as exc:
|
except YAMLError as exc:
|
||||||
print(exc)
|
print(exc)
|
||||||
|
sleep(60) # 60s delay before restarting
|
||||||
|
os.execl(sys.executable, os.path.abspath(__file__), *sys.argv) # Restart propgram
|
||||||
btcast = hci.HCIBroadcaster(key) # set up HCI Broadcaster
|
btcast = hci.HCIBroadcaster(key) # set up HCI Broadcaster
|
||||||
btcast.start_advertising(5000) # start advertising with interval of 5 seconds
|
|
||||||
|
|
||||||
|
client = mqtt.Client("RaspberryPi") # set up MQTT client
|
||||||
|
client.connect("add8.duckdns.org", 8883, 60) # connect to MQTT broker
|
||||||
|
client.loop_start() # Start a new thread to handle sending MQTT messages
|
||||||
|
|
||||||
|
# Main Loop
|
||||||
while True:
|
while True:
|
||||||
btcast.start_advertising(5000) # start advertising with interval of 5 seconds
|
data = {
|
||||||
|
"devID": "testdoggo",
|
||||||
|
"air_temp": temphum.temperature,
|
||||||
|
"day_steps": dailysteps,
|
||||||
|
"hum_perc": temphum.humidity,
|
||||||
|
"pet_temp": irtemp.temperature
|
||||||
|
}
|
||||||
|
mqtt_data = json.dumps(data)
|
||||||
|
client.publish("/data", mqtt_data)
|
||||||
|
btcast.start_advertising() # Send out BT advertisement
|
||||||
|
sleep(5) # Sleep for 5 seconds to lower power consumption
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
colorzero==2.0
|
colorzero==2.0
|
||||||
gpiozero==1.6.2
|
gpiozero==1.6.2
|
||||||
PyYAML==6.0
|
paho-mqtt==1.6.1
|
||||||
|
python-dateutil==2.8.2
|
||||||
|
six==1.16.0
|
||||||
smbus2==0.4.1
|
smbus2==0.4.1
|
||||||
|
strictyaml==1.6.1
|
||||||
|
|
Loading…
Reference in a new issue