Raspberry Pi+Python으로 전자동 체중기록기 만들기

3328 단어 RaspberryPi
Arduino로 전자동 체중기록기 만들기 보도에서 Raspi+Python으로 적외선을 수신하고 데이터를 투시하는 부분을 시도했다.그래서 노트를 위해 소스를 붙인다.
#!/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를 연결합니다.

좋은 웹페이지 즐겨찾기