2016年1月26日火曜日

ESP-WROOM-02を使ってみるー気温、湿度モニター

思い通りに動くので気をよくして、温度湿度モニターまで進めることに、、、。
手元に秋月で買ったDHT11があったので早速動かしてみることにした。 DHT11を使ったモニタはArduinoIDEのスケッチのサンプルにDHTtesterというのがあるので、それを使いました。 全く問題なく動くことを確認したので、これにWiFiClient機能を追加しました。  サーバーにLoggerを立てていないので、実際に接続しての動作は確認していませんが、文字列として確かに送信されているので、動作しているようです。 deep sleepを60分にして、シリアルモニターで動作確認を実施中。 これがうまくゆけば、取りあえず庭で使う温湿度計として筐体を含めて検討することにします。 (サーバープログラムはphpでもデータを取得時間とともにファイルに書き込むだけなので省略)

あれ、deep sleepがこれでは動きません?! メッセージが文字化けしているので、どうなっているのか、見当がつかない。
参考文献3を見て、簡単なプログラムで確認したのですが、何故か立ち上がらない(PG startが出てこない!)。  使ったボードの内部でRSTがどっかに接続されているとか、、、。 SwitchScienceのfull set versionでもう一度確認する必要がありますが、少なくともこのボードではdeep sleepが使えないのかもしれません。(要確認)



次は土壌水分のセンサーの検討を始めなくては、、、。


参考にした記事:
  WROOM から温度湿度センサー値を スマホ ストリーミング 表示
  ESP-WROOM-02 を使った リモート温度・湿度・気圧計の製作
   ESP8266の真骨頂Deep-Sleepモードの使い方 
   ESP-WROOM-02プログラミング:スリープのテスト


20160127 追記 > apahce24/php55をFreeBSDサーバに立ち上げて、取りあえずデータ送信の確認。 phpは10年近く前に弄ったことがありましたが大きなプログラムを書くのには適さないのとデバグが面倒で捨てましたが、今回もちっちゃなプログラムの癖して . (ピリオド)が一個抜けていて手こずりました。 その上に、スケッチも”Connection: close\r\:n\r\n"のコロンがセミコロンになっていたのに気が付くのに半日掛かってしまいました。 目が悪いkとを言い訳にしよう!!
 取りあえず、これでサーバにデータの蓄積は出来るようになりましたので、csvでなくてPostgreSQLに蓄積させてゆくようにしよう! ave/min/max等便利な関数があるので、データを取り出すときの処理が簡単になる可能性がありますし、データの安全性も向上させることが出来ますので、、、。 何故PostgreSQLって? 慣れているから。


================== WeatherMonitorUsing DHT11.ino ====================
// Example testing sketch for various DHT humidity/temperature sensors

#include "DHT.h"
#include

#define DHTPIN 5     // Use IO5

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11

// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);

const char* ssid = "Buffalo-G-4CC0"; // SSID to access network
const char* password = "65rhwrit3nnee"; // password to connect to the network
const char* host = "192.168.1.3"; // host address
const char* Identifier = "A";  // Identifier to locate the monitor

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

unsigned long number = 0;
 
void setup() {
  Serial.begin(115200);
  Serial.println("DHTxx test!");
   
  delay(10);
  // 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());
  // DHT11 to start.
  dht.begin();
}
int value = 0;

void loop() {
  // Wait a few seconds between measurements.
  delay(5000);
  ++value;

  // 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!");
    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("Temperature: ");
  Serial.print(t);
  Serial.print(" *C\t");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("; Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");

  // Use WiFiClient class to create TCP connection
  Serial.print("connecting to ");
  Serial.println(host);
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)){
    Serial.println("connection failed");
    //return;
  }
  String url = "/temperature/tempera_log.php";
        url = url + "?ID"= Identifier; // ID to identify location
        url = url + "&temperature="+String(t);
        url = url + "&humidity=" +String(h);
  Serial.print("Requesting URL: ");
  Serial.println(url);

  client.print(String("Get ")+url+" HTTP/1.1\r\n"
                + "Host: " + host + "\r\n"
                + "Connection; close\r\n\r\n");   <--- connection="" span="">
  delay(10);
   
  // 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");

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

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

=================================================================


  

0 件のコメント:

php のインストールの確認

phpって最初のfacebook書くときに使われたみたいで、それなりに歴史のある言語で、私も2006年位から使っていますが、CLIで使う事はあまり無いので、apacheとの連携のトラブル(mod_phpのバージョンの齟齬)などは気になりますが、拡張モジュールのインストールの問題に...