Advertisement
EmptySet5150

Python3 - RPi3 - Auto Garden Watering - Save to Database

May 23rd, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. #!/usr/lib/python3.4
  2. """
  3. This file  is to be used  as a cron
  4. job ran  every ten  minutes to save
  5. the sensor readings to the database
  6. """
  7.  
  8. # Lets import a few things needed for the system
  9. from time import sleep
  10. from datetime import datetime
  11. from model import SensorData
  12. import RPi.GPIO as GPIO
  13. import DHT22
  14. import pigpio
  15.  
  16. pi = pigpio.pi()
  17.  
  18. # Setup pin names
  19. GPIO.setwarnings(False)
  20. GPIO.setmode(GPIO.BOARD)
  21. dht22 = DHT22.sensor(pi, 22)
  22. rainSensor = 40
  23. waterValveControl = 22
  24. pumpControl = 32
  25. waterLevel = 16
  26. sensorDatabase = SensorData()
  27.  
  28. # Setup GPIO pin modes
  29. GPIO.setup(waterValveControl, GPIO.OUT)
  30. GPIO.setup(pumpControl, GPIO.OUT)
  31. GPIO.setup(rainSensor, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  32. GPIO.setup(waterLevel, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  33.  
  34.  
  35. # Functions to return temperature and humidity readings from the DHT22 sensor
  36. def temperature_status():
  37.     dht22.trigger()
  38.     sleep(.2)
  39.     temp = '%.1f' % (dht22.temperature() * 1.8 + 32)
  40.     return temp
  41.  
  42.  
  43. def humidity_status():
  44.     dht22.trigger()
  45.     sleep(.2)
  46.     humid = '%.1f' % (dht22.humidity())
  47.     return humid
  48.  
  49.  
  50. # Functions read the GPIO pins to determine if they are on or off and return a status
  51. def water_status():
  52.     if GPIO.input(waterLevel) == False:
  53.         return 9
  54.     elif GPIO.input(waterLevel) == True:
  55.         return 77
  56.  
  57.  
  58. def rain_status():
  59.     if GPIO.input(rainSensor) == False:
  60.         return 'WET'
  61.     elif GPIO.input(rainSensor) == True:
  62.         return 'DRY'
  63.  
  64.  
  65. def pump_status():
  66.     if GPIO.input(pumpControl) == False:
  67.         return 'ON'
  68.     elif GPIO.input(pumpControl) == True:
  69.         return 'OFF'
  70.  
  71.  
  72. def valve_status():
  73.     if GPIO.input(waterValveControl) == False:
  74.         return 'OPEN'
  75.     elif GPIO.input(waterValveControl) == True:
  76.         return 'SHUT'
  77.  
  78.  
  79. # def get_last():
  80.  
  81.  
  82. # Function gets data from sensors and get_last() and saves it to the database
  83. def save_database():
  84.     #    lastwater, nextwater, pumpstop = get_last()
  85.     lastwater = 'N/A'
  86.     nextwater = 'N/A'
  87.     pumpstop = 'N/A'
  88.     timenow = (datetime.now().strftime('%d/%h/%m %H:%M'))
  89.     temp = temperature_status()
  90.     humid = humidity_status()
  91.     pump = pump_status()
  92.     rain = rain_status()
  93.     waterlevel = water_status()
  94.     valve = valve_status()
  95.     sensorDatabase.saveData(timenow, lastwater, nextwater, pumpstop, temp, humid, waterlevel, rain, pump, valve)
  96.  
  97. save_database()
  98. sensorDatabase.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement