Advertisement
Guest User

Libelium AirQ test code

a guest
Nov 28th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.00 KB | None | 0 0
  1. #include <WaspSensorGas_Pro.h>
  2. #include <BME280.h>
  3. #include <WaspOPC_N2.h>
  4.  
  5. #define MEASURE_TPH   1
  6. #define MEASURE_GASES 1
  7. #define MEASURE_PM    1
  8.  
  9. Gas O3(SOCKET_A);
  10. Gas CO(SOCKET_B);
  11. Gas NO2(SOCKET_C);
  12. Gas SO2(SOCKET_F);
  13.  
  14. float conc_o3, conc_co, conc_no2, conc_so2; // Stores the concentration level in ppm
  15. float temperature;  // Stores the temperature in ΒΊC
  16. float humidity;     // Stores the realitve humidity in %RH
  17. float pressure;     // Stores the pressure in Pa
  18.  
  19. void setup()
  20. {
  21.     USB.println(F("SmartEnv-PRO debug test"));
  22.  
  23.     // temperature, humidity, pressure
  24.     pinMode(GP_I2C_MAIN_EN, OUTPUT);
  25.     digitalWrite(GP_I2C_MAIN_EN, HIGH);
  26.     BME.ON();  
  27.    
  28.     // Power on the electrochemical sensor.
  29.     // If the gases PRO board is off, turn it on automatically.
  30.     O3.ON();
  31.     CO.ON();
  32.     NO2.ON(LMP91000_GAIN_5);
  33.     SO2.ON();
  34.    
  35.     // First sleep time
  36.     // After 10 minutes, Waspmote wakes up thanks to the RTC Alarm
  37.     PWR.deepSleep("00:00:10:00", RTC_OFFSET, RTC_ALM1_MODE1, ALL_ON);
  38. }  
  39.  
  40.  
  41. void loop()
  42. {
  43.     USB.println(F("***************************************"));
  44.     USB.print("Timestamp: ");
  45.     USB.println(RTC.getTime());
  46.     USB.print("Battery: ");
  47.     USB.print((int)PWR.getBatteryLevel());
  48.     USB.println(" %");
  49.  
  50.     if (MEASURE_TPH) {
  51.       // Read enviromental variables
  52.       temperature = BME.getTemperature(BME280_OVERSAMP_16X, BME280_FILTER_COEFF_OFF);
  53.       pressure = BME.getPressure(BME280_OVERSAMP_16X, BME280_FILTER_COEFF_OFF);
  54.       humidity = BME.getHumidity(BME280_OVERSAMP_16X);
  55.  
  56.       USB.print(F("Temperature: "));
  57.       USB.print(temperature);
  58.       USB.println(F(" Celsius degrees"));
  59.       USB.print(F("Pressure: "));
  60.       USB.print(pressure);
  61.       USB.println(F(" Pa"));
  62.       USB.print(F("RH: "));
  63.       USB.print(humidity);
  64.       USB.println(F(" %"));
  65.     }
  66.  
  67.     if (MEASURE_GASES) {
  68.       // Read the electrochemical sensor and compensate with the temperature internally
  69.       for (int i = 0; i < 3; i++) {
  70.         conc_co = CO.ppm2ugm3(CO.getConc());
  71.         if (conc_co != 0) break;
  72.       }
  73.       if (conc_co == 0) {
  74.         CO.OFF();
  75.         delay(1000);
  76.         CO.ON();
  77.       }
  78.       for (int i = 0; i < 3; i++) {
  79.         conc_so2 = SO2.ppm2ugm3(SO2.getConc());
  80.         if (conc_so2 != 0) break;
  81.       }
  82.       if (conc_so2 == 0) {
  83.         SO2.OFF();
  84.         delay(1000);
  85.         SO2.ON();
  86.       }
  87.       float conc_no2_ppm;
  88.       for (int i = 0; i < 3; i++) {
  89.         conc_no2_ppm = NO2.getConc((uint8_t)MCP3421_HIGH_RES);
  90.         conc_no2 = NO2.ppm2ugm3(conc_no2_ppm);
  91.         if (conc_no2 != 0) break;
  92.       }
  93.       if (conc_no2 == 0) {
  94.         NO2.OFF();
  95.         delay(1000);
  96.         NO2.ON();
  97.       }    
  98.       for (int i = 0; i < 3; i++) {
  99.         conc_o3 = O3.ppm2ugm3(O3.getConc((uint8_t)MCP3421_HIGH_RES, temperature, conc_no2_ppm));
  100.         if (conc_o3 != 0) break;
  101.       }
  102.       if (conc_o3 == 0) {
  103.         O3.OFF();
  104.         delay(1000);
  105.         O3.ON();
  106.       }
  107.  
  108.       USB.print(F("O3: "));
  109.       USB.print(conc_o3);
  110.       USB.println(F(" ug/m3"));
  111.       USB.print(F("CO: "));
  112.       USB.print(conc_co);
  113.       USB.println(F(" ug/m3"));
  114.       USB.print(F("NO2: "));
  115.       USB.print(conc_no2);
  116.       USB.println(F(" ug/m3"));
  117.       USB.print(F("SO2: "));
  118.       USB.print(conc_so2);
  119.       USB.println(F(" ug/m3"));
  120.     }
  121.  
  122.     // particle matter
  123.     if (MEASURE_PM) {
  124.       if (OPC_N2.ON()) {
  125.         if (OPC_N2.getPM(5000, 5000)) {
  126.           USB.print(F("PM1: "));
  127.           USB.print(OPC_N2._PM1);
  128.           USB.println(F(" ug/m3"));
  129.           USB.print(F("PM2.5: "));
  130.           USB.print(OPC_N2._PM2_5);
  131.           USB.println(F(" ug/m3"));
  132.           USB.print(F("PM10: "));
  133.           USB.print(OPC_N2._PM10);
  134.           USB.println(F(" ug/m3"));
  135.           OPC_N2.OFF();
  136.         } else {
  137.           USB.println("Error: Can't get measurements from particle matter sensor");
  138.         }
  139.       } else {
  140.         USB.println("Error: Can't start particle matter sensor");
  141.       }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement