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

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