DHT11 センサを使った、小型シールド風 基板を作成し
esp8266の機能を拡張を検討してみました。
片側8ピンで、接続、基板固定します。
# 材料
ユニバーサル基板: 幅30mm程度に切断
ピンヘッダ: 8P
ピンソケット: 4P(DHT11 側)
抵抗: 10K
# 配置
# 基板、ピン側
# 正面側
# Log
# ドライバ
ビルド:
ESP8266 Arduino core : 2.2.0
Arduino IDE: 1.6.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
DHT11+ esp8266 | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <DHT.h> | |
#define DHTPIN 4 // what digital pin we're connected to | |
#define DHTTYPE DHT11 // DHT 11 | |
DHT dht(DHTPIN, DHTTYPE); | |
const char* ssid = ""; | |
const char* password = ""; | |
const char* host = "api.thingspeak.com"; | |
String mAPI_KEY=""; | |
static uint32_t mTimerTmp; | |
static uint32_t mTimerPost; | |
String mTemp=""; | |
String mHum=""; | |
// | |
void proc_http(String sTemp, String sHum){ | |
//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 = "/update?key="+ mAPI_KEY + "&field1="+ sTemp +"&field2=" + sHum; | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
delay(10); | |
int iSt=0; | |
while(client.available()){ | |
String line = client.readStringUntil('\r'); | |
Serial.print(line); | |
} | |
} | |
// | |
void setup() { | |
//pinMode(mLedPin, OUTPUT); | |
Serial.begin( 9600 ); | |
Serial.println("#Start-setup"); | |
Serial.print("millis.Start: "); | |
Serial.println(millis() ); | |
dht.begin(); | |
delay(10); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
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()); | |
} | |
void loop() { | |
delay( 100 ); | |
if (millis() > mTimerTmp) { | |
mTimerTmp = millis()+ 3000; | |
float h = dht.readHumidity(); | |
float t = dht.readTemperature(); | |
if (isnan(h) || isnan(t) ) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
int itemp =(int)t; | |
int iHum = (int)h; | |
mTemp=String(itemp); | |
mHum=String(iHum); | |
Serial.print("dat="); | |
Serial.print(mTemp); | |
Serial.println( mHum ); | |
} | |
if (millis() > mTimerPost) { | |
mTimerPost= millis()+ 60000; | |
proc_http(mTemp, mHum); | |
//delay(100); | |
} | |
} | |
ソケット差込方式で、
周辺デバイスへの配線(ワイヤ)無しで、機能を拡張できるのは楽ですね。
そのままケースに配置しても、小型のデバイスにできそうです。
# 関連の部品
esp8266モジュール[MK-PKBN-028]
http://kuc-arc-f.com/make/?p=726
# 関連のまとめ
IoT な電子工作まとめ
http://knaka0209.blogspot.jp/2015/11/iot-matome.html