Schlagwort-Archive: CO2

Sensoren4Sketch

  • 0.8 Verwendung der korrigierten Lib von RSTOIKA
  • Der R0 Wert kann automatisch zum Setzen in FHEM verwendet werde. Hiermit wird ein Button oben angezeigt. attr CO2_SZ setReading_R0 textField<br>
  • Einbrennzeit und Stabilisierung abwarten
  • Kalibrierung im Freien bei Windstille
  • Redesign MySensors 2.0
  • Grundwert R0Cor wird gesetzt, wenn CO2 < 390
  • Spezielle Lib ist für den Kalibierung – Button notwendig
  • EEPROM Speicherung R0Cor

MQ135lib.tar

//Sens4Sketch (Motion, Temperature, Humidity, CO2)

//v0.8 Corr. Lib von rstoica (set r0, correction) impl
//v0.7 set r0 to eeprom
//v0.6 SetR0-Button
//v0.5 Motion
//v0.4 autosetr0 if co2<400
//v0.3 api 2.0, redesign
//v0.2 CorrectedRZero basis for calculating r0
//G. Krocker K.-H. Wind et. al. ++++JH
//Caution: The Sketch erorder an amended mq135.h Library

//#define MY_DEBUG    // Enables debug messages in the serial log
#define MY_RADIO_NRF24
#include <SPI.h>
#include <MySensors.h>
#include <DHT.h>
#include <MQ135.h>
//----------------------------------------------------------------------------
// Timer
unsigned long SLEEP_TIME = 15*1000; // Sleep time between reads (in milliseconds)
//-----------------------------------------------------------------------------
//MOTION
#define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
#define INTERRUPT DIGITAL_INPUT_SENSOR-3 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define CHILD_ID_MOTION 8   // Id of the sensor child
MyMessage msgMotion(CHILD_ID_MOTION, S_MOTION);
//-----------------------------------------------------------------------------
// DHT22
#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1
#define HUMIDITY_SENSOR_DIGITAL_PIN 4
DHT dht;
float lastTemp;
float lastHum;
boolean metric = true; 
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
//-----------------------------------------------------------------------------
// MQ135
#define CHILD_ID_CO2C 24
#define CHILD_ID_CO2 25
#define CHILD_ID_R0C 26
#define CHILD_ID_R0 27
#define EEPROM_R0C 0
int C02;
int CO2C;
float R0;
float R0C;
float R02;
MQ135 gasSensor = MQ135(0);//PIN0 
MyMessage msgCO2C(CHILD_ID_CO2C, V_VAR1);
MyMessage msgCO2(CHILD_ID_CO2, V_VAR1);
MyMessage msgR0C(CHILD_ID_R0C, V_VAR1);
MyMessage msgR0(CHILD_ID_R0, V_VAR1);
//-----------------------------------------------------------------------------
void setup() {
//--------------------------------------------------------------------- 
 //R0C aus EEPROM sonst 55
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
pinMode(DIGITAL_INPUT_SENSOR, INPUT);
uint8_t R02 = loadState(EEPROM_R0C);
// get R0C from EEPROM
float R0C = R02 * 2;
if (R0C > 1.0 && R0C < 500.0)
{
Serial.print(F("Setting R0 from EEPROM: "));
}
else
{
Serial.print(F("Setting default R0C: "));
R0C = 50;
}
Serial.print(R0C);
Serial.println(F(""));
gasSensor.setRZero(R0C);
}
//-----------------------------------------------------------------------------
void presentation(){
sendSketchInfo("Sens4", "v0.7");
present(CHILD_ID_TEMP, S_TEMP);
present(CHILD_ID_HUM, S_HUM);
present(CHILD_ID_CO2C, S_CUSTOM);
present(CHILD_ID_CO2, S_CUSTOM);
present(CHILD_ID_R0C, S_CUSTOM);
present(CHILD_ID_R0, S_CUSTOM);
present(CHILD_ID_MOTION, S_MOTION);
}
//-----------------------------------------------------------------------------



void loop(){



//-----------------------------------------------------------------------------
//TEMP_FEUCHTE 

wait(dht.getMinimumSamplingPeriod());// TheoL
  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);
    }
   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;
      send(msgHum.set(humidity, 1));
      Serial.print("H: ");
      Serial.println(humidity);
  }
//-----------------------------------------------------------------------------  
//CO2
  float R0 = gasSensor.getRZero();
  float R0C = gasSensor.getCorrectedRZero(temperature, humidity);
  float CO2 = gasSensor.getPPM();
  float CO2C = gasSensor.getCorrectedPPM(temperature, humidity);
  
  Serial.print("R0: "); 
  Serial.println(R0);
  send(msgR0.set(R0,1));

  Serial.print("R0C: ");
  Serial.println(R0C);
  send(msgR0C.set(R0C,1));

  Serial.print("CO2: ");
  Serial.println(CO2);
  send(msgCO2.set(CO2,1));

  Serial.print("CO2C: ");
  Serial.println(CO2C);
  send(msgCO2C.set(CO2C,1));
  
  Serial.println();
// Grundwert CO2 < 400 ppm dann setze R0C in Lib
if (CO2C < 420)
{
  saveState(EEPROM_R0C, (uint8_t)(R0C/2));
  gasSensor.setRZero(R0C);
  send(msgR0C.set(R0C, 2));
  Serial.print(F("CO2 < 420: R0 gesetzt "));
}
//-----------------------------------------------------------------------------
//Read digital motion value
boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
Serial.println(tripped);
send(msgMotion.set(tripped?"1":"0"));  
//-----------------------------------------------------------------------------
wait(SLEEP_TIME); //sleep for: sleepTime // byTheo: Changed
}
//-----------------------------------------------------------------------------
//incomingMessage
void receive(const MyMessage& message){
 Serial.println(F("Incoming Message:"));

if (message.isAck())
{
Serial.println(F("This is an ack from gateway"));
}

uint8_t sensor = message.sensor;
if (sensor == CHILD_ID_R0C)
{
float R0C = message.getFloat();

Serial.print(F("Incoming R0C: "));
Serial.print(R0C);
Serial.println(F(""));

saveState(EEPROM_R0C, (uint8_t)(R0C/2));
gasSensor.setRZero(R0C);
send(msgR0.set(R0C, 2));
}
}
//-----------------------------------------------------------------------------

 

MQ135.h Beispiel Sketch zum Verstehen der Bibliothek für die CO2 Messung

Anbei ein kleiner Sketch zum Verstehen des Bibliothek vom G. Krocker zur Anzeige der relevanten Werte des MQ135 und Verwendung der Temperatur-Kompensation. Der MQ135 Sensor wird verwendet um die Luftgüte, hier speziell das CO2 zu messen. Mit den Werten ist eine Eichung des Sensors auf den mittleren CO2 Wert in der Atmosphäre (ca. 400 ppm) möglich (rzeroc). Einen kompletten Sketch findet man hier Vier Sensoren Sketch

Hilfreich:

https://www.grower.ch/forum/threads/wir-basteln-ein-arduino-co2-messgeraet.89083/

https://hackaday.io/project/3475-sniffing-trinket

 

#include "MQ135.h"
#include <DHT.h>  

#define HUMIDITY_SENSOR_DIGITAL_PIN 4 // DHT22 an PIN4

MQ135 gasSensor = MQ135(0); //GAS Sensor PIN A0

DHT dht;



void setup() {
   Serial.begin(115200);
    dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
    
}

void loop() {
{
  float h = dht.getHumidity();
  float t = dht.getTemperature();
 
  float rzero = gasSensor.getRZero();
  float rzeroc = gasSensor.getCorrectedRZero( t, h);
   
  float ppm = gasSensor.getPPM();
  float ppmc = gasSensor.getCorrectedPPM(t, h);
 
  Serial.println(t);
  Serial.println(h);   
  Serial.println(rzero);
  Serial.println(rzeroc);
  Serial.println(ppm);
  Serial.println(ppmc);
  Serial.println();

  delay(3000);
 
}
}

 

 

Smarthome Arduino MYSENSORS FHEM Sketch CO2 Bewegung Temperatur Feuchte

Allegmein

Der Sketch stammt von Karl-Heinz Wind und wurde von mir angepasst. Er enthält die Berechnung und Anzeige der Sensoren für CO2 (MQ135), Feuchte, Temperatur und Bewegung. Es wurden ausschließlich typische MYSENSORS-Bauteile verwendet. Sollten bestimmte Bibliotheken fehlen, kann ich die gerne noch bereitstellen.

 

Sketch

#include <SPI.h>
#include <MySensor.h>

#include <Wire.h>

#include <DHT.h>
#include <MQ135.h>

#include „Timer.h“

//—————————————————————————-
//unsigned long SLEEP_TIME = 10000; //Sleep time between reports (in milliseconds)
#define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
#define INTERRUPT DIGITAL_INPUT_SENSOR-3 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define CHILD_ID 1   // Id of the sensor child

//—————————————————————————–
// Timer
Timer timer;
#define TEMP_UPDATE_INTERVAL 30000

