def getReading(): # Choose a gain of 1 for reading voltages from 0 to 4.09V. # Or pick a different gain to change the range of voltages that are read: # - 2/3 = +/-6.144V # - 1 = +/-4.096V # - 2 = +/-2.048V # - 4 = +/-1.024V # - 8 = +/-0.512V # - 16 = +/-0.256V # See table 3 in the ADS1015/ADS1115 datasheet for more info on gain. # see UV csensor data sheet: https://cdn.sparkfun.com/datasheets/Sensors/LightImaging/ML8511_3-8-13.pdf GAIN = 2 / 3 # the channel on the ADC to use CHANNEL = 1 # Create an ADS1115 ADC (16-bit) instance and do stuff with it adc = Adafruit_ADS1x15.ADS1115() adc.start_adc(CHANNEL, gain=GAIN) start = time.time() value = 0 cnt = 0 totalVoltage = 0 while (time.time() - start) <= 5.0: totalVoltage += adc.get_last_result() cnt += 1 time.sleep(0.5) # Stop continuous conversion. After this point you can't get data from get_last_result! adc.stop_adc() avgVoltage = totalVoltage / cnt return (SensorReading("uv", "UV", voltToUvIndex(avgVoltage), "index [DEBUG: bad math]"), SensorReading("solarradiation", "solarradiation", avgVoltage, "W/m^2"))
def getReading(): #NOT WORKING: hum, tem = Adafruit_DHT.read_retry(sensor, gpio) #WORKINGISH: hum, tem = Adafruit_DHT.read_retry(11, 5) humid, tempC = Adafruit_DHT.read_retry(11, 5) tempf = c_to_f(tempC) dhtHum = SensorReading("dht11", "humidity", humid, "%") dhtTem = SensorReading("dht11", "tempf", tempf, "f") return (dhtTem,dhtHum)
def getReading(): # Get I2C bus bus = smbus.SMBus(1) # MPL3115A2 address, 0x60(96) # Select control register, 0x26(38) # 0xB9(185) Active mode, OSR = 128, Altimeter mode bus.write_byte_data(0x60, 0x26, 0xB9) # MPL3115A2 address, 0x60(96) # Select data configuration register, 0x13(19) # 0x07(07) Data ready event enabled for altitude, pressure, temperature bus.write_byte_data(0x60, 0x13, 0x07) # MPL3115A2 address, 0x60(96) # Select control register, 0x26(38) # 0xB9(185) Active mode, OSR = 128, Altimeter mode bus.write_byte_data(0x60, 0x26, 0xB9) time.sleep(1) # MPL3115A2 address, 0x60(96) # Read data back from 0x00(00), 6 bytes # status, tHeight MSB1, tHeight MSB, tHeight LSB, temp MSB, temp LSB data = bus.read_i2c_block_data(0x60, 0x00, 6) # Convert the data to 20-bits tHeight = ((data[1] * 65536) + (data[2] * 256) + (data[3] & 0xF0)) / 16 temp = ((data[4] * 256) + (data[5] & 0xF0)) / 16 thisAltitude = tHeight / 16.0 cTemp = temp / 16.0 fTemp = c_to_f(cTemp) # MPL3115A2 address, 0x60(96) # Select control register, 0x26(38) # 0x39(57) Active mode, OSR = 128, Barometer mode bus.write_byte_data(0x60, 0x26, 0x39) time.sleep(1) # MPL3115A2 address, 0x60(96) # Read data back from 0x00(00), 4 bytes # status, pres MSB1, pres MSB, pres LSB data = bus.read_i2c_block_data(0x60, 0x00, 4) # Convert the data to 20-bits pres = ((data[1] * 65536) + (data[2] * 256) + (data[3] & 0xF0)) / 16 thisPressure = (pres / 4.0) / 3000.0 mplPres = SensorReading("MPL3115A2", "baromin", thisPressure, "hpa") mplTem = SensorReading("MPL3115A2", "tempf", fTemp, "f") mplAlt = SensorReading("MPL3115A2", "altitude", thisAltitude, "m") return (mplTem, mplPres, mplAlt)
def getPeriodTotal(self, newLabel, period): global sensorLog thisSum = 0 # need to have some readings for this to make sense if len(self.sensorLog) > 0: for thisReading in self.sensorLog: if (time.time() - thisReading.timeStamp) <= period: thisSum += thisReading.value return (SensorReading(self.sensor, newLabel, thisSum, self.unit))
def logSensorEvent(self, channel): global sensorLog # add to the global sensor array self.sensorLog.append( SensorReading(self.sensor, self.label, self.calibration, self.unit)) # remove last until no more older than periodToKeep while (time.time() - self.sensorLog[0].timeStamp) >= self.periodToKeep: del self.sensorLog[0]
def getReading(): # Choose a gain of 1 for reading voltages from 0 to 4.09V. # Or pick a different gain to change the range of voltages that are read: # - 2/3 = +/-6.144V # - 1 = +/-4.096V # - 2 = +/-2.048V # - 4 = +/-1.024V # - 8 = +/-0.512V # - 16 = +/-0.256V # See table 3 in the ADS1015/ADS1115 datasheet for more info on gain. GAIN = 16 # the channel on the ADC to use CHANNEL = 0 # Create an ADS1115 ADC (16-bit) instance and do stuff with it adc = Adafruit_ADS1x15.ADS1115() adc.start_adc(CHANNEL, gain=GAIN) start = time.time() value = 0 totalVoltage = 0 cnt = 0 #DEBUG #print("[PRE]adc.get_last_result()[",adc.get_last_result(),"]") while (time.time() - start) <= 5.0: # will sometimes give negative results thisRead = -1 while thisRead < 1: thisRead = adc.get_last_result() #DEBUG: finding they are about a decimal place out #DEBUG: hacky #DEBUG #print(cnt,": thisRead[",thisRead,"]") totalVoltage += thisRead / 10 #DEBUG: /10 to get it into a measurable range. this is bad and wrong cnt += 1 time.sleep(0.5) #DEBUG #print("[POST]adc.get_last_result()[",adc.get_last_result(),"]") # Stop continuous conversion. After this point you can't get data from get_last_result! adc.stop_adc() avgVoltage = totalVoltage / cnt #DEBUG #print("avgVoltage[",avgVoltage,"] = totalVoltage[",totalVoltage,"] / cnt[",cnt,"] (G:[",GAIN,"] C:[",CHANNEL,"])") return(SensorReading("winddir", "winddir", convertors.voltToDeg(avgVoltage,WIND_READ_VOLT,WIND_DIR_MOUNT_ADJ), "degree angle"))
def getLast(self): global sensorLog if len(self.sensorLog) > 0: return (self.sensorLog[-1]) else: return (SensorReading(self.sensor, self.label, 0, self.unit))