副題 『えらいこっちゃ』

おはようございます。

昨晩は、何度もスマホに届いた警報メールで目が覚めました。

誤報かと思っていたら、

なのですね。
 
昨日は、らぶ ちゃんに会いに行ってきました。相変わらず元気です。。。
その場で里親の申し込みをして返答待ちでしたが、昨晩12:00PM前に預かりさんから連絡メールが届きました。
預かりさんのお話ですと、現在の体重は36Kgになったそうです。
 
連絡メールでは、2つばかり条件が付いていました。
長女、長男も含めてLINEでの家族会議となりました。
 
さて、

からの続きになります。

上差し松バージョンです。

ブレッドボード右下にある双眼鏡の様なパーツは、後日紹介予定のHC-SR04超音波距離センサーです。

HC-SR04超音波距離センサー用に使用するGPIOを追記しました。

 

  # Create the sensor object for WiFi Kit 32 V1
  sensor = HCSR04(trigger_pin=22, echo_pin=21, echo_timeout_us=10000)

 

今回は、BME280で読み込んだデータをoled SSD1306に表示するスクリプトの紹介です。

Web Server機能は持たせてありません。

"""
 Read the data from Bosch sensor

 The MIT License (MIT)

 Copyright (c) 2022 jk1brk

 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.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.

 Reference:
 http://nopnop2002.webcrow.jp/OrangePi2GIOT/python-periphery-22.html
 https://akizukidenshi.com/download/ds/bosch/BST-BME280_DS001-10.pdf
 https://akizukidenshi.com/download/ds/akizuki/AE-BME280_manu_v1.1.pdf
"""
from machine import Pin, SoftI2C
import ssd1306, utime

# Same as VSPI for BME280
SCK = 18 
MOSI = 23
MISO = 19
CS = 17 

# Set GPIO direction for SPI
gpio_mosi = Pin(MOSI, Pin.OUT)
gpio_miso = Pin(MISO, Pin.IN)
gpio_sck = Pin(SCK, Pin.OUT)
gpio_cs = Pin(CS, Pin.OUT)
  
# Operating Modes
BME280_OSAMPLE_1 = 1
BME280_OSAMPLE_2 = 2
BME280_OSAMPLE_4 = 3
BME280_OSAMPLE_8 = 4
BME280_OSAMPLE_16 = 5

# BME280 Registers
BME280_REGISTER_DIG_T1 = 0x88  # Trimming parameter registers
BME280_REGISTER_DIG_T2 = 0x8A
BME280_REGISTER_DIG_T3 = 0x8C

BME280_REGISTER_DIG_P1 = 0x8E
BME280_REGISTER_DIG_P2 = 0x90
BME280_REGISTER_DIG_P3 = 0x92
BME280_REGISTER_DIG_P4 = 0x94
BME280_REGISTER_DIG_P5 = 0x96
BME280_REGISTER_DIG_P6 = 0x98
BME280_REGISTER_DIG_P7 = 0x9A
BME280_REGISTER_DIG_P8 = 0x9C
BME280_REGISTER_DIG_P9 = 0x9E

BME280_REGISTER_DIG_H1 = 0xA1
BME280_REGISTER_DIG_H2 = 0xE1
BME280_REGISTER_DIG_H3 = 0xE3
BME280_REGISTER_DIG_H4 = 0xE4
BME280_REGISTER_DIG_H5 = 0xE5
BME280_REGISTER_DIG_H6 = 0xE6
BME280_REGISTER_DIG_H7 = 0xE7

BME280_REGISTER_CHIPID = 0xD0
BME280_REGISTER_VERSION = 0xD1
BME280_REGISTER_SOFTRESET = 0xE0

BME280_REGISTER_CONTROL_HUM = 0xF2
BME280_REGISTER_CONTROL = 0xF4
BME280_REGISTER_CONFIG = 0xF5
BME280_REGISTER_PRESSURE_DATA = 0xF7
BME280_REGISTER_TEMP_DATA = 0xFA
BME280_REGISTER_HUMIDITY_DATA = 0xFD

def readChipId():
    ChipId = read8bit(BME280_REGISTER_CHIPID)
    print("ChipId = 0x%x " % ChipId),
    if (ChipId == 0x58): print("BMP280")
    elif (ChipId == 0x60): print("BME280")
    elif (ChipId == 0x61): print("BME680")
    else:print("Unknown")
      
def readChipVer():
    ChipVer = read8bit(BME280_REGISTER_VERSION)
    print("ChipVer = 0x%x " % ChipVer)