//—————————————————————————–
// DHT22
#define CHILD_ID_TEMP 2
#define CHILD_ID_HUM 3
#define HUMIDITY_SENSOR_DIGITAL_PIN 4

DHT dht;
float lastTemp;
float lastHum;
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);

//—————————————————————————–
// MQ135
#define CHILD_ID_CO2_CORRECTED 0
#define CHILD_ID_CO2 1
#define CHILD_ID_R0 4
#define CO2_SENSOR_ANALOG_PIN 0
#define CO2_SENSOR_BUFFER_SIZE 3

/// Calibration resistance at atmospheric CO2 level
// Buero 21Grad Regen
#define RZERO 300.0
#define EEPROM_R0 0

MQ135 gasSensor = MQ135(CO2_SENSOR_ANALOG_PIN, RZERO);
int lastC02;
int lastCO2Corrected;
float lastR0;

//RunningAverage lastCO2Values(CO2_SENSOR_BUFFER_SIZE);

//—————————————————————————–
// MySensor
MySensor gw;
MyMessage msgCO2Corrected(CHILD_ID_CO2_CORRECTED, V_VAR1);
MyMessage msgCO2(CHILD_ID_CO2, V_VAR1);
MyMessage msgR0(CHILD_ID_R0, V_VAR1);
MyMessage msg(CHILD_ID, V_TRIPPED);

//—————————————————————————–
void setup()
{
Serial.begin(115200);
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);

gw.begin(incomingMessage, AUTO, true);
gw.sendSketchInfo(„CO2 Sensor MQ-135“, „1.3“);
gw.present(CHILD_ID_CO2_CORRECTED, S_AIR_QUALITY);
gw.present(CHILD_ID_CO2, S_AIR_QUALITY);
gw.present(CHILD_ID_TEMP, S_TEMP);
gw.present(CHILD_ID_HUM, S_HUM);
gw.present(CHILD_ID_R0, S_CUSTOM);

gw.sendSketchInfo(„Motion Sensor“, „1.0“);
pinMode(DIGITAL_INPUT_SENSOR, INPUT);
gw.present(CHILD_ID, S_MOTION);

uint8_t R02 = gw.loadState(EEPROM_R0);

// get R0 from EEPROM
float R0 = R02 * 2;
if (R0 > 1.0 && R0 < 400.0)
{
Serial.print(F(„Setting R0 from EEPROM: „));
}
else
{
Serial.print(F(„Setting default R0: „));
R0 = RZERO;
}

Serial.print(R0);
Serial.println(F(„“));

gasSensor.setR0(R0);
//float ppm = gasSensor.getPPM();
//lastCO2Values.fillValue(ppm, CO2_SENSOR_BUFFER_SIZE);

//int tickEvent1 =
timer.every(TEMP_UPDATE_INTERVAL, timerHandler);

}

bool DHT22Changed(bool waitMinimumSamplingPeriod = true)
{
bool changed = false;

if (waitMinimumSamplingPeriod)
{
delay(dht.getMinimumSamplingPeriod());
}

float temperature = dht.getTemperature();
if (isnan(temperature))
{
Serial.println(F(„Failed reading temperature from DHT“));
}
else if (temperature != lastTemp)
{
lastTemp = temperature;
Serial.print(„T: „);
Serial.println(temperature);
changed = true;
}

float humidity = dht.getHumidity();
if (isnan(humidity))
{
Serial.println(F(„Failed reading humidity from DHT“));
}
else if (humidity != lastHum)
{
lastHum = humidity;
Serial.print(F(„H: „));
Serial.println(humidity);
changed = true;
}

return changed;
}

bool MQ135Changed(float t, float h)
{
bool changed = false;

lastR0 = gasSensor.getRZero();
Serial.print(F(„R0: „));
Serial.println(lastR0);

{
float ppm = gasSensor.getPPM();

Serial.print(F(„CO2 ppm: „));
Serial.print(ppm);

//lastCO2Values.addValue(ppm);
//ppm = lastCO2Values.getAverage();

//Serial.print(“ average: „);
//Serial.print(ppm);

int roundedPpm = (int)ppm;
Serial.print(F(“ –> „));
Serial.println(roundedPpm);

if (roundedPpm != lastC02)
{
lastC02 = roundedPpm;
changed = true;
}
}

{
float ppm = gasSensor.getCorrectedPPM(t, h);

Serial.print(F(„CO2 corrected ppm: „));
Serial.print(ppm);

int roundedPpm = (int)ppm;
Serial.print(F(“ –> „));
Serial.println(roundedPpm);

if (roundedPpm != lastCO2Corrected)
{
lastCO2Corrected = roundedPpm;
changed = true;
}
}
// Read digital motion value
boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;

Serial.println(tripped);
gw.send(msg.set(tripped?“1″:“0“));  // Send tripped value to gw

// Sleep until interrupt comes in on motion sensor. Send update every two minute.
//gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);

return changed;
}

