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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #from picamera import PiCamera
  2. import adafruit_dht
  3. import adafruit_tsl2561
  4. import board
  5. import json
  6. class RaspySensors:
  7. """
  8. Class to handle all sensors
  9. Returns:
  10. _type_: _description_
  11. """
  12. def __init__(self):
  13. """
  14. Init all Sensors
  15. """
  16. #[ ]TODO Message if Error
  17. #Air Temperature & Humidity
  18. self.dht22 = adafruit_dht.DHT22(board.D4, use_pulseio=False)
  19. #Brightness
  20. self.tsl2561 = adafruit_tsl2561.TSL2561(board.I2C())
  21. #global Variables
  22. self.sensorData ={
  23. "AirTemperature": 0.0,
  24. "AirHumidity" : 0.0,
  25. "SoilMoisture" : 0.0,
  26. "Brightness" : 0,
  27. "PlantID": 0,
  28. "ActionID": 0
  29. }
  30. def readSensors(self):
  31. """
  32. Read all Sensors and return Dictionary with data
  33. Returns:
  34. sensordata (dict): all data of sensors
  35. """
  36. #read DHT22
  37. #if Error reading Data try again
  38. while True:
  39. try:
  40. self.sensorData["AirTemperature"] = self.dht22.temperature
  41. self.sensorData["AirHumidity"] = self.dht22.humidity
  42. except:
  43. continue
  44. break
  45. #read TSL2561
  46. if type(self.tsl2561.lux) == type(None): #Max Value 40.000 -> above error
  47. self.sensorData["Brightness"] = 40000
  48. else:
  49. self.sensorData["Brightness"] = int(self.tsl2561.lux)
  50. #TODO SoilMoisture Sensor
  51. return self.sensorData
  52. #TODO - take picture function
  53. def takePicture(self):
  54. """
  55. Take picture and return picture
  56. Returns:
  57. _type_: _description_
  58. """
  59. return self.picture
  60. #TODO - read position with sensor
  61. def readPosition(self):
  62. """
  63. Read and return Position
  64. Returns:
  65. _type_: _description_
  66. """
  67. return self.position
  68. #TODO - read battery from EV3
  69. def readBattery(self):
  70. """
  71. Read and return battery of ev3
  72. Returns:
  73. _type_: _description_
  74. """
  75. return self.battery
  76. #for Testing only
  77. def main():
  78. sensors = RaspySensors()
  79. test = sensors.readSensors()
  80. print("Data:" + json.dumps(test))
  81. if __name__ == "__main__":
  82. main()