2019年6月18日 星期二

用wemos d1 mini 玩物聯網(7) PMS5003T 實作PM2.5 感測器

一、感測器介紹

PMS5003T是一款可以同時監測空氣中顆粒物濃度及溫濕度的二合一感測器。其中顆粒物濃度的監測基於鐳射散射原理,可連續採集並計算單位體積內空氣中不同粒徑的懸浮顆粒物個數,即顆粒物濃度分佈,進而換算成為質量濃度。


二、接線圖
VCC-> 5V
GND->GND
RXD->D6
TXD->D5 

三、程式碼


#include <Wire.h>
#include <esp_LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>

#include <SoftwareSerial.h>

const char* ssid = "HJN";    //wifi 帳號
const char* password = "11111111";// wifi 密碼
const char* safekey = "fhjh";//檢查碼
const uint16_t port = 80;           //上傳的主機port 
const char* host = "192.168.137.1"; //上傳的主機ip
const char* php_url = "/airbox/t2.php";//上傳在主機的程式
byte sensor_id = 1;           //偵測器ID

long  lPmcf10;
long  lPmcf25;
long  lPmcf100;
long  lTemperature;
long  lHumidity;
esp_LiquidCrystal_I2C mylcd(0x27, 16, 2);
SoftwareSerial pms5003t_Serial(D5, D6);

#define PMS5003T_PM10      1
#define PMS5003T_PM25      2
#define PMS5003T_PM100     3
#define PMS5003T_TEMPER    4
#define PMS5003T_HUMI      5
long pms_pmcf10 = 0;
long pms_pmcf25 = 0;
long pms_pmcf100 = 0;
long pms_pmat10 = 0;
long pms_pmat25 = 0;
long pms_pmat100 = 0;
long pms_Temperature = 0;
long pms_Humidity = 0;
int no = 0;


WiFiClient client;

void setup()
{
  Serial.begin(9600);
  mylcd.init();
  mylcd.backlight();

  pms5003t_Serial.begin(9600);

  lPmcf10 = 0;
  lPmcf25 = 0;
  lPmcf100 = 0;
  lTemperature = 0;
  lHumidity = 0;
  mylcd.clear();
  mylcd.setCursor(0, 0);
  mylcd.print("Yo Airbox");
  WiFi.begin(ssid, password);
  Serial.println("");

  int t = 0;
  mylcd.setCursor(0, 1);
  while (WiFi.status() != WL_CONNECTED) {
    t++;
    if (t > 15) break;

    Serial.print(".");
    mylcd.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  mylcd.setCursor(0, 1);
  mylcd.print(WiFi.localIP());
  delay(2000);
  mylcd.clear();
}


void loop()
{
  lPmcf10 = 0;
  lPmcf10 = pms5003t_read_(PMS5003T_PM10);
  Serial.println((String("PM1.0: ") + String(lPmcf10)));
  delay(500);
  lPmcf25 = pms5003t_read_(PMS5003T_PM25);
  Serial.println((String("PM2.5: ") + String(lPmcf25)));
  delay(500);
  lPmcf100 = pms5003t_read_(PMS5003T_PM100);
  Serial.println((String("PM10: ") + String(lPmcf100)));
  delay(500);
  lTemperature = pms5003t_read_(PMS5003T_TEMPER);
  Serial.println((String("Temperature: ") + String(lTemperature)));
  delay(500);
  lHumidity = pms5003t_read_(PMS5003T_HUMI);
  Serial.println((String("Humidity: ") + String(lHumidity)));
  delay(500);

  if (lPmcf10 > 0) {
    no++;
    String  things_request = "GET " + String(php_url);
    things_request += "?safekey=" + String(safekey);
    things_request += "&sensor_id=" + String(sensor_id);
    things_request += "&pm10=" + String(lPmcf10);
    things_request += "&pm25=" + String(lPmcf25);
    things_request += "&pm100=" + String(lPmcf100);
    things_request += "&tt=" + String(lTemperature);
    things_request += "&hh=" + String(lHumidity) + "\r\n\r\n";
    Serial.println(things_request);
    if (client.connect(host, port)) {
      client.print(things_request);
      Serial.println("Request: [ok]");
      client.stop();
    } else {
      Serial.println("Request: [error]");
    }




    if (client.connect("api.thingspeak.com", 80)) {
      String  things_request = "GET /update?api_key=Your_Write_API_Key&field1=" + String(lPmcf10) + "&field2=" + String(lPmcf25) + "&field3=" + String(lPmcf100) + "&field4=" + String(lTemperature) + "&field5=" + String(lHumidity) + "\r\n\r\n";
      // client.print(things_request);
      client.stop();
    }
    mylcd.clear();
    mylcd.setCursor(0, 0);
    mylcd.print(no);
    mylcd.setCursor(5, 0);
    mylcd.print(String(" PM2.5: ") + String(lPmcf25) );
    mylcd.setCursor(0, 1);
    mylcd.print(String("T:") + String(lTemperature) + String("C"));
    mylcd.setCursor(9, 1);
    mylcd.print(String("H:") + String(lHumidity) + String("%"));
    delay(5000);
  }

}

//G5t check
long pms5003t_read_(int datatype) {
  int count = 0;
  unsigned char c;
  unsigned char high;

  pms5003t_Serial.listen();
  while (pms5003t_Serial.available()) {
    c = pms5003t_Serial.read();
    if ((count == 0 && c != 0x42) || (count == 1 && c != 0x4d)) {
      Serial.println("pms5003t check failed");
      break;
    }
    if (count > 27) {
      Serial.println("pms5003t completed");
      break;
    }
    else if (count == 4 || count == 6 || count == 8 || count == 10 || count == 12 || count == 14 || count == 24 || count == 26) {
      high = c;
    }
    else if (count == 5) {
      pms_pmcf10 = 256 * high + c;
    }
    else if (count == 7) {
      pms_pmcf25 = 256 * high + c;
    }
    else if (count == 9) {
      pms_pmcf100 = 256 * high + c;
    }
    else if (count == 11) {
      pms_pmat10 = 256 * high + c;
    }
    else if (count == 13) {
      pms_pmat25 = 256 * high + c;
    }
    else if (count == 15) {
      pms_pmat100 = 256 * high + c;
    }
    else if (count == 25) {
      pms_Temperature = (256 * high + c) / 10;
    }
    else if (count == 27) {
      pms_Humidity = (256 * high + c) / 10;
    }
    count++;
  }
  while (pms5003t_Serial.available()) pms5003t_Serial.read();

  if (datatype == PMS5003T_PM10)
    return pms_pmcf10;
  else if (datatype == PMS5003T_PM25)
    return pms_pmcf25;
  else if (datatype == PMS5003T_PM100)
    return pms_pmcf100;
  else if (datatype == PMS5003T_TEMPER)
    return pms_Temperature;
  else if (datatype == PMS5003T_HUMI)
    return pms_Humidity;
}
四、執行結果

五、參考資料

沒有留言:

張貼留言