0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

センサーデータをラズパイに保存!!(準備編)

Last updated at Posted at 2020-11-05

センサーデータをラズパイに保存!!(準備編)

Iot のセンシングを行う際、データを保存しないといけない。
Json、txt、などいろいろな方法で保存する場合がある。
今回は、一番オーソドックスなデータベースに追加することにしましょう。

使用機器

  • raspberry Pi
  • PC
  • 何かしらのセンサー(今回は、土中水分)
  • ESP32
  • Wi-Fi ルーター

通信方式

MQTT を使用します。
これは、ブローカーに対して送信データに topic を設定することで、受信したい PC などが指定 topic に関するデータを受け取れるというものです。
また、Iot なので、省電力などの面からもこの通信方式がよく使われているように感じます。

準備

  • raspberry Pi に mySql をインストールします。
  • ssh 設定
  • IPAddress 固定
  • MQTT インストール
  • ESP32 の準備

MySql

sudo apt-get install mysql-server

これで、インストール中に設定させるので次は、ログインできるかを確認します。

mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.12-0ubuntu1 (Ubuntu)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

ssh 設定

設定に関しては、非常に簡単です。
GUI の場合は raspberry の設定を開いてインターフェースの部分の ssh を有効にするだけです。
設定=>インターフェース=> ssh 有効

そうすると TeraTerm とかで IP 指定して

pi  
自分で設定したパスワードを打ち込むとログインできます。

IPAddress の固定化

IPAddress は、ずっと動いている場合は変わらないと思います。
何あるかわからないので、IP アドレスを固定しときましょう。

まず、自分の環境の IP アドレスを知りましょう。

ipconfig /all

<!-- ==========抜粋=========== -->
 サブネット マスク . . . . . . . . . .: 255.255.255.0
  デフォルト ゲートウェイ . . . . . . .: fe80::30ff:fe0c:2043%4
                                         192.168.11.1
  DHCP サーバー . . . . . . . . . . . .: 192.168.11.1
  DHCPv6 IAID . . . . . . . . . . . . .: 36740131

サブネットマスクは、255.255.255.0 なので、24 ですね。
デフォルトゲートウェイが、192.168.11.1
DHCP サーバー も同様ですね
IP アドレスを 192.168.11.90 に固定済ます。

sudo nano /etc/dhcpcd.conf

<!-- 追記します。(Wi-Fi接続) -->
interface wlan0
static ip_address=192.168.11.90/24
static routers=192.168.11.1
static domain_name_servers=192.168.11.1

再起動して IP が固定化していれば完了です。

MQTT インストール

モスキートをインストール

<!-- ブローカーです。 -->
sudo apt install mosquitto

<!-- サーバ起動 -->
sudo systemctl start mosquitto

ラズパイで MQTT のトピック送信する場合

sudo apt install mosquitto-clients

test

<!-- サーバ側 -->
mosquitto_sub -d -t test

<!-- クライアント側 -->
mosquitto_pub -d -t test -m "こんにちは"

test という topic で送信したデータが表示できれば完了です。

ESP32

ボードのインストール
ライブラリのインストールを行ってください
使用するライブラリは、以下の通りです。

#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

ライブラリのインストールに関してですが自分の周りでも環境によってエラーが違う?場合があり、ここで詳細の説明はしません。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?