|
各位前輩好,已經研究ESP8266快要一個月了,我無論怎麼設定,都一直無法將DHT-11的溫溼度值上傳到thingspeak上頭。我是依循:ㄆㄜ ㄊㄧㄡ ㄙˋ的機碼進行修改的:
#include <SoftwareSerial.h>
#include "DHT.h"
#define DEBUG
#define _ledpin 13
//*-- Hardware Serial
#define _baudrate 115200
//*-- Software Serial
//
#define _rxpin 2
#define _txpin 3
SoftwareSerial debug( _rxpin, _txpin ); // RX, TX
//*-- DHT11
#define _dhtpin 8
#define _dhttype DHT11
DHT dht11( _dhtpin, _dhttype );
uint8_t dhtbuf[2];
//*-- IoT Information
#define SSID "KKMAN"
#define PASS "0727071289"
#define IP "184.106.153.149" // ThingSpeak IP Address: 184.106.153.149
// 使用 GET 傳送資料的格式
// GET /update?key=[THINGSPEAK_KEY]&field1=[data 1]&filed2=[data 2]...;
String GET = "GET /update?key=IBLZ275PB8XKAKBZ";
void setup() {
Serial.begin( _baudrate );
debug.begin( _baudrate );
sendDebug("AT");
delay(5000);
if(Serial.find("OK"))
{
debug.println("RECEIVED: OK\nData ready to sent!");
connectWiFi();
}
// DHT11
dht11.begin();
pinMode( _ledpin, OUTPUT );
digitalWrite( _ledpin, LOW );
}
void loop() {
dhtbuf[0] = dht11.readHumidity();
dhtbuf[1] = dht11.readTemperature();
// 確認取回的溫溼度數據可用
if( isnan(dhtbuf[0]) || isnan(dhtbuf[1]) )
{
debug.println( "Failed to read form DHT11" );
}
else
{
digitalWrite( _ledpin, HIGH );
char buf[3];
String HH, TT;
buf[0] = 0x30 + dhtbuf[1] / 10;
buf[1] = 0x30 + dhtbuf[1] % 10;
TT = (String(buf)).substring( 0, 2 );
buf[0] = 0x30 + dhtbuf[0] / 10;
buf[1] = 0x30 + dhtbuf[0] % 10;
HH = (String(buf)).substring( 0, 2 );
updateDHT11( TT, HH );
#ifdef DEBUG
debug.print("Humidity: ");
debug.print( HH );
debug.print(" %\t");
debug.print("Temperature: ");
debug.print( TT );
debug.println(" *C\t");
#endif
digitalWrite( _ledpin, LOW );
}
delay(60000); // 60 second
}
void updateDHT11( String T, String H )
{
// 設定 ESP8266 作為 Client 端
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
sendDebug(cmd);
delay(2000);
if( Serial.find( "Error" ) )
{
debug.print( "RECEIVED: Error\nExit1" );
return;
}
cmd = GET + "&field1=" + T + "&field2=" + 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" );
}
}
void sendDebug(String cmd)
{
debug.print("SEND: ");
debug.println(cmd);
Serial.println(cmd);
}
boolean connectWiFi()
{
Serial.println("AT+CWMODE=1");
delay(2000);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
sendDebug(cmd);
delay(5000);
if(Serial.find("OK"))
{
debug.println("RECEIVED: OK");
return true;
}
else
{
debug.println("RECEIVED: Error");
return false;
}
cmd = "AT+CIPMUX=0";
sendDebug( cmd );
if( Serial.find( "Error") )
{
debug.print( "RECEIVED: Error" );
return false;
}
}
然後可以看得到ESP8266上頭除了紅色的電源燈有亮外,基板在發訊號的時候,藍色的燈也會閃一下閃一下....但是去看thingspeak仍然是沒有任何紀錄的,請問基本上會是那個地方出錯呢?
謝謝了~ |
|