2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ESP32でセンサのログを出力する

Last updated at Posted at 2022-07-13

ESP32

ESP32はEspressif Systemsが開発したWi-FiとBluetoothを内蔵するマイクロコントローラである.ESP32は接続されているセンサと通信を行う方法としてI2C通信が挙げられる.今回はI2C通信が行えるセンサを対象とした実装を行った.

今回使用したセンサ

今回は温度センサを使用した.
温度センサはエイブリック株式会社が開発したS-5851Aを使用している.
詳しい情報は下記のデータシートを参照
S-5851Aのデータシート

ログの使用目的

ログを閲覧することでセンサが通電しているかが分かる.通電しているのにセンサデータが取得できなかった場合は、センサではなくプログラムに異常があるのが分かる.

構築環境

  • ESP32
    • マイクロコントローラ
  • MicroPython(1.17.0)
    • 使用したプログラミング言語
  • S-5851A
    • 温度センサ

出力するログ

  • ログの出力日時(time stamp)
  • センサのI2Cアドレス
  • 使用している言語のバージョン
  • 使用しているマイクロコントローラ名

ログを出力するプログラム

from machine import I2C, Pin, RTC
import utime
import ntptime
import sys

#ピンの設定
p21 = Pin(21,Pin.IN,Pin.PULL_UP)
p22 = Pin(22,Pin.IN,Pin.PULL_UP)
i2c =I2C(scl=Pin(22), sda=Pin(21))

#時間の設定
rtc = RTC()
ntptime.settime() 
tm = utime.localtime(utime.time()) # UTC now
datetime_jst = str(tm[0])+'/'+str(tm[1])+'/'+str(tm[2])+' '+str((tm[3]+9)%24)+':'+str(tm[4])+':'+str(tm[5])

#センサのI2Cアドレス取得
def SencerAd():
    ad = 0
    if i2c.scan():
        ad = i2c.scan()
        return ad
# センサのI2Cアドレスを取得できなかった場合
    else:
        ad = '[No sensor.]'
        return ad

#使用している言語のバージョンを取得
impl=sys.implementation
#使用している言語とバージョンを配列に格納
lang_version = str(impl[0])+","+str(impl[1])

#マイクロコントローラ名を取得
plat=sys.platform

#ログをまとめる
log = datetime_jst+"--"+str(SencerAd())+"--"+lang_version+"--"+plat
#ログを出力
print(log)

ログの出力結果

ログを"--"でパース出来るようにした.
センサのI2Cアドレスが取得できた場合に出力されるログである.
[]の中にセンサのI2Cアドレス値が表示される.

2022/7/13 17:48:46--[72]--micropython,(1, 17, 0)--esp32

センサのアドレスが取得できなかった場合に出力されるログである.
[]の中に”No sensor”と表示される.

2022/7/13 17:50:4--[No sensor.]--micropython,(1, 17, 0)--esp32

[]の中に表示されている内容でセンサがESP32に接続されているかが判断できるため、センサデータが取得できなかった際に原因の判別が出来るようになる.

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?