Multi (5) Sensor für smarthome Arduino FHEM MYSENSORS

5sens Modul

Einleitung

Heute veröffentliche ich die Version 0.11. des Sensor-Sketches. Änderung ist die Korrektur des Sketchteils für den Bewegungssensors.

 Sensoren

HC-SR501 (Motion)

DHT-22 (Temperature/Humidity)

MQ135 (Airquality)

MH-Z19 (CO2) –> demnächst

 

Entscheidung MQ135 / MH-Z19(14)

Die Entscheidung welcher CO2 Wert verwendet wird ist für den MH-Z14(19) gefallen. Dieser liefert deutlich plausiblere Werte. So ist  eine Übereinstimmung beider Kurven ist grob gegeben. Aber der MQ135 reagiert eben dennoch auf eine Menge andere Gase, so dass ich ihn vorerst weiter verwende und im Sketch belasse. Zukünftig teste ich mit dem MH-Z14 der bei Lieferung durch den MH-Z19 ersetzt und auf die Platine integriert wird.

 

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

//v0.xx MH-Z19 integration
//v0.11 Motion corrected
//v0.10 ---- installation co2 value specification outside----under construction
//v0.9 Autoset r0 off, vaule < 400 possible!!!!
//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
#define MY_REPEATER_FEATURE  // Enables repeater functionality for a radio node
#include <SPI.h>
#include <MySensors.h>
#include <DHT.h>
#include <MQ135.h>
//----------------------------------------------------------------------------
// Timer
unsigned long SLEEP_TIME = 30*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 16   // Id of the sensor child
MyMessage msgMotion(CHILD_ID_MOTION, V_TRIPPED);
//-----------------------------------------------------------------------------
// 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();
//-----------------------------------------------------------------------------
//Read digital motion value
boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
 Serial.print("Motion: ");
Serial.println(tripped);
send(msgMotion.set(tripped?"1":"0"));  
//-----------------------------------------------------------------------------
wait(SLEEP_TIME); //sleep/wait 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));
}
}
//-----------------------------------------------------------------------------