# from picamera import PiCamera import adafruit_dht import adafruit_tsl2561 import board import json import spidev from picamera import PiCamera 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: temperature [°C] float: 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 and returns integer value Raises: Exception: If TSL2561 not connected properly Returns: int: brightness [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 def readMCP3008(): """ Reads YL-69 via MCP3008 ADC soil moisture in percent and returns float value Raises: Exception: If YL-69 not connected properly Returns: float: soil moisture [%] """ channel = 0 # Input channel into MCP3008 ADC try: spi = spidev.SpiDev() spi.open(0,0) spi.max_speed_hz = 1000000 except: raise Exception("YL69 not connected") val = spi.xfer2([1,(8+channel) << 4, 0]) data = ((val[1] & 3) << 9) +val[2] percentage = data - 680 # Return values between ~1780 and ~680 percentage = round(((1100 - percentage) / 1100) *100, 2) # 680 -> 100% moisture, 1780 -> 0% moisture if percentage > 100 or percentage < 0: # If not connected values above 100% appear percentage = 0 raise Exception("YL69 not connected") return percentage # TODO Function for all sensors def readSensors(): """ Read DHT22, TSL2561 and Humidity Sensor Raises: Exception: DHT22 not connected Exception: TSL2561 not connected Exception: YL69 not connected Returns: dict: Sensordata """ sensorData = {} sensorData |= SENSORDATA errorMessage = "" # read DHT22 try: sensorData["AirTemperature"], sensorData["AirHumidity"] = readDHT22() except Exception as e: sensorData["AirHumidity"] = 0 # otherwise old value sensorData["AirTemperature"] = 0 errorMessage = str(e) + "\n" # read TSL2561 try: sensorData["Brightness"] = readTSL2561() except Exception as e: sensorData["Brightness"] = 0 # otherwise old value errorMessage = errorMessage + str(e) + "\n" # read YL-69 try: sensorData["SoilMoisture"] = readMCP3008() except Exception as e: sensorData["SoilMoisture"] = 0 errorMessage = errorMessage + str(e) + "\n" # combined error message if errorMessage != "": raise Exception(errorMessage) return sensorData def takePicture(): """ Take picture and return picture Returns: _type_: _description_ """ try: camera = PiCamera() except: raise Exception("Camera not connected") camera.start_preview() camera.capture("picture.png") camera.stop_preview() return # TODO - read position with sensor def readPosition(): """ Read and return Position Returns: _type_: _description_ """ position = "" return position # Testing def main(): value = SENSORDATA try: value |= readSensors() except Exception as e: print(str(e)) print(value) if __name__ == "__main__": main()