LoginSignup
2
2

More than 3 years have passed since last update.

リクガメのおウチの温度/湿度をモニターしてawsに投げるのを1000円くらいでやる on arduino

Last updated at Posted at 2018-12-30

ちょっと前にmicroPythonで温度/湿度センサーのデータをaws iotに投げるのをやりましたが、どうもあんまし安定しない。sleepさせて起きて投げてってループするんだけど、気づいたら普通に起動しててスクリプトが走ってないとかあってなんでかなと。
で、まあ一回arduinoでも試してみようかなと。esp32じゃなくてesp8266の方で。

ちなみにmicroPython版は
リクガメのおウチの温度/湿度をモニターしてawsに投げるのを1000円くらいでやる

アイテム

こんな感じのやつ。合わせて1000円くらいか。

テンプレ

このあたりを元にさせていただいてすすめます。

arduino ideに以下ライブラリ追加

  • Adafruit ESP8266 結局使わず
  • Adafruit MQTT Library 結局使わず
  • Adafruit Unified sensor
  • DHT sensor library

33.png

焼いてみる

とりあえずサンプルコードをビルドして焼いてみる

https://github.com/esp8266/Arduino
↑ここのInstalling with Boards Managerのとこをやります。

そしてESPDuino(ESP-12 Module)を選択。
ひとまずその他はデフォルトでマイコンボードに書き込むをしてみます。
そして・・・

34.png

お、良さげ。ちなみにこの段階のソースはこちら。

/**
 * Example for reading temperature and humidity
 * using the DHT22 and ESP8266
 *
 * Copyright (c) 2016 Losant IoT. All rights reserved.
 * https://www.losant.com
 */

#include "DHT.h"

#define DHTPIN 4     // what digital pin the DHT22 is conected to
#define DHTTYPE DHT22   // there are multiple kinds of DHT sensors

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  Serial.setTimeout(2000);

  // Wait for serial to initialize.
  while(!Serial) { }

  Serial.println("Device Started");
  Serial.println("-------------------------------------");
  Serial.println("Running DHT!");
  Serial.println("-------------------------------------");

}

int timeSinceLastRead = 0;
void loop() {

  // Report every 2 seconds.
  if(timeSinceLastRead > 2000) {
    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
    float f = dht.readTemperature(true);

    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t) || isnan(f)) {
      Serial.println("Failed to read from DHT sensor!");
      timeSinceLastRead = 0;
      return;
    }

    // Compute heat index in Fahrenheit (the default)
    float hif = dht.computeHeatIndex(f, h);
    // Compute heat index in Celsius (isFahreheit = false)
    float hic = dht.computeHeatIndex(t, h, false);

    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print(" *C ");
    Serial.print(f);
    Serial.print(" *F\t");
    Serial.print("Heat index: ");
    Serial.print(hic);
    Serial.print(" *C ");
    Serial.print(hif);
    Serial.println(" *F");

    timeSinceLastRead = 0;
  }
  delay(100);
  timeSinceLastRead += 100;
}

wifi繋ぐ

https://www.losant.com/blog/getting-started-with-the-esp8266-and-dht22-sensor
調子に乗ってCODE PT. 2に進んでみますね。
でもLosant.hのくだりはambientにかえましょう。

https://ambidata.io/refs/arduino/

↑最終的にソースに残ってないし。

mqtt

mqttもいっちゃおう。
ちょっと試行錯誤したんだけど、これがわかりやすいか。

とまあ、こちら をベースにいろいろうまくいかず、こちらにたどり着く。esp8266はtls1.1にしか対応してないんだけど、awsはtls1.2じゃないと接続できないっぽい。どうも証明書での認証のハードルが高い。。

とりあえず一回iamでの接続でやってみます。

そんなわけで送信できました。
MQTT over WebSocket プロトコルというのを使ってるみたい。

35.png
36.png

aws-mqtt-websocketのwebsocket-pahoの方のサンプルスケッチをテンプレにしてます。
ソースはこちら。

証明書をSPIFFSに置いてみてloadCerificateしたりとかいろいろしてみたのですが、SSL周りがややこしい。。
まあ、ぜんぜんiam認証でもいいんですけどね。

ちょっと動かしてみてるけどmicroPythonより安定してそうだ。スクリプト言語だし仕方ないけど。
サブスクライブもいけるのでサーボつなげてみようかな。
しかしmicroRubyもつかってみたい。今度なにか簡単なやつで使ってみよう。

ちなみにですが、ESPDuinoの古い方のやつで、書き込みの時はD0をGNDに落とす、deepsleep使う時はD16をRSTつなげてます。
あとDHT22センサーはD4に測定用のピンを繋げてる。VCCは3.3Vの方。

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