モチログ

はてなでのブログ

ESP-WROOM-02で温湿度センサー情報をAT&T M2Xに投稿するシステム

R0019191-1

前回でファームウェアを書き換えてATコマンドで動作を確認したのは良い物の、使い道を決められるほったらかしにしていたので、ここはえいやと適当にセンサー情報をWebにあげるシステムを作ってみた。

構成

下記構成で作成をした。細かい部品等はそのうち回路図をアップしようと思っています。

  • ESP-WROOM-02 + ESP-WROOM-02用2.54mmピッチ変換基板(AE-ESP-WROOM-02)
  • HT7733A (3.3V昇圧用)
  • DHT22 (DHT11より精度の良いらしい温湿度センサ)
  • 単4電池2本
  • aitendo ミニ基板 P-32x32mm 1枚
  • ALTOIDS缶 (ケース)

AT&T M2X

センサーデバイスの情報をどこにあげれば良いのかを調べたところ、AT&TのM2Xというのが良さそうだったので使ってみた。
Developerプランだと10デバイスまで登録出来て、1デバイスあたり100,000回/月投稿出来て無料の様子。
その他のプランの料金についてはPricing参照。

AT&T M2Xでの流れ

  1. アカウント作成 メールアドレスと名前、パスワードを入力すれば確認メールが来た GitHubのアカウントでログイン出来るらしい (やってない)
  2. バイスの登録 ログイン後の画面でCreate Newとかで作成出来たと思う。1デバイスに複数の情報 (Stream) を投稿出来る
    2.1. 今回はESP-WROOM-02というデバイス名に、esp-temp, esp-humというStreamを作成し、ここに温度と湿度を投稿する
  3. 投稿にはDevice IDとAPI Keyが必要になる。この情報をDevice作成後、Deviceの画面にわかりやすく出てくる
  4. HTTP PUTで情報を投稿する
  5. 投稿したデータが管理画面に表示される

とこんな感じ。

ESP-WROOM-02Arduinoスケッチ

Arduino IDEESP-WROOM-02を扱うために必要なファイルをGitHubesp8266/Arduinoから取得。
説明に従って、Arduino IDEにインストールしておく。
DHT22はIO12pinに接続している。また、Deep-sleepからの復帰のためにIO16をリセットピンに接続している。 AT&T M2Xへの投稿は、Streamを作成した際に出てくるサンプルcurlコマンドのパケットをWiresharkで確認してそれを使用した。
動作は下記流れになっている

  • 起動 -> WiFi接続 -> DHT22から温度と湿度取得 -> M2Xに投稿 -> Deep-Sleepに入る(5分)-> 起動に戻る

Arduinoソースコードは下記ソースコードを使わせてもらった。

下記 .inoファイルになる
適宜ssid, password, m2xKey, tempurl, humurlについてはそれぞれの環境に合わせて書き換える必要がある。

/*
 *  This sketch sends data via HTTP GET requests to data.sparkfun.com service.
 *
 *  You need to get streamId and privateKey at data.sparkfun.com and paste them
 *  below. Or just customize this script to talk to other HTTP servers.
 *
 */
#include <ESP8266WiFi.h>
#include "DHT.h"

#define DHTPIN 12     // what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT22   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

const char* ssid     = "WIFI-NO-SSID";
const char* password = "WIFI-NO-PASSWORD";

const char* host = "api-m2x.att.com";
const char* m2xKey = "M2X-NO-KEY";
// We now create a URI for the request
String tempurl = "/v2/devices/DEVICE_ID/streams/STREAM-NO-NAME/value";
String humurl = "/v2/devices/DEVICE_ID/streams/STREAM-NO-NAME/value";

void setup() {
  Serial.begin(115200);
  delay(1000);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("DHT22 test!");
  dht.begin();
}

void loop() {
  delay(2000);

  Serial.print("connecting to ");
  Serial.println(host);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  Serial.print("Requesting URL: ");
  Serial.println(tempurl);

  // 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();
  // 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\n");

  // This will send the request to the server
  client.print(String("PUT ") + tempurl + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: curl/7.43.0\r\n" +
               "Accept: */*\r\n" +
               "X-M2X-KEY: " + m2xKey + "\r\n" +
               "Content-Type: application/json\r\n" +
               "Content-Length: 20\r\n\r\n" +
               "{ \"value\": \""+ String(t,2) +"\" }");

  delay(1000);

  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");

  client.print(String("PUT ") + humurl + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: curl/7.43.0\r\n" +
               "Accept: */*\r\n" +
               "X-M2X-KEY: " + m2xKey + "\r\n" +
               "Content-Type: application/json\r\n" +
               "Content-Length: 20\r\n\r\n" +
               "{ \"value\": \""+ String(h,2) +"\" }");
   delay(1000);

   // Read all the lines of the reply from server and print them to Serial
   while(client.available()){
     String line = client.readStringUntil('\r');
     Serial.print(line);
   }

   Serial.println();
   Serial.println("closing connection");

  //1:μ秒での復帰までのタイマー時間設定  2:復帰するきっかけの設定(モード設定)
  ESP.deepSleep(300 * 1000 * 1000 , WAKE_RF_DEFAULT);

  //deepsleepモード移行までのダミー命令
  delay(1000);

}