其實多數文章應該都是教你用DHT11 或DHT22 ,但是他們的精準度DHT11根本扯到不行,DHT22比較準一點,有時候誤差個一兩度攝氏度(不知道為什麼明明spec上說誤差應該只有+-0.5而已)
所以我目前乾脆就改用SHT30 實測溫濕度就差異不大了
下面範例是,D1 MINI 配上一般的LCD大(?)螢幕+SHT30並透過wifi回傳伺服器的範例
SHT30用的I2C是0x44 所以可以這樣並聯,就不用特別做設定
這邊要注意的是SHT3X 有的板他的順序排列會有點不一樣,可能要注意一下板子上英文是放在哪個洞,不要傻傻的亂插就是。
//管理程式庫,新增Adafruit_SHT31 或是下載 https://github.com/adafruit/Adafruit_SHT31
//新增LiquidCrystal_PCF8574
//http://www.esp8266learning.com/wemos-and-sht31.php
#include "ESP8266WiFi.h"
#include "ESP8266WiFiMulti.h"
#include <LiquidCrystal_I2C.h>
// Set the pins on the I2C chip used for LCD connections:
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
ESP8266WiFiMulti WiFiMulti;
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
Adafruit_SHT31 sht31 = Adafruit_SHT31();
int ServerCounter = 1;
void setup() {
Serial.begin(115200);
lcd.begin(16, 2); // 初始化 LCD,一行 16 的字元,共 2 行,預設開啟背光
// 閃爍三次
for(int i = 0; i < 3; i++) {
lcd.backlight(); // 開啟背光
delay(250);
lcd.noBacklight(); // 關閉背光
delay(250);
}
lcd.backlight(); // 開啟背光
// 輸出初始化文字
lcd.clear();
lcd.setCursor(0, 0); // 設定游標位置在第一行行首
lcd.print("Hello, world!");
delay(1000);
lcd.setCursor(0, 1); // 設定游標位置在第二行行首
lcd.print("davidou.org");
if (! sht31.begin(0x44))
{
Serial.println("Couldn't find SHT31");
while (1) delay(1);
}
delay(10);
// We start by connecting to a WiFi network
//davidou 應該要改成你家的ssid 而 00000000是wifi密碼
WiFiMulti.addAP("davidou", "00000000");
Serial.println();
Serial.println();
lcd.setCursor(0, 0); // 設定游標位置在第二行行首
lcd.print("Wait for WiFi...");
Serial.print("Wait for WiFi... ");
while(WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("[成功]WiFi 已連接");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
Serial.println("溫溼度測試.");
delay(8000);
}
//void(* resetFunc) (void) = 0; //declare reset function @ address 0
String floatToString(float x, byte precision = 2) {
char tmp[50];
dtostrf(x, 0, precision, tmp);
return String(tmp);
}
void loop() {
float t = sht31.readTemperature();
float h = sht31.readHumidity();
if (isnan(h) || isnan(t) ) {
Serial.println("Failed to read from SHT3X sensor!");
return;
}
Serial.print("濕Humidity: ");
Serial.println(h);
Serial.print("%\t");
Serial.print("溫: ");
Serial.print(t);
Serial.print("*C\t");
const uint16_t port = 80;
const char * host = "arduino.davidou.org"; // ip or dns
Serial.print("連線至");
Serial.println(host);
if(ServerCounter>=600){ //超過600秒才寫到server
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
Serial.println("wait 5 sec...");
delay(5000);
//resetFunc(); //call reset
}
// 地點選擇
String url = "temperatureDetect.php?location=home&tmp="+floatToString(t)+"&h="+floatToString(h);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BoardDetectorESP8266davidouHomeSystem\r\n" +
"Connection: close\r\n\r\n");
Serial.println("資料送出,連線關閉");
client.stop();
ServerCounter=1;
}else{
ServerCounter++;
Serial.println(ServerCounter);
}
// 輸出初始化文字
lcd.clear();
lcd.setCursor(0, 0); // 設定游標位置在第一行行首
lcd.print("Temp : "+floatToString(t)+" C");
lcd.setCursor(0, 1); // 設定游標位置在第二行行首
lcd.print("Humi : "+floatToString(h)+" %");
Serial.println("ok");
delay(1000);
}