2016年6月13日月曜日

100円温度センサー+ESP-WROOM-02でWiFi風呂水温計



ってことで、昨日試した100円温度センサーSTTS751をESP-WROOM-02と電池に接続し、100均のジャム瓶?に入れました。フタの発泡ウレタンを少し切り取ってアルミ地金をむき出し、そこに放熱グリースを塗ってセンサーを貼り付けました。

風呂の中でフタを下にして浮かんで欲しいので、重い電池がフタ側に来るようにするのがコツ?です。ただ、フタが下だとどうしても浸水してしまいます。内部の温度が下がるときに熱膨張(の逆)によって水を吸い込みますので、風呂が湧いたらお湯から出すようにするのが一番効果がありました(経験者は語る)。

対策としては膨張した空気の容積差を吸収すればいいので、もっと柔らかいボトルを使うか、ボトル側に穴をあけてビニールシートで空気室を作ってやるのが一番簡単かなと思います。

また、風呂のフタをしめていれば、それほど大きく温度がズレることもないので、電池をボトル側の底に入れても問題ないかと思います。その辺は使い方に応じて選択してください。


ESP-WROOM-02がdeep sleepから立ち上がった時にWiFi経由でThingSpeak.comへデータを送信します。風呂の温度はそれほど急激に変化するものでもありませんから、送信頻度は5分に1回にしました。以前1分間隔で動かした時には単4白エネループ3本で110時間動きました。今回黒エネループで何とか一ヶ月=720時間程度動いてくれると便利なのですが。

なおSTTS751とのやりとりは、こちらを参考にさせていただきました。ありがとうございます。

以下ソースです。初期化処理にbegin transmissionを追加して、データ受信のところを多少書き換えました。上記のサンプルコードには問題があるので後日修正します(6/24)。ThingSpeak.comへの送信は以前と同じというかほぼコピペです。

// ESP_STTS751_Bathtab
// by koichi kurahashi 2016-06-13
// 2016-06-25 : どうしようもなくダメなバグを修正(ftoa->dtostrf)
//
// Thanks:
// 電子工作部
// https://plus.google.com/u/0/communities/114133885055784182372
//
extern "C" {
#include "user_interface.h"
}
#include <Time.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Wire.h>
//
// Wifi
//
#include "../../private_ssid.h"
//const char *ssid = "*************";
//const char *password = "*************";
WiFiClient client;
//
// STTS751 I2C address is 0x39(57)
//
//
// MCU - STTS751
// 3v3 - 3 Vcc
// GND - 2 Gnd
// SDA - 6 SDA
// SCL - 4 SCL
//
#include "STTS751.h"
STTS751 stts751;
//
// Setup
//
int waitSec = 5 * 60;
void setup() {
Serial.begin(115200);
Wire.begin();
// GND for special board
pinMode(12, OUTPUT);
digitalWrite(12, LOW);
stts751.setup();
//
// Wifi
//
WiFi.begin ( ssid, password );
Serial.println("Started");
// read temperature
float temperature = stts751.readTemperature();
// Wait for connection
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
Serial.println("Wifi Connected");
// send to thingspeak.com
sendTemperature(temperature);
// into deep sleep
waitSec = temperature > 30.0 ? 300 : 600;
ESP.deepSleep((waitSec - second()) * 1000 * 1000, WAKE_RF_DEFAULT);
delay(1000);
}
void loop() {
// Serial.println(stts751.readTemperature());
delay(1000);
}
//
// send to thing speak
//
void sendTemperature(float inTemperature) {
char fBuf[10];
// ftoa(inTemperature, fBuf);
dtostrf(inTemperature, 1, 2, fBuf);
String postStr = "&field1=" + String(fBuf);
Serial.println(postStr);
send(postStr);
Serial.println(postStr);
}
void send(String inPostStr) {
String apiKey = "7TV00ISUCC7E2VQY";
Serial.print("Connecting...");
if (client.connect("184.106.153.149", 80)) { // api.thingspeak.com
Serial.print("Connected....");
String postStr = apiKey + inPostStr + "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.println("posted.");
}
client.stop();
}
//void ftoa(float f, char *buf) {
// float a = int(f);
// float b = int((f - a) * 100.0000);
//
// sprintf(buf, "%d.%d", (int)a, (int)b);
//}

■追記■

電池のさらなる長持ちを目指してプログラムを改良しました(風呂水温計 ver 3.1)。

0 件のコメント:

コメントを投稿

注: コメントを投稿できるのは、このブログのメンバーだけです。