4
6

More than 3 years have passed since last update.

Raspberry PiでGrove Pi+のセンサ値を取得しkintoneに保管する

Last updated at Posted at 2020-01-05

概要

ハッカソンでRaspberry PiとGrove Pi+スターターキット、kintoneを活用することになり、Grove Pi+スターターキットのセンサ値をkintoneにアップするサンプルプログラムを作成しました。

Raspberry PiとGrove Pi+の準備

Raspberry Piの環境は以前設定した以下を用いました。
Raspberry PiでGrove Pi+スターターキットとカメラを使う初期設定
https://qiita.com/yukataoka/items/9df2c74f7cd514e04b97

今回使用したGrove Pi+スターターキットのセンサは、温湿度、光、音です。
sensor04A.png
こちらも以前試験した以下を参考に構成しました。
Grove Pi+スターターキットのセンサを試す 5.Example Project: Sensor Twitter Feed(センサ値のツイート)
https://qiita.com/yukataoka/items/ef73eb1a329f18015aab#5example-project-sensor-twitter-feed%E3%82%BB%E3%83%B3%E3%82%B5%E5%80%A4%E3%81%AE%E3%83%84%E3%82%A4%E3%83%BC%E3%83%88

kintone アプリの追加

kintoneのアプリをはじめから作成し、フォームを作成

フィールドは以下のように設定します。

フィールド名 タイプ フィードコート・要素ID
日時 日時 datetime
光センサ 数値(少数2桁) light
音センサ 数値(少数2桁) sound
温度センサ 数値(少数2桁) temp
湿度センサ 数値(少数2桁) humidity

kin-py-01.png

アプリの設定画面でAPIトークンを設定

アプリの設定画面から、設定 -> カスタマイズ/サービス連携 APIトークン の順でAPIトークンの設定画面が開きます。
「生成する」ボタンを押すとAPIトークンが追加されるので、アクセス権を"追加"のみチェックして「保存」します。

プログラム開発

kintone API SDK for Pythonを利用して、Grove Pi+のセンサ計測値をkintoneレコードに追加する実装を行いました。

Grove Pi+センサ計測値の取得については以下を参照ください。
Grove Pi+スターターキットのセンサを試す 5.Example Project: Sensor Twitter Feed(センサ値のツイート)
https://qiita.com/yukataoka/items/ef73eb1a329f18015aab#5example-project-sensor-twitter-feed%E3%82%BB%E3%83%B3%E3%82%B5%E5%80%A4%E3%81%AE%E3%83%84%E3%82%A4%E3%83%BC%E3%83%88

kintone API SDK for Pythonについては以下を参照ください。
Raspberry Piでkintone API SDK for Pythonを利用する(Raspberry Pi から簡単にkintoneにデータを保管)
https://qiita.com/yukataoka/items/9025e1b9951feb419fac

wifi_kintone.py
#!/usr/bin/python
# -*- coding: utf-8 -*-

import codecs
import math
import sys
import time

# Grove Pi+
import grovepi

# kintone API SDK for Python
import pykintone
from pykintone import model
import pykintone.structure_field as sf

#  Grove Pi+ Connections
sound_sensor = 0        # port A0
light_sensor = 1        # port A1
temperature_sensor = 2  # port D2

# Connect to kintone
subdomein = "kintoneのサブドメイン"
appId     = "kintoneのアプリケーションID"
token     = "kintoneのAPIトークン"
app       = pykintone.app(subdomein, appId, token)

# kintone record layout
class recordLayout(model.kintoneModel):
    def __init__(self):
        super(recordLayout, self).__init__()
        self.datetime = ""
        self.light    = 0.0
        self.sound    = 0.0
        self.temp     = 0.0
        self.humidity = 0.0

# example code
if __name__=="__main__":

    grovepi.pinMode(led,"OUTPUT")
    grovepi.analogWrite(led,255)  #turn led to max to show readiness

    while True:

        try:
            # Get value from light sensor
            light_intensity = grovepi.analogRead(light_sensor)

            # Get sound level
            sound_level = grovepi.analogRead(sound_sensor)

            time.sleep(0.5)

            # Get value from temperature sensor
            [t,h]=[0,0]
            [t,h] = grovepi.dht(temperature_sensor,0)

            print ("Temp: %d C, Humidity: %d, Light: %d, Sound: %d" %(t,h,light_intensity/10,sound_level))

            # kintone POST
            record = recordLayout()
            record.datetime = time.strftime('%Y-%m-%dT%H:%M:%S+09:00')
            record.light    = light_intensity/10
            record.sound    = sound_level
            record.temp     = t
            record.humidity = h
            res = app.create(record)
            if res.ok:
                print("kintone record add id=" + str(res.record_id) + " revision=" + str(res.revision))
            else:
                print(res.error.id.encode('utf-8'))
                print(res.error.message.encode('utf-8'))
                print(res.error.code)

            time.sleep(60 * 10)

        except KeyboardInterrupt:
            exit()

        except (IOError,TypeError) as e:
            print("Error")
            print(e)
            exit()

結果

以下のようにRaspberry Piで計測したセンサ値がkintoneに保管できました。
kin-py-02.png

以下のようなグラフをkintoneで表示してみました。
kin-py-03.png
kintoneにデータを保管できると便利ですね!

参考

Raspberry PiでGrove Pi+スターターキットとカメラを使う初期設定
https://qiita.com/yukataoka/items/9df2c74f7cd514e04b97
Grove Pi+スターターキットのセンサを試す
https://qiita.com/yukataoka/items/ef73eb1a329f18015aab
Raspberry Piでkintone API SDK for Pythonを利用する(Raspberry Pi から簡単にkintoneにデータを保管)
https://qiita.com/yukataoka/items/9025e1b9951feb419fac

4
6
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
4
6