-
Notifications
You must be signed in to change notification settings - Fork 0
/
movement logger with switch
102 lines (87 loc) · 4.29 KB
/
movement logger with switch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import board
import busio
import adafruit_pcf8523
import digitalio
import os
import adafruit_sdcard
import storage
import adafruit_lsm6ds.lsm6ds33
import microcontroller
import time
import neopixel
import adafruit_bmp280
import adafruit_lis3mdl
import adafruit_sht31d
#Sensoren vorbereiten
i2c = board.I2C()
lis3mdl = adafruit_lis3mdl.LIS3MDL(i2c)
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
sht31d = adafruit_sht31d.SHT31D(i2c)
lsm6ds33 = adafruit_lsm6ds.lsm6ds33.LSM6DS33(i2c)
#RealTimeClock vorbereiten
rtc = adafruit_pcf8523.PCF8523(i2c)
days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
t = rtc.datetime
#Initialisierung der SD Karte
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
#use a digitalio pin 10 for breakout wiring:
cs = digitalio.DigitalInOut(board.D10)
sdcard = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
#blaue LED vorbereiten
led = digitalio.DigitalInOut(board.BLUE_LED)
led.direction = digitalio.Direction.OUTPUT
#neopixel vorbereiten
neo = neopixel.NeoPixel(board.NEOPIXEL, 1)
neo.brightness = 0.3
#Benutzerknopf vorbereiten
button=digitalio.DigitalInOut(board.SWITCH)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
#Zählen des Knopfstatus vorbereiten
status=False
altbutton=True
temperature="{:.1f}".format(bmp280.temperature)
pressure="{}".format(bmp280.pressure)
humidity="{:.1f}".format(sht31d.relative_humidity)
#Initiales öffnen der Datei bei Start des Loggers
with open("/sd/datalogging.csv", "a") as f:
led.value = True # turn on LED to indicate we're writing to the file
f.write("date %s %d/%d/%d\n time %d:%02d:%02d \n timestamp,temperature,pressure,magnetic_x,magnetic_y,magnetic_z,accel_x,accel_y,accel_z,gyro_x,gyro_y,gyro_z,humidity \n s,deg C,hPa,uTesla,uTesla,uTesla,m/s^2,m/s^2,m/s^2,deg/s,deg/s,deg/s,pecent \n" % (days[t.tm_wday], t.tm_mday, t.tm_mon, t.tm_year,t.tm_hour, t.tm_min, t.tm_sec))
print("date %s %d/%d/%d\n time %d:%02d:%02d \n" % (days[t.tm_wday], t.tm_mday, t.tm_mon, t.tm_year,t.tm_hour, t.tm_min, t.tm_sec))
led.value = False # turn off LED to indicate we're done
# file is saved
#Whileshleife um Daten abzurufen wenn Knopf gedrückt wurde
while True:
#Knopfdruck "halten"
timing1=time.monotonic()
if button.value == False and altbutton==True:
status = not status
altbutton=button.value
else:
altbutton=button.value
#falls der Knopf gedrückt wurde diese Schleife ausführen
if status:
neo[0] = (0, 255, 0) #green light when data logging is active
# open file for append
temperature="{:.1f}".format(bmp280.temperature)
data01="{},{},{},{:.3f},{:.3f},{:.3f},{:.2f},{:.2f},{:.2f},{:.2f},{:.2f},{:.2f},{} \n".format(time.monotonic(),temperature,pressure,lis3mdl.magnetic[0],lis3mdl.magnetic[1],lis3mdl.magnetic[2],lsm6ds33.acceleration[0],lsm6ds33.acceleration[1],lsm6ds33.acceleration[2],lsm6ds33.gyro[0],lsm6ds33.gyro[1],lsm6ds33.gyro[2],humidity)
pressure="{:.3f}".format(bmp280.pressure)
data02="{},{},{},{:.3f},{:.3f},{:.3f},{:.2f},{:.2f},{:.2f},{:.2f},{:.2f},{:.2f},{} \n".format(time.monotonic(),temperature,pressure,lis3mdl.magnetic[0],lis3mdl.magnetic[1],lis3mdl.magnetic[2],lsm6ds33.acceleration[0],lsm6ds33.acceleration[1],lsm6ds33.acceleration[2],lsm6ds33.gyro[0],lsm6ds33.gyro[1],lsm6ds33.gyro[2],humidity)
humidity="{:.1f}".format(sht31d.relative_humidity)
data03="{},{},{},{:.3f},{:.3f},{:.3f},{:.2f},{:.2f},{:.2f},{:.2f},{:.2f},{:.2f},{} \n".format(time.monotonic(),temperature,pressure,lis3mdl.magnetic[0],lis3mdl.magnetic[1],lis3mdl.magnetic[2],lsm6ds33.acceleration[0],lsm6ds33.acceleration[1],lsm6ds33.acceleration[2],lsm6ds33.gyro[0],lsm6ds33.gyro[1],lsm6ds33.gyro[2],humidity)
with open("/sd/datalogging.csv", "a") as f:
led.value = True # turn on LED to indicate we're writing to the file
f.write(data01)
f.write(data02)
f.write(data03)
led.value = False # turn off LED to indicate we're done
# file is saved
else:
neo[0] = (255, 0, 0) #red light when data logging is inactive
timing2=time.monotonic()
if timing2-timing1>=0.3:
print("Abtastrate=",1/(3*(timing2-timing1)),"Hz")
else:
time.sleep(0.3+timing1-timing2)