| 
 | 
6#
 
 
 樓主 |
發表於 2016-4-8 23:54:25
|
只看該作者
 
 
 
我有成功的將比較基本的溫溼度顯示於I2C 1602以及藉由SENSOR去控制RELAY的程式以變數包裹的方式來依據擷取秒數進行控制,但是目前比較頭大的是~我還希望可以用ESP8266將溫濕度的資料上傳到thingspeak上面,問題就來了~我將該上傳的程式用變數包裹後放入,就會一直出現錯誤。 
以下是大致的程式碼 
int c0,c1,c2; 
void loop(){ 
delay(1000); 
if(++c0 >= 1){ 
c0 =0; 
... 
} 
if(++c1 >= 180){ 
c1 =0; 
... 
} 
if(++c2 >= 600){ 
c2 =0; 
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; 
    } 
} 
} 
 
另外還有一個問題,於上傳雲端的程式中須有等待的時間,這是不是也會影響整個loop的運作時間? |   
 
 
 
 |