Python3をデフォルトにする
シンボリックリンクの削除
$sudo unlink python
python3へのシンボリックリンク作成
$sudo ln -s python3 python
Python プログラミング
GPIOのテスト
特に指定しなければWiringPi番号になる。HDC-1000温度センサーのテスト 接続はI2cと Rdy をwPi4
#!/usr/bin/env python3 import wiringpi import os import struct from time import sleep import csv import threading import time def read_sens(): i2c.writeReg16(dev,0x02,0x10) #Temp + Hidi 32-bit transfer mode, LSB-MSB inverted, why? i2c.writeReg8(dev,0x00,0x00) #start conversion. sleep((6350.0 + 6500.0 + 500.0)/1000000.0) #wait for conversion. #LSB-MSB inverted, again... temp = ((struct.unpack('4B', os.read(dev,4)))[0] << 8 | (struct.unpack('4B', os.read(dev,4)))[1]) hudi = ((struct.unpack('4B', os.read(dev,4)))[2] << 8 | (struct.unpack('4B', os.read(dev,4)))[3]) os.close(dev) #Don't leave the door open. hum=(( hudi / 65535.0 ) * 100) tmp=(( temp / 65535.0) * 165 - 40 ) hum=round(hum) tmp=round(tmp) print "Humidity: %.2f %%" % (hum) print "Temperature: %.2f C" % (tmp) list.append(hum) list.append(tmp) f = open('env_data.csv', 'a') writer = csv.writer(f, lineterminator='\n') writer.writerow(list) f.close() list=[] wiringpi.wiringPiSetup() i2c = wiringpi.I2C() dev = i2c.setup(0x40) while 1: t=threading.Timer(5,read_sens) t.start() time.sleep(5) |
---|