Advertisement
Guest User

sensors script

a guest
Jul 16th, 2017
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # GrovePi Project for smart library.
  4. # * Reads the data from light, temperature and humidity, sound and ultrasonic range sensors
  5. # * Sensor Connections on the GrovePi:
  6. #
  7. # -> Grove light sensor - Port A0
  8. # -> Grove DHT sensors - Port D7
  9. # -> Grove sound sensor - Port A1
  10. # -> Grove distance sensor - Port D8
  11. # -> Grove LED - Port D5
  12. #
  13. # NOTE:
  14. #
  15. # The GrovePi connects the Raspberry Pi and Grove sensors. You can learn more about GrovePi here: http://www.dexterindustries.com/GrovePi
  16. #
  17. # Have a question? Ask on the forums here: http://forum.dexterindustries.com/c/grovepi
  18. #
  19. '''
  20. ## License
  21.  
  22. The MIT License (MIT)
  23.  
  24. GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
  25. Copyright (C) 2015 Dexter Industries
  26.  
  27. Permission is hereby granted, free of charge, to any person obtaining a copy
  28. of this software and associated documentation files (the "Software"), to deal
  29. in the Software without restriction, including without limitation the rights
  30. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  31. copies of the Software, and to permit persons to whom the Software is
  32. furnished to do so, subject to the following conditions:
  33.  
  34. The above copyright notice and this permission notice shall be included in
  35. all copies or substantial portions of the Software.
  36. '''
  37.  
  38. import time
  39. import grovepi
  40. import subprocess
  41. import math
  42.  
  43. print("System Working")
  44.  
  45.  
  46. #analog sensor port number
  47. light_sensor = 2
  48.  
  49. #digital sensor
  50. temp_humidity_sensor = 7
  51.  
  52. #temp_humidity_sensor type
  53. # grove starter kit comes with the bluew sensor
  54. blue=0
  55. white=1
  56.  
  57. #ultrasonic sensor for distance
  58. ultrasonic_ranger = 8
  59.  
  60. # Connect the Grove Sound Sensor to analog port A1
  61. #sound_sensor = 1
  62.  
  63. # PIN modes
  64. grovepi.pinMode(1,"INPUT")
  65. grovepi.pinMode(8,"INPUT")
  66. grovepi.pinMode(temp_humidity_sensor,"INPUT")
  67. grovepi.pinMode(light_sensor,"INPUT")
  68.  
  69. # The threshold to turn the led on depending on sound 400.00 * 5 / 1024 = 1.95v
  70. #threshold_value = 350
  71.  
  72.  
  73. #############
  74. #test timings
  75. time_for_sensor = 2 # 1 seconds
  76.  
  77.  
  78. time_to_sleep = 1
  79. log_file="log_file.csv"
  80.  
  81. #Read the data from the sensors
  82. def read_sensor():
  83. try:
  84. light=grovepi.analogRead(light_sensor)
  85. distance = grovepi.ultrasonicRead(ultrasonic_ranger)
  86. [temp,humidity] = grovepi.dht(temp_humidity_sensor,blue)
  87. #Return -1 in case of bad temp/humidity sensor reading
  88. if math.isnan(temp) or math.isnan(humidity): #temp/humidity sensor sometimes gives nan
  89. return [-1,-1,-1,-1]
  90. temp = -1;
  91. humidity = -1;
  92. #sound = grovepi.analogRead(sound_sensor)
  93. return [light,temp,humidity,distance]
  94.  
  95. #Return -1 in case of sensor error
  96. except IOError as TypeError:
  97. return [-1,-1,-1,-1]
  98.  
  99.  
  100. #Save the initial time, we will use this to find out when it is time to take a picture or save a reading
  101. last_read_sensor= int(time.time())
  102.  
  103. while True:
  104. try:
  105. curr_time_sec=int(time.time())
  106.  
  107. # If it is time to take the sensor reading
  108. if curr_time_sec-last_read_sensor>time_for_sensor:
  109. [light,temp,humidity,distance]=read_sensor()
  110. # If any reading is a bad reading, skip the loop and try again
  111. if light==-1 or temp==-1 or humidity ==-1 or distance==-1:
  112. print("Bad reading of one of the sensors")
  113. #time.sleep(time_to_sleep)
  114. continue
  115. curr_time = time.strftime("%Y-%m-%d:%H-%M-%S")
  116. print(("Time:%s\nLight: %d\nTemp: %.2f\nHumidity:%.2f\nDistance: %d\n" %(curr_time,light,temp,humidity,distance)))
  117.  
  118. #Update the last read time
  119. last_read_sensor=curr_time_sec
  120.  
  121.  
  122. #Slow down the loop
  123. time.sleep(time_to_sleep)
  124. except IOError:
  125. print ("Error")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement