Hi members
I am new here. I was working with particle Photon and MQ4 I2C gas sensor.
I have a normal I2C code for this sensor
#include <application.h>
#include <spark_wiring_i2c.h>
// ADC121C_MQ4 I2C address is 0x50(80)
#define Addr 0x50
int raw_adc = 0;
double ppm = 0.0;
void setup()
{
// Set variable
Particle.variable("i2cdevice", "ADC121C_MQ4");
Particle.variable("PPM", ppm);
// Initialise I2C communication as MASTER
Wire.begin();
// Initialise serial communication, set baud rate = 9600
Serial.begin(9600);
delay(300);
}
void loop()
{
unsigned int data[2];
// Start I2C transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write(0x00);
// Stop I2C transmission
Wire.endTransmission();
// Request 2 bytes of data
Wire.requestFrom(Addr, 2);
// Read 2 bytes of data
// raw_adc msb, raw_adc lsb
if (Wire.available() == 2)
{
data[0] = Wire.read();
data[1] = Wire.read();
}
delay(300);
// Convert the data to 12-bits
raw_adc = ((data[0] & 0x0F) * 256) + data[1];
ppm = (10000 / 4096.0) * raw_adc + 200.0;
// Output data to dashboard
Particle.publish("Methane concentration : ", String(ppm));
delay(1000);
}
So i am a noob in coding and dont know how to seperate this code into .cpp and .h files to get the clean code.
Please help me out, how this code can be converted into .cpp and .h files.
Thanx in advance.
@rjrajbir, one approach is to create an MQ4 Class object which you define in the .h file and provide the class code in the .cpp file. The class would have a constructor and one function for reading the sensor which is essentially most of the code in loop() and another for getting the calculated ppm value. The constructor would be empty. The begin() class function could do the Wire.begin() and set the private or public class raw_adc and ppm variables to zero. A read_raw() function would return an int and a get_ppm() function would return a float of the last read raw value.
Using a class us the basis of a proper Arduino/Particle library so there are plenty of examples to draw from. Give it a shot and come back for help as you progress.
thanx for your response @peekay123,i’ll try this…but in the meantime could you share some example of this
This example is a little “over complicated” but the idea behind is maintained 
You have to add all 3 files (MQ4.ino, MQ4.cpp, MQ4.h) to your Web IDE
Please note I didn’t tested this, is compiling without any errors but I don’t have MQ4 sensor.
MQ4.ino:
#include "MQ4.h"
MQ4 sensor;
double ppm_results = 0.0;
void setup(){
Particle.variable("i2cdevice", "ADC121C_MQ4");
Particle.variable("PPM", ppm_results);
Wire.begin();
Serial.begin(9600);
delay(300);
}
void loop(){
ppm_results = sensor.convert_To_ppm();
delay(300);
Particle.publish("Methane concentration : ", String(ppm_results));
delay(1000);
}
MQ4.cpp:
#include "MQ4.h"
#include "Particle.h"
int MQ4::getRawAdc(){
int raw = getRawMQ4Reading();
return raw;
}
double MQ4::convert_To_ppm(){
int raw_adc = getRawAdc();
double ppm = (10000 / 4096.0) * raw_adc + 200.0;
return ppm;
}
int MQ4::getRawMQ4Reading(){
// unsigned int data[2];
Wire.begin();
Wire.beginTransmission(address);
Wire.write(MQ4_Register);
byte status = Wire.endTransmission();
if(status != 0){
Serial.println("read MQ2 failed");
return 0;
}else{
//It worked
Wire.requestFrom(address, 2);
if (Wire.available() == 2){
byte msb = Wire.read();
byte lsb = Wire.read();
int reading = ((msb & 0x0F) *256)+lsb;
return reading;
}
}
}
MQ4.h:
class MQ4{
public:
int getRawAdc();
double convert_To_ppm();
private:
int address = 0x50;
int MQ4_Register = 0x00;
int getRawMQ4Reading();
};
Thank you so much @dreamER, I’ll try this out.
Thanx Buddy Its working fine.