void timerHandler()
{
bool humidityChanged = DHT22Changed(false);
bool airQualityChanged = MQ135Changed(lastTemp, lastHum);

if (humidityChanged || airQualityChanged)
{
gw.send(msgTemp.set(lastTemp, 1));
gw.send(msgHum.set(lastHum, 1));
gw.send(msgCO2Corrected.set(lastCO2Corrected));
gw.send(msgCO2.set(lastC02));
gw.send(msgR0.set(lastR0, 2));
}
}

void loop()
{
gw.process();

timer.update();

}

void incomingMessage(const MyMessage& message)
{
Serial.println(F(„Incoming Message:“));

if (message.isAck())
{
Serial.println(F(„This is an ack from gateway“));
}

uint8_t sensor = message.sensor;
if (sensor == CHILD_ID_R0)
{
float R0 = message.getFloat();

Serial.print(F(„Incoming R0: „));
Serial.print(R0);
Serial.println(F(„“));

gw.saveState(EEPROM_R0, (uint8_t)(R0/2));
gasSensor.setR0(R0);
gw.send(msgR0.set(R0, 2));
}
}

Programm für die Raumluftgüte MQ135 MySensors FHEM

Ein Programm das Messen und Abbilden der Raumluftgüte mit dem MQ135 auf dem ARDUINO mit MYSENSORS nach FHEM

Auf dem Weg zum Bau von multiplen Raumsensoren mit FHEM und MYSENSORS war die erste Aufgabe den Programm-Code der CO²-Raumwerte zu entwickeln. Ziel ist die Daten von MYSENSORS nach FHEM zu übertragen, damit diese darstellungskonform im WEBUI abgebildet und weiter verwendet werden können. Es gibt jede Menge Programm-Schnipsel aber leider kein fertiges Beispiel, welches mich zufrieden stellte. Die plausibelsten Werte erzeugt die MQ135.h Bibliothek auf https://github.com/GeorgK/MQ135 . Hierfür habe ich dann noch den Code aus FHEM/MYSENSORS angepasst.

Wichtig ist dass der Sensor 24 Stunden „eingebrannt“ wird und anschließend bei ca. 20 Grad 35 % Feuchte außen kalibriert wird. Ich habe dazu in der Bibliothek den RZERO Wert solange verändert bis der Wert plausibel war.

On the way to the construction of multiple room sensors with FHEM and MYSENSORS the first task was to develop the programm code CO² space values. The aim is to transfer the data from MYSENSORS after FHEM, so they can be displayed in the WebUI representation compliant and reused. There is plenty of programm snippets but no complete example, which introduced me satisfied. The most plausible values produces the MQ135.h library https://github.com/GeorgK/MQ135. For this, I still have adapted the code from FHEM / MYSENSORS.
 It is important that the sensor is „burned“ 24 Hours will be followed at 20 degree 35% humidity is outside calibrated. I have this in the library the RZERO value unless changed until the value was plausible.

/*
Arduino MQ135

Anpassung für Bibliothek MQ135.h und MYSENSORS / FHEM von jotthaeff 25.12.2015
Contribution: epierre
Based on David Gironi http://davidegironi.blogspot.fr/2014/01/cheap-co2-meter-using-mq135-sensor-with.html
http://skylink.dl.sourceforge.net/project/davidegironi/avr-lib/avr_lib_mq135_01.zip

*/
#include "MQ135.h"
#include "SPI.h"
#include "MySensor.h"
#include "Wire.h"

#define CHILD_ID_AIQ 0
#define ANALOGPIN 0

float valAIQ =0.0;
float lastAIQ =0.0;

// Initialize the gas Sensor
MQ135 gasSensor = MQ135(ANALOGPIN);

MySensor gw;
MyMessage msg(CHILD_ID_AIQ, V_LEVEL);

void setup()
{
//Serial.begin(9600);
gw.begin();

// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("AIQ Sensor MQ135", "1.0");

// Register all sensors to gateway (they will be created as child devices)
gw.present(CHILD_ID_AIQ, S_AIR_QUALITY);

}

void loop()     {

float ppm = gasSensor.getPPM();
Serial.print(" CO2: ");
Serial.print(ppm);
Serial.println(" ppm ");

valAIQ=ppm;

if (valAIQ != lastAIQ) {
gw.send(msg.set((int)ceil(valAIQ)));
lastAIQ = ceil(valAIQ);
}

delay(10000);
}