ESPでやろうと思ったが結構ソースが面倒な感じだ。pythonでやりたいかな。ESPはこちらが主力な方法か。ライブラリの関数(メソッドか)に隠ぺいされて何だか返って分からない。ラズパイのI2C関数で意識させてプログラムの方がいいな。まあライブラリ頼みいいか悪いかは個々の判断だから。楽がなによりだ。まずは出来ること優先は正しい。
●ラズパイで先行することに
●ラズパイのソース(多謝)
#coding: utf-8 import smbus import time import datetime bus_number = 1 i2c_address = 0x76 bus = smbus.SMBus(bus_number) digT = [] digP = [] digH = [] t_fine = 0.0 sensor_data = {'temp':'0.0', 'pressure':'0.0','humidity':'0.0'} def writeReg(reg_address, data): bus.write_byte_data(i2c_address,reg_address,data) def get_calib_param(): calib = [] for i in range (0x88,0x88+24): calib.append(bus.read_byte_data(i2c_address,i)) calib.append(bus.read_byte_data(i2c_address,0xA1)) for i in range (0xE1,0xE1+7): calib.append(bus.read_byte_data(i2c_address,i)) digT.append((calib[1] << 8) | calib[0]) digT.append((calib[3] << 8) | calib[2]) digT.append((calib[5] << 8) | calib[4]) digP.append((calib[7] << 8) | calib[6]) digP.append((calib[9] << 8) | calib[8]) digP.append((calib[11]<< 8) | calib[10]) digP.append((calib[13]<< 8) | calib[12]) digP.append((calib[15]<< 8) | calib[14]) digP.append((calib[17]<< 8) | calib[16]) digP.append((calib[19]<< 8) | calib[18]) digP.append((calib[21]<< 8) | calib[20]) digP.append((calib[23]<< 8) | calib[22]) digH.append( calib[24] ) digH.append((calib[26]<< 8) | calib[25]) digH.append( calib[27] ) digH.append((calib[28]<< 4) | (0x0F & calib[29])) digH.append((calib[30]<< 4) | ((calib[29] >> 4) & 0x0F)) digH.append( calib[31] ) for i in range(1,2): if digT[i] & 0x8000: digT[i] = (-digT[i] ^ 0xFFFF) + 1 for i in range(1,8): if digP[i] & 0x8000: digP[i] = (-digP[i] ^ 0xFFFF) + 1 for i in range(0,6): if digH[i] & 0x8000: digH[i] = (-digH[i] ^ 0xFFFF) + 1 def readData(): data = [] for i in range (0xF7, 0xF7+8): data.append(bus.read_byte_data(i2c_address,i)) pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4) temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4) hum_raw = (data[6] << 8) | data[7] compensate_T(temp_raw) compensate_P(pres_raw) compensate_H(hum_raw) def compensate_P(adc_P): global t_fine pressure = 0.0 v1 = (t_fine / 2.0) - 64000.0 v2 = (((v1 / 4.0) * (v1 / 4.0)) / 2048) * digP[5] v2 = v2 + ((v1 * digP[4]) * 2.0) v2 = (v2 / 4.0) + (digP[3] * 65536.0) v1 = (((digP[2] * (((v1 / 4.0) * (v1 / 4.0)) / 8192)) / 8) + ((digP[1] * v1) / 2.0)) / 262144 v1 = ((32768 + v1) * digP[0]) / 32768 if v1 == 0: return 0 pressure = ((1048576 - adc_P) - (v2 / 4096)) * 3125 if pressure < 0x80000000: pressure = (pressure * 2.0) / v1 else: pressure = (pressure / v1) * 2 v1 = (digP[8] * (((pressure / 8.0) * (pressure / 8.0)) / 8192.0)) / 4096 v2 = ((pressure / 4.0) * digP[7]) / 8192.0 pressure = pressure + ((v1 + v2 + digP[6]) / 16.0) print ("気圧 : %7.2f hPa" % (pressure/100)) #sensor_data['pressure'] = pressure/100 def compensate_T(adc_T): global t_fine v1 = (adc_T / 16384.0 - digT[0] / 1024.0) * digT[1] v2 = (adc_T / 131072.0 - digT[0] / 8192.0) * (adc_T / 131072.0 - digT[0] / 8192.0) * digT[2] t_fine = v1 + v2 temperature = t_fine / 5120.0 print ("温度 : %-6.2f ℃" % (temperature)) #sensor_data['temp'] = temperature def compensate_H(adc_H): global t_fine var_h = t_fine - 76800.0 if var_h != 0: var_h = (adc_H - (digH[3] * 64.0 + digH[4]/16384.0 * var_h)) * (digH[1] / 65536.0 * (1.0 + digH[5] / 67108864.0 * var_h * (1.0 + digH[2] / 67108864.0 * var_h))) else: return 0 var_h = var_h * (1.0 - digH[0] * var_h / 524288.0) if var_h > 100.0: var_h = 100.0 elif var_h < 0.0: var_h = 0.0 print ("湿度 : %6.2f %" % (var_h)) #sensor_data['humidity'] = var_h def setup(): osrs_t = 1 #Temperature oversampling x 1 osrs_p = 1 #Pressure oversampling x 1 osrs_h = 1 #Humidity oversampling x 1 mode = 3 #Normal mode t_sb = 5 #Tstandby 1000ms filter = 0 #Filter off spi3w_en = 0 #3-wire SPI Disable ctrl_meas_reg = (osrs_t << 5) | (osrs_p << 2) | mode config_reg = (t_sb << 5) | (filter << 2) | spi3w_en ctrl_hum_reg = osrs_h writeReg(0xF2,ctrl_hum_reg) writeReg(0xF4,ctrl_meas_reg) writeReg(0xF5,config_reg) setup() get_calib_param() #if __name__ == '__main__': while 1: try: print("---------") readData() time_str = datetime.datetime.today().strftime("%Y/%m/%d %H:%M:%S") temp_str = str(sensor_data['temp']) humid_str = str(sensor_data['humidity']) pressure_str = str(sensor_data['pressure']) # print(sensor_data) #print(time_str+","+temp_str+","+humid_str+","+pressure_str) time.sleep(1) except KeyboardInterrupt: break #pass
●以下ESP8266での使用ソース
#define D0 16 #define D1 5 // I2C Bus SCL (clock) #define D2 4 // I2C Bus SDA (data) #define D3 0 #define D4 2 // Same as "LED_BUILTIN", but inverted logic #define D5 14 // SPI Bus SCK (clock) #define D6 12 // SPI Bus MISO #define D7 13 // SPI Bus MOSI #define D8 15 // SPI Bus SS (CS) #define D9 3 // RX0 (Serial console) #define D10 1 // TX0 (Serial console)
/* Copyright (c) 2015, Embedded Adventures All rights reserved. Contact us at source [at] embeddedadventures.com www.embeddedadventures.com Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of Embedded Adventures nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ // BME280 MOD-1022 weather multi-sensor Arduino demo // Written originally by Embedded Adventures #include <BME280_MOD-1022.h> #include <Wire.h> // Arduino needs this to pring pretty numbers void printFormattedFloat(float x, uint8_t precision) { char buffer[10]; dtostrf(x, 7, precision, buffer); Serial.print(buffer); } // print out the measurements void printCompensatedMeasurements(void) { float temp, humidity, pressure, pressureMoreAccurate; double tempMostAccurate, humidityMostAccurate, pressureMostAccurate; char buffer[80]; temp = BME280.getTemperature(); humidity = BME280.getHumidity(); pressure = BME280.getPressure(); pressureMoreAccurate = BME280.getPressureMoreAccurate(); // t_fine already calculated from getTemperaure() above tempMostAccurate = BME280.getTemperatureMostAccurate(); humidityMostAccurate = BME280.getHumidityMostAccurate(); pressureMostAccurate = BME280.getPressureMostAccurate(); Serial.println(" Good Better Best"); Serial.print("Temperature "); printFormattedFloat(temp, 2); Serial.print(" "); printFormattedFloat(tempMostAccurate, 2); Serial.println(); Serial.print("Humidity "); printFormattedFloat(humidity, 2); Serial.print(" "); printFormattedFloat(humidityMostAccurate, 2); Serial.println(); Serial.print("Pressure "); printFormattedFloat(pressure, 2); Serial.print(" "); printFormattedFloat(pressureMoreAccurate, 2); Serial.print(" "); printFormattedFloat(pressureMostAccurate, 2); Serial.println(); } // setup wire and serial void setup() { Wire.begin(); // Wire.begin(sda, scl) pinMode(12, OUTPUT); Serial.begin(115200); } // main loop void loop() { uint8_t chipID; Serial.println(); Serial.println(); Serial.println("Welcome to the BME280 MOD-1022 weather multi-sensor test sketch!"); Serial.println("Embedded Adventures (www.embeddedadventures.com)"); chipID = BME280.readChipId(); // find the chip ID out just for fun Serial.print("ChipID = 0x"); Serial.println(chipID, HEX); // need to read the NVM compensation parameters BME280.readCompensationParams(); // Need to turn on 1x oversampling, default is os_skipped, which means it doesn't measure anything BME280.writeOversamplingPressure(os1x); // 1x over sampling (ie, just one sample) BME280.writeOversamplingTemperature(os1x); BME280.writeOversamplingHumidity(os1x); // example of a forced sample. After taking the measurement the chip goes back to sleep BME280.writeMode(smForced); while (BME280.isMeasuring()) { Serial.println("Measuring..."); delay(50); } Serial.println("Done!"); // read out the data - must do this before calling the getxxxxx routines BME280.readMeasurements(); Serial.print("Temp="); Serial.println(BME280.getTemperature()); // must get temp first Serial.print("Humidity="); Serial.println(BME280.getHumidity()); Serial.print("Pressure="); Serial.println(BME280.getPressure()); Serial.print("PressureMoreAccurate="); Serial.println(BME280.getPressureMoreAccurate()); // use int64 calculcations Serial.print("TempMostAccurate="); Serial.println(BME280.getTemperatureMostAccurate()); // use double calculations Serial.print("HumidityMostAccurate="); Serial.println(BME280.getHumidityMostAccurate()); // use double calculations Serial.print("PressureMostAccurate="); Serial.println(BME280.getPressureMostAccurate()); // use double calculations // Example for "indoor navigation" // We'll switch into normal mode for regular automatic samples BME280.writeStandbyTime(tsb_0p5ms); // tsb = 0.5ms BME280.writeFilterCoefficient(fc_16); // IIR Filter coefficient 16 BME280.writeOversamplingPressure(os16x); // pressure x16 BME280.writeOversamplingTemperature(os2x); // temperature x2 BME280.writeOversamplingHumidity(os1x); // humidity x1 BME280.writeMode(smNormal); while (1) { digitalWrite(12, HIGH); while (BME280.isMeasuring()) { } // read out the data - must do this before calling the getxxxxx routines BME280.readMeasurements(); printCompensatedMeasurements(); delay(1000); Serial.println(); digitalWrite(12, LOW); delay(1000); } }No tags for this post.