K30 CO2 sensor library

Hello!

I'm trying to use particle photon with k30 co2 sensor but I end up with the following error message when running the code (which works on arduino):

SoftwareSerial.cpp:41:27: fatal error: avr/interrupt.h: No such file or directory
#include <avr/interrupt.h>

which I suspect is based on the hardware-library compatibility. Any ideas how I should move on? I could either use an arduino and somehow connect it to photon I suppose and subsequently send the data through particle cloud...'

/kosmonaut

The Photon features a HW serial interface via the RX/TX pins and Serial1 or you need to import ParticleSoftSerial to give you one software interface.

Thanks! I added serial1 to the code - it works, but the values are returning 0. In the case of SoftwareSerial the ports are virtual i.e. port 12 to Rx and port 13 for Tx on Arduino. What would be the equivalent using Serial1 - how does it ‘know’ what port is which, or is it assumed when using Serial1?

Edit: I am now receiving results, but very unreliable. Either 0,0,0… 65280, 0,0,0,2560,0,0 etc

/*
  Basic Arduino example for K-Series sensor
  Created by Jason Berger
  Co2meter.com  
*/

/*
Edited by kosmonaut
*/

//SoftwareSerial K_30_Serial(rx,tx);  //Sets up a virtual serial port
                                    //Using pin 12 for Rx and pin 13 for Tx

byte readCO2[] = {0xFE, 0X44, 0X00, 0X08, 0X02, 0X9F, 0X25};  //Command packet to read Co2 (see app note)
byte response[] = {0,0,0,0,0,0,0};  //create an array to store the response

//multiplier for value. default is 1. set to 3 for K-30 3% and 10 for K-33 ICB
int valMultiplier = 1;


void setup() 
{
  // put your setup code here, to run once:
  Serial1.begin(9600);         //Opens the main serial port to communicate with the computer
  //K_30_Serial.begin(9600);    //Opens the virtual serial port with a baud of 9600
}

void loop() 
{
  sendRequest(readCO2);
  unsigned long valCO2 = getValue(response);
  Serial1.print("Co2 ppm = ");
  Serial1.println(valCO2);
  delay(2000);
  
  String str_co2 = String(valCO2);   
  Particle.publish("co2", str_co2);
  
}

void sendRequest(byte packet[])
{
  while(!Serial1.available())  //keep sending request until we start to get a response
  {
    Serial1.write(readCO2,7);
    delay(50);
  }
  
  int timeout=0;  //set a timeoute counter
  while(Serial1.available() < 7 ) //Wait to get a 7 byte response
  {
    timeout++;  
    if(timeout > 10)    //if it takes to long there was probably an error
      {
        while(Serial1.available())  //flush whatever we have
          Serial1.read();
          
          break;                        //exit and try again
      }
      delay(50);
  }
  
  for (int i=0; i < 7; i++)
  {
    response[i] = Serial1.read();
  }  
}

unsigned long getValue(byte packet[])
{
    int high = packet[3];                        //high byte for value is 4th byte in packet in the packet
    int low = packet[4];                         //low byte for value is 5th byte in the packet

  
    unsigned long val = high*256 + low;                //Combine high byte and low byte with this formula to get value
        
    
    return val* valMultiplier;         

}

Serial1 is hard wired to the pins labeld RX and TX, so there is no need to tell the device what it already knows.

But why are you also writing some text to the same port?

  Serial1.print("Co2 ppm = ");
  Serial1.println(valCO2);

I guess you want that text sent to USB Serial, but that would be Serial not Serial1.
Have another look at the link I provided above for Serial1 (it also talks about Serial and the differences).

Your setup should be

void setup() 
{
  // put your setup code here, to run once:
  Serial.begin(115200);   // Opens the main serial port to communicate with the computer
  Serial1.begin(9600);    // Opens the virtual serial port with a baud of 9600
}

And in the rest of your code use the respective interfaces.

Interestingly, when using:

..
void loop() 
{
  sendRequest(readCO2);
  unsigned long valCO2 = getValue(response);
  delay(10000);  
  String str_co2 = String(valCO2);   
  Particle.publish("co2", str_co2);
}
..

… the values are returning only 0. But when using:

..
void loop() 
{
  sendRequest(readCO2);
  delay(10000);  
  String str_co2 = String(getValue(response));   
  Particle.publish("co2", str_co2);
}
..

the values are returning 0 and with varying numbers such as 65280, 57600, 43520, 64512, 2560, 255

The problem came from the wiring! Normal temporary pins (prototype with breadboard) did not work - soldering was required!