But before trying that can you please tell me whether it's correct to declare or read bytes like this in the code?
byte readCO[] = {0xFF, 0X86, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X7A}; //Command packet to read CO
byte response[] = {0,0,0,0,0,0,0,0,0}; //create an array to store the response
#include <SoftwareSerial.h>
#define RxPin 10
#define TxPin 11
SoftwareSerial softSerial = SoftwareSerial(RxPin, TxPin); //this is my virtual serial port
byte readCO[] = {0xFF, 0X86, 0X00, 0X08, 0X00, 0X00, 0X00, 0X00, 0X7A}; //Command packet to read CO
byte response[] = {0,0,0,0,0,0,0, 0, 0}; //create an array to store the response
int valMultiplier = 1;
void setup() {
pinMode(RxPin, INPUT);
pinMode(TxPin, OUTPUT);
softSerial.begin(9600);
Serial.begin(9600);
}
//void loop() {
//int data = softSerial.read();
//Serial.println(data);
// delay (1000);
//}
void loop()
{
sendRequest(readCO);
unsigned long valCO = getValue(response);
Serial.print("CO ppm = ");
Serial.println(valCO);
delay(2000);
}
void sendRequest(byte packet[])
{
while(!softSerial.available()) //keep sending request until we start to get a response
{
softSerial.write(readCO,9);
delay(50);
}
int timeout=0; //set a timeoute counter
while(softSerial.available() < 10 ) //Wait to get a 9 byte response
{
timeout++;
if(timeout > 10) //if it takes to long there was probably an error
{
while(softSerial.available()) //flush whatever we have
softSerial.read();
break; //exit and try again
}
delay(50);
}
for (int i=0; i < 8; i++)
{
response[i] = softSerial.read();
}
}
unsigned long getValue(byte packet[])
{
int high = packet[2]; //high byte for value is 4th byte in packet in the packet
int low = packet[3]; //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;
}
output:
CO ppm = 4294935296
CO ppm = 4294935552
CO ppm = 4294935552
CO ppm = 4294935552
CO ppm = 4294935552
CO ppm = 4294935552
CO ppm = 4294967295
CO ppm = 644
CO ppm = 4294936066
CO ppm = 4294967174
CO ppm = 4294964479
CO ppm = 244
CO ppm = 0
CO ppm = 0
CO ppm = 0
CO ppm = 4294935296
CO ppm = 644
CO ppm = 4294936066
CO ppm = 4294967174
CO ppm = 4294964479
CO ppm = 4294935552
Confused why I'm getting number like these?