0
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 3 years have passed since last update.

Raspberry pi + SHT31, SHT32 で温湿度測定

Last updated at Posted at 2021-04-05

参考
秋月 Webページ
http://akizukidenshi.com/download/ds/sensirion/Sensirion_Humidity_Sensors_SHT3x_DIS_Datasheet_V3_J.pdf
Sensirion Webページ
https://www.sensirion.com/jp/download-center/
Qiita
https://qiita.com/nishiwakki/items/877b10517c26a8a4b583

尚、投稿段階ではまだSQL、Grafanaはインストールしたのみで、使っていない


Raspberry Pi OS Lite をダウンロード
https://www.raspberrypi.org/software/operating-systems/#raspberry-pi-os-32-bit
ダウンロードしたimgをmicroSDに書き込む

OS起動後 SSHを有効にする I2Cも有効にする
sudo raspi-config にてインターフェイス SSH 及び I2C を有効にする

sudo apt-get update sudo apt-get install python3-smbus
i2c-tools が入っていなかったら別途 apt-get install i2c-tools

MySQL インストール

sudo apt-get upgrade
sudo apt-get install apache2
apache2 -v                    (2021.3時点では  2.4が入る)
systemctl enable apache2

sudo apt-get install php     (2021.3時点では 7.3が入る)
php -v
nano  /var/www/html/phpinfo.php
   <?php   phpinfo();  ?>

sudo apt-get install mariadb-server
mariadb -V

mysql用rootパスワード設定

sudo mysql_secure_installation
 Enter current password for root (enter for none): enter
 Set root password? [Y/n] y
 Remove anonymous users? [Y/n] y
 Disallow root login remotely? [Y/n] n
 Remove test database and access to it? [Y/n] y
 Reload privilege tables now? [Y/n] y

mysql_unix_socketプラグイン無効化
sudo mysql -u root
 MariaDB [(none)]> use mysql;
 use mysql;
 select user,host,plugin from user;
 update user set plugin='' where user='root';
 select user,host,plugin from user;
 exit

sudo systemctl restart mysql
mysql -u root -p     #sudoをせずに入れるようになった

Grafanaは以下を参考
https://grafana.com/grafana/download?platform=arm
Raspberry-Pi 3 はArm v8 なので64で動くらしい、 Pi 2 はv7、 Pi 1 はv6用を入れるとよいかと

SHT32 及び SHT31 での計測サンプルpython3コード

import time
import smbus

def tempChanger(msb, lsb):
    mlsb = ((msb << 8) | lsb)                                             
    return (-45 + 175 * int(str(mlsb), 10) / (pow(2, 16) - 1))            

def humidChanger(msb, lsb):
    mlsb = ((msb << 8) | lsb)
    return (100 * int(str(mlsb), 10) / (pow(2, 16) - 1))

i2c = smbus.SMBus(1)
SHT32_addr = 0x45                                                         
SHT31_addr = 0x44                                        #ADDR pin をGNDにショートさせアドレス変更

while 1:
    i2c.write_byte_data(SHT32_addr, 0x2C, 0x06)
    data  = i2c.read_i2c_block_data(SHT32_addr, 0x00, 6)

    i2c.write_byte_data(SHT31_addr, 0x2C, 0x06)
    data2 = i2c.read_i2c_block_data(SHT31_addr, 0x00, 6)

    print( "SHT32: " + str('{:.4g}'.format(tempChanger(data[0], data[1])))   + "C " + str('{:.4g}'.format(humidChanger(data[3], data[4])))   + "% " \
         + "SHT31: " + str('{:.4g}'.format(tempChanger(data2[0], data2[1]))) + "C " + str('{:.4g}'.format(humidChanger(data2[3], data2[4]))) + "%")

    time.sleep(1)
0
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
0
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?