2023-04-04 15:56:13 +02:00
|
|
|
#from picamera import PiCamera
|
|
|
|
import adafruit_dht
|
2023-04-04 17:24:44 +02:00
|
|
|
import adafruit_tsl2561
|
2023-04-04 15:56:13 +02:00
|
|
|
import board
|
|
|
|
import json
|
2023-04-03 14:59:23 +02:00
|
|
|
|
|
|
|
class RaspySensors:
|
|
|
|
'''Class to handle all sensors'''
|
|
|
|
def __init__(self) -> None:
|
2023-04-04 15:56:13 +02:00
|
|
|
'''Init all Sensors'''
|
|
|
|
#Message if Error
|
2023-04-04 17:24:44 +02:00
|
|
|
|
|
|
|
|
2023-04-04 15:56:13 +02:00
|
|
|
#Air Temperature & Humidity
|
|
|
|
self.dht22 = adafruit_dht.DHT22(board.D4, use_pulseio=False)
|
|
|
|
|
2023-04-04 17:24:44 +02:00
|
|
|
#Brightness
|
|
|
|
self.tsl2561 = adafruit_tsl2561.TSL2561(board.I2C())
|
|
|
|
|
2023-04-04 15:56:13 +02:00
|
|
|
#global Variables
|
|
|
|
self.sensorData ={
|
2023-04-04 17:24:44 +02:00
|
|
|
"Air Temperature [°C]" : 0,
|
|
|
|
"Air Humidity [%]" : 0,
|
|
|
|
"Earth Humidity [%]" : 0,
|
|
|
|
"Brightness [Lux]" : 0
|
2023-04-04 15:56:13 +02:00
|
|
|
}
|
2023-04-03 14:59:23 +02:00
|
|
|
|
|
|
|
def readSensors(self):
|
|
|
|
'''Read all Sensors and return Dictionary with data'''
|
2023-04-04 15:56:13 +02:00
|
|
|
|
|
|
|
#read DHT22
|
|
|
|
#if Error reading Data try again
|
|
|
|
while True:
|
|
|
|
try:
|
2023-04-04 17:24:44 +02:00
|
|
|
self.sensorData["Air Temperature [°C]"] = self.dht22.temperature
|
|
|
|
self.sensorData["Air Humidity [%]"] = self.dht22.humidity
|
2023-04-04 15:56:13 +02:00
|
|
|
except:
|
|
|
|
continue
|
|
|
|
|
|
|
|
break
|
|
|
|
|
2023-04-04 17:24:44 +02:00
|
|
|
#read TSL2561
|
|
|
|
self.sensorData["Brightness [Lux]"] = round(self.tsl2561.lux, 2)
|
|
|
|
|
2023-04-03 14:59:23 +02:00
|
|
|
return self.sensorData
|
|
|
|
|
|
|
|
def takePicture(self):
|
|
|
|
'''Take picture and return image'''
|
|
|
|
return self.image
|
|
|
|
|
|
|
|
def readPosition(self):
|
|
|
|
'''Read and return Position'''
|
2023-04-04 15:56:13 +02:00
|
|
|
return self.position
|
|
|
|
|
|
|
|
|
|
|
|
#for Testing only
|
|
|
|
def main():
|
|
|
|
sensors = RaspySensors()
|
|
|
|
test = sensors.readSensors()
|
|
|
|
print("Data:" + json.dumps(test))
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|