Oui j'ai vu le MHZ-14 mais le prix me calme pas mal si j'utilise Mysensors outre le faite du DiY c'est pour le cout de revient .... et vu le prix de ce capteur cela perd un peut de son intérêt pour moi...
voici le ton code que j'ai modifié pour mon utilisation (d'autres capteurs viendrons peut être en plus avec le temps)
Code : Tout sélectionner
/*
Arduino Multiple Air Quality Sensors
connect the sensor as follows when standalone:
A H A >>> 5V
B >>> A0
H >>> GND
B >>> 10K/20K ohm >>> GND
Contribution: epierre
Based on David Gironi http://davidegironi.blogspot.fr/2014/01/cheap-co2-meter-using-mq135-sensor-with.html
License: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
*/
#include <SPI.h>
#include <MySensor.h>
#include <Wire.h>
#include <DHT.h>
#include <Adafruit_BMP085.h>
/************************Hardware Related Macros************************************/
//define which analog input channel you are going to use
#define MQ135_SENSOR (4)
#define HUMIDITY_SENSOR_DIGITAL_PIN (6)
#define PRESSURE_SENSOR_DIGITAL_PIN (14)
#define RL_VALUE (990) //define the load resistance on the board, in ohms
/***********************Software Related Macros************************************/
#define CALIBRATION_SAMPLE_TIMES (50) //define how many samples you are going to take in the calibration phase
#define CALIBRATION_SAMPLE_INTERVAL (500) //define the time interal(in milisecond) between each samples in the
//cablibration phase
#define READ_SAMPLE_INTERVAL (50) //define how many samples you are going to take in normal operation
#define READ_SAMPLE_TIMES (5) //define the time interal(in milisecond) between each samples in
/**********************Application Related Macros**********************************/
#define GAS_CO2 (0)
#define GAS_CO (1)
#define GAS_NH4 (2)
#define GAS_CH3 (3)
#define GAS_CH3_2CO (4)
#define GAS_C2H5OH (5) //Alcohol, Ethanol
#define GAS_CO_sec (6)
/*****************************Globals***********************************************/
float CO2Curve[2] = {113.7105289, -3.019713765}; //MQ135
float CO_secCurve[2] = {726.7809737, -4.040111669}; //MQ135
float NH4Curve[2] = {84.07117895, -4.41107687}; //MQ135
float C2H50H_Curve[2] = {74.77989144, 3.010328075}; //MQ135
float CH3Curve[2] = {47.01770503, -3.281901967}; //MQ135
float CH3_2COCurve[2] = {7.010800878, -2.122018939}; //MQ135
float Ro = 10000; //Ro is initialized to 10 kilo ohms
unsigned long SLEEP_TIME = 600; // Sleep time between reads (in seconds)
//VARIABLES
float Ro0 = 2.511; //MQ135 2.51 this has to be tuned 10K Ohm
float RL0 = 0.990; //MQ135 FC-22
int val = 0; // variable to store the value coming from the sensor
float calcVoltage = 0;
float dustDensity = 0;
boolean metric = true;
//DHT11
float lastTemp;
float lastHum;
//BMP085
float lastPressure = -1;
int lastForecast = -1;
char *weather[]={"stable","sunny","cloudy","unstable","thunderstorm","unknown"};
int minutes;
float pressureSamples[180];
int minuteCount = 0;
bool firstRound = true;
float pressureAvg[7];
float dP_dt;
//test
float a=0;
boolean pcReceived = false;
#define CHILD_ID_MQ135 0
#define CHILD_ID_HUM 1
#define CHILD_ID_TEMP 2
#define CHILD_ID_PRESSURE 3
#define CHILD_ID_FORECAST 4
DHT dht;
Adafruit_BMP085 bmp = Adafruit_BMP085(); // Digital Pressure Sensor
MySensor gw(48,49); // Arduino Mega initialization
MyMessage msg_mq135(CHILD_ID_MQ135, 44); //AqCO
MyMessage pcMsg_mq135(CHILD_ID_MQ135,V_VAR1);
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage pressureMsg(CHILD_ID_PRESSURE, V_PRESSURE);
MyMessage forecastMsg(CHILD_ID_FORECAST, V_FORECAST);
void setup()
{
gw.begin(incomingMessage);
/* gw.request(CHILD_ID_MQ2, V_VAR1);
gw.request(CHILD_ID_MQ6, V_VAR1);
gw.request(CHILD_ID_MQ131, V_VAR1);
gw.request(CHILD_ID_TGS2600, V_VAR1);
gw.request(CHILD_ID_MQ135, V_VAR1);
gw.request(CHILD_ID_2SH12, V_VAR1);
gw.request(CHILD_ID_TGS2602, V_VAR1);*/
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) { }
}
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("AIQ Multi Sensors", "1.0");
// Register all sensors to gateway (they will be created as child devices)
gw.present(CHILD_ID_MQ135, S_AIR_QUALITY);
gw.present(CHILD_ID_HUM, S_HUM);
gw.present(CHILD_ID_TEMP, S_TEMP);
gw.present(CHILD_ID_PRESSURE, S_BARO);
metric = gw.getConfig().isMetric;
// delay(50*1000); //delay to allow serial to fully print before sleep
Serial.print(" MQ135:");
Ro0 = MQCalibration(MQ135_SENSOR,10,RL0,CO_secCurve);
Serial.println(Ro0);
gw.send(pcMsg_mq135.set((long int)Ro0));
}
void loop()
{
//DHT11 Temp+Hum
delay(dht.getMinimumSamplingPeriod());
float temperature = dht.getTemperature();
if (isnan(temperature)) {
Serial.println("Failed reading temperature from DHT");
} else if (temperature != lastTemp) {
lastTemp = temperature;
if (!metric) {
temperature = dht.toFahrenheit(temperature);
}
gw.send(msgTemp.set(temperature, 1));
Serial.print("T: ");
Serial.println(temperature);
}
float humidity = dht.getHumidity();
if (isnan(humidity)) {
Serial.println("Failed reading humidity from DHT");
} else if (humidity != lastHum) {
lastHum = humidity;
gw.send(msgHum.set(humidity, 1));
Serial.print("H: ");
Serial.println(humidity);
}
//BMP085 Pressure
float pressure = bmp.readPressure()/100;
float altitude = bmp.readAltitude();
if (!metric) {
// Convert to fahrenheit
temperature = temperature * 9.0 / 5.0 + 32.0;
}
int forecast = sample(pressure);
if (pressure != lastPressure) {
gw.send(pressureMsg.set(pressure,0));
lastPressure = pressure;
}
if (forecast != lastForecast) {
gw.send(forecastMsg.set(weather[forecast]));
lastForecast = forecast;
}
//MQ135 CO NH4 CH3 CO2
Serial.print("MQ135 :");
Serial.print("CO2 :");
Serial.print(MQGetGasPercentage(MQRead(MQ135_SENSOR,RL0),Ro0,GAS_CO2,MQ135_SENSOR) );
Serial.print( "ppm" );
Serial.print(" ");
Serial.print("CO :");
Serial.print(MQGetGasPercentage(MQRead(MQ135_SENSOR,RL0),Ro0,GAS_CO,MQ135_SENSOR) );
Serial.print( "ppm" );
gw.send(msg_mq135.set((int)ceil(MQGetGasPercentage(MQRead(MQ135_SENSOR,RL0),Ro0,GAS_CO,MQ135_SENSOR))));
Serial.print(" ");
Serial.print("CH3 :");
Serial.print(MQGetGasPercentage(MQRead(MQ135_SENSOR,RL0),Ro0,GAS_CH3,MQ135_SENSOR) );
Serial.print( "ppm" );
Serial.print(" ");
Serial.print("NH4 :");
Serial.print(MQGetGasPercentage(MQRead(MQ135_SENSOR,RL0),Ro0,GAS_NH4,MQ135_SENSOR) );
Serial.print( "ppm" );
Serial.print("\n");
// Power down the radio. Note that the radio will get powered back up
// on the next write() call.
delay(SLEEP_TIME * 1000); //delay to allow serial to fully print before sleep
//gw.powerDown();
//sleep.pwrDownMode(); //set sleep mode
//gw.sleep(SLEEP_TIME * 1000); //sleep for: sleepTime
}
/****************** MQResistanceCalculation ****************************************
Input: raw_adc - raw value read from adc, which represents the voltage
Output: the calculated sensor resistance
Remarks: The sensor and the load resistor forms a voltage divider. Given the voltage
across the load resistor and its resistance, the resistance of the sensor
could be derived.
************************************************************************************/
float MQResistanceCalculation(int raw_adc,float rl_value)
{
// return ( ((float)rl_value*(1023-raw_adc)/raw_adc));
return (long)((long)(1024*1000*(long)rl_value)/raw_adc-(long)rl_value);
;
}
/***************************** MQCalibration ****************************************
Input: mq_pin - analog channel
Output: Ro of the sensor
Remarks: This function assumes that the sensor is in clean air. It use
MQResistanceCalculation to calculates the sensor resistance in clean air. .
************************************************************************************/
float MQCalibration(int mq_pin, double ppm, double rl_value,float *pcurve )
{
int i;
float val=0;
for (i=0;i<CALIBRATION_SAMPLE_TIMES;i++) { //take multiple samples
val += MQResistanceCalculation(analogRead(mq_pin),rl_value);
delay(CALIBRATION_SAMPLE_INTERVAL);
}
val = val/CALIBRATION_SAMPLE_TIMES; //calculate the average value
//Ro = Rs * sqrt(a/ppm, b) = Rs * exp( ln(a/ppm) / b )
return (long)val*exp((log(pcurve[0]/ppm)/pcurve[1]));
}
/***************************** MQRead *********************************************
Input: mq_pin - analog channel
Output: Rs of the sensor
Remarks: This function use MQResistanceCalculation to caculate the sensor resistenc (Rs).
The Rs changes as the sensor is in the different consentration of the target
gas. The sample times and the time interval between samples could be configured
by changing the definition of the macros.
************************************************************************************/
float MQRead(int mq_pin,float rl_value)
{
int i;
float rs=0;
for (i=0;i<READ_SAMPLE_TIMES;i++) {
rs += MQResistanceCalculation(analogRead(mq_pin),rl_value);
delay(READ_SAMPLE_INTERVAL);
}
rs = rs/READ_SAMPLE_TIMES;
return rs;
}
/***************************** MQGetGasPercentage **********************************
Input: rs_ro_ratio - Rs divided by Ro
gas_id - target gas type
Output: ppm of the target gas
Remarks: This function passes different curves to the MQGetPercentage function which
calculates the ppm (parts per million) of the target gas.
************************************************************************************/
int MQGetGasPercentage(float rs_ro_ratio, float ro, int gas_id, int sensor_id)
{
if (sensor_id == MQ135_SENSOR ){
if ( gas_id == GAS_CO2 ) {
return MQGetPercentage(rs_ro_ratio,ro,CO2Curve); //MQ135
} else if ( gas_id == GAS_NH4 ) {
return MQGetPercentage(rs_ro_ratio,ro,NH4Curve); //MQ135
} else if ( gas_id == GAS_C2H5OH ) {
return MQGetPercentage(rs_ro_ratio,ro,C2H50H_Curve); //MQ135
} else if ( gas_id == GAS_CH3 ) {
return MQGetPercentage(rs_ro_ratio,ro,CH3Curve); //MQ135
} else if ( gas_id == GAS_CH3_2CO ) {
return MQGetPercentage(rs_ro_ratio,ro,CH3_2COCurve); //MQ135
} else if ( gas_id == GAS_CO_sec ) {
return MQGetPercentage(rs_ro_ratio,ro,CO_secCurve); //MQ135
}
}
return 0;
}
/***************************** MQGetPercentage **********************************
Input: rs_ro_ratio - Rs divided by Ro
pcurve - pointer to the curve of the target gas
Output: ppm of the target gas
************************************************************************************/
int MQGetPercentage(float rs_ro_ratio, float ro, float *pcurve)
{
return (double)(pcurve[0] * pow(((double)rs_ro_ratio/ro), pcurve[1]));
}
/********************************** sample ***************************************
Input: pressure
Output: an int containing the weather based on pressure
************************************************************************************/
int sample(float pressure) {
// Algorithm found here
// http://www.freescale.com/files/sensors/doc/app_note/AN3914.pdf
if (minuteCount > 180)
minuteCount = 6;
pressureSamples[minuteCount] = pressure;
minuteCount++;
if (minuteCount == 5) {
// Avg pressure in first 5 min, value averaged from 0 to 5 min.
pressureAvg[0] = ((pressureSamples[1] + pressureSamples[2]
+ pressureSamples[3] + pressureSamples[4] + pressureSamples[5])
/ 5);
} else if (minuteCount == 35) {
// Avg pressure in 30 min, value averaged from 0 to 5 min.
pressureAvg[1] = ((pressureSamples[30] + pressureSamples[31]
+ pressureSamples[32] + pressureSamples[33]
+ pressureSamples[34]) / 5);
float change = (pressureAvg[1] - pressureAvg[0]);
if (firstRound) // first time initial 3 hour
dP_dt = ((65.0 / 1023.0) * 2 * change); // note this is for t = 0.5hour
else
dP_dt = (((65.0 / 1023.0) * change) / 1.5); // divide by 1.5 as this is the difference in time from 0 value.
} else if (minuteCount == 60) {
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
pressureAvg[2] = ((pressureSamples[55] + pressureSamples[56]
+ pressureSamples[57] + pressureSamples[58]
+ pressureSamples[59]) / 5);
float change = (pressureAvg[2] - pressureAvg[0]);
if (firstRound) //first time initial 3 hour
dP_dt = ((65.0 / 1023.0) * change); //note this is for t = 1 hour
else
dP_dt = (((65.0 / 1023.0) * change) / 2); //divide by 2 as this is the difference in time from 0 value
} else if (minuteCount == 95) {
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
pressureAvg[3] = ((pressureSamples[90] + pressureSamples[91]
+ pressureSamples[92] + pressureSamples[93]
+ pressureSamples[94]) / 5);
float change = (pressureAvg[3] - pressureAvg[0]);
if (firstRound) // first time initial 3 hour
dP_dt = (((65.0 / 1023.0) * change) / 1.5); // note this is for t = 1.5 hour
else
dP_dt = (((65.0 / 1023.0) * change) / 2.5); // divide by 2.5 as this is the difference in time from 0 value
} else if (minuteCount == 120) {
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
pressureAvg[4] = ((pressureSamples[115] + pressureSamples[116]
+ pressureSamples[117] + pressureSamples[118]
+ pressureSamples[119]) / 5);
float change = (pressureAvg[4] - pressureAvg[0]);
if (firstRound) // first time initial 3 hour
dP_dt = (((65.0 / 1023.0) * change) / 2); // note this is for t = 2 hour
else
dP_dt = (((65.0 / 1023.0) * change) / 3); // divide by 3 as this is the difference in time from 0 value
} else if (minuteCount == 155) {
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
pressureAvg[5] = ((pressureSamples[150] + pressureSamples[151]
+ pressureSamples[152] + pressureSamples[153]
+ pressureSamples[154]) / 5);
float change = (pressureAvg[5] - pressureAvg[0]);
if (firstRound) // first time initial 3 hour
dP_dt = (((65.0 / 1023.0) * change) / 2.5); // note this is for t = 2.5 hour
else
dP_dt = (((65.0 / 1023.0) * change) / 3.5); // divide by 3.5 as this is the difference in time from 0 value
} else if (minuteCount == 180) {
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
pressureAvg[6] = ((pressureSamples[175] + pressureSamples[176]
+ pressureSamples[177] + pressureSamples[178]
+ pressureSamples[179]) / 5);
float change = (pressureAvg[6] - pressureAvg[0]);
if (firstRound) // first time initial 3 hour
dP_dt = (((65.0 / 1023.0) * change) / 3); // note this is for t = 3 hour
else
dP_dt = (((65.0 / 1023.0) * change) / 4); // divide by 4 as this is the difference in time from 0 value
pressureAvg[0] = pressureAvg[5]; // Equating the pressure at 0 to the pressure at 2 hour after 3 hours have past.
firstRound = false; // flag to let you know that this is on the past 3 hour mark. Initialized to 0 outside main loop.
}
if (minuteCount < 35 && firstRound) //if time is less than 35 min on the first 3 hour interval.
return 5; // Unknown, more time needed
else if (dP_dt < (-0.25))
return 4; // Quickly falling LP, Thunderstorm, not stable
else if (dP_dt > 0.25)
return 3; // Quickly rising HP, not stable weather
else if ((dP_dt > (-0.25)) && (dP_dt < (-0.05)))
return 2; // Slowly falling Low Pressure System, stable rainy weather
else if ((dP_dt > 0.05) && (dP_dt < 0.25))
return 1; // Slowly rising HP stable good weather
else if ((dP_dt > (-0.05)) && (dP_dt < 0.05))
return 0; // Stable weather
else
return 5; // Unknown
}
void incomingMessage(const MyMessage &message) {
if (message.type==V_VAR1) {
long int pulseCount = message.getLong();
Serial.print("Received last pulse count from gw:");
Serial.println(pulseCount);
pcReceived = true;
}
}
quand je fait une vérification (compilation du code ) avec un nano ( pas un pro mini mais bien un nano avec l'usb et le 5 et 3,3v ) il me met ceci, tu pense que ça passerais ?
Désolé de toutes mes question mais je débute dans l'arduino ...