##やろうとしていること
① Raspberry PiでBME280の温度・湿度・気圧データを取得 (済)
② 取得したBME280のデータをGoogle Driveに保存(今ここの段階です。)
##困っていること
次のプログラムコードを試しているときにエラーが発生しました。
エラーの原因がつかめずに困っています。
##エラー内容
html
1Traceback (most recent call last): 2 File "5_BME280GDV.py", line 178, in <module> 3 column_datas.append("%7.2f" % (pressure/100)) 4NameError: name 'pressure' is not defined
##プログラムコード
html
1#!/usr/bin/python 2# -*- coding: utf-8 -*- 3 4from smbus import SMBus 5import time 6 7bus_number = 1 8i2c_address = 0x76 9 10bus = SMBus(bus_number) 11 12digT = [] 13digP = [] 14digH = [] 15 16t_fine = 0.0 17 18 19def writeReg(reg_address, data): 20 bus.write_byte_data(i2c_address,reg_address,data) 21 22def get_calib_param(): 23 calib = [] 24 25 for i in range (0x88,0x88+24): 26 calib.append(bus.read_byte_data(i2c_address,i)) 27 calib.append(bus.read_byte_data(i2c_address,0xA1)) 28 for i in range (0xE1,0xE1+7): 29 calib.append(bus.read_byte_data(i2c_address,i)) 30 31 digT.append((calib[1] << 8) | calib[0]) 32 digT.append((calib[3] << 8) | calib[2]) 33 digT.append((calib[5] << 8) | calib[4]) 34 digP.append((calib[7] << 8) | calib[6]) 35 digP.append((calib[9] << 8) | calib[8]) 36 digP.append((calib[11]<< 8) | calib[10]) 37 digP.append((calib[13]<< 8) | calib[12]) 38 digP.append((calib[15]<< 8) | calib[14]) 39 digP.append((calib[17]<< 8) | calib[16]) 40 digP.append((calib[19]<< 8) | calib[18]) 41 digP.append((calib[21]<< 8) | calib[20]) 42 digP.append((calib[23]<< 8) | calib[22]) 43 digH.append( calib[24] ) 44 digH.append((calib[26]<< 8) | calib[25]) 45 digH.append( calib[27] ) 46 digH.append((calib[28]<< 4) | (0x0F & calib[29])) 47 digH.append((calib[30]<< 4) | ((calib[29] >> 4) & 0x0F)) 48 digH.append( calib[31] ) 49 50 for i in range(1,2): 51 if digT[i] & 0x8000: 52 digT[i] = (-digT[i] ^ 0xFFFF) + 1 53 54 for i in range(1,8): 55 if digP[i] & 0x8000: 56 digP[i] = (-digP[i] ^ 0xFFFF) + 1 57 58 for i in range(0,6): 59 if digH[i] & 0x8000: 60 digH[i] = (-digH[i] ^ 0xFFFF) + 1 61 62def readData(): 63 data = [] 64 for i in range (0xF7, 0xF7+8): 65 data.append(bus.read_byte_data(i2c_address,i)) 66 pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4) 67 temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4) 68 hum_raw = (data[6] << 8) | data[7] 69 70 compensate_T(temp_raw) 71 compensate_P(pres_raw) 72 compensate_H(hum_raw) 73 74def compensate_P(adc_P): 75 global t_fine 76 pressure = 0.0 77 78 v1 = (t_fine / 2.0) - 64000.0 79 v2 = (((v1 / 4.0) * (v1 / 4.0)) / 2048) * digP[5] 80 v2 = v2 + ((v1 * digP[4]) * 2.0) 81 v2 = (v2 / 4.0) + (digP[3] * 65536.0) 82 v1 = (((digP[2] * (((v1 / 4.0) * (v1 / 4.0)) / 8192)) / 8) + ((digP[1] * v1) / 2.0)) / 262144 83 v1 = ((32768 + v1) * digP[0]) / 32768 84 85 if v1 == 0: 86 return 0 87 pressure = ((1048576 - adc_P) - (v2 / 4096)) * 3125 88 if pressure < 0x80000000: 89 pressure = (pressure * 2.0) / v1 90 else: 91 pressure = (pressure / v1) * 2 92 v1 = (digP[8] * (((pressure / 8.0) * (pressure / 8.0)) / 8192.0)) / 4096 93 v2 = ((pressure / 4.0) * digP[7]) / 8192.0 94 pressure = pressure + ((v1 + v2 + digP[6]) / 16.0) 95 print "pressure : %7.2f hPa" % (pressure/100) 96 97def compensate_T(adc_T): 98 global t_fine 99 v1 = (adc_T / 16384.0 - digT[0] / 1024.0) * digT[1] 100 v2 = (adc_T / 131072.0 - digT[0] / 8192.0) * (adc_T / 131072.0 - digT[0] / 8192.0) * digT[2] 101 t_fine = v1 + v2 102 temperature = t_fine / 5120.0 103 print "temp : %-6.2f ℃" % (temperature) 104 105def compensate_H(adc_H): 106 global t_fine 107 var_h = t_fine - 76800.0 108 if var_h != 0: 109 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))) 110 else: 111 return 0 112 var_h = var_h * (1.0 - digH[0] * var_h / 524288.0) 113 if var_h > 100.0: 114 var_h = 100.0 115 elif var_h < 0.0: 116 var_h = 0.0 117 print "hum : %6.2f %" % (var_h) 118 119 120def setup(): 121 osrs_t = 1 #Temperature oversampling x 1 122 osrs_p = 1 #Pressure oversampling x 1 123 osrs_h = 1 #Humidity oversampling x 1 124 mode = 3 #Normal mode 125 t_sb = 5 #Tstandby 1000ms 126 filter = 0 #Filter off 127 spi3w_en = 0 #3-wire SPI Disable 128 129 ctrl_meas_reg = (osrs_t << 5) | (osrs_p << 2) | mode 130 config_reg = (t_sb << 5) | (filter << 2) | spi3w_en 131 ctrl_hum_reg = osrs_h 132 133 writeReg(0xF2,ctrl_hum_reg) 134 writeReg(0xF4,ctrl_meas_reg) 135 writeReg(0xF5,config_reg) 136 137 138setup() 139get_calib_param() 140 141 142if __name__ == '__main__': 143 try: 144 readData() 145 except KeyboardInterrupt: 146 pass 147 148 149import gspread 150from oauth2client.service_account import ServiceAccountCredentials 151 152from datetime import datetime 153 154column_datas = [] 155 156def read_data(): 157 global column_datas 158 159column_datas.append(datetime.now().strftime("%Y/%m/%d %H:%M:%S")) 160 161 162def write_data(): 163 global column_datas 164 filename = '/home/pi/xxx/abcde.json' 165 client_email = "abcdef@gmail.com" 166 with open(filename) as f: private_key = f.read() 167 scopes = ['https://spreadsheets.google.com/feeds/', 'https://docs.google.com/feeds'] 168 credentials = ServiceAccountCredentials.from_json_keyfile_name(filename, scopes) 169 gs = gspread.authorize(credentials) 170 doc = gs.open('BME280') 171 sheet = doc.worksheet("BME280") 172 sheet.append_row(column_datas) 173 174 175def compensate_P(adc_P): 176 global column_datas 177 178column_datas.append("%7.2f" % (pressure/100)) 179 180 181def compensate_T(adc_T): 182 global column_datas 183 184column_datas.append("%-6.2f" % (temperature)) 185 186 187def compensate_H(adc_H): 188 global column_datas 189 190column_datas.append("%6.2f" % (var_h)) 191 192 193if __name__ == '__main__': 194 try: 195 read_data() 196 tweet_data() 197 write_data() 198 except: 199 pass
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/10/09 02:46