| 
 | 
 
程式碼如下,想請教各位前輩,1.想請問為何啟動程式時會一直卡在SD card is ready,是語法錯誤? 
2.加入SD卡模組後,WIFI模組卻無法啟動 
3.上傳程式後出現可用記憶體低下,這樣會有影響嗎? 
 
 
煩請前輩們幫忙解惑,感恩#include  
#include "DHT.h" 
#include  
#include  
#include  
#include  
#include 
 
#define DEBUG 
#define _ledpin     13 
#define _rxpin      0 
#define _txpin      1 
SoftwareSerial debug( _rxpin, _txpin ); // RX, TX 
 
#define _dhtpin     8 
#define _dhttype    DHT11 
DHT dht11( _dhtpin, _dhttype ); 
uint8_t dhtbuf[2]; 
 
File myFile; 
 
int measurePin = 10; //Connect dust sensor to Arduino A0 pin 
int ledPower = 3;   //Connect 3 led driver pins of dust sensor to Arduino D2 
   
int samplingTime = 460; 
int deltaTime = 40; 
int sleepTime = 9500; 
char buf[50]; 
char fn[12];   
float voMeasured = 0; 
float calcVoltage = 0; 
float dustDensity = 0; 
int mean_pm, mean_v0; 
char pm[16];   
char HT[7]; 
char pm_SD[22]; 
int m, d, H, M, S; 
 
namespace { 
const int kCePin   = 5; 
const int kIoPin   = 6; 
const int kSclkPin = 7; 
DS1302 rtc(kCePin, kIoPin, kSclkPin); 
}; 
 
#define SSID "ASUS" 
#define PASS "密碼" 
#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=UR0WZRMIAUXQVUBI"; 
 
void setup() { 
  Serial.begin(9600); 
   
  debug.begin(9600); 
  
 
  sendDebug("AT"); 
    delay(5000); 
    if(Serial.find("OK")) 
    { 
        debug.println("RECEIVED: OK\nData ready to sent!"); 
        connectWiFi(); 
    } 
  dht11.begin(); 
  pinMode( _ledpin, OUTPUT ); 
  digitalWrite( _ledpin, LOW ); 
   
  Serial.println("Initial SD card..."); 
  pinMode(10, OUTPUT); 
 
   if (!SD.begin(4)) { 
    Serial.println("Check SD and Reset!"); 
    return;  
  } 
   else { 
    Serial.println("SD card is ready."); 
    } 
  
  Time t = rtc.time(); 
  m=t.mon; 
  while (m>12) { 
    t=rtc.time(); 
    m=t.mon; 
    delay(50); 
  } 
  d=t.date; 
  H=t.hr; 
  M=t.min; 
  S=t.sec; 
 
  delay(2000);   
} 
   
void loop() { 
    Time t = rtc.time(); 
   
  if (t.mon>12) { 
    S=S+1; 
  } 
  else { 
    m=t.mon; 
    d=t.date; 
    H=t.hr; 
    M=t.min; 
    S=t.sec; 
  } 
  snprintf(fn, sizeof(fn), "t%02d%02d%02d.csv", 
           m, d, H); 
  myFile = SD.open(fn, FILE_WRITE); 
 
  snprintf(buf, sizeof(buf), "%02d/%02d %02d:%02d:%02d  ", 
           m, d, 
           H, M, S); 
 
  Serial.print(fn); 
  Serial.print(" "); 
  Serial.print(buf); 
  Serial.print(" ");   
 
    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 ); 
    } 
 snprintf(HT, sizeof(HT), "%2d, %d", int(dhtbuf[0]), int(dhtbuf[1])); 
  //Serial.print(HT); 
    delay(2000);   // 60 second 
 
 
  dustDensity = 0; 
  mean_v0=0; 
  float vs=140; 
      
    digitalWrite(ledPower, LOW); 
    delayMicroseconds(samplingTime); 
    voMeasured = analogRead(measurePin); 
    delayMicroseconds(deltaTime); 
    digitalWrite(ledPower, HIGH); 
    delayMicroseconds(sleepTime); 
     
    calcVoltage = voMeasured * (5.0 / 1024.0); 
   if (dhtbuf[0]>50) { 
      dustDensity += 0.6*(1-0.01467)*(dhtbuf[0]-50)*(voMeasured-vs)/6.0; 
    } 
    else { 
      dustDensity += 0.6*(voMeasured-vs)/6.0; 
    } 
   
    
   Serial.print("RH:"); 
   Serial.print(dhtbuf[0]); 
   Serial.print("% "); 
   Serial.print("T:"); 
   Serial.print(dhtbuf[1]); 
   Serial.print("C ");  
   Serial.print("   Raw input voltage: "); 
   Serial.print(calcVoltage); 
   Serial.print("   Raw Signal Valued: "); 
   Serial.print(voMeasured);   
   Serial.print("   PM2.5: "); 
   Serial.println(dustDensity); 
   Serial.println(" ug/m3 ");    
    
 
   delay(2000); 
 mean_v0=mean_v0/30; 
 mean_pm = dustDensity / 30.0; 
 snprintf(pm, sizeof(pm), "%04d", mean_pm); 
 snprintf(pm_SD, sizeof(pm_SD), "%05d", int(mean_v0));  
  myFile.print(buf); 
  myFile.print(','); 
  myFile.print(HT); 
  myFile.print(", "); 
  myFile.print(", "); 
  myFile.print(pm); 
  myFile.print(", "); 
  myFile.print(pm_SD); 
  myFile.println(""); 
  myFile.close();   
} 
  
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 +"&field3=" + dustDensity + "&field4=" + voMeasured+ "\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; 
    } 
}  |   
 
 
 
 |