ESP8266 と DHT11 で温湿度測定
先月買ったキットに温湿度センサー DHT11 が入っていたので、ESP8266 に温湿度センサー DHT11 をつないで、温度と湿度を測定したい。
今回は以下のサイトを参考にした。
- Interface DHT11 DHT22 w/ ESP8266 NodeMCU Using Web Server
- ESP8266とDHT11とAmbientを使って部屋の温度と湿度をグラフ化 | elchika
回路図は前者のサイトに従った。
まず、DHT11 を扱うためのライブラリ DHT sensor library をインストールしておく。依存ライブラリは、Arduino IDE からインストールするらしい。
(PratformIOを使うと、依存関係をコード管理できるっぽい?)
DHT11 のデータピンの接続先を、最初は D8 ピンにしていたが、以下のエラーが出てスケッチが書き込めなかった。
Uploading... esptool.py v2.8 Serial port /dev/tty.usbserial-0001 Connecting........_____....._____....._____....._____.....Traceback (most recent call last): File "/Users/***/Library/Arduino15/packages/esp8266/hardware/esp8266/2.7.4/tools/upload.py", line 65, in <module> esptool.main(cmdline) File "/Users/***/Library/Arduino15/packages/esp8266/hardware/esp8266/2.7.4/tools/esptool/esptool.py", line 2890, in main esp.connect(args.before) File "/Users/***/Library/Arduino15/packages/esp8266/hardware/esp8266/2.7.4/tools/esptool/esptool.py", line 483, in connect raise FatalError('Failed to connect to %s: %s' % (self.CHIP_NAME, last_error)) esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header An error occurred while uploading the sketch [Error] Exit with code=1
同じような issue を参考にして D6 ピンに変更したところ、解決した。
今回試したソースは以下の通り。温度と湿度を出力するだけの、ごく簡単なプログラム。
#include "DHT.h" #define DHTPIN D6 // D6 ピンからセンサーの値を得る #define DHTTYPE DHT11 // DHT 11 を使う DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(115200); dht.begin(); } void loop() { float h = dht.readHumidity(); float t = dht.readTemperature(); Serial.println("Temperature: " + String(t, 1) + "°C\tHumidity: " + String(h, 0) + "%"); delay(2000); }
出力はこんな感じ。途中で息を吹きかけたので、温度・湿度ともに上昇しているのが分かる。
Temperature: 25.7°C Humidity: 46% Temperature: 25.7°C Humidity: 46% Temperature: 25.7°C Humidity: 46% Temperature: 25.7°C Humidity: 46% Temperature: 25.7°C Humidity: 46% Temperature: 25.7°C Humidity: 57% Temperature: 25.8°C Humidity: 60% Temperature: 25.7°C Humidity: 57% Temperature: 25.7°C Humidity: 54% Temperature: 26.1°C Humidity: 95% Temperature: 26.7°C Humidity: 95% Temperature: 27.1°C Humidity: 95% Temperature: 27.3°C Humidity: 95% Temperature: 27.6°C Humidity: 95% Temperature: 28.1°C Humidity: 95%
温度の分解能が 1℃ と書かれているサイトもある1が、0.1℃ 単位で値が取得できていた。湿度は 1% 単位。
次は Wifi 経由でデータを送信したい。
ESP8266 の開発を VS Code 上から行う
ESP8266 とか ESP32 とかいうのを使うと、Wifi につながる温湿度センサーなどが安く簡単に作れるらしい。しかも Arduino 互換とのこと。電子工作初心者なのでよくわかっていないが、手持ちのパーツも何もないので、とりあえず「OSOYOO ESP8266ではじめるNodeMCU IoT MQTTプログラミング学習キット」というセットを買ってみた。
OSOYOO(オソヨー) ESP8266ではじめるNodeMCU IoT MQTT プログラミング 学習キット 実験キット 初心者演習用電子パーツセット (NodeMCU IoT)
- メディア: Personal Computers
Amazon の画像では Ai-Thinker 製のチップ(技適あり)になっているが、届いたのは技適なしのチップだったので、Wifi を使う場合は注意。
今回は、開発環境の構築から。Arduino IDE をそのまま使ってもよいが、VS Code からでも開発できるらしいので、そうしてみる。
- Arduino IDE をダウンロードしてインストール。現在のバージョンは 1.8.13
- Arduino IDE に ESP8266 core for Arduino をインストールする。以下の手順はドキュメントのとおり。
- Arduino IDE を閉じる
- VS Code に MS 製 Arduino 拡張機能をインストール
- 画面右下の <Select board type> をクリックして、
NodeMCU 1.0 (ESP-12E Module) (esp8266)
を選択- このあたりのボードの設定は
.vscode/arduino.json
に保存される様子
- このあたりのボードの設定は
- USB ケーブルで ESP8266 ボードを接続する
- 「ESP8266 が検出されました。ボード設定を変更しますか?」的なトーストが VS Code に出るが、無視する
- [Yes] を選択した場合、
Adafruit Feather HUZZAH ESP8266 (esp8266)
に変更されたが、これだとうまく動かない
- [Yes] を選択した場合、
そして HelloWorld/HelloWorld.ino
に、適当なプログラムをコピペした。
void setup() { Serial.begin(115200); } void loop() { Serial.println("Hello World!"); delay(5000); }
コマンドパレット or エディタ右上のアイコンから、Arduino: Upload
を実行して ESP8266 にアップロード。結構時間がかかる。完了後、Arduino: Open serial monitor
コマンドで Serial monitor を開いたら、Hello World!
という出力が確認できた。
次は LED でも。
参考
Haskell に入門するために開発環境を整える
『すごい H 本』こと『すごい Haskell たのしく学ぼう!』をざざっと読んだので、実際に手を動かしてみたい。ということで、開発環境を整える。
Stack
Stack というツールで GHC (コンパイラ)のインストールやコードのビルドができるらしい。
- Home - The Haskell Tool Stack
- 本気で Haskell したい人向けの Stack チュートリアル - Qiita
- Stackでやる最速Haskell Hello world! (GHCのインストール付き!) - Qiita
まず Homebrew でインストール。
$ brew install stack
新規プロジェクト hello-world
を作る。
$ stack new hello-world Downloading template "new-template" to create project "hello-world" in hello-world/ ... The following parameters were needed by the template but not provided: author-name You can provide them in /Users/mimikakimemo/.stack/config.yaml, like this: templates: params: author-name: value Or you can pass each one as parameters like this: stack new hello-world new-template -p "author-name:value" The following parameters were needed by the template but not provided: author-email, author-name, category, copyright, github-username You can provide them in /Users/mimikakimemo/.stack/config.yaml, like this: templates: params: author-email: value author-name: value category: value copyright: value github-username: value Or you can pass each one as parameters like this: stack new hello-world new-template -p "author-email:value" -p "author-name:value" -p "category:value" -p "copyright:value" -p "github-username:value" Looking for .cabal or package.yaml files to use to init the project. Using cabal packages: - hello-world/ Selecting the best among 15 snapshots... Selected mirror https://s3.amazonaws.com/hackage.fpcomplete.com/ Downloading timestamp Downloading snapshot Downloading mirrors Cannot update index (no local copy) Downloading index Updated package index downloaded Update complete Populated index cache. * Matches lts-13.5 Selected resolver: lts-13.5 Initialising configuration using resolver: lts-13.5 Total number of user packages considered: 1 Writing configuration to file: hello-world/stack.yaml All done.
途中の Downloading index
でやたらと時間がかかってフリーズしたかと思ったが、10分ほどで完了した。
$ cd hello-world $ tree -a . ├── .gitignore ├── ChangeLog.md ├── LICENSE ├── README.md ├── Setup.hs ├── app/ │ └── Main.hs ├── hello-world.cabal ├── package.yaml ├── src/ │ └── Lib.hs ├── stack.yaml └── test/ └── Spec.hs
stack build
で GHC のインストールもしてくれる。
$ stack build Preparing to install GHC to an isolated location. This will not interfere with any system-level installation. Downloaded ghc-8.6.3. Installed GHC. [1 of 2] Compiling Main ( /Users/mimikakimemo/.stack/setup-exe-src/setup-mPHDZzAJ.hs, /Users/mimikakimemo/.stack/setup-exe-src/setup-mPHDZzAJ.o ) [2 of 2] Compiling StackSetupShim ( /Users/mimikakimemo/.stack/setup-exe-src/setup-shim-mPHDZzAJ.hs, /Users/mimikakimemo/.stack/setup-exe-src/setup-shim-mPHDZzAJ.o ) Linking /Users/mimikakimemo/.stack/setup-exe-cache/x86_64-osx/tmp-Cabal-simple_mPHDZzAJ_2.4.0.1_ghc-8.6.3 ... Building all executables for `hello-world' once. After a successful build of all of them, only specified executables will be rebuilt. hello-world-0.1.0.0: configure (lib + exe) Configuring hello-world-0.1.0.0... hello-world-0.1.0.0: build (lib + exe) Preprocessing library for hello-world-0.1.0.0.. Building library for hello-world-0.1.0.0.. [1 of 2] Compiling Lib ( src/Lib.hs, .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/Lib.o ) [2 of 2] Compiling Paths_hello_world ( .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/autogen/Paths_hello_world.hs, .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/Paths_hello_world.o ) Preprocessing executable 'hello-world-exe' for hello-world-0.1.0.0.. Building executable 'hello-world-exe' for hello-world-0.1.0.0.. [1 of 2] Compiling Main ( app/Main.hs, .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/hello-world-exe/hello-world-exe-tmp/Main.o ) [2 of 2] Compiling Paths_hello_world ( .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/hello-world-exe/autogen/Paths_hello_world.hs, .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/hello-world-exe/hello-world-exe-tmp/Paths_hello_world.o ) Linking .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/hello-world-exe/hello-world-exe ... hello-world-0.1.0.0: copy/register Installing library in /Users/mimikakimemo/repos/hello-world/.stack-work/install/x86_64-osx/lts-13.5/8.6.3/lib/x86_64-osx-ghc-8.6.3/hello-world-0.1.0.0-NpaWwN61fJ91LUORGYsB Installing executable hello-world-exe in /Users/mimikakimemo/repos/hello-world/.stack-work/install/x86_64-osx/lts-13.5/8.6.3/bin Registering library for hello-world-0.1.0.0..
stack exec <プロジェクト名>-exe
で実行できるらしい。
$ stack exec hello-world-exe someFunc
用意されている app/Main.hs
の内容。
module Main where import Lib main :: IO () main = someFunc
インポートされている src/Lib.hs
はこうなっている。
module Lib ( someFunc ) where someFunc :: IO () someFunc = putStrLn "someFunc"
よって、最終行を someFunc = putStrLn "Hello, world!"
に書き換えてビルド・実行すれば、
$ stack build hello-world-0.1.0.0: unregistering (local file changes: src/Lib.hs) hello-world-0.1.0.0: build (lib + exe) Preprocessing library for hello-world-0.1.0.0.. Building library for hello-world-0.1.0.0.. [2 of 2] Compiling Lib ( src/Lib.hs, .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/Lib.o ) Preprocessing executable 'hello-world-exe' for hello-world-0.1.0.0.. Building executable 'hello-world-exe' for hello-world-0.1.0.0.. [1 of 2] Compiling Main ( app/Main.hs, .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/hello-world-exe/hello-world-exe-tmp/Main.o ) [Lib changed] Linking .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/hello-world-exe/hello-world-exe ... hello-world-0.1.0.0: copy/register Installing library in /Users/mimikakimemo/repos/hello-world/.stack-work/install/x86_64-osx/lts-13.5/8.6.3/lib/x86_64-osx-ghc-8.6.3/hello-world-0.1.0.0-NpaWwN61fJ91LUORGYsB Installing executable hello-world-exe in /Users/mimikakimemo/repos/hello-world/.stack-work/install/x86_64-osx/lts-13.5/8.6.3/bin Registering library for hello-world-0.1.0.0.. $ stack exec hello-world-exe Hello, world!
Hello, world!
と表示される。
その他
- 作者: Miran Lipovača,田中英行,村主崇行
- 出版社/メーカー: オーム社
- 発売日: 2012/05/23
- メディア: 単行本(ソフトカバー)
- 購入: 25人 クリック: 580回
- この商品を含むブログ (73件) を見る
XREA 上の既存サイトに対して無料 SSL の設定をする
XREA で無料 SSL が使えるようになっていたので、独自ドメインで運用しているサイトに対して、今さらながらこれを有効にした。
無料SSLの新規設定 | サイト設定 | マニュアル | 無料から使える高機能・高品質レンタルサーバー | XREA(エクスリア)
これまでは、"Main" サイトに独自ドメインを割り当てて運用していた。この "Main" のサイト設定を開き、上のマニュアルを参考に設定しようとしたが、「無料 SSL」の項目がグレーアウトしていて選択できない。
そこで、以下のように設定すると、うまくいくことがわかった。
- "Main" のドメインを、独自ドメインからデフォルトの
<アカウント名>.sXXX.xrea.com
に変更 - [サイト設定の新規作成] ボタンをクリックし、以下の設定で作成
- ドキュメントルートが変更されるので、
putblic_html
の内容をpublic_html/<独自ドメイン>
へと移動
この設定方法に関しては、 XREA 掲示板の以下の書き込みを参考にした。
ちなみにこの無料 SSL は、Let's Encrypt を利用しているらしい。
11ac 対応で GbE なルータ FFP-1200DHP を購入した
これまでは、2年半ぐらい前に買った Buffalo のルータ WZR-HP-G302H を使っていた。
が、最近 WAN 側の接続が一日一回ぐらい切れるようになり、調子が悪かった。無線も 2.4GHz 帯のみだったり、11ac に対応していなかったりと、今どきの使い方だと少し不満も出てきたので、ここで買い替えることにした。
- 出版社/メーカー: プラネックス
- 発売日: 2013/11/05
- メディア: Personal Computers
- この商品を含むブログ (4件) を見る
買ったのは Planex の FFP-1200DHP という機種。Amazon のタイムセールで見つけて、あまり考えずに選んだ。4,980円。この FFP-1200DHP は、下の MZK-1200DHP の包装を簡易にした(だけの)ものらしく、500円ぐらい安くなっている。
PLANEX 11ac 無線LAN中継機 オールギガ 866+300Mbps MZK-1200DHP(SC) 見えルンです対応
- 出版社/メーカー: PLANEX
- メディア: エレクトロニクス
- この商品を含むブログを見る
写真だと本体の色がグレーやアイボリーあたりにも見えるが、実物を見てみると薄い水色といった感じ。少し変わった色だが、とくに不満はない。これまでの WZR-HP-G302H は黒の鏡面仕上げで、ホコリが気になっていた。大きさもこちらの方がコンパクトで良い。現在、以下の機器が LAN 内にある。
- 有線
- LIVA
- 鼻毛鯖
- 無線(11ac対応)
- 無線(11ac非対応)
- kobo glo
- プリンタ
デフォルトの設定画面は、かなりシンプルでわかりやすいデザインになっている。もちろん、詳細設定もできる。今回設定した部分は以下のとおり。
- SSID を hogehoge_nomap にする
- LAN 側の IP アドレスを 192.168.111.1 から 192.168.11.1 に変更(前の環境に合わせる)
- DHCP を無効にする(LIVA で動かしている dnsmasq を使う)
- 「ポートフォワード」で 5555/TCP, 500/UDP, 4500/UDP を LIVA に転送する(SoftEther VPN 用)
まだ使い始めだが、とりあえず良い感じ。