How do I code PMSA003i with a virtual pin?

I’m using and adafruit feather huzzah32 as a bluetooth module and while I can get it to connect to the blynk app, I have no idea how to write the sensor data to a virtual pin since that’s the only way to communicate with an I2C connection. Can anyone help me modify this to make it send data to a virtual pin?

#include "Adafruit_PM25AQI.h"

Adafruit_PM25AQI aqi = Adafruit_PM25AQI();

void setup() {
  // Wait for serial monitor to open
  Serial.begin(115200);
  while (!Serial) delay(10);

  Serial.println("Adafruit PMSA003I Air Quality Sensor");

  // Wait one second for sensor to boot up!
  delay(1000);

  // If using serial, initialize it and set baudrate before starting!
  // Uncomment one of the following
  //Serial1.begin(9600);
  //pmSerial.begin(9600);

  // There are 3 options for connectivity!
  if (! aqi.begin_I2C()) {      // connect to the sensor over I2C
  //if (! aqi.begin_UART(&Serial1)) { // connect to the sensor over hardware serial
  //if (! aqi.begin_UART(&pmSerial)) { // connect to the sensor over software serial 
    Serial.println("Could not find PM 2.5 sensor!");
    while (1) delay(10);
  }

  Serial.println("PM25 found!");
}

void loop() {
  PM25_AQI_Data data;
  
  if (! aqi.read(&data)) {
    Serial.println("Could not read from AQI");
    delay(500);  // try again in a bit!
    return;
  }
  Serial.println("AQI reading success");

  Serial.println();
  Serial.println(F("---------------------------------------"));
  Serial.println(F("Concentration Units (standard)"));
  Serial.println(F("---------------------------------------"));
  Serial.print(F("PM 1.0: ")); Serial.print(data.pm10_standard);
  Serial.print(F("\t\tPM 2.5: ")); Serial.print(data.pm25_standard);
  Serial.print(F("\t\tPM 10: ")); Serial.println(data.pm100_standard);
  Serial.println(F("Concentration Units (environmental)"));
  Serial.println(F("---------------------------------------"));
  Serial.print(F("PM 1.0: ")); Serial.print(data.pm10_env);
  Serial.print(F("\t\tPM 2.5: ")); Serial.print(data.pm25_env);
  Serial.print(F("\t\tPM 10: ")); Serial.println(data.pm100_env);
  Serial.println(F("---------------------------------------"));
  Serial.print(F("Particles > 0.3um / 0.1L air:")); Serial.println(data.particles_03um);
  Serial.print(F("Particles > 0.5um / 0.1L air:")); Serial.println(data.particles_05um);
  Serial.print(F("Particles > 1.0um / 0.1L air:")); Serial.println(data.particles_10um);
  Serial.print(F("Particles > 2.5um / 0.1L air:")); Serial.println(data.particles_25um);
  Serial.print(F("Particles > 5.0um / 0.1L air:")); Serial.println(data.particles_50um);
  Serial.print(F("Particles > 10 um / 0.1L air:")); Serial.println(data.particles_100um);
  Serial.println(F("---------------------------------------"));
  

  delay(1000);
}

You need to start by cleaning-up your void loop and getting rid of the delay and replacing it with a BlynkTimer.

Then you need to read the documentation on the Blynk.virtualWrite(vPin) command to send the data to the Blynk app.

BTW, Bluetooth is an awful connection method for this type of thing, and you probably won’t get the functionality you’re hoping for.

Pete.

Yeah thanks. I can use wifi, no big deal. I read the documentation. I guess I need to figure out if I need to set up 3 parameters since the sensor checks three different particle sizes and I wanted to find out how to write the function properly. The sketch I uploaded was from Arduino. I just was wondering how the code should look when sending the data that this sketch reads, in the form of a blynk function.

Think of the Blynk.virtualWrite(vPin) command as being the same as a serial print comm@nd, just that it “prints” the data to Blynk.

Take a look at the Sketch Builder examples for something like the DHT sensor, it will give you a good idea of the code structure and how to send data (humidity and temperature in the example) to different virtual pins.

Pete.

Thank you very much, Pete. That made a big difference. For some reason I couldn’t get that through my thick skull. The sketch builder filled in some gaps too. Much appreciated.

1 Like