def int16(x):
  if x>0xFFFF:
    raise OverflowError
  if x>0x7FFF:
    x=int(0x10000-x)
    if x<2147483648:
      return -x
    else:
      return -2147483648
  return x

def calibration_T(adc_T):
    global t_fine
    #Get the compensated temperature in 0.01 of a degree celsius.
    adc = adc_T
    var1 = ((adc >> 3) - (dig_T1 << 1)) * (dig_T2 >> 11)
    var2 = ((
        (((adc >> 4) - dig_T1) * ((adc >> 4) - dig_T1)) >> 12) *
        dig_T3) >> 14
    t_fine = var1 + var2
    return (t_fine * 5 + 128) >> 8

def calibration_P(adc_P):
    global t_fine
    #Gets the compensated pressure in Pascals.
    adc = adc_P
    var1 = t_fine - 128000
    var2 = var1 * var1 * dig_P6
    var2 = var2 + ((var1 * dig_P5) << 17)
    var2 = var2 + (dig_P4 << 35)
    var1 = (((var1 * var1 * dig_P3) >> 8) +
            ((var1 * dig_P2) >> 12))
    var1 = (((1 << 47) + var1) * dig_P1) >> 33
    if var1 == 0:
      return 0
    p = 1048576 - adc
    p = (((p << 31) - var2) * 3125) // var1
    var1 = (dig_P9 * (p >> 13) * (p >> 13)) >> 25
    var2 = (dig_P8 * p) >> 19
    return ((p + var1 + var2) >> 8) + (dig_P7 << 4)

def calibration_H(adc_H):
    adc = adc_H
    # print 'Raw humidity = {0:d}'.format (adc)
    h = t_fine - 76800
    h = (((((adc << 14) - (dig_H4 << 20) - (dig_H5 * h)) +
           16384) >> 15) * (((((((h * dig_H6) >> 10) *
           (((h * dig_H3) >> 11) + 32768)) >> 10) + 2097152) *
           dig_H2 + 8192) >> 14))
    h = h - (((((h >> 15) * (h >> 15)) >> 7) * dig_H1) >> 4)
    h = 0 if h < 0 else h
    h = 419430400 if h > 419430400 else h
    return h >> 12

def writeReg(reg_address, data):
    gpio_cs.off()
    SpiWrite(reg_address & 0x7F)  # write, bit 7 low
    SpiWrite(data)
    gpio_cs.on()

def read16bit(reg):
    gpio_cs.off()
    SpiWrite(reg | 0x80)  # read, bit 7 high
    d1 = SpiRead()
    d2 = SpiRead()
    data = (d2 << 8) | d1
    gpio_cs.on()
    return data

def read8bit(reg):
    gpio_cs.off()
    SpiWrite(reg | 0x80)  # read, bit 7 high
    data = SpiRead();
    gpio_cs.on()
    return data;

# Write for Bit Banging SPI
def SpiWrite(data):
  mask = 0x80
  for x in range(8):
    gpio_sck.off()
    bit = data & mask
    if (bit != 0):
      gpio_mosi.on()
    if (bit == 0):
      gpio_mosi.off()
    gpio_sck.on()
    mask = mask >> 1

# Read for Bit Banging SPI
def SpiRead():
  r_data = 0;
  mask = 0x80
  gpio_mosi.off()
  for x in range(8):
    r_data = r_data << 1
    gpio_sck.off()
    gpio_sck.on()
    bit = gpio_miso.value()
    if (bit == True):
      r_data = r_data + 1
  return r_data;

def main():
  while True: 
   print('Read temperature, pressure and humidity')
   data = []
   register = BME280_REGISTER_PRESSURE_DATA
   for x in range(8):
      value = read8bit(register)
      #if(DEBUG == 1):print value,
      data.append(value)
      register=register+1
   pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4)  #0xF7, msb+lsb+xlsb=19bit
   temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4)  #0xFA, msb+lsb+xlsb=19bit
   humi_raw = (data[6] << 8) | data[7]                           #0xFD, msb+lsb=15bit 
  
   #Return the temperature in degrees.
   t = calibration_T(temp_raw)
   ti = t // 100
   td = t - ti * 100
   temp_act = "{}.{:02d} *C".format(ti, td)
  
   #Return the pressure in hPa.
   p = calibration_P(pres_raw) // 256
   pi = p // 100
   pd = p - pi * 100
   press_act = "{}.{:02d} hPa".format(pi, pd)

   #Return the humidity in percent.
   h = calibration_H(humi_raw)
   hi = h // 1024
   hd = h * 100 // 1024 - hi * 100
   humid_act = "{}.{:02d} %".format(hi, hd)

   print("-----------------------");
   print("Temperature =",temp_act)
   print("Pressure =",press_act)
   print("Humidity =",humid_act)
 
   oled.fill(0)
   oled.text('Temperature:', 0, 0)
   oled.text(temp_act, 0, 10)
   oled.text('Pressure:' , 0, 20)
   oled.text(press_act, 0, 30)
   oled.text('Humidity:', 0, 40)
   oled.text(humid_act, 0, 50)
   oled.show()
   
   utime.sleep(10)
      
