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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # from picamera import PiCamera
  2. import adafruit_dht
  3. import adafruit_tsl2561
  4. import board
  5. import json
  6. from defines import SENSORDATA
  7. def readDHT22():
  8. """
  9. Reads DHT22 air temperature and air humidity and return values as float
  10. Raises:
  11. Exception: If DHT22 not connected properly
  12. Returns:
  13. float: air temperature in °C
  14. float: air humidity %
  15. """
  16. try:
  17. dht22 = adafruit_dht.DHT22(board.D4, use_pulseio=False)
  18. except:
  19. raise Exception("DHT22 not connected")
  20. # read DHT22
  21. try:
  22. temperature = dht22.temperature
  23. humidity = dht22.humidity
  24. except:
  25. raise Exception("DHT22 not connected")
  26. return temperature, humidity
  27. def readTSL2561():
  28. """
  29. Reads TSL2561 brightness in Lux
  30. Raises:
  31. Exception: If TSL2561 not connected properly
  32. Returns:
  33. int: brightness in Lux
  34. """
  35. try:
  36. tsl2561 = adafruit_tsl2561.TSL2561(board.I2C())
  37. except:
  38. raise Exception("TSL2561 not connected")
  39. # read TSL2561
  40. brightness = 0
  41. if type(tsl2561.lux) == type(None): # Max Value 40.000 -> above error
  42. brightness = 40000
  43. else:
  44. brightness = int(tsl2561.lux)
  45. return brightness
  46. # TODO SoilMoisture Sensor Function
  47. # TODO Function for all sensors
  48. def readSensors():
  49. """
  50. Read DHT22, TSL2561 and Humidity Sensor
  51. Raises:
  52. Exception: DHT22 not connected
  53. Exception: TSL2561 not connected
  54. Returns:
  55. dict: Sensordata
  56. """
  57. sensorData = SENSORDATA
  58. errorMessage = ""
  59. try:
  60. sensorData["AirTemperature"], sensorData["AirHumidity"] = readDHT22()
  61. except Exception as e:
  62. sensorData["AirHumidity"] = 0 # otherwise old value
  63. sensorData["AirTemperature"] = 0
  64. errorMessage = str(e) + "\n"
  65. try:
  66. sensorData["Brightness"] = readTSL2561()
  67. except Exception as e:
  68. sensorData["Brightness"] = 0 # otherwise old value
  69. errorMessage = errorMessage + str(e) + "\n"
  70. # combined error message
  71. if errorMessage != "":
  72. raise Exception(errorMessage)
  73. return sensorData
  74. # TODO - take picture function
  75. def takePicture():
  76. """
  77. Take picture and return picture
  78. Returns:
  79. _type_: _description_
  80. """
  81. picture = ""
  82. return picture
  83. # TODO - read position with sensor
  84. def readPosition():
  85. """
  86. Read and return Position
  87. Returns:
  88. _type_: _description_
  89. """
  90. position = ""
  91. return position
  92. # for Testing only
  93. def main():
  94. sensors = RaspySensors()
  95. test = sensors.readSensors()
  96. print("Data:" + json.dumps(test))
  97. if __name__ == "__main__":
  98. main()