作者:chenyuechi | 更新時間:2016-05-07 | 瀏覽量:5042
本設備是不需要arduino等其他MCU,直接一個ESP8266就可以上傳溫濕度,使用的溫濕度模塊是DHT11
前提是大家會用arduino IDE給8266燒寫程序
不會的話,可以先看一下下面兩篇帖子:
http://www.arduino.cn/thread-17895-1-1.html
http://www.arduino.cn/forum.php?mod=viewthread&tid=17896&page=1&extra=#pid148026
#include <ESP8266WiFi.h>
WiFiClient client;
int temp;//溫度
int humi;//濕度
int tol;//校對碼
int j;
unsigned int loopCnt;
int chr[40] = {0};//創建數字數組,用來存放40個bit
unsigned long time1;
const char *ssid = "******";//這里是我的wifi,你使用時修改為你要連接的wifi ssid
const char *password = "********";//你要連接的wifi密碼
const char *host = "121.42.180.30";//貝殼物聯IP
const int httpPort =8181;
#define pin 2//將dht11的data口接在8266的GPIO2上
//char *ssid="";
//char *password="";
/*void smartConfig()
{
WiFi.mode(WIFI_STA);
Serial.println("\r\nWait for Smartconfig");
WiFi.beginSmartConfig();
while (1)
{
Serial.print(".");
//digitalWrite(relay1, 0);
//delay(500);
//digitalWrite(relay1, 1);
//delay(500);
if (WiFi.smartConfigDone())
{
Serial.println("SmartConfig Success");
Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
strcpy(ssid, WiFi.SSID().c_str());
strcpy(password, WiFi.psk().c_str());
break;
}
}
}*/
void setup()
{
Serial.begin(115200);
delay(10);
//pinMode(relay1,INPUT);
// 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);
//smartConfig();
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
while (!client.connect(host, httpPort)) {
Serial.println("connection failed");
//return;
delay(1000);
}
Serial.print("connecting to ");
Serial.println(host);
client.write("{\"M\":\"checkin\",\"ID\":\"**\",\"K\":\"******\"}\r\n");//登陸設備,修改成自己的ID和KEY
delay(100);
}
void loop()
{
bgn:
delay(2000);
//設置2號接口模式為:輸出
//輸出低電平20ms(>18ms)
//輸出高電平40μs
pinMode(pin,OUTPUT);
digitalWrite(pin,LOW);
delay(20);
digitalWrite(pin,HIGH);
delayMicroseconds(40);
digitalWrite(pin,LOW);
//設置2號接口模式:輸入
pinMode(pin,INPUT);
//高電平響應信號
loopCnt=10000;
while(digitalRead(pin) != HIGH)
{
if(loopCnt-- == 0)
{
//如果長時間不返回高電平,輸出個提示,重頭開始。
Serial.println("HIGH");
goto bgn;
}
}
//低電平響應信號
loopCnt=30000;
while(digitalRead(pin) != LOW)
{
if(loopCnt-- == 0)
{
//如果長時間不返回低電平,輸出個提示,重頭開始。
Serial.println("LOW");
goto bgn;
}
}
//開始讀取bit1-40的數值
for(int i=0;i<40;i++)
{
while(digitalRead(pin) == LOW)
{}
//當出現高電平時,記下時間“time”
time1 = micros();
while(digitalRead(pin) == HIGH)
{}
//當出現低電平,記下時間,再減去剛才儲存的time
//得出的值若大于50μs,則為‘1’,否則為‘0’
//并儲存到數組里去
if (micros() - time1 >50)
{
chr[i]=1;
}else{
chr[i]=0;
}
}
//濕度,8位的bit,轉換為數值
humi=chr[0]*128+chr[1]*64+chr[2]*32+chr[3]*16+chr[4]*8+chr[5]*4+chr[6]*2+chr[7];
//溫度,8位的bit,轉換為數值
temp=chr[16]*128+chr[17]*64+chr[18]*32+chr[19]*16+chr[20]*8+chr[21]*4+chr[22]*2+chr[23];
//校對碼,8位的bit,轉換為數值
//tol=chr[32]*128+chr[33]*64+chr[34]*32+chr[35]*16+chr[36]*8+chr[37]*4+chr[38]*2+chr[39];
//輸出:溫度、濕度、校對碼
Serial.print("temp:");
Serial.println(temp);
String str1="{\"M\":\"update\",\"ID\":\"**\",\"V\":{\"**\":\"";//修改成你自己的ID和數據接口ID1存溫度
str1+=temp;
str1+="\",\"**\":\"";//修改成你自己的數據接口ID2存濕度
str1+=humi;
str1+="\"}}\n";
client.print(str1);
Serial.print(str1);
Serial.print("humi:");
Serial.println(humi);
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);}
delay(3000);
}