if __name__=="__main__":
  t_sb = 5                   #stanby 1000ms
  filter = 0                 #filter O = off
  spi3or4 = 0                #SPI 3wire or 4wire, 0=4wire, 1=3wire
  osrs_t = BME280_OSAMPLE_8  #OverSampling Temperature x8
  osrs_p = BME280_OSAMPLE_8  #OverSampling Pressure x8
  osrs_h = BME280_OSAMPLE_8  #OverSampling Humidity x8
  Mode = BME280_OSAMPLE_4    #Normal mode

  temp_raw = 0
  pres_raw = 0
  humi_raw = 0
  t_fine = 0
  
  print('Read the contents of the ID & Ver register')
  readChipId()
  readChipVer()
  
  # Send a command to the control register[0xF2]
  ctrl_meas_reg_h = osrs_h
  writeReg(BME280_REGISTER_CONTROL_HUM,ctrl_meas_reg_h)
  
  # Send a command to the control register[0xF4]
  ctrl_meas_reg = (osrs_t << 5) | (osrs_p << 2) | Mode
  writeReg(BME280_REGISTER_CONTROL,ctrl_meas_reg)

  # Send a command to the config register[0xF5]
  config_reg = (t_sb << 5) | (filter << 2) | spi3or4
  writeReg(BME280_REGISTER_CONFIG,config_reg)
    
  print('Read calibration data')
  dig_T1 = read16bit(BME280_REGISTER_DIG_T1)
  dig_T2 = int16(read16bit(BME280_REGISTER_DIG_T2))
  dig_T3 = int16(read16bit(BME280_REGISTER_DIG_T3))

  dig_P1 = read16bit(BME280_REGISTER_DIG_P1)
  dig_P2 = int16(read16bit(BME280_REGISTER_DIG_P2))
  dig_P3 = int16(read16bit(BME280_REGISTER_DIG_P3))
  dig_P4 = int16(read16bit(BME280_REGISTER_DIG_P4))
  dig_P5 = int16(read16bit(BME280_REGISTER_DIG_P5))
  dig_P6 = int16(read16bit(BME280_REGISTER_DIG_P6))
  dig_P7 = int16(read16bit(BME280_REGISTER_DIG_P7))
  dig_P8 = int16(read16bit(BME280_REGISTER_DIG_P8))
  dig_P9 = int16(read16bit(BME280_REGISTER_DIG_P9))

  dig_H1 = read8bit(BME280_REGISTER_DIG_H1)
  dig_H2 = int16(read16bit(BME280_REGISTER_DIG_H2))
  dig_H3 = read8bit(BME280_REGISTER_DIG_H3)
  dig_H6 = read8bit(BME280_REGISTER_DIG_H7)
  h4 = int(read8bit(BME280_REGISTER_DIG_H4))
  h4 = (h4 << 24) >> 20
  dig_H4 = h4 | (read8bit(BME280_REGISTER_DIG_H5) & 0x0F)
  h5 = int(read8bit(BME280_REGISTER_DIG_H6))
  h5 = (h5 << 24) >> 20
  dig_H5 = h5 | (read8bit(BME280_REGISTER_DIG_H5) >> 4 & 0x0F)

  # Heltec WiFi kit 32 V1 with OLED Display
  print('OLED set up')
  oled_width = 128
  oled_height = 64
  # OLED reset pin
  i2c_rst = Pin(16, Pin.OUT)
  # Initialize the OLED display
  i2c_rst.off()
  time.sleep_ms(5)
  i2c_rst.on() # must be held high after initialization
  # Setup the I2C lines
  i2c_scl = Pin(15, Pin.OUT, Pin.PULL_UP)
  i2c_sda = Pin(4, Pin.OUT, Pin.PULL_UP)

  # Create the bus object
  i2c = SoftI2C(scl=i2c_scl, sda=i2c_sda)
  
  # Create the display object
  oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
  oled.fill(0)
  oled.text(wlan.ifconfig()[0], 0, 0)
  oled.text('HELLO WiFi ESP32', 0, 25)
  oled.text('jk1brk.net', 0, 50)
  
  oled.show()
  utime.sleep(3)
     
  main()
 
TBC