作者:chenyuechi | 更新时间:2016-05-07 | 浏览量:5376
本设备是不需要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); }