Skip to content

Instantly share code, notes, and snippets.

@yuma-m
Last active February 28, 2016 14:59
Show Gist options
  • Save yuma-m/aac11bb9f03c517d3d01 to your computer and use it in GitHub Desktop.
Save yuma-m/aac11bb9f03c517d3d01 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import RPi.GPIO as GPIO
import time
import milkcocoa.milkcocoa as milkcocoa
# PIRセンサを接続したGPIOピン番号
SENSOR_PIN = 13
APP_ID = 'アプリID'
API_KEY = 'APIキー'
SECRET = 'APIシークレット'
DB_NAME = 'データストア名'
def add_count_record(count):
try:
mc_client = milkcocoa.Milkcocoa.connectWithApiKey(APP_ID, API_KEY, SECRET, useSSL=False)
datastore = mc_client.datastore(DB_NAME)
datastore.push({"count": count})
except Exception as e:
print(e)
def main():
GPIO.setmode(GPIO.BCM)
GPIO.setup(SENSOR_PIN, GPIO.IN)
counter = 0
pir_counter = 0
while True:
pin_status = GPIO.input(SENSOR_PIN)
counter += 1
pir_counter += pin_status
# 30秒ごとに検知回数をMilkcocoaに送信
if counter == 30:
add_count_record(pir_counter)
counter = 0
pir_counter = 0
time.sleep(1.0)
GPIO.cleanup()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment