repository to manage all files related to the makeathon farm bot project (Software + Documentation).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

raspy_sensors.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. """
  2. created by waldhauser
  3. This file contains the functions to read the sensors DHT22, TSL2561, earth humidity with MCP3008 ADC and RaspberryPi camera
  4. Every sensor can be read on its own
  5. Reading all sensors is possible with the readSensors() function and provides the data in the format of SENSORDATA dictionary
  6. """
  7. import adafruit_dht
  8. import adafruit_tsl2561
  9. import board
  10. import spidev
  11. from picamera import PiCamera
  12. from defines import SENSORDATA
  13. def readDHT22():
  14. """
  15. Reads DHT22 air temperature and air humidity and return values as float
  16. Raises:
  17. Exception: If DHT22 not connected properly
  18. Returns:
  19. float: temperature [°C]
  20. float: humidity [%]
  21. """
  22. try:
  23. dht22 = adafruit_dht.DHT22(board.D4, use_pulseio=False)
  24. except:
  25. raise Exception("DHT22 not connected")
  26. # read DHT22
  27. try:
  28. temperature = dht22.temperature
  29. humidity = dht22.humidity
  30. except:
  31. raise Exception("DHT22 not connected")
  32. return temperature, humidity
  33. def readTSL2561():
  34. """
  35. Reads TSL2561 brightness via I2C in Lux and returns integer value
  36. Raises:
  37. Exception: If TSL2561 not connected properly
  38. Returns:
  39. int: brightness [Lux]
  40. """
  41. try:
  42. tsl2561 = adafruit_tsl2561.TSL2561(board.I2C())
  43. except:
  44. raise Exception("TSL2561 not connected")
  45. # read TSL2561
  46. brightness = 0
  47. if type(tsl2561.lux) == type(None): # Max Value 40.000 -> above error
  48. brightness = 40000
  49. else:
  50. brightness = int(tsl2561.lux)
  51. return brightness
  52. def readMCP3008():
  53. """
  54. Reads YL-69 via MCP3008 ADC and SPI soil moisture in percent and returns float value
  55. Raises:
  56. Exception: If YL-69 not connected properly
  57. Returns:
  58. float: soil moisture [%]
  59. """
  60. channel = 0 # Input channel into MCP3008 ADC
  61. try:
  62. spi = spidev.SpiDev()
  63. spi.open(0,0)
  64. spi.max_speed_hz = 1000000
  65. except:
  66. raise Exception("YL69 not connected")
  67. val = spi.xfer2([1,(8+channel) << 4, 0])
  68. data = ((val[1] & 3) << 9) +val[2]
  69. percentage = data - 680 # Return values between ~1780 and ~680
  70. percentage = round(((1100 - percentage) / 1100) *100, 2) # 680 -> 100% moisture, 1780 -> 0% moisture
  71. if percentage > 100 or percentage < 0: # If not connected values above 100% appear
  72. raise Exception("YL69 not connected")
  73. return percentage
  74. def readSensors(sensorData):
  75. """
  76. Read DHT22, TSL2561 and Humidity Sensor
  77. Dictionary is passed to ensure that values are available when errors occur
  78. When error occurs during reading of sensor, affected values are set to 0
  79. Args:
  80. sensorData (dictionary): Dictionary of type SENSORDATA
  81. Raises:
  82. Exception: DHT22 not connected
  83. Exception: TSL2561 not connected
  84. Exception: YL69 not connected
  85. """
  86. errorMessage = ""
  87. # read DHT22
  88. try:
  89. sensorData["AirTemperature"], sensorData["AirHumidity"] = readDHT22()
  90. except Exception as e:
  91. sensorData["AirHumidity"] = 0 # No value returend if error occurs -> setting safe values
  92. sensorData["AirTemperature"] = 0
  93. errorMessage = str(e) + "\n" # Appending received error message to later forward all occured errors
  94. # read TSL2561
  95. try:
  96. sensorData["Brightness"] = readTSL2561()
  97. except Exception as e:
  98. sensorData["Brightness"] = 0 # No value returend if error occurs -> setting safe value
  99. errorMessage = errorMessage + str(e) + "\n" # Appending received error message to later forward all occured errors
  100. # read YL-69
  101. try:
  102. sensorData["SoilMoisture"] = readMCP3008()
  103. except Exception as e:
  104. sensorData["SoilMoisture"] = 0 # No value returend if error occurs -> setting safe value
  105. errorMessage = errorMessage + str(e) + "\n" # Appending received error message to later forward all occured errors
  106. # raise combined error message, successfull values still available
  107. if errorMessage != "":
  108. raise Exception(errorMessage)
  109. def takePicture():
  110. """
  111. Take picture and store as "picture.png" in current folder
  112. """
  113. try:
  114. camera = PiCamera()
  115. except:
  116. raise Exception("Camera not connected")
  117. camera.start_preview()
  118. camera.capture("picture.png")
  119. camera.stop_preview()
  120. def readPosition():
  121. """
  122. Read and return Position
  123. ***Not implemented, available GPS-sensor was not working***
  124. Returns:
  125. _type_: _description_
  126. """
  127. position = ""
  128. return position
  129. # Testing
  130. def main():
  131. value = SENSORDATA
  132. try:
  133. readSensors(value)
  134. except Exception as e:
  135. print(str(e))
  136. print(value)
  137. if __name__ == "__main__":
  138. main()