def initialize(): print("Proceeding with EnviroPlus\'s warm up...") for x in range(10): print("Cycle {} out of 10".format(str(x + 1))) bme280.get_temperature() bme280.get_pressure() bme280.get_humidity() ltr559.get_lux() pause.seconds(1) print("Enviro is now warmed up and ready!")
def update_graph_temperature(input_data): try: value = ltr559.get_lux() except: value = None Y_light['values']['Light'].append(value) return update_graph(X, Y_light)
def get_data(): # get scd30 data data_raw_scd30 = sensor_scd30.readMeasurement() if (data_raw_scd30 == False): exit(1) [float_co2, float_T, float_rH] = data_raw_scd30 scd30_data["co2"] = float_co2 scd30_data["Temp"] = float_T scd30_data["Hum"] = float_rH # get enviro data enviro_data["proximity"] = ltr559.get_proximity() enviro_data["raw_temp"] = bme280.get_temperature() enviro_data["pressure"] = bme280.get_pressure() enviro_data["humidity"] = bme280.get_humidity() enviro_data["light"] = ltr559.get_lux() enviro_gas = gas.read_all() enviro_data["ox"] = enviro_gas.oxidising / 1000 enviro_data["red"] = enviro_gas.reducing / 1000 enviro_data["nh3"] = enviro_gas.nh3 / 1000 # Outputs # print(scd30_data) # print(enviro_data) # time.sleep(10) data["enviro"] = enviro_data data["scd30"] = scd30_data return data
def read_values(self): values = {} cpu_temp = self.get_cpu_temperature() raw_temp = self.bme280.get_temperature() comp_temp = raw_temp - ((cpu_temp - raw_temp) / self.comp_factor) values['temperature'] = round(comp_temp, 2) values['pressure'] = round(self.bme280.get_pressure(), 2) values['humidity'] = round(self.bme280.get_humidity(), 2) data = gas.read_all() values['oxidising'] = round(data.oxidising / 1000, 4) values['reducing'] = round(data.reducing / 1000, 4) values['nh3'] = round(data.nh3 / 1000, 4) values['lux'] = round(ltr559.get_lux(), 2) try: pm_values = self.pms5003.read() values['P2.5'] = pm_values.pm_ug_per_m3(2.5) values['P10'] = pm_values.pm_ug_per_m3(10) values['P1.0'] = pm_values.pm_ug_per_m3(1.0) except ReadTimeoutError: self.pms5003.reset() pm_values = self.pms5003.read() values['P2.5'] = pm_values.pm_ug_per_m3(2.5) values['P10'] = pm_values.pm_ug_per_m3(10) values['P1.0'] = pm_values.pm_ug_per_m3(1.0) values['ts'] = time.time_ns() return values
def get_light(): """Get all light readings""" lux = ltr559.get_lux() prox = ltr559.get_proximity() LUX.set(lux) PROXIMITY.set(prox)
def output(): # Get Temperature, pressure and humidity bus = SMBus(1) bme280 = BME280(i2c_dev=bus) cpu_temps = [get_cpu_temperature()] * 5 factor = 0.8 raw_temp = bme280.get_temperature() cpu_temp = get_cpu_temperature() cpu_temps = cpu_temps[1:] + [cpu_temp] avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps)) compTemp = raw_temp - ((avg_cpu_temp - raw_temp) / factor) pressure = bme280.get_pressure() humidity = bme280.get_humidity() # Get gases gases = gas.read_all() nh3 = gases.nh3 no2 = gases.oxidising # Get light and proximity light = ltr559.get_lux() proxim = ltr559.get_proximity() # Calculate current time as string curTime = time.strftime("%d/%m/%y,%H:%M", time.localtime()) return compTemp, humidity, nh3, no2, light, proxim, pressure, curTime
def main(): i = 0 # First data from enviro sensor is flawed. # This gets data after 3 cycles each with 1 second waiting time while i <= 2: try: temperature = bme280.get_temperature() temperature_compensated = get_compensated_temperature() pressure = bme280.get_pressure() humidity = bme280.get_humidity() lux = ltr559.get_lux() prox = ltr559.get_proximity() i += 1 time.sleep(1) except: print(traceback.format_exc()) sensor_id = "raspi-" + get_serial_number() sensor_type = "Enviro-plus" # mymeasurement,tag1=tag1,tag2=tag2 fieldA="aaa",fieldB="bbb print("sensor_temperature,sensor_id={},sensor_type={} temperature={:.2f},temperature_compensated={:.2f}".format( sensor_id, sensor_type, temperature, temperature_compensated)) print("sensor_pressure,sensor_id={},sensor_type={} pressure={:.2f}".format(sensor_id, sensor_type, pressure)) print("sensor_humidity,sensor_id={},sensor_type={} humidity={:.2f}".format(sensor_id, sensor_type, humidity)) print("sensor_light,sensor_id={},sensor_type={} light={:.2f},proximity={:.2f}".format( sensor_id, sensor_type, lux, prox))
def read_values(bme280, pms5003): # Compensation factor for temperature comp_factor = 2.25 values = {} cpu_temp = get_cpu_temperature() raw_temp = bme280.get_temperature() # float comp_temp = raw_temp - ((cpu_temp - raw_temp) / comp_factor) values["temperature"] = int(comp_temp) values["pressure"] = round( int(bme280.get_pressure() * 100), -1 ) # round to nearest 10 values["humidity"] = int(bme280.get_humidity()) try: pm_values = pms5003.read() # int values["pm1"] = pm_values.pm_ug_per_m3(1) values["pm25"] = pm_values.pm_ug_per_m3(2.5) values["pm10"] = pm_values.pm_ug_per_m3(10) except ReadTimeoutError: pms5003.reset() pm_values = pms5003.read() values["pm1"] = pm_values.pm_ug_per_m3(1) values["pm25"] = pm_values.pm_ug_per_m3(2.5) values["pm10"] = pm_values.pm_ug_per_m3(10) data = gas.read_all() values["oxidised"] = int(data.oxidising / 1000) values["reduced"] = int(data.reducing / 1000) values["nh3"] = int(data.nh3 / 1000) values["lux"] = int(ltr559.get_lux()) return values
def get_lux(): lux = ltr559.get_lux() for x in range(1,5): x_lux = lux time.sleep(0.5) lux_rounded = round(x_lux,2) lux_str = str(lux_rounded) return(lux_str)
def get_light(): proximity = get_proximity() if proximity < 10: data = ltr559.get_lux() else: data = 1 return data
def displayData(): proximity = ltr559.get_proximity() # If the proximity crosses the threshold, toggle the mode if proximity > 1500 and time.time() - last_page > delay: mode += 1 mode %= len(variables) last_page = time.time() # datetime for timestamp now = datetime.now() dt_string = now.strftime("%d/%m/%y %H:%M:%S") # variable = "temperatureF" unit = "F" cpu_temp = get_cpu_temperature() # Smooth out with some averaging to decrease jitter global cpu_temps cpu_temps = cpu_temps[1:] + [cpu_temp] avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps)) raw_temp = bme280.get_temperature() dataTemp = raw_temp - ((avg_cpu_temp - raw_temp) / factor) dataTemp = (dataTemp * 1.8) + 32 # variable = "pressure" # unit = "hPa" dataPressure = bme280.get_pressure() # variable = "humidity" #unit = "%" dataHumidity = bme280.get_humidity() # variable = "light" #unit = "Lux" if proximity < 10: dataLight = ltr559.get_lux() else: dataLight = 1 # Format the variable name and value messageTemp = "{:.1f}{} {:.1f}{}".format(dataTemp, "F", dataPressure, "hPa") #messagePressure = "{}: {:.1f} {}".format("pressure"[:4], dataPressure, "hPa") messageHumidity = "{}: {:.1f} {}".format("humidity", dataHumidity, "%") #messageLight = "{}: {:.1f} {}".format("light"[:4], dataLight, "Lux") logging.info("Update") draw.rectangle((0, 0, WIDTH, HEIGHT), (13, 13, 13)) draw.rectangle((0, 25, WIDTH, HEIGHT), (13, 13, 13)) draw.rectangle((0, 50, WIDTH, HEIGHT), (13, 13, 13)) #draw.rectangle((0, 75, WIDTH, HEIGHT), (13, 13, 13)) # Write the text at the top in black draw.text((0, 0), messageTemp, font=font, fill=(217, 217, 217)) #draw.text((0, 25), messagePressure, font=font, fill=(217, 217, 217)) draw.text((0, 25), messageHumidity, font=font, fill=(217, 217, 217)) #draw.text((0, 75), messageLight, font=font, fill=(0, 0, 0)) draw.text((0, 50), dt_string, font=font, fill=(217, 217, 217)) st7735.display(img)
def get_ligh(): unit = "Lux" if prox1 < 10: data = ltr559.get_lux() data_l = "{:.2f}".format(data) else: data_l = 1 l1 = "Light: " + str(data_l) + unit return l1
def get_illuminance(): try: lux = ltr559.get_lux() local_data_gateway.post_metrics_update('light', 'ok') return lux except Exception as exception: logger.error( f'Unable to read from ltr559 (light) sensor due to {exception}') local_data_gateway.post_metrics_update('light', 'errors') setup()
def get_light(): """Get all light readings""" try: lux = ltr559.get_lux() prox = ltr559.get_proximity() except OSError as exception: logging.warning( "Failed to read light sensor with error: {}".format(exception)) else: LUX.set(lux) PROXIMITY.set(prox)
def get_light(): """Get all light readings""" try: lux = ltr559.get_lux() prox = ltr559.get_proximity() LUX.set(lux) PROXIMITY.set(prox) except IOError: logging.error( "Could not get lux and proximity readings. Resetting i2c.") reset_i2c()
def get_light(log=False): # variable = "light" unit = "Lux" proximity = ltr559.get_proximity() if proximity < 10: data = ltr559.get_lux() else: data = 1 if log == True: logging.info("light {} {}".format(data, unit)) # display_text("light", data, unit) return (data, unit)
def read_data(time): temperature = bme280.get_temperature() pressure = bme280.get_pressure() humidity = bme280.get_humidity() lux = ltr559.get_lux() if gas_sensor: gases = gas.read_all() oxi = round(gases.oxidising / 1000, 1) red = round(gases.reducing / 1000) nh3 = round(gases.nh3 / 1000) else: oxi = red = nh3 = 0 if particle_sensor: while True: try: particles = pms5003.read() break except RuntimeError as e: print("Particle read failed:", e.__class__.__name__) if not run_flag: raise e pms5003.reset() sleep(30) pm100 = particles.pm_per_1l_air(10.0) pm50 = particles.pm_per_1l_air(5.0) - pm100 pm25 = particles.pm_per_1l_air(2.5) - pm100 - pm50 pm10 = particles.pm_per_1l_air(1.0) - pm100 - pm50 - pm25 pm5 = particles.pm_per_1l_air(0.5) - pm100 - pm50 - pm25 - pm10 pm3 = particles.pm_per_1l_air(0.3) - pm100 - pm50 - pm25 - pm10 - pm5 else: pm100 = pm50 = pm25 = pm10 = pm5 = pm3 = 0 record = { 'time': asctime(localtime(time)), 'temp': round(temperature, 1), 'humi': round(humidity, 1), 'pres': round(pressure, 1), 'lux': round(lux), 'oxi': oxi, 'red': red, 'nh3': nh3, 'pm03': pm3, 'pm05': pm5, 'pm10': pm10, 'pm25': pm25, 'pm50': pm50, 'pm100': pm100, } return record
def read_bme280(bme280): # Compensation factor for temperature comp_factor = 2.25 values = {} cpu_temp = get_cpu_temperature() raw_temp = bme280.get_temperature() # float comp_temp = raw_temp values["temperature"] = int(comp_temp) values["pressure"] = round( int(bme280.get_pressure() * 100), -1 ) # round to nearest 10 values["humidity"] = int(bme280.get_humidity()) values["lux"] = int(ltr559.get_lux()) return values
def take_readings(self): gas_data = gas.read_all() readings = { "proximity": ltr559.get_proximity(), "lux": ltr559.get_lux(), "temperature": self.bme280.get_temperature(), "pressure": self.bme280.get_pressure(), "humidity": self.bme280.get_humidity(), "gas/oxidising": gas_data.oxidising, "gas/reducing": gas_data.reducing, "gas/nh3": gas_data.nh3, } readings.update(self.latest_pms_readings) return readings
def measure_data(n_measurements, sleep_secs): temps = np.array([]) press = np.array([]) humid = np.array([]) light = np.array([]) nh3 = np.array([]) oxi = np.array([]) red = np.array([]) lux = np.array([]) for i in range(0, n_measurements): temps = np.append(temps, bme280.get_temperature()) press = np.append(press, bme280.get_pressure()) humid = np.append(humid, bme280.get_humidity()) gases = gas.read_all() nh3 = np.append(nh3, gases.nh3) oxi = np.append(oxi, gases.oxidising) red = np.append(red, gases.reducing) lux = np.append(lux, ltr559.get_lux()) time.sleep(sleep_secs) temperature_raw = np.median(temps) pressure = np.median(press) humidity = np.median(humid) ammonium = np.median(nh3) oxidising = np.median(oxi) reducing = np.median(red) light = np.median(lux) temperature_cpu = get_cpu_temp() temperature_corrected = temperature_raw if temperature_cpu > temperature_raw: temperature_corrected = temperature_raw - (temperature_cpu - temperature_raw) * 0.3 return { 'temperature_raw': (temperature_raw, '°C'), 'temperature_cpu': (temperature_cpu, '°C'), 'temperature_corrected': (temperature_corrected, '°C'), 'pressure': (pressure, 'hPa'), 'humidity': (humidity, '%'), 'ammonium': (ammonium, ''), 'oxidising': (oxidising, ''), 'reducing': (reducing, ''), 'light': (light, 'lux') }
def read_data(time): temperature = bme280.get_temperature() pressure = bme280.get_pressure() humidity = bme280.get_humidity() lux = ltr559.get_lux() gases = gas.read_all() record = { 'time': asctime(localtime(time)), 'temp': round(temperature, 1), 'humi': round(humidity, 1), 'pres': round(pressure, 1), 'lux': round(lux), 'oxi': round(gases.oxidising / 1000, 1), 'red': round(gases.reducing / 1000), 'nh3': round(gases.nh3 / 1000), } return record
def readSensors(): global Datas factor = 0.8 cpu_temp = get_cpu_temperature() raw_temp = bme280.get_temperature() temperature = raw_temp - ((cpu_temp - raw_temp) / factor) datasgas = gas.read_all() Datas['cpu_temp'] = round(cpu_temp,2) Datas['temperature'] = round(temperature,2) Datas['pressure'] = round(bme280.get_pressure(),2) Datas['humidity'] = round(bme280.get_humidity(),2) Datas['light'] = round(ltr559.get_lux(),2) Datas['oxidised'] = round(datasgas.oxidising / 1000,2) Datas['reduced'] = round(datasgas.reducing / 1000,2) Datas['nh3'] = round(datasgas.nh3 / 1000,2) display_datas()
def get_measurement(self): """ :returns: dict. Example:: output = { "light": 109.3543, # Lux "proximity": 103 # The higher the value, the nearest the object, within a ~5cm range } """ import ltr559 ltr559.set_proximity_active() return { 'light': ltr559.get_lux(), 'proximity': ltr559.get_proximity(), }
def read_bme280(bme280): # Compensation factor for temperature comp_factor = 2.25 values = {} cpu_temp = get_cpu_temperature() raw_temp = bme280.get_temperature() # float comp_temp = raw_temp - ((cpu_temp - raw_temp) / comp_factor) values["temperature"] = int(comp_temp) values["pressure"] = round( int(bme280.get_pressure() * 100), -1 ) # round to nearest 10 values["humidity"] = int(bme280.get_humidity()) data = gas.read_all() values["oxidised"] = int(data.oxidising / 1000) values["reduced"] = int(data.reducing / 1000) values["nh3"] = int(data.nh3 / 1000) values["lux"] = int(ltr559.get_lux()) return values
def hentData(): listeVerdata = [] # Ei tom liste som skal innehalde data listeVerdata.append(datetime.now()) # Legg til tidspunkt for avlesing temp = bme280.get_temperature() listeVerdata.append(temp) # Legg til temperatur trykk = bme280.get_pressure() listeVerdata.append(trykk) # Legg til trykk fukt = bme280.get_humidity() listeVerdata.append(fukt) # Legg til fuktighet lys = ltr559.get_lux() listeVerdata.append(lys) # Legg til lys try: dataPM1 = pms5003.read() except pmsReadTimeoutError: print("Feil ved avlesing av PMS5003, pm1") dataPM1 = 0 else: dataPM1 = float(dataPM1.pm_ug_per_m3(1.0)) listeVerdata.append(dataPM1) try: dataPM25 = pms5003.read() except pmsReadTimeoutError: print("Feil ved avlesing av PMS5003, pm2.5") dataPM25 = 0 else: dataPM25 = float(dataPM25.pm_ug_per_m3(2.5)) listeVerdata.append(dataPM25) try: dataPM10 = pms5003.read() except pmsReadTimeoutError: print("Feil ved avlesing av PMS5003, pm10") dataPM10 = 0 else: dataPM10 = float(dataPM10.pm_ug_per_m3(10)) listeVerdata.append(dataPM10) return listeVerdata # Returnerer svaret
def get_data_sample(data_samples_for_average): # reset data to empty lists for key in data_samples_for_average.keys(): data_samples_for_average[key] = [] for _ in range(40): data_samples_for_average["temp(c)"].append(get_temp()) data_samples_for_average["pressure(hPa)"].append(bme280.get_pressure()) data_samples_for_average["humidity(percent)"].append( bme280.get_humidity()) data_samples_for_average["light(lux)"].append(ltr559.get_lux()) time.sleep(1) return_data = {} for data_type, values in data_samples_for_average.items(): return_data[data_type] = round(stat.mean(values), 2) return return_data
def read_values(): values = {} cpu_temp = get_cpu_temperature() raw_temp = bme280.get_temperature() comp_temp = raw_temp - ((cpu_temp - raw_temp) / comp_factor) lux = ltr559.get_lux() proximity = ltr559.get_proximity() readings = gas.read_all() oxidising = readings.oxidising nh3 = readings.nh3 reducing = readings.reducing adc = readings.adc noise = Noise() amps = noise.get_amplitudes_at_frequency_ranges([(100, 200), (500, 600), (1000, 1200)]) amps = [n * 32 for n in amps] values["temperature"] = "{:.2f}".format(comp_temp) values["cpu_temp"] = "{:.2f}".format(cpu_temp) values["pressure"] = "{:.2f}".format(bme280.get_pressure() * 100) values["humidity"] = "{:.2f}".format(bme280.get_humidity()) values["lux"] = "{:05.02f}".format(lux) values["proximity"] = "{:05.02f}".format(proximity) values["nh3"] = "{:05.02f}".format(nh3) values["oxidising"] = "{:05.02f}".format(oxidising) values["reducing"] = "{:05.02f}".format(reducing) values["adc"] = "{:05.02f}".format(adc) values["amp_100_200"] = "{:05.02f}".format(amps[0]) values["amp_500_600"] = "{:05.02f}".format(amps[1]) values["amp_1000_1200"] = "{:05.02f}".format(amps[2]) try: pm_values = pms5003.read() values["P2"] = str(pm_values.pm_ug_per_m3(2.5)) values["P1"] = str(pm_values.pm_ug_per_m3(10)) except ReadTimeoutError: pms5003.reset() pm_values = pms5003.read() values["P2"] = str(pm_values.pm_ug_per_m3(2.5)) values["P1"] = str(pm_values.pm_ug_per_m3(10)) return values
def read_data(time): temperature = bme280.get_temperature() - 8 pressure = bme280.get_pressure() humidity = bme280.get_humidity() lux = ltr559.get_lux() gases = gas.read_all() # while True: # try: # particles = pms5003.read() # break # except RuntimeError as e: # print("Particle read failed:", e.__class__.__name__) # if not run_flag: # raise e # pms5003.reset() # sleep(30) # # pm100 = particles.pm_per_1l_air(10.0); # pm50 = particles.pm_per_1l_air(5.0) - pm100; # pm25 = particles.pm_per_1l_air(2.5) - pm100 - pm50; # pm10 = particles.pm_per_1l_air(1.0) - pm100 - pm50 - pm25; # pm5 = particles.pm_per_1l_air(0.5) - pm100 - pm50 - pm25 - pm10; record = { 'time' : asctime(localtime(time)), 'temp' : round(temperature,1), 'humi' : round(humidity, 1), 'pres' : round(pressure,1), 'lux' : round(lux), 'oxi' : round(gases.oxidising / 1000, 1), 'red' : round(gases.reducing / 1000), 'nh3' : round(gases.nh3 / 1000), # 'pm03' : pm3, # 'pm05' : pm5, # 'pm10' : pm10, # 'pm25' : pm25, # 'pm50' : pm50, # 'pm100': pm100, } return record
def start(self, callback): while True: # Climate self.temperature = self.bme280.get_temperature() self.pressure = self.bme280.get_pressure() self.humidity = self.bme280.get_humidity() time.sleep(1) # Light self.lux = ltr559.get_lux() self.prox = ltr559.get_proximity() time.sleep(1) # Gas gases = gas.read_all() self.oxidising = (gases.oxidising / 1000) self.nh3 = (gases.nh3 / 1000) self.reducing = (gases.reducing / 1000) time.sleep(1) # Callback all data in json format callback(self.get_all_data()) time.sleep(1)
def take_reading(reading_number): BME280.update_sensor() gas = enviroplus.gas.read_all() pm_ok = False pm_tries = 0 while not pm_ok and pm_tries < 10: try: pm = PMS5003.read() pm_ok = True except: pm_tries += 1 if not pm_ok: pm1_0 = pm2_5 = pm10_0 = "NULL" else: (pm1_0, pm2_5, pm10_0) = [pm.pm_ug_per_m3(n) for n in [1.0, 2.5, 10.0]] reading = READING( timestamp=datetime.datetime.now().isoformat(), reading_number=reading_number, temperature=BME280.temperature, humidity=BME280.humidity, pressure=BME280.pressure, light=ltr559.get_lux(), proximity=ltr559.get_proximity(), gas_reducing=gas.reducing, gas_oxidising=gas.oxidising, gas_NH3=gas.nh3, PM1_0=pm1_0, PM2_5=pm2_5, PM10_0=pm10_0, ) return reading