Go Down

Topic: Interfacing CO2 sensor with Android phone (Read 1 time) previous topic - next topic

erwinreu

Jul 12, 2011, 10:02 pm Last Edit: Jul 13, 2011, 02:27 am by erwinreu Reason: 1
Hello, I am working on interfacing a CO2 sensor with an Android phone via Arduino. I'm using this toolkit called Amarino

http://www.amarino-toolkit.net/

So far I have the bluetooth module communicating with the arduino over serial and the phone can successfully connect to the bluetooth module. Now the CO2 sensor I plan on connecting to the arduino via serial as well, so after some reading I decided I needed to use a software serial implementation as my arduino the Duemilanove only has one serial.

Now communication with the co2 sensor involves a Master/Slave situation. I must send the CO2 sensor 8 hexadecimal bytes and then read seven hex bytes of which the 4th and fifth concatenated is the co2 concentration.

I have some code, but as this is my first Arduino and I just learned about serial communication I am unsure if I am going in the write direction code wise.

Code: [Select]
/*
 Sends sensor data to Android
*/

#include <MeetAndroid.h>

//Software Serial Start
// include the SoftwareSerial library so you can use its functions:
#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3
// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
byte pinState = 0;
byte inArray[8];
byte outArray[7];
//Software Serial End

MeetAndroid meetAndroid;




void setup()  
{
 //Software Serial Start
   // define pin modes for tx, rx, led pins:
 pinMode(rxPin, INPUT);
 pinMode(txPin, OUTPUT);
 // set the data rate for the SoftwareSerial port
 mySerial.begin(9600);
 //Software Serial End
 
// Bluetooth Module Baud rate
 Serial.begin(115200);
 //Software Serial Start
 inArray[0]=0xFE;
 inArray[1]=0x04;
 inArray[2]=0x00;
 inArray[3]=0x03;
 inArray[4]=0x00;
 inArray[5]=0x01;
 inArray[6]=0xD5;
 inArray[7]=0xC5;
}


void loop()
{
 meetAndroid.receive(); // you need to keep this in your loop() to receive events
 
 // print out the character:
 for (int i = 0; i<8; i+=1){
   mySerial.print(inArray[i], HEX);
 }  
   // listen for new serial coming in:
 for (int i=0; i<7; i++){
   outArray[i] = mySerial.read();
 }
 /* To be implemented: obtaining the CO2 readings.
  concatenate outArray[4] and outArray[5] and convert to
  decimal.
  */
 String ppm = "CO2 in ppm";
 //send result to Android
 meetAndroid.send(ppm);
 
 // add a little delay otherwise the phone is pretty busy
 delay(100);
}



I am quite new at this; any help is welcome and appreciated.

oscarcar

What CO2 sensor are you using?

It sounds similar to one that I was working with.

erwinreu

#2
Jul 13, 2011, 02:12 am Last Edit: Jul 13, 2011, 02:28 am by erwinreu Reason: 1
I'm using this sensor.

http://www.co2meter.com/collections/co2-sensors/products/k-30-co2-sensor-development-kit

I guess what I'm asking is: Is my communication over the Software Serial being done correctly?

robtillaart


You should use NewSoftSerial iso SoftSerial as it is far more stable.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

oscarcar

I haven't used NewSoftSerial yet. But I don't think you'll have much trouble.

erwinreu

Thanks for the advice; I'll look into it.

Andrew Robinson

Hello,

This is Andrew Robinson with CO2 Meter. I saw your post this morning.

I would recommend using I2C communication personally, it's a lot simpler and you'll be able to use the hardware I2C engine most likely.

Check out the simple Arduino application note, it's for the older platforms, but it should be directly relevant:
http://cdn.shopify.com/s/files/1/0019/5952/files/Senseair-Arduino.pdf?1264294173

Do let me know if you have any further questions! We love to help out people using our product in interesting ways.
Andrew Robinson
andrewbrobinson.com

Go Up