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.0KB

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