|
#include <SoftwareSerial.h>
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI
#define mySerial Serial1
uint16_t pm25;
uint16_t pm10;
void pmRead() {
uint8_t data[] = {0xAA, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x67, 0xBB};
mySerial.write(data, 9);
delay(100);
for (int i = 0; i < 9; i++) {
if (mySerial.available()) {
data[i] = mySerial.read ();
}
}
if (data[0] == 0xAA && data[8] == 0xBB) {
pm25 = data[4] * 256 + data[5];
pm10 = data[2] * 256 + data[3];
}
}
void pmOn() {
uint8_t data[] = {0xAA, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x66, 0xBB};
mySerial.write(data, 9);
delay(1000);
}
void pmOff() {
uint8_t data[] = {0xAA, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x68, 0xBB};
mySerial.write(data, 9);
}
void draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_unifont);
u8g.setPrintPos(0, 20);
u8g.print("PM2.5 = ");
u8g.print(pm25);
u8g.setPrintPos(0, 60);
u8g.print("PM10 = ");
u8g.print(pm10);
}
void setup() {
// put your setup code here, to run once:
mySerial.begin(9600);
Serial.begin(115200);
pmOn();
delay(50);
}
void loop() {
// put your main code here, to run repeatedly:
pmRead();
u8g.firstPage();
do {
draw();
} while ( u8g.nextPage() );
delay(50);
}
|
|