โมดูล PMS5003 เป็นโมดูลที่สามารถวัดค่าฝุ่นละอองในอากาศได้ทั้งหมด 3 ขนาด คือ pm1.0 pm2.5 และ pm10 ข้อมูลหน้าที่ตำแหน่งขาต่างๆ ดังรูป
ที่มาภาพดัดแปลงจาก : http://arduinosensor.tumblr.com/post/157168123230/pms5003-sensor-in-action
ขาสัญญาณ Nodemcu ต่อเข้ากับโมดูล PMS5003
ที่มาภาพดัดแปลงจาก : https://www.sgbotic.com/index.php?dispatch=products.view&product_id=2656 และ https://github.com/avaldebe/AQmon/issues/8
สำหรับการต่อกับบอร์ด Nodemcu ร่วมกับ OLED I2Cและโมดูล PMS5003 สามารถเชื่อมต่อได้ตามรูปและตารางด้านล่างครับ
ขาสัญญาณ Nodemcu ต่อเข้ากับโมดูล OLED ชนิด I2C
ตัวอย่าง 1. code การทำงานผ่าน Serial monitor ใน Arduino IDE
การใช้งานผ่าน Serial monitor
#include <Arduino.h>
#define LENG 31 //0x42 + 31 bytes equal to 32 bytes
unsigned char buf[LENG];
int PM01Value=0; //define PM1.0 value of the air detector module
int PM2_5Value=0; //define PM2.5 value of the air detector module
int PM10Value=0; //define PM10 value of the air detector module
void setup()
{
Serial.begin(9600);
Serial.setTimeout(1500);
}
void loop()
{
if(Serial.find(0x42)){ //start to read when detect 0x42
Serial.readBytes(buf,LENG);
if(buf[0] == 0x4d){
if(checkValue(buf,LENG)){
PM01Value=transmitPM01(buf); //count PM1.0 value of the air detector module
PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module
PM10Value=transmitPM10(buf); //count PM10 value of the air detector module
}
}
}
static unsigned long OledTimer=millis();
if (millis() - OledTimer >=1000)
{
OledTimer=millis();
Serial.print("PM1.0: ");
Serial.print(PM01Value);
Serial.println(" ug/m3");
Serial.print("PM2.5: ");
Serial.print(PM2_5Value);
Serial.println(" ug/m3");
Serial.print("PM1 0: ");
Serial.print(PM10Value);
Serial.println(" ug/m3");
Serial.println();
}
}
char checkValue(unsigned char *thebuf, char leng)
{
char receiveflag=0;
int receiveSum=0;
for(int i=0; i<(leng-2); i++){
receiveSum=receiveSum+thebuf[i];
}
receiveSum=receiveSum + 0x42;
if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the serial data
{
receiveSum = 0;
receiveflag = 1;
}
return receiveflag;
}
int transmitPM01(unsigned char *thebuf)
{
int PM01Val;
PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
return PM01Val;
}
//transmit PM Value to PC
int transmitPM2_5(unsigned char *thebuf)
{
int PM2_5Val;
PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
return PM2_5Val;
}
//transmit PM Value to PC
int transmitPM10(unsigned char *thebuf)
{
int PM10Val;
PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module
return PM10Val;
}
ตัวอย่าง 2. code การทำงานผ่าน Serial monitor และแสดงผลผ่านจอ OLED I2C ใน Arduino IDE
Code Serial monitor and OLED
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 OLED(-1);
#define LENG 31 //0x42 + 31 bytes equal to 32 bytes
unsigned char buf[LENG];
int PM01Value=0; //define PM1.0 value of the air detector module
int PM2_5Value=0; //define PM2.5 value of the air detector module
int PM10Value=0; //define PM10 value of the air detector module
void setup()
{
Serial.begin(9600);
Serial.setTimeout(1500);
OLED.begin(SSD1306_SWITCHCAPVCC,0x3C);
}
void loop()
{
if(Serial.find(0x42)){ //start to read when detect 0x42
Serial.readBytes(buf,LENG);
if(buf[0] == 0x4d){
if(checkValue(buf,LENG)){
PM01Value=transmitPM01(buf); //count PM1.0 value of the air detector module
PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module
PM10Value=transmitPM10(buf); //count PM10 value of the air detector module
}
}
}
static unsigned long OledTimer=millis();
if (millis() - OledTimer >=1000)
{
OledTimer=millis();
Serial.print("PM1.0: ");
Serial.print(PM01Value);
Serial.println(" ug/m3");
Serial.print("PM2.5: ");
Serial.print(PM2_5Value);
Serial.println(" ug/m3");
Serial.print("PM1 0: ");
Serial.print(PM10Value);
Serial.println(" ug/m3");
Serial.println();
OLED.setTextColor(WHITE,BLACK); //ตัวอักษรสีขาว ,ฉากหลังดำ
OLED.clearDisplay(); // ลบหน้าจอ
OLED.setTextSize(1); // ขนาดตัวอักษร
OLED.setCursor(0, 0);
OLED.println(" KLS Dust Sensor");
OLED.setTextSize(1); // ขนาดตัวอักษร
OLED.print(" PM1.0 = ");
OLED.print(PM01Value);
OLED.println(" ug/m3");
OLED.print(" PM2.5 = ");
OLED.print(PM2_5Value);
OLED.println(" ug/m3");
OLED.print(" PM10 = ");
OLED.print(PM10Value);
OLED.println(" ug/m3");
OLED.display(); // แสดงตัวอักษรทั้งหมด
}
}
char checkValue(unsigned char *thebuf, char leng)
{
char receiveflag=0;
int receiveSum=0;
for(int i=0; i<(leng-2); i++){
receiveSum=receiveSum+thebuf[i];
}
receiveSum=receiveSum + 0x42;
if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the serial data
{
receiveSum = 0;
receiveflag = 1;
}
return receiveflag;
}
int transmitPM01(unsigned char *thebuf)
{
int PM01Val;
PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
return PM01Val;
}
//transmit PM Value to PC
int transmitPM2_5(unsigned char *thebuf)
{
int PM2_5Val;
PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
return PM2_5Val;
}
//transmit PM Value to PC
int transmitPM10(unsigned char *thebuf)
{
int PM10Val;
PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module
return PM10Val;
}
ตัวอย่าง 3. code การทำงานผ่าน Serial monitor แสดงผลผ่านจอ OLED และการใช้งานร่วมกับ Appication Blynk
Code Serial Monitor - OLED-Blynk
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 OLED(-1);
////////////////////////////////////////////
#define LENG 31 //0x42 + 31 bytes equal to 32 bytes
unsigned char buf[LENG];
int PM01Value=0; //define PM1.0 value of the air detector module
int PM2_5Value=0; //define PM2.5 value of the air detector module
int PM10Value=0; //define PM10 value of the air detector module
///////////////////////////////////////////////
char auth[] = "askfjasasfdsaf"; // กรอก auth ที่ได้มาจาก e-mail
char ssid[] = "xxxxxxxxx"; // กรอกชื่อ wifi
char pass[] = "xxxxxxxxxx"; // กรอกรหัสผ่าน wifi
///////////////////////////////////////////////
BlynkTimer timer;
void sendSensor()
{
if(Serial.find(0x42)){ //start to read when detect 0x42
Serial.readBytes(buf,LENG);
if(buf[0] == 0x4d){
if(checkValue(buf,LENG)){
PM01Value=transmitPM01(buf); //count PM1.0 value of the air detector module
PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module
PM10Value=transmitPM10(buf); //count PM10 value of the air detector module
}
}
}
static unsigned long OledTimer=millis();
if (millis() - OledTimer >=1000)
{
OledTimer=millis();
Serial.print("PM1.0: ");
Serial.print(PM01Value);
Serial.println(" ug/m3");
Serial.print("PM2.5: ");
Serial.print(PM2_5Value);
Serial.println(" ug/m3");
Serial.print("PM1 0: ");
Serial.print(PM10Value);
Serial.println(" ug/m3");
Serial.println();
OLED.setTextColor(WHITE,BLACK); //ตัวอักษรสีขาว ,ฉากหลังดำ
OLED.clearDisplay(); // ลบหน้าจอ
OLED.setTextSize(1); // ขนาดตัวอักษร
OLED.setCursor(0, 0);
OLED.println(" KLS Dust Sensor");
OLED.setTextSize(1); // ขนาดตัวอักษร
OLED.print(" PM1.0 = ");
OLED.print(PM01Value);
OLED.println(" ug/m3");
OLED.print(" PM2.5 = ");
OLED.print(PM2_5Value);
OLED.println(" ug/m3");
OLED.print(" PM10 = ");
OLED.print(PM10Value);
OLED.println(" ug/m3");
OLED.display(); // แสดงตัวอักษรทั้งหมด
Blynk.virtualWrite(V0, PM01Value); // ทำ virtual ที่ v5 โดยรับค่าความชืน
Blynk.virtualWrite(V1, PM2_5Value); // ทำ virtual ที่ v6 โดยรับค่าความอุณหภูมิ
Blynk.virtualWrite(V2, PM10Value);
}
}
char checkValue(unsigned char *thebuf, char leng)
{
char receiveflag=0;
int receiveSum=0;
for(int i=0; i<(leng-2); i++){
receiveSum=receiveSum+thebuf[i];
}
receiveSum=receiveSum + 0x42;
if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the serial data
{
receiveSum = 0;
receiveflag = 1;
}
return receiveflag;
}
int transmitPM01(unsigned char *thebuf)
{
int PM01Val;
PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
return PM01Val;
}
//transmit PM Value to PC
int transmitPM2_5(unsigned char *thebuf)
{
int PM2_5Val;
PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
return PM2_5Val;
}
//transmit PM Value to PC
int transmitPM10(unsigned char *thebuf)
{
int PM10Val;
PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module
return PM10Val;
}
//}
void setup()
{
Serial.begin(9600);
Serial.setTimeout(1500);
OLED.begin(SSD1306_SWITCHCAPVCC,0x3C);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run();
}