Raspberry Pi 3 ã§K09421(BME280)ã®ã»ã³ãµåºåãå®æçã«csvã¸åºåãã
ç®ç
ãK09421ããã»ã³ãµåºåãåå¾ã§ããããã«ãªãã¾ããã®ã§ä»åº¦ã¯å®æçã«ãã¼ã¿ãåå¾ãã¦ãã°ãã¡ã¤ã«ã«æ®ããã¨ãç®æãã¾ãã
æ¹é
æ¹éã¨ãã¦ã¯cronã§BME280ããåºåãèªã¿ã¨ãããã°ã©ã ãå®æçã«å¼ã³åºãã¦"yyyyMMdd_bme280.csv"ã¨ããå½¢å¼ã§ä¸æ¥ãã¨ã«ãã¡ã¤ã«ãåå²ãã¦csvãã¡ã¤ã«ã«æ¸ãè¾¼ãã¨ããæµãã§ãã
ã»ã³ãµã®å¤ãèªã¿ã¨ã£ã¦csvã«åºåããããã°ã©ã ã¯前回ã«ä½ã£ããã®ãæ¹é ãã¦å®è£
ãã¾ãã
hh:mm:ss,温度(â),å§å(hPa),湿度(%)
ä¿åããcsvã®ãã©ã¼ãããã¯ãã®ããã«ãã¾ããæé(hh):å(mm):ç§(dd)ã¨ãã¦ãã¾ããããã¯ä»å¾å¦çããã¨ãã«å®¹æã«ä»ã®ããã°ã©ã ã§ãåãè¾¼ããããã«isoæ¡å¼µãã©ã¼ãããã¨ãã¦ãã¾ãã
ãã®æ¹éã«å¾ã£ã¦è¨å®ãã³ã¼ãã£ã³ã°ãé²ãã¦ããã¾ãã
ã³ã¼ãã£ã³ã°
ã¾ãååä½æããã»ã³ãµå¤åå¾ããã°ã©ã ãå
ã»ã©æ±ºããæ¹éã«å¾ã£ã¦æ¸ãæãã¦ããã¾ãã
追å ããæ©è½ã¯ä»¥ä¸ã®2ã¤ã§ãã
- ç¾å¨ã®æå»ã¨æ¥ä»ãåå¾ãã
- åå¾ããæå»ã¨æ¥ä»ãããã¡ã¤ã«åãçæããæå»ã¨ã»ã³ãµå¤ãcsvã«æ¸ãè¾¼ã
ã¢ã¸ã¥ã¼ã«ã®ã¤ã³ãã¼ã
ããã§æ°ããæ©è½ãå®ç¾ããããã« datetimeã¢ã¸ã¥ã¼ã«ã¨csvã¢ã¸ã¥ã¼ã«ã追å ãã¾ãã
import smbus #import time #ä¸ä½¿ç¨ import datetimeã #追å ãã import csvãããã#追å
å¤æ°è¨å®é¨
å¤æ°ãè¨å®ããé¨åã«ãã¼ã¿ãä¿åãããã£ã¬ã¯ããªãè¨å®ãã¾ãã
i2c_address = 0x76 DATA_DIR = '/home/pi/data/' #追å bus = smbus.SMBus(bus_number)
é¢æ°
次ã«å¤æ´ãå ããé¢æ°ã®ä¸è¦§ã§ãã
- readData
- compensate_T
- compensate_P
- compensate_H
readDataã§ã¯csvã¸ã®æ¸ãåºããè¡ãããã«å¤æ´ãã¾ãã
compensate_*ã§ã¯readDataã«ãã¼ã¿ãåã渡ãããã«å¤æ´ãè¡ãã¾ãã
compensate_T
ã¾ããcompensate_Tã§ã¯ä»¥ä¸ã®ããã«ããã°ã©ã ã®æå¾ã«ä¸æã追å ãã¾ãã
print "temp : %-6.2f â %" %(temperature) return temperature #追å
compensate_P
print "pressure : %7.2 f hPa" %(pressure/100) return pressure/100.0 #追å
compensate_H
print "hum : %6.2f %" %(var h) return var_h #追å
readData
#get record time and date record_datetime = datetime.datetime.now() #ç¾å¨ã®æ¥æã¨æå»ãåå¾ record_file_name = record_datetime.strftime('%Y%m%d')+'_bme280.csv'ã#ãã¡ã¤ã«åãçæ record_time = record_datetime.strftime('%X') #æå»æååãçæ data=[]
ãã®ããã«ç¾å¨æå»ãåå¾ãã¦ãã¡ã¤ã«åã¨æå»ã®æååãçæãã¾ãã
T=compensate_T(temp_raw) P=compensate_P(pres_raw) H=compensate_H(hum_raw)
å ã»ã©è¿ãå¤ãåºåãããããã«å¤æ´ããã®ã§ãã®åºåãå¤æ°ã«æ ¼ç´ãã¾ãã
writer = csv.writer(open(DATA_DIR+record_file_name, 'ab')) writer.writerow([record_time,T,P,H])
csvãappendã¢ã¼ãã§éãã[record_time,T,P,H]ã®é çªã«é åãæ ¼ç´ãã¦csvã«æ¸ãè¾¼ã¿ã¾ãã
ããã°ã©ã é å¸
以ä¸ã«ä½æããããã°ã©ã ãbme280_csv.pyãå
¬éãã¦ãã¾ãã使ç¨ãããæ¹ã¯ãèªç±ã«ãå©ç¨ãã ããã
GitHub - kagemomiji/raspi3
cronã®è¨å®
次ã«ä½æããããã°ã©ã ãå®æçã«å®è¡ããcronã®è¨å®ããã¾ãã
crontab -e
crontabãç¨ãã¦è¨å®ãã¾ãã
*/5 * * * * python {ãã¡ã¤ã«ã¸ã®çµ¶å¯¾ãã¹}
ä¸è¨ã®ããã«5åããã«å®è¡ãããããã«è¨å®ãã¾ããã
10åããã§ã¯ */10 * * * * ã³ãã³ã
1æéããã§ã¯ (å®è¡ãããå) * * * * ã³ãã³ã
ã¨ããããã«è¨å®ãã¾ãã
è¨å®ãçµããç·¨éãå®äºãããcronã«è¨å®ãåæ ãããã®å¾å®è¡ãããããã«ãªãã¾ãã
åºåä¾
22:30:01,29.46672882,1001.354688,56.09067817 22:35:01,29.39716974,1001.405821,54.73285085 22:40:01,29.43100929,1001.44832,54.44254727 22:45:01,29.4075096,1001.442746,54.00647973 22:50:02,29.36019691,1001.457592,53.52315578 22:55:01,29.33419061,1001.406453,52.97012485 23:00:01,29.30661768,1001.435586,52.58357062 23:05:01,29.25241182,1001.510298,52.34396932 23:10:01,29.23110548,1001.519919,52.43595777 23:15:01,29.58548072,1001.406111,53.4440672 23:20:02,29.4075096,1001.373139,53.34538086 23:25:01,29.31006429,1001.384926,53.00620219 23:30:01,29.56448763,1001.39142,54.19737383 23:35:01,29.5441212,1001.356211,53.91712263 23:40:01,29.38996317,1001.359144,53.30443224 23:45:01,29.59174732,1001.302045,53.63377335 23:50:02,29.55947436,1001.348349,54.10231546 23:55:01,29.40030303,1001.362922,53.60009775
1æéã®ãã¼ã¿ã確èªãã¦ã¿ã¾ãããã¨ã¢ã³ã³ãä»ãã¦ããããã»ã¨ãã©å¤åãã¦ããªããã¨ããããã¾ãã
ãããã«
ããã§å®æçã«ãã°ãåå¾ããã·ã¹ãã ãå®æãã¾ããã
次ã¯ã¨ã¢ã³ã³ã®å¶å¾¡ãããããã®ãªã¢ã³ã³é¨åã®è¨è¨ããããã¨æãã¾ãã