Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- # GrovePi Project for smart library.
- # * Reads the data from light, temperature and humidity, sound and ultrasonic range sensors
- # * Sensor Connections on the GrovePi:
- #
- # -> Grove light sensor - Port A0
- # -> Grove DHT sensors - Port D7
- # -> Grove sound sensor - Port A1
- # -> Grove distance sensor - Port D8
- # -> Grove LED - Port D5
- #
- # NOTE:
- #
- # The GrovePi connects the Raspberry Pi and Grove sensors. You can learn more about GrovePi here: http://www.dexterindustries.com/GrovePi
- #
- # Have a question? Ask on the forums here: http://forum.dexterindustries.com/c/grovepi
- #
- '''
- ## License
- The MIT License (MIT)
- GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
- Copyright (C) 2015 Dexter Industries
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- '''
- import time
- import grovepi
- import subprocess
- import math
- print("System Working")
- #analog sensor port number
- light_sensor = 2
- #digital sensor
- temp_humidity_sensor = 7
- #temp_humidity_sensor type
- # grove starter kit comes with the bluew sensor
- blue=0
- white=1
- #ultrasonic sensor for distance
- ultrasonic_ranger = 8
- # Connect the Grove Sound Sensor to analog port A1
- #sound_sensor = 1
- # PIN modes
- grovepi.pinMode(1,"INPUT")
- grovepi.pinMode(8,"INPUT")
- grovepi.pinMode(temp_humidity_sensor,"INPUT")
- grovepi.pinMode(light_sensor,"INPUT")
- # The threshold to turn the led on depending on sound 400.00 * 5 / 1024 = 1.95v
- #threshold_value = 350
- #############
- #test timings
- time_for_sensor = 2 # 1 seconds
- time_to_sleep = 1
- log_file="log_file.csv"
- #Read the data from the sensors
- def read_sensor():
- try:
- light=grovepi.analogRead(light_sensor)
- distance = grovepi.ultrasonicRead(ultrasonic_ranger)
- [temp,humidity] = grovepi.dht(temp_humidity_sensor,blue)
- #Return -1 in case of bad temp/humidity sensor reading
- if math.isnan(temp) or math.isnan(humidity): #temp/humidity sensor sometimes gives nan
- return [-1,-1,-1,-1]
- temp = -1;
- humidity = -1;
- #sound = grovepi.analogRead(sound_sensor)
- return [light,temp,humidity,distance]
- #Return -1 in case of sensor error
- except IOError as TypeError:
- return [-1,-1,-1,-1]
- #Save the initial time, we will use this to find out when it is time to take a picture or save a reading
- last_read_sensor= int(time.time())
- while True:
- try:
- curr_time_sec=int(time.time())
- # If it is time to take the sensor reading
- if curr_time_sec-last_read_sensor>time_for_sensor:
- [light,temp,humidity,distance]=read_sensor()
- # If any reading is a bad reading, skip the loop and try again
- if light==-1 or temp==-1 or humidity ==-1 or distance==-1:
- print("Bad reading of one of the sensors")
- #time.sleep(time_to_sleep)
- continue
- curr_time = time.strftime("%Y-%m-%d:%H-%M-%S")
- print(("Time:%s\nLight: %d\nTemp: %.2f\nHumidity:%.2f\nDistance: %d\n" %(curr_time,light,temp,humidity,distance)))
- #Update the last read time
- last_read_sensor=curr_time_sec
- #Slow down the loop
- time.sleep(time_to_sleep)
- except IOError:
- print ("Error")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement