123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- # from picamera import PiCamera
- import adafruit_dht
- import adafruit_tsl2561
- import board
- import json
- from defines import SENSORDATA
-
-
- def readDHT22():
- """
- Reads DHT22 air temperature and air humidity and return values as float
-
- Raises:
- Exception: If DHT22 not connected properly
-
- Returns:
- float: air temperature in °C
- float: air humidity %
- """
- try:
- dht22 = adafruit_dht.DHT22(board.D4, use_pulseio=False)
- except:
- raise Exception("DHT22 not connected")
-
- # read DHT22
- try:
- temperature = dht22.temperature
- humidity = dht22.humidity
- except:
- raise Exception("DHT22 not connected")
-
- return temperature, humidity
-
-
- def readTSL2561():
- """
- Reads TSL2561 brightness in Lux
-
- Raises:
- Exception: If TSL2561 not connected properly
-
- Returns:
- int: brightness in Lux
- """
- try:
- tsl2561 = adafruit_tsl2561.TSL2561(board.I2C())
- except:
- raise Exception("TSL2561 not connected")
-
- # read TSL2561
- brightness = 0
- if type(tsl2561.lux) == type(None): # Max Value 40.000 -> above error
- brightness = 40000
- else:
- brightness = int(tsl2561.lux)
-
- return brightness
-
-
-
- # TODO SoilMoisture Sensor Function
-
-
- # TODO Function for all sensors
- def readSensors():
- """
- Read DHT22, TSL2561 and Humidity Sensor
-
- Raises:
- Exception: DHT22 not connected
- Exception: TSL2561 not connected
-
- Returns:
- dict: Sensordata
- """
- sensorData = SENSORDATA
- errorMessage = ""
-
- try:
- sensorData["AirTemperature"], sensorData["AirHumidity"] = readDHT22()
- except Exception as e:
- sensorData["AirHumidity"] = 0 # otherwise old value
- sensorData["AirTemperature"] = 0
- errorMessage = str(e) + "\n"
-
- try:
- sensorData["Brightness"] = readTSL2561()
- except Exception as e:
- sensorData["Brightness"] = 0 # otherwise old value
- errorMessage = errorMessage + str(e) + "\n"
-
- # combined error message
- if errorMessage != "":
- raise Exception(errorMessage)
-
- return sensorData
-
-
- # TODO - take picture function
- def takePicture():
- """
- Take picture and return picture
- Returns:
- _type_: _description_
- """
- picture = ""
-
- return picture
-
-
- # TODO - read position with sensor
- def readPosition():
- """
- Read and return Position
-
- Returns:
- _type_: _description_
- """
- position = ""
- return position
-
-
- # for Testing only
- def main():
- sensors = RaspySensors()
- test = sensors.readSensors()
- print("Data:" + json.dumps(test))
-
-
- if __name__ == "__main__":
- main()
|