Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: no readings from air quality sensor 🐛 #327

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 68 additions & 23 deletions examples/KIT/PM25/PM25.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,31 @@ float humd = 0.0;
#define X_OFFSET 160
#define Y_OFFSET 23

uint16_t CheckSum;
uint16_t CheckCode;
#define DATA_LEN 32

#define OUT_BAUD 115200
#define SENSOR_BAUD 9600

#define FIRST_START_BIT 66
#define SECOND_START_BIT 77

#define FRONT 2

bool firstStartBitIsRead;
bool secondStartBitIsRead;

uint8_t serial_bytes[DATA_LEN] = {0};
uint8_t currentByte = 0;

void resetSerial() {
currentByte = 0;

memset(serial_bytes, 0, sizeof(serial_bytes) / sizeof(serial_bytes[0]) * sizeof(serial_bytes[0]));

firstStartBitIsRead = false;
secondStartBitIsRead = false;
}


// Print the header for a display screen
void header(const char *string, uint16_t color) {
Expand All @@ -28,30 +51,30 @@ void header(const char *string, uint16_t color) {
M5.Lcd.setTextDatum(TC_DATUM);
M5.Lcd.drawString(string, 160, 3, 4);
}

void setup() {
M5.begin();
Serial.begin(9600);
Serial2.begin(9600, SERIAL_8N1, 16, 17);
Serial.begin(OUT_BAUD);
Serial2.begin(SENSOR_BAUD, SERIAL_8N1, 16, 17);
pinMode(13, OUTPUT);
digitalWrite(13, 1);

M5.Lcd.fillScreen(TFT_BLACK);
header("P M 2.5", TFT_BLACK);
}

uint8_t Air_val[32] = {0};
int16_t p_val[16] = {0};
uint8_t i = 0;
resetSerial();
}

#define FRONT 2

void LCD_Display_Val(void) {
uint16_t p_val[16] = {0};

for (int i = 0, j = 0; i < 32; i++) {
if (i % 2 == 0) {
p_val[j] = Air_val[i];
p_val[j] = serial_bytes[i];
p_val[j] = p_val[j] << 8;
} else {
p_val[j] |= Air_val[i];
p_val[j] |= serial_bytes[i];
j++;
}
}
Expand Down Expand Up @@ -142,16 +165,18 @@ void LCD_Display_Val(void) {
M5.Lcd.setCursor(X_LOCAL + X_OFFSET, Y_LOCAL + Y_OFFSET * 7, FRONT);
M5.Lcd.print("10um : ");
M5.Lcd.print(p_val[13]);

resetSerial();
}

void TempHumRead(void) {
temp = 0, humd = 0;

if (sht30.get() == 0) { // Obtain the data of shT30. 获取sht30的数据
temp = sht30.cTemp; // Store the temperature obtained from shT30.
// 将sht30获取到的温度存储
// 将sht30获取到的温度存储
humd = sht30.humidity; // Store the humidity obtained from the SHT30.
// 将sht30获取到的湿度存储
} else {
temp = 0, humd = 0;
// 将sht30获取到的湿度存储
}

M5.Lcd.setTextColor(TFT_GREEN, TFT_BLACK);
Expand All @@ -168,17 +193,37 @@ void TempHumRead(void) {
M5.Lcd.print(humd);
}

void loop() {
if (Serial2.available()) {
Air_val[i] = Serial2.read();
Serial.write(Air_val[i]);
i++;
} else {
i = 0;
void readCurrentBit() {
uint8_t bit = Serial2.read();

if (!firstStartBitIsRead) {
if (bit != FIRST_START_BIT) {
resetSerial();
return;
}
firstStartBitIsRead = true;
} else if (!secondStartBitIsRead) {
if (bit != SECOND_START_BIT) {
resetSerial();
return;
}
secondStartBitIsRead = true;
}

if (i == DATA_LEN) {
if (currentByte < DATA_LEN) {
serial_bytes[currentByte] = bit;
}

currentByte++;
}

void loop() {
if (currentByte >= DATA_LEN) {
LCD_Display_Val();
TempHumRead();
}

if (Serial2.available()) {
readCurrentBit();
}
}