ChinoDuino(Chino × Arduino)

自発性のないプログラマーがArduinoと出会って悪戦苦闘する様を綴っていく⇒今はいろいろ(^^;

Google Cloud IoT Coreの環境構築ではまったところ(デバイス側)

この記事は、Google Cloud IoT Coreを試そうと思い、Raspberry Pi 1 Model Bに環境構築したときに、はまったことの備忘録

【目次】

試したこと

こちらの記事を参考に、サンプルソースを使用してMQTTでPublishする!

qiita.com

サンプルソースはこちら

github.com

環境

$ cat /proc/cpuinfo
processor   : 0
model name  : ARMv6-compatible processor rev 7 (v6l)
BogoMIPS    : 697.95
Features    : half thumb fastmult vfp edsp java tls
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part    : 0xb76
CPU revision    : 7

Hardware    : BCM2835
Revision    : 000e
Serial      : 00000000ad5e97c1
  • OS:Raspbian Stretch Lite
$ cat /etc/debian_version
9.6
$ uname -a
Linux modelb1 4.14.79+ #1159 Sun Nov 4 17:28:08 GMT 2018 armv6l GNU/Linux
  • Python3.5

はまったこと

grpcioライブラリのインストールが終わらない・・・

pip install 後、grpcioライブラリのインストールが完了しない

事象

以下のmqtt_example内にある「requirements.txt」をダウンロード

github.com

Pythonの仮想環境を作成して、pip installしたところ発生

# 仮想環境作成
$ python3 -m venv venv
# activate
$ source venv/bin/activate
(venv) $ pip install -r requirements.txt
・・・
・・・
Building wheels for collected packages: grpcio
  Running setup.py bdist_wheel for grpcio ... \
解決

ネ申issuesがあり解決

github.com

ソースからビルドする必要があり、非常に時間がかかるとのこと。このため、プラットフォームごと、バイナリファイルを以下のURLにアップロードしてくれてあった!

バイナリファイル

ダウンロードして解決!本当に助かりました。あざます(>_<)

# whlファイルをダウンロード
$ wget https://www.piwheels.hostedpi.com/simple/grpcio/grpcio-1.16.1-cp35-cp35m-linux_armv6l.whl#sha256=fdf4f70b0328309bc0d5c8fd5e4485ef7344ced0a1def8ffa0638e58dcb269f3
# 事前にインストール→解決!
$ pip install grpcio-1.16.1-cp35-cp35m-linux_armv6l.whl
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Processing ./grpcio-1.16.1-cp35-cp35m-linux_armv6l.whl
Collecting six>=1.5.2 (from grpcio==1.16.1)
  Using cached https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
Installing collected packages: six, grpcio
Successfully installed grpcio-1.16.1 six-1.12.0

TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'

上記のgrpcioライブラリのインストール成功後に、pip installしたところエラーが発生・・・

事象
(venv) $ pip install -r requirements.txt
・・・
・・・
Exception:
Traceback (most recent call last):
  File "/home/pi/google_cloud_iot_core/venv/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 594, in urlopen
    chunked=chunked)
  File "/home/pi/google_cloud_iot_core/venv/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 391, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/home/pi/google_cloud_iot_core/venv/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 387, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.5/http/client.py", line 1198, in getresponse
    response.begin()
  File "/usr/lib/python3.5/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.5/http/client.py", line 266, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/google_cloud_iot_core/venv/lib/python3.5/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/home/pi/google_cloud_iot_core/venv/lib/python3.5/site-packages/pip/commands/install.py", line 353, in run
    wb.build(autobuilding=True)
  File "/home/pi/google_cloud_iot_core/venv/lib/python3.5/site-packages/pip/wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "/home/pi/google_cloud_iot_core/venv/lib/python3.5/site-packages/pip/req/req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/home/pi/google_cloud_iot_core/venv/lib/python3.5/site-packages/pip/req/req_set.py", line 620, in _prepare_file
    session=self.session, hashes=hashes)
  File "/home/pi/google_cloud_iot_core/venv/lib/python3.5/site-packages/pip/download.py", line 821, in unpack_url
    hashes=hashes
  File "/home/pi/google_cloud_iot_core/venv/lib/python3.5/site-packages/pip/download.py", line 659, in unpack_http_url
    hashes)
  File "/home/pi/google_cloud_iot_core/venv/lib/python3.5/site-packages/pip/download.py", line 853, in _download_http_url
    stream=True,
  File "/home/pi/google_cloud_iot_core/venv/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 501, in get
    return self.request('GET', url, **kwargs)
  File "/home/pi/google_cloud_iot_core/venv/lib/python3.5/site-packages/pip/download.py", line 386, in request
    return super(PipSession, self).request(method, url, *args, **kwargs)
  File "/home/pi/google_cloud_iot_core/venv/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/pi/google_cloud_iot_core/venv/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/home/pi/google_cloud_iot_core/venv/share/python-wheels/CacheControl-0.11.7-py2.py3-none-any.whl/cachecontrol/adapter.py", line 47, in send
    resp = super(CacheControlAdapter, self).send(request, **kw)
  File "/home/pi/google_cloud_iot_core/venv/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/adapters.py", line 423, in send
    timeout=timeout
  File "/home/pi/google_cloud_iot_core/venv/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 643, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/pi/google_cloud_iot_core/venv/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/util/retry.py", line 315, in increment
    total -= 1
TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'
解決

こちらを参考に、パッケージの更新、pipの再インストール、virtualenvで仮想環境を作成するようにしたところ解決

Python 開発環境のセットアップ  |  Python  |  Google Cloud

パッケージの更新等
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt install python python-dev python3 python3-dev
pipの再インストール

ディストリビューションpython-pipをインストール済みだったが、「pip を自分でインストールして、最新バージョンを入手することをおすすめします。」と記載があり実施

# ディストリビューションのアンインストール
$ sudo apt-get remove python-pip python3-pip
# pipをインストール
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py
$ sudo python3 get-pip.py
virtualenvで仮想環境を作成
# virtualenvをインストール
$ sudo pip install --upgrade virtualenv
# 仮想環境作成(python3で作成)
$ virtualenv -p python3 env
# activate
$ source env/bin/activate
# 事前にgrpcioライブラリをインストール(仮想環境を作成しなおしたので再インストール)
$ pip install grpcio-1.16.1-cp35-cp35m-linux_armv6l.whl
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Processing ./grpcio-1.16.1-cp35-cp35m-linux_armv6l.whl
Collecting six>=1.5.2 (from grpcio==1.16.1)
  Using cached https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
Installing collected packages: six, grpcio
Successfully installed grpcio-1.16.1 six-1.12.0
# ライブラリのインストール
(env) $ pip install -r requirements.txt

Publish成功!

無事インストール完了し、以下を実行したところ、publishに成功!

(env) $ python cloudiot_mqtt_example.py --registry_id=CloudIoTCoreTest --project_id=secret-footing-227506 --device_id=modelb1 --algorithm=RS256 --private_key_file=./rsa_private.pem --cloud_region=asia-east1
Creating JWT using RS256 from private key file ./rsa_private.pem
Subscribing to /devices/modelb1/commands/#
Publishing message 1/100: 'CloudIoTCoreTest/modelb1-payload-1'
on_connect Connection Accepted.
Publishing message 2/100: 'CloudIoTCoreTest/modelb1-payload-2'
Publishing message 3/100: 'CloudIoTCoreTest/modelb1-payload-3'
Received message 'b''' on topic '/devices/modelb1/config' with Qos 1
Received message 'b''' on topic '/devices/modelb1/config' with Qos 1
on_publish
Publishing message 4/100: 'CloudIoTCoreTest/modelb1-payload-4'
on_publish
on_publish
on_publish
Publishing message 5/100: 'CloudIoTCoreTest/modelb1-payload-5'
on_publish
Publishing message 6/100: 'CloudIoTCoreTest/modelb1-payload-6'
・・・・

参考

他、参考にしたページなど

github.com