def main(): TIME = getTime() # Initialize time value SensorValueArr = [TIME, LPG, SMOKE, CO, CO2, TEMP, HUM, FAILURE] # Sensor arr keep all sensor data, time and failure data to send cloud. db = Database() com = comm.Cummunication() port = com.connect() dht22_sensor = dht22.DHT22() while(True): msg = com.read_message(port) val, msgID = com.getValue(msg) print ("value:", val) print("type:", msgID) if msgID != 0: # if the mqsensor data was read, other sensor's data will read. SensorValueArr[4] = mh_z19.read()["co2"] SensorValueArr[5] = dht22_sensor.read_dht(False) SensorValueArr[6] = dht22_sensor.read_dht(True) SensorValueArr[msgID] = val if SensorValueArr[1] >= LPG_LIMIT and SensorValueArr[2] >= SMOKE_LIMIT and \ SensorValueArr[3] >= CO_LIMIT and SensorValueArr[4] >= CO2_LIMIT and \ SensorValueArr[5] >= TEMP_LIMIT and SensorValueArr[6] >= HUM_LIMIT: # Check Sensor failure limits. SensorValueArr[7] = True if msgID == 3: # When All sensor data was read. The data will send cloud. msgID = 0 SensorValueArr[0] = getTime() db.prepare_data(SensorValueArr) db.send_data() print("Data sent...") print(SensorValueArr)
def main(): TIME = getTime() SensorValueArr = [ TIME, LPG, METAN, PROPAN, CO, HYDROGEN, SMOKE, CO2, TEMP, HUM, FAILURE ] db = Database() com = comm.Cummunication() port = com.connect() dht22_sensor = dht22.DHT22() while (True): msg = com.read_message(port) val, msgID = com.getValue(msg) print("value:", val) print("type:", msgID) if msgID != 0: SensorValueArr[7] = mh_z19.read()["co2"] SensorValueArr[8] = dht22_sensor.read_dht(False) SensorValueArr[9] = dht22_sensor.read_dht(True) SensorValueArr[msgID] = val if SensorValueArr[1] >= LPG_LIMIT and SensorValueArr[2] >= METAN_LIMIT and \ SensorValueArr[3] >= PROPAN_LIMIT and SensorValueArr[4] >= CO_LIMIT and \ SensorValueArr[5] >= HYDROGEN_LIMIT and SensorValueArr[6] >=SMOKE_LIMIT and \ SensorValueArr[7] >= CO2_LIMIT and SensorValueArr[8] >= TEMP_LIMIT and SensorValueArr[9] >= HUM_LIMIT: SensorValueArr[10] = True if msgID == 6: msgID = 0 SensorValueArr[0] = getTime() db.prepare_data(SensorValueArr) db.send_data() print("Data sent...") print(SensorValueArr)
def GetTemp(self): # 温湿度を取得 instance = dht22.DHT22(pin=TEMP_SENSOR_PIN) retry_count = 0 while True: # MAX_RETRY回まで繰り返す retry_count += 1 result = instance.read() if result.is_valid(): # 取得できたら温度と湿度を返す return result.temperature, result.humidity elif retry_count >= MAX_RETRY: return 99.9, 99.9 # MAX_RETRYを過ぎても取得できなかった時に温湿度99.9を返す sleep(RETRY_TIME)
def getSenzorTemp(): timest = calendar.timegm(datetime.datetime.now().timetuple()) if senzor_type == "dht22": while True: gpio.init() instance = dht22.DHT22(pin=gport.PA12) result = instance.read() if result.is_valid(): return { "info": {"type": "senzor_data", "id": id}, "data": {"temp": result.temperature, "humidity": result.humidity, "timestamp": timest} } elif senzor_type == "18b20": temp = sensor.get_temperature() return { "info": {"type": "senzor_data", "id": id}, "data": {"temp": temp, "timestamp": timest} }
PIN2 = port.PA6 gpio.init() #gpio.cleanup() while 1: # Connexion serveur FTP ftp = FTP('192.168.1.170', 'pi', 'raspberry') etat = ftp.getwelcome() print "Etat : ", etat def average(list): if (list != []): return float(sum(list)) / len(list) # read data using pin 14 instance = dht22.DHT22(pin=PIN2) i = 0 list_temp = [] list_hum = [] while 1: result = instance.read() if result.is_valid(): list_temp.append(result.temperature) list_hum.append(result.humidity) i = i + 1 os.system('clear') print(str(i) + "0%") time.sleep(6) os.system('clear')
def main(): user = False saveDHTData = True logFile = "logger.txt" # Get the sensors dht22S = dht22.DHT22() hcsr04S = hcsr04.HCSR04() ledS = led.LED() resetPB = push_button.PushButton(dht22S, ledS, 20) # Create the queues for each sensor qDatadht = Q.Queue(1000) qdhtExit = Q.Queue(1) qDataHC = Q.Queue(1000) qhcExit = Q.Queue(1) serverQ = Q.Queue(100) sQExit = Q.Queue(1) # Create locks dhtLock = t.Lock() hcLock = t.Lock() dhtExitL = t.Lock() hcExitL = t.Lock() serverLock = t.Lock() sExitL = t.Lock() # Create the thread for DHT sensor thread1 = sThread.SensorThread(1, "Thread1", qDatadht, dhtLock, qdhtExit, dhtExitL, dht22S) # Create thread for HC-SR04 sensor thread2 = sThread.SensorThread(2, "Thread2", qDataHC, hcLock, qhcExit, hcExitL, hcsr04S) # Thread for the server serverT = server.ServerS(3, "Server", logFile, serverQ, serverLock, sQExit, sExitL) # Start thread1.start() thread2.start() serverT.start() endProcess = False while not endProcess: dht22Data = None # Is there data from DHT22? dhtLock.acquire() if not qDatadht.empty(): dht22Data = qDatadht.get() dhtLock.release() # Is there data from HC-SR04? hcData = None hcLock.acquire() if not qDataHC.empty(): hcData = qDataHC.get() hcLock.release() # Was there a presence? if hcData: pTime = time.asctime() data = ["HC", hcData, pTime] ledS.turnOnLED() writeToFile(logFile, data) # DHT data if saveDHTData: if dht22Data: if dht22Data[0] != None: data = [" DHT", dht22Data[0], dht22Data[1]] writeToFile(logFile, data) # Verify is there are new params data = [] interval = [] serverLock.acquire() # Get the new parameters while not serverQ.empty(): data.append(serverQ.get()) serverLock.release() if not data: continue # Modify params is necessary for d in data: dSplit = d.split(",") value = float(dSplit[1]) if dSplit[0] == "RS" and value == 1: dht22S.setDefaults() ledS.turnOffLED() if dSplit[0] == "LI" and value != -1: interval.append(dSplit[1]) if dSplit[0] == "UI" and value != -1: interval.append(dSplit[1]) if dSplit[0] == "SP" and value != -1: dht22S.setPSampling(float(dSplit[1])) if dSplit[0] == "SV" and value != -1: saveDHTData = bool(int(dSplit[1])) if dSplit[0] == "TF" and value != -1: ledS.turnOffLED() # if len(interval) > 0: dht22S.setSamplingInterval(int(interval[1]), int(interval[2])) if not dht22S.withinInterval(time.time()): saveDHTData = False
import RPi.GPIO as GPIO import dht22 import time import datetime # initialize GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.cleanup() # read data using pin 4 instance = dht22.DHT22(pin=4) while True: result = instance.read() if result.is_valid(): print("Last valid input: " + str(datetime.datetime.now())) print("Temperature: %.1f C" % result.temperature) print("Humidity: %.1f %%" % result.humidity) time.sleep(1)
def startup(self): """ Main function for the program :return: """ self.run_event.set() if 'dht_led_pin' in self.config['pins']: dht_led_pin = self.config['pins']['dht_led_pin'] GPIO.setup(dht_led_pin, GPIO.OUT) else: dht_led_pin = None dht_data_pin = self.config['pins']['dht_data_pin'] button_pin = self.config['pins']['button_pin'] dht = dht22.DHT22(data_gpio_pin=dht_data_pin, led_gpio_pin=dht_led_pin) tsl = tsl_sensor.TSLSensor() # TODO: original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN) # Subprocess handlers thread_temp = TemperatureThread(self.config['timeouts']['wx_timeout'], dht, self.run_event, self.queue) thread_lux = LuxThread(self.config['timeouts']['lux_timeout'], tsl, self.run_event, self.queue) thread_wheel = WheelCounterThread( self.config['pins']['wheel_sensor_pin'], self.config['pins']['wheel_led_pin'], self.config['timeouts']['wheel_loop_timer'], self.config['timeouts']['wheel_inactivity_timer'], self.config['wheel_info']['circumference'], self.run_event, self.queue) thread_lcd = DataReportingThread( self.run_event, self.reporting_queue, self.lcd, __version__, self.config['timeouts']['lcd_fadeout_time'], self.config['database']['filename']) thread_button = ButtonHandlerThread(button_pin, self.run_event, self.queue) signal.signal(signal.SIGINT, original_sigint_handler) # Start the subprocesses thread_lcd.start() thread_temp.start() thread_lux.start() thread_wheel.start() thread_button.start() while True: try: # Wait for something on the queue, but only for a short time (1 ms). This will allow some # responsiveness on the loop (e.g. for CTRL+C shutdown) and will also act as the loop # delay since we're basically in a busy-wait data = self.queue.get(True, 0.001) logger.info("{0}: {1}".format(threading.currentThread().name, data)) # Get the data over to the reporting thread self.reporting_queue.put(data) except KeyboardInterrupt: logger.info("Keyboard Stop, terminating worker processes") self.run_event.clear() thread_temp.join() thread_lux.join() thread_wheel.join() GPIO.cleanup() # Cleanup the LCD self.lcd.set_backlight(1) self.lcd.clear() logger.info("Threads shutdown") break except Exception as ex: pass
from pyA20.gpio import gpio from pyA20.gpio import port import dht22 import time import datetime gpio.init() instance = dht22.DHT22(pin=port.PA6) result = instance.read() if result.is_valid(): print('{0:0.1f} | {1:0.1f}'.format(result.temperature, result.humidity)) else: print('-1 | -1')