ESP-WROOM-02で温度測定サーバー

ESP-WROOM-02で温度測定サーバー

ESP-WROOM-02で温度測定サーバー

Arduinoのスターターキットの中に温度・湿度センサー(DHT-11)がありましたのでワイヤレス温度計にしてみました。

家の内外の温度を測ってスマホで見たいと思い遊び心で、測定データを送るサンプルと、温湿度測定のサンプルを組み合わせれば簡単かと始めましたが、思いのほかArduino サーバーの勉強になってしまいました。

写真の左側にあるUNOと小さなブレッドボードは電池駆動時の電源電圧変化を確認するために使用した、九州工業大学が公開しているArduinoオシロスコープ で、右側が今回の温度測定サーバーです。

WiFiで測定データを送るサンプル
http://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/server-examples.html

DHT-11のスケッチサンプル
http://platformio.org/lib/show/849/SimpleDHT

まだarduinoを使い始めたばかりなので、このサンプル2つをほぼ単純に組み合わせるだけでも時間がかかりましたが、なんとか動作するようになりました。

9Vの乾電池(006P)+電源ボードでWROOM-02を駆動すると正常に動作しなかったので、電源電圧の変化を測定したときのデータです。リセット後の電流増によって電圧降下が起きることが原因と判りました。

見苦しいと思いますが、今回のスケッチです。

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <SimpleDHT.h>

const char* ssid = “xxxxxx”;
const char* password = “yyyyyyyyy”;

WiFiServer server(80);

int pinDHT11 = 5;
SimpleDHT11 dht11;
int temp_f;
int humi_f;

void setup()
{
Serial.begin(115200);
Serial.println();
WiFi.config(IPAddress(192, 168, 0, 144), WiFi.gatewayIP(), WiFi.subnetMask());

Serial.printf(“Connecting to %s “, ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(“.”);
}
Serial.println(” connected”);

if (!MDNS.begin(“esp8266”)) {
Serial.println(“Error setting up MDNS responder!”);
while(1) {
delay(1000);
}
}
Serial.println(“mDNS responder started”);

server.begin();
Serial.printf(“Web server started, open %s in a web browser\n”, WiFi.localIP().toString().c_str());

// Add service to MDNS-SD
MDNS.addService(“http”, “tcp”, 80);
}// prepare a web page to be send to a client (web browser)
String prepareHtmlPage()
{
String htmlPage =
String(“HTTP/1.1 200 OK\r\n”) +
“Content-Type: text/html; charset=utf-8\r\n” +
“Connection: close\r\n” + // the connection will be closed after completion of the response
“Refresh: 5\r\n” + // refresh the page automatically every 5 sec
“\r\n” +
“<!DOCTYPE HTML>” +
“<html>” + “<h2>” +
“温度: ” + String(temp_f) + ” 度” + “<BR />” +
“湿度: ” + String(humi_f) + ” %” + “</h2>” +

“</html>” +
“\r\n”;
return htmlPage;
}

void loop()
{
WiFiClient client = server.available();
// wait for a client (web browser) to connect
if (client)
{
Serial.println(“\n[Client connected]”);
while (client.connected())
{
// read line by line what the client (web browser) is requesting
if (client.available())
{
String line = client.readStringUntil(‘\r’);
Serial.print(line);
// wait for end of client’s request, that is marked with an empty line
if (line.length() == 1 && line[0] == ‘\n’)
{
client.println(prepareHtmlPage());
break;
}
}
}
delay(1); // give the web browser time to receive the data

// close the connection:
client.stop();
Serial.println(“[Client disonnected]”);
}
// read temperature and humidity.

byte temperature = 0;
byte humidity = 0;
if (dht11.read(pinDHT11, &temperature, &humidity, NULL))
{
Serial.print(“Read DHT11 failed.”);
return;
}

Serial.print(“Sample OK: “);
Serial.print((int)temperature); Serial.print(” *C, “);
Serial.print((int)humidity); Serial.println(” %”);
temp_f = temperature ;
humi_f = humidity ;
// DHT11 sampling rate is 0.5Hz.
delay(2000);
}