I have two sensors Air Quality PMS5003 (RX/TX 20/21) and Pressure Sensor xgzp6897d (default i assume is SCL/SDA D7/D6 or GPIO7/6) connected to XIao ESP32C3. When I run the code as shown here it seems not to get to the pressure readings from the 7/6 as device is not responding. SO its not finding the pressure sensor but does not move on to get the air quality sensor readings. When i remove the pressure sensor part from the code it does give the air quality readings. Is the sdl/sca pin interfereing with rx/tx? I assume the scl/sda pins are set default, am i right? Is there a need to define the SCL/SDA pins in the code then how do i define them?
The code works fine with ESP32 Wroom.
#include <XGZP6897D.h>
#define K 8192 // see table above for the correct value for your sensor, according to the sensitivity.
XGZP6897D mysensor(K);
float pressure, temperature;
#include <SoftwareSerial.h>
SoftwareSerial pmsSerial(20, 21);
void setup() {
// our debugging output
Serial.begin(115200);
// sensor baud rate is 9600
pmsSerial.begin(115200);
while (!mysensor.begin()) // initialize and check the device
{
Serial.println("Device not responding.");
delay(500);
}
}
struct pms5003data {
uint16_t framelen;
uint16_t pm10_standard, pm25_standard, pm100_standard;
uint16_t pm10_env, pm25_env, pm100_env;
uint16_t particles_03um, particles_05um, particles_10um, particles_25um, particles_50um, particles_100um;
uint16_t unused;
uint16_t checksum;
};
struct pms5003data data;
void loop() {
pmsSerial.begin(115200);
if (readPMSdata(&pmsSerial)) {
Serial.println(data.pm10_standard);
Serial.println(data.pm25_standard);
Serial.println(data.pm100_standard);
Serial.println(data.particles_03um);
Serial.println(data.particles_05um);
Serial.println(data.particles_10um);
Serial.println(data.particles_25um);
Serial.println(data.particles_50um);
Serial.println(data.particles_100um);
}
if (mysensor.readSensor(temperature, pressure))
{
Serial.println(temperature);
Serial.println(pressure);
Serial.println();
}
else Serial.println("Reading fails. Timeout ??");
delay(100);
}
boolean readPMSdata(Stream *s) {
if (! s->available()) {
return false;
}
// Read a byte at a time until we get to the special '0x42' start-byte
if (s->peek() != 0x42) {
s->read();
return false;
}
// Now read all 32 bytes
if (s->available() < 32) {
return false;
}
uint8_t buffer[32];
uint16_t sum = 0;
s->readBytes(buffer, 32);
// get checksum ready
for (uint8_t i=0; i<30; i++) {
sum += buffer[i];
}
/* debugging
for (uint8_t i=2; i<32; i++) {
Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", ");
}
Serial.println();
*/
// The data comes in endian'd, this solves it so it works on all platforms
uint16_t buffer_u16[15];
for (uint8_t i=0; i<15; i++) {
buffer_u16[i] = buffer[2 + i*2 + 1];
buffer_u16[i] += (buffer[2 + i*2] << 8);
}
// put it into a nice struct :)
memcpy((void *)&data, (void *)buffer_u16, 30);
if (sum != data.checksum) {
Serial.println("Checksum failure");
return false;
}
// success!
return true;
}