找回密码
 立即注册

QQ登录

只需一步,快速开始

点击进入授权页面

只需一步,快速开始

  • QQ空间
  • 回复
  • 收藏

参加活动: 0

组织活动: 0

  • TA的每日心情

    2017-9-15 01:56
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    jfvspig 新手上路 2017-9-10 19:17 楼主
    10金币
    求助各位大大我的編碼到底有什麼問題

    非常不穩定,紀錄的資料數值會重複

    //------------------ Time ------------------
    #include <Wire.h>
    #include <RTClib.h>
    RTC_Millis rtc;
    //------------------ DHT22 -----------------
    #include <DHT.h>
    #define DHTPIN A1
    #define DHTTYPE DHT22   
    DHT dht(DHTPIN, DHTTYPE);
    float hum;  //Stores humidity value
    float temp; //Stores temperature value
    //----------------- PMS3003 ----------------
    #include <SoftwareSerial.h>
    #define pmsDataLen  24
    uint8_t pmsDatabuf[pmsDataLen];
    SoftwareSerial pmsSerial(2, 3); // RX, TX
    bool CheckSum(uint8_t *__buf, int __n)
    {
      bool __flag = false; int __sum = 0;
      for(int i=0; i<(__n-2); i++)
      {
        __sum += __buf;
      }
      
      if(__sum == ((__buf[__n-2]<<8) + __buf[__n-1]))
      {
        __flag = true;
      }
      return __flag;
    }
          
    //------------- SD --------------
    #include <SD.h>
    const int chipSelect = 10;
    File dataFile;
    DateTime now;
    //--------------------------------- void setup --------------------------------------------
    void setup () {
      dht.begin();
      Serial.begin(9600);
      pmsSerial.begin(9600);  
      pmsSerial.setTimeout(1500); // set the Timeout to 1500ms, longer than the data transmission periodic time of the sensor
    //------------- Set Time ---------------
        {
        Serial.begin(9600);
        // following line sets the RTC to the date & time this sketch was compiled
        rtc.begin(DateTime(__DATE__, __TIME__));
        }
      
    //------------  setup SD card  -------------

      Serial.begin(9600);
      while (!Serial){
        ; // wait for serial port to connect. Needed for Leonardo only
      }
      Serial.print("Initializing SD card...");
      pinMode(SS, OUTPUT);
      
      if (!SD.begin(chipSelect)) {
        Serial.println("Card failed, or not present");
        while (1) ;
      }
      Serial.println("card initialized.");
      
      DateTime now = rtc.now();
      dataFile = SD.open("datalog.txt", FILE_WRITE);
      if (! dataFile) {
        Serial.println("error opening datalog.txt");
        while (1) ;
      }
    }
    //--------------------------------- void loop -----------------------------------------
    void loop () {
    //-------------- DHT22 ---------------
      hum = dht.readHumidity();
      temp= dht.readTemperature();
    //------------- SD 紀錄 --------------
      String dataString = "";
      
      for (int analogPin = 0; analogPin < 3; analogPin++) {
        int sensor = analogRead(analogPin);
        dataString += String(sensor);
        if (analogPin < 2) {
          dataString += ",";
        }
      }
      DateTime now = rtc.now();
      dataFile = SD.open("datalog.txt", FILE_WRITE);
       
      if (dataFile) {
        dataFile.print(now.year(), DEC);
        dataFile.print('/');
        dataFile.print(now.month(), DEC);
        dataFile.print('/');
        dataFile.print(now.day(), DEC);
        dataFile.print(",");
        dataFile.print(now.hour(), DEC);
        dataFile.print(":");
        dataFile.print(now.minute(), DEC);
        dataFile.print(":");
        dataFile.print(now.second(), DEC);
        dataFile.print(",");
        dataFile.print((float)temp);
        dataFile.print(",");
        dataFile.print((float)hum);
        dataFile.print(",");
        dataFile.print((pmsDatabuf[10]<<8) + pmsDatabuf[11]);
        dataFile.print(",");
        dataFile.print((pmsDatabuf[12]<<8) + pmsDatabuf[13]);
        dataFile.print(",");
        dataFile.print((pmsDatabuf[14]<<8) + pmsDatabuf[15]);
        dataFile.println(" ");
        dataFile.close();
        Serial.println("data stored");
        dataFile.flush();
         }
      else {
        Serial.println("error opening datalog.txt");
      }
       

       
    //--------------- PMS3003 ---------------   
      if(pmsSerial.available())
      {
        static int __index = 0;
        pmsDatabuf[__index] = pmsSerial.read();
        switch(__index)
        {
        case 0:
          __index = (pmsDatabuf[0] == 0x42) ? 1 : 0;
          break;
        case 1:
          __index = (pmsDatabuf[1] == 0x4d) ? 2 : 0;
          break;
        case 2:
          __index = 3;
          break;
        case 3:
          int __len = (pmsDatabuf[2] << 8) + pmsDatabuf[3];
          
          if(__len == (pmsDataLen-4))
          {
            if(pmsSerial.readBytes(&pmsDatabuf[4], __len) == __len)
            {
              if(CheckSum(pmsDatabuf, pmsDataLen) == 1)
              {
                int __value;
             
    //print------OK  
        Serial.print(now.year(), DEC);
        Serial.print('/');
        Serial.print(now.month(), DEC);
        Serial.print('/');
        Serial.print(now.day(), DEC);
        Serial.print(' ');
        Serial.print(now.hour(), DEC);
        Serial.print(':');
        Serial.print(now.minute(), DEC);
        Serial.print(':');
        Serial.print(now.second(), DEC);
        Serial.println();
        Serial.print("Temp = ");
        Serial.print(temp);
        Serial.print(" C ");
        Serial.print("  RH = ");
        Serial.print(hum);
        Serial.println(" % ");
       
        Serial.print("PM1.0= ");
        __value = (pmsDatabuf[10]<<8) + pmsDatabuf[11];
        Serial.print(__value);
        Serial.print("  PM2.5= ");
        __value = (pmsDatabuf[12]<<8) + pmsDatabuf[13];
        Serial.print(__value);
        Serial.print("  PM10= ");
        __value = (pmsDatabuf[14]<<8) + pmsDatabuf[15];
        Serial.print(__value);
       
        Serial.println();
           }
          }
         }
        delay(10000);
       
        __index = 0;
        break;
      }
    }   
    }

    PMS3003.rar

    2.06 KB, 下载次数: 2

    之前做过,有偿支援
    suoma 发表于 2017-9-10 20:16
    之前做过,有偿支援

    需要支援

    加QQ3455482668,详聊,帮你做
    jfvspig 来自手机 新手上路 2017-9-15 15:04
    5#
    我的 QQ:434236921
    jfvspig 来自手机 新手上路 2017-9-15 15:22
    6#
    suoma 发表于 2017-9-13 22:20
    加QQ3455482668,详聊,帮你做

    我的QQ: 434236921
    发新帖
    发表评论
    高级模式  
    您需要登录后才可以回帖 登录 | 立即注册  
    关闭

    推荐主题 上一条 /2 下一条