From 8a7951993e3342f61a4ed587ef8b550c36c5e5a4 Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Thu, 24 Feb 2022 20:29:49 +0000 Subject: [PATCH] Update to final program design Accelerometer setup and class code to be added --- main.py | 72 ++++++++++++++++++++++++++++++++---------------- requirements.txt | 5 +++- 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/main.py b/main.py index cc0f23f..82cbad4 100644 --- a/main.py +++ b/main.py @@ -1,37 +1,61 @@ +import os, sys from time import sleep -import yaml -import smbus2 -import si7201 -import tmp006 -import hci +from strictyaml import load, Map, Str, YAMLError +import paho.mqtt.client as mqtt +import json, smbus2, si7201, tmp006, hci, gpiozero +# 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 -print("========== Testing Si7201 ==========") -temphumsensor = si7201.Si7201(bus) # set up Si7201 sensor -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 +temphum = si7201.Si7201(bus, 0x40) # set up Si7201 sensor +temphum.reset() # reset Si7201 -print("========== Testing TMP006 ==========") -irtempsensor = tmp006.TMP006(bus, 0x41, tmp006.SAMPLERATE_4HZ) # set up TMP006 sensor -irtempsensor.active = 1 # turn on the sensor -sleep(1) # wait for sensor to turn on -print(irtempsensor.manID) # read the manufacturer ID and print -print(irtempsensor.devID) # read the device ID and print -print(irtempsensor.temperature) # read the temperature and print +irtemp = tmp006.TMP006(bus, 0x41, tmp006.SAMPLERATE_4HZ) # set up TMP006 sensor +irtemp.active = 1 # turn on TMP006 + +# accel = lis3dh.LIS3DH(bus, 0x18, otherconfigflags) # set up LIS3DH sensor +# TODO: other LIS3DH Setup +step = gpiozero.Button(18, pull_up = False) # GPIO18: Step Counter Interrupt (INT1) +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: 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 - except yaml.YAMLError as exc: + except YAMLError as 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.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: - 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 diff --git a/requirements.txt b/requirements.txt index bf38714..d6290a9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,7 @@ colorzero==2.0 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 +strictyaml==1.6.1