2022-02-24 20:29:49 +00:00
|
|
|
import os, sys
|
2022-02-09 10:49:00 +00:00
|
|
|
from time import sleep
|
2022-03-01 17:19:44 +00:00
|
|
|
import yaml
|
2022-02-24 20:29:49 +00:00
|
|
|
import paho.mqtt.client as mqtt
|
2022-03-01 16:48:55 +00:00
|
|
|
import json, smbus2, si7201, tmp006, lis3dh, hci, gpiozero
|
2022-02-09 10:49:00 +00:00
|
|
|
|
2022-02-24 20:29:49 +00:00
|
|
|
# Global Sensor Data Variables
|
|
|
|
dailysteps = 0
|
|
|
|
fallen = False
|
|
|
|
|
2022-03-01 16:48:55 +00:00
|
|
|
def incrementStepCount(interrupt, sensor) -> None:
|
2022-02-24 20:29:49 +00:00
|
|
|
global dailysteps
|
|
|
|
dailysteps += 1
|
2022-03-01 16:48:55 +00:00
|
|
|
sensor.resetint2()
|
2022-02-24 20:29:49 +00:00
|
|
|
|
2022-03-01 16:48:55 +00:00
|
|
|
def setFallen(interrupt, sensor) -> None:
|
2022-02-24 20:29:49 +00:00
|
|
|
global fallen
|
|
|
|
fallen = True
|
2022-03-01 16:48:55 +00:00
|
|
|
sensor.resetint1()
|
2022-02-24 20:29:49 +00:00
|
|
|
|
|
|
|
# Setup
|
2022-02-09 10:49:00 +00:00
|
|
|
bus = smbus2.SMBus(1) # set up I2C bus 1
|
|
|
|
|
2022-02-24 20:29:49 +00:00
|
|
|
temphum = si7201.Si7201(bus, 0x40) # set up Si7201 sensor
|
|
|
|
temphum.reset() # reset Si7201
|
|
|
|
|
|
|
|
irtemp = tmp006.TMP006(bus, 0x41, tmp006.SAMPLERATE_4HZ) # set up TMP006 sensor
|
|
|
|
irtemp.active = 1 # turn on TMP006
|
|
|
|
|
2022-03-01 16:48:55 +00:00
|
|
|
accel = lis3dh.LIS3DH(bus, 10, 0x18) # set up LIS3DH sensor
|
|
|
|
fall = gpiozero.Button(18, pull_up = False) # GPIO17: Freefall Interrupt (INT1)
|
2022-03-01 17:19:44 +00:00
|
|
|
fall.when_activated = lambda: setFallen(fall, accel) # set fallen to True when Freefall Interrupt (INT1) is triggered
|
2022-03-01 16:48:55 +00:00
|
|
|
step = gpiozero.Button(17, pull_up = False) # GPIO18: Step Counter Interrupt (INT2)
|
2022-03-01 17:19:44 +00:00
|
|
|
step.when_activated = lambda: incrementStepCount(step, accel) # increment step count when Step Counter Interrupt (INT2) is triggered
|
2022-02-24 20:29:49 +00:00
|
|
|
|
2022-02-18 17:43:49 +00:00
|
|
|
with open(".secrets.yml", "r") as secrets:
|
|
|
|
try:
|
2022-03-01 17:19:44 +00:00
|
|
|
secrets = yaml.load(secrets, Loader = yaml.SafeLoader)
|
2022-02-18 17:43:49 +00:00
|
|
|
key = secrets["key"] # Get Base64 encoded device public key from secrets file
|
2022-03-01 17:19:44 +00:00
|
|
|
except ImportError as exc:
|
2022-02-18 17:43:49 +00:00
|
|
|
print(exc)
|
2022-02-24 20:29:49 +00:00
|
|
|
sleep(60) # 60s delay before restarting
|
|
|
|
os.execl(sys.executable, os.path.abspath(__file__), *sys.argv) # Restart propgram
|
2022-02-18 17:43:49 +00:00
|
|
|
btcast = hci.HCIBroadcaster(key) # set up HCI Broadcaster
|
|
|
|
|
2022-02-24 20:29:49 +00:00
|
|
|
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
|
2022-02-09 10:49:00 +00:00
|
|
|
while True:
|
2022-02-24 20:29:49 +00:00
|
|
|
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
|