[Help] - Improvement for Multisensor sketch

  • Hello everybody, I'm new to this forum but starting reading some months ago. Really great work you're doing here and sharing with the community.
    I'm looking for some help to improve my understanding of the mySensors sketches. Until now, I succeeded in setting up a gateway (serial for OpenHab) and some basic sensors. Also the combination of several sketches seemed to work (thanks again for the big pile of information, that can be found in this forum). And that's where my questions start. I've a working sketch, that I'd like to improve. What and how would you do this? Is there a better way to distribute the PINs? Etc. any comment, that will help me to better understand, why and how things are working is welcome.

     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     * Version 1.0 - Henrik EKblad
     * Version 2.0 - Ruric
     * My general Multisensor build
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    #include <SPI.h>
    #include <MySensors.h>  
    #include <DHT.h>
    #define MY_NODE_ID 1
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define CHILD_ID_LIGHT 2
    #define CHILD_ID_MOTION 3
    #define CHILD_ID_DOOR 4
    // Set this to the pin you connected the sensor's data pin to
    #define DHT_DATA_PIN 3
    #define MOTION_DATA_PIN 4
    #define DOOR_DATA_PIN 5
    // Set this to the pin you connected the light level analog pin to
    // Set this offset if the sensor has a permanent small offset to the real temperatures.
    // In Celsius degrees (as measured by the device)
    #define SENSOR_TEMP_OFFSET 0
    // Sleep time between sensor updates (in milliseconds)
    // Must be >1000ms for DHT22 and >2000ms for DHT11  
    static const uint16_t UPDATE_INTERVAL = 60000; // max 65500 due to uint16_t
    // Force sending an update of the temperature after n sensor reads, so a controller showing the
    // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
    // the value didn't change since;
    // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
    static const uint8_t FORCE_UPDATE_N_READS = 10;
    int lastLight;
    float lastTemp;
    float lastHum;
    uint8_t nNoUpdatesTemp;
    uint8_t nNoUpdatesHum;
    uint8_t nNoUpdatesLight;
    uint8_t nNoUpdatesMotion;
    bool metric = true;
    bool lastMotion;
    bool lastDoor;
    unsigned long lastRefreshTime = 0; // Use this to implement a non-blocking delay function
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msgLight(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    MyMessage msgMotion(CHILD_ID_MOTION, V_TRIPPED);
    MyMessage msgDoor(CHILD_ID_DOOR, V_TRIPPED);
    DHT dht;
    void presentation()
    	// Send the sketch version information to the gateway and Controller
    	sendSketchInfo("MySensor", "2.0");
    	// Register all sensors to gateway (they will be created as child devices)
      present(CHILD_ID_HUM, S_HUM);
      present(CHILD_ID_TEMP, S_TEMP);
      present(CHILD_ID_MOTION, S_MOTION);
      present(CHILD_ID_DOOR, S_DOOR);
    void setup()
      dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
      if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
        Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
      // Sleep for the time of the minimum sampling period to give the sensor time to power up
      // (otherwise, timeout errors might occure for the first reading)
      // sleep(dht.getMinimumSamplingPeriod());
      pinMode(MOTION_DATA_PIN, INPUT);
    void loop()
      // Read digital motion value
      bool motion = digitalRead(MOTION_DATA_PIN) == HIGH;
      if (motion != lastMotion) {
      send(msgMotion.set(motion?"1":"0"));  // Send tripped value to gw
      lastMotion = motion;
      bool door = digitalRead(DOOR_DATA_PIN) == HIGH;
      if (door != lastDoor) {
        lastDoor = door;
      boolean needRefresh = (millis() - lastRefreshTime) > UPDATE_INTERVAL ;
      if (needRefresh)
          lastRefreshTime = millis();
        // Force reading sensor, so it works also after sleep()
        // Get temperature from DHT library
        float temperature = dht.getTemperature();
        if (isnan(temperature)) {
          Serial.println("Failed reading temperature from DHT!");
        } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
          // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
          lastTemp = temperature;
          // apply the offset before converting to something different than Celsius degrees
          temperature += SENSOR_TEMP_OFFSET;
          // Reset no updates counter
          nNoUpdatesTemp = 0;
          send(msgTemp.set(temperature, 1));
          #ifdef MY_DEBUG
          Serial.print("T: ");
        } else {
          // Increase no update counter if the temperature stayed the same
        // Get humidity from DHT library
        float humidity = dht.getHumidity();
        if (isnan(humidity)) {
          Serial.println("Failed reading humidity from DHT");
        } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
          // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
          lastHum = humidity;
          // Reset no updates counter
          nNoUpdatesHum = 0;
          send(msgHum.set(humidity, 1));
          #ifdef MY_DEBUG
          Serial.print("H: ");
        } else {
          // Increase no update counter if the humidity stayed the same
        // Get LightLevel from Analog input
      	uint16_t light = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23;
        if (light != lastLight || nNoUpdatesLight == FORCE_UPDATE_N_READS) {
          // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
          lastLight = light;
          // Reset no updates counter
          nNoUpdatesLight = 0;
          #ifdef MY_DEBUG
          Serial.print("L: ");
        } else {
          // Increase no update counter if the temperature stayed the same

  • @ruric

    To improve? Do not use DHT temperature sensors

    ...But seriously, you can add descriptions to the presentation of sensors, and if seriously enough - I did not see the use of " void receive (const MyMessage & message)", no protection from situations where delivery failed.

  • Seems already to be quite good, congratiulations!

    Beside the DHT-topic: Use a different PIN as 3 as long as possible - reserving an interrupt-option, e.g. for adding a counter imo is always a good idea 😀 .

    Besides that you may use a debouncing method like bounce2.h for the door and motion PIN's to avoid false triggers.

  • Thanks for this feedback. For what kind of interrupt should I preferably use pin3? For the door sensor? Do you happen to have a link, where I could find more about interupt and its usecases?

    I‘ll try to see, what I can find about adding void receive. And come back with the results (or more questions to understand better).

    Which temperature and humidity sensors would you recommand?

  • @ruric For usage of interrupts see the Pulse-Meter (Water/Engergy) examples in Mysensors. As your sensor is not sleeping and has no longer blocking phases, usage of interrupt functionality for door or motion is not necessary.

    Combined Temp/Humidity: BME280 or SI7021, Temp alone: DS18B20.

Log in to reply

Suggested Topics

  • 1
  • 2
  • 198
  • 2
  • 1
  • 2