Raspberry Pi+Python으로 전자동 체중기록기 만들기
3328 단어 RaspberryPi
#!/usr/bin/python
# coding: UTF-8
import sys
import time
import RPi.GPIO as GPIO
INPUT_PIN = 2
BIT_LEN = 39
class SaluteConnector:
def __init__(self):
GPIO.setmode(GPIO.BCM)
GPIO.setup(INPUT_PIN, GPIO.IN)
return
def getStatus(self):
bits,count = self.readBits()
status = 0
weight = 0
if count == BIT_LEN:
status = (bits >> 26) & 0b11
weight = (bits >> 8) & 0b1111111111111111
# status 0:さいしょ / 1:とちゅう / 3:決定
return status,weight
def readBits(self):
count = 0
bits = 0
while True:
pulse = self.pulseIn(INPUT_PIN, GPIO.HIGH, 0.07)
if pulse == 0:
break
# 0: 1000ms / 1: 500ms
bit = 0 if 0.00075 < pulse else 1
pos = 0 if BIT_LEN < count else (BIT_LEN - count)
bits = bits | (bit << pos)
count+=1
return bits,count
def pulseIn(self, PIN, value=GPIO.HIGH, timeout=1000000):
if value==GPIO.HIGH:
inv_value = GPIO.LOW
elif value==GPIO.LOW:
inv_value = GPIO.HIGH
t_start = time.time()
t_end1 = t_start
t_end2 = t_start
while GPIO.input(PIN) == inv_value:
t_end1 = time.time()
if timeout < time.time() - t_start:
return 0
while GPIO.input(PIN) == value:
t_end2 = time.time()
if timeout < time.time() - t_start:
return 0
return t_end2 - t_end1
def setup():
global _salute;
_salute = SaluteConnector()
return
def loop():
global _salute;
status,weight = _salute.getStatus()
print("status={0}, weight={1}".format(status, weight))
return
if __name__ == "__main__":
try:
setup()
while True:
loop()
except KeyboardInterrupt:
print("Keyboard Intterupt")
except Exception as e:
print("Exception Error", e.message)
finally:
print("End")
결과는 이렇다.$ python --version
Python 2.7.9
$ python test06.py
status=0, weight=0
status=0, weight=0
status=0, weight=0
status=0, weight=51
status=0, weight=123
status=0, weight=199
status=0, weight=244
status=0, weight=261
status=0, weight=261
status=0, weight=247
status=1, weight=211
status=1, weight=211
status=3, weight=211
status=3, weight=208
status=3, weight=208
status=3, weight=208
status=3, weight=208
status=3, weight=208
배선의 쪽지.Salute라는 체중계.
raspi3의 gpiopin은 로 설정되어 있으며, GPIO2에서vout, 5V에서 vcc를 연결합니다.
Reference
이 문제에 관하여(Raspberry Pi+Python으로 전자동 체중기록기 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/kidapu/items/4ca6528d69130ecd34b4텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)