/**1.DHT11½ÓArduino£ºvccÁ¬3.3v£¬DATAÁ¬A0£¬GNDÁ¬GND*2.ESP8266Á¬Arduino£ºTXÁ¬0£¬RXÁ¬1£¬ENºÍ3V3Á¬3.3v£¬GNDÁ¬GND*/#includeSoftwareSerial.h#defineDEBUG#define_baudrate9600//USBTTL¼ì´íÓÃ#define_rxpin2#define_txpin3SoftwareSerialdebug(_rxpin,_txpin);//RX,TX//DHTÎÂʪ¶È´«¸ÐÆ÷#definedht_dpinA0bytebGlobalErr;bytedht_dat[5];//ESP8266WifiÄ£¿é/*#defineSSIDONEPLUS3#definePASS12345678#defineIP192.168.43.125*/#defineSSIDiBuildingS102#definePASSzyzhang128104#defineIP192.168.1.162//ʹÓÃGET¸ñʽStringGET=\rReadsensor:OK\n;voidsetup(){InitDHT();Serial.begin(_baudrate);debug.begin(_baudrate);delay(300);sendDebug(AT);delay(500);if(Serial.find(OK)){debug.println(RECEIVED:OK\nDatareadytosent!);connectWiFi();}delay(700);}voidloop(){ReadDHT();switch(bGlobalErr){case0:StringTT,HH;HH=String(dht_dat[0])+'.'+String(dht_dat[1]);TT=String(dht_dat[2])+'.'+String(dht_dat[3]);updateDHT11(TT,HH);#ifdefDEBUGdebug.print(Humidity:);debug.print(HH);debug.print(%\t);debug.print(Temperature:);debug.print(TT);debug.println(*C\t);#endif}delay(2000);}voidupdateDHT11(StringT,StringH){//ʹÓÃATÖ¸ÁîÉÏ´«Stringcmd=AT+CIPSTART=\TCP\,\;cmd+=IP;cmd+=\,8080;sendDebug(cmd);delay(2000);if(Serial.find(Error)){debug.print(RECEIVED:Error\nExit1);return;}cmd=GET+Temperature=+T+oCHumidity=+H+%+\r\n;Serial.print(AT+CIPSEND=);Serial.println(cmd.length());if(Serial.find()){debug.print();debug.print(cmd);Serial.print(cmd);}else{sendDebug(AT+CIPCLOSE);}if(Serial.find(OK)){debug.println(RECEIVED:OK);}else{debug.println(RECEIVED:Error\nExit2);}}voidsendDebug(Stringcmd){//´«µ½USBTTLdebug.print(SEND:);debug.println(cmd);Serial.println(cmd);}booleanconnectWiFi(){//Á¬½Óµ½WifiSerial.println(AT+CWMODE=1);delay(2000);Stringcmd=AT+CWJAP=\;cmd+=SSID;cmd+=\,\;cmd+=PASS;cmd+=\;sendDebug(cmd);delay(2000);if(Serial.find(OK)){debug.println(RECEIVED:OK);returntrue;}else{debug.println(RECEIVED:Error);returnfalse;}cmd=AT+CIPMUX=0;sendDebug(cmd);if(Serial.find(Error)){debug.print(RECEIVED:Error);returnfalse;}}voidInitDHT(){//³õʼ»¯DHTpinMode(dht_dpin,OUTPUT);digitalWrite(dht_dpin,HIGH);}voidReadDHT(){bGlobalErr=0;bytedht_in;bytei;digitalWrite(dht_dpin,LOW);delay(20);digitalWrite(dht_dpin,HIGH);delayMicroseconds(40);pinMode(dht_dpin,INPUT);dht_in=digitalRead(dht_dpin);if(dht_in){bGlobalErr=1;return;}delayMicroseconds(80);dht_in=digitalRead(dht_dpin);if(!dht_in){bGlobalErr=2;return;}delayMicroseconds(80);for(i=0;i5;i++)dht_dat[i]=read_dht_dat();pinMode(dht_dpin,OUTPUT);digitalWrite(dht_dpin,HIGH);bytedht_check_sum=dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];if(dht_dat[4]!=dht_check_sum){bGlobalErr=3;}};byteread_dht_dat(){bytei=0;byteresult=0;for(i=0;i8;i++){while(digitalRead(dht_dpin)==LOW);delayMicroseconds(30);if(digitalRead(dht_dpin)==HIGH)result|=(1(7-i));while(digitalRead(dht_dpin)==HIGH);}returnresult;}