Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. [Help] - Improvement for Multisensor sketch

[Help] - Improvement for Multisensor sketch

Scheduled Pinned Locked Moved Development
5 Posts 3 Posters 1.0k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ruric
    wrote on last edited by
    #1

    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.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik EKblad
     * Version 2.0 - Ruric
     * 
     * DESCRIPTION
     * 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
    #define LIGHT_SENSOR_ANALOG_PIN 0
    
    // 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_LIGHT, S_LIGHT_LEVEL);
      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());
      wait(dht.getMinimumSamplingPeriod());
    
      pinMode(MOTION_DATA_PIN, INPUT);
      pinMode(DOOR_DATA_PIN, INPUT_PULLUP);
    
    }
    
    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) {
        send(msgDoor.set(door?"1":"0"));
        lastDoor = door;
      }
    
      
      boolean needRefresh = (millis() - lastRefreshTime) > UPDATE_INTERVAL ;
      if (needRefresh)
      {
          lastRefreshTime = millis();
      
        // Force reading sensor, so it works also after sleep()
        dht.readSensor(true);
      
        // 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: ");
          Serial.println(temperature);
          #endif
        } else {
          // Increase no update counter if the temperature stayed the same
          nNoUpdatesTemp++;
        }
      
        // 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: ");
          Serial.println(humidity);
          #endif
        } else {
          // Increase no update counter if the humidity stayed the same
          nNoUpdatesHum++;
        }
        
        // 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;
          send(msgLight.set(light));
      
          #ifdef MY_DEBUG
          Serial.print("L: ");
          Serial.println(light);
          #endif
        } else {
          // Increase no update counter if the temperature stayed the same
          nNoUpdatesLight++;
        }
      }
     
    }
      
    
    berkseoB 1 Reply Last reply
    0
    • R Ruric

      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.
       *
       *******************************
       *
       * REVISION HISTORY
       * Version 1.0 - Henrik EKblad
       * Version 2.0 - Ruric
       * 
       * DESCRIPTION
       * 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
      #define LIGHT_SENSOR_ANALOG_PIN 0
      
      // 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_LIGHT, S_LIGHT_LEVEL);
        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());
        wait(dht.getMinimumSamplingPeriod());
      
        pinMode(MOTION_DATA_PIN, INPUT);
        pinMode(DOOR_DATA_PIN, INPUT_PULLUP);
      
      }
      
      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) {
          send(msgDoor.set(door?"1":"0"));
          lastDoor = door;
        }
      
        
        boolean needRefresh = (millis() - lastRefreshTime) > UPDATE_INTERVAL ;
        if (needRefresh)
        {
            lastRefreshTime = millis();
        
          // Force reading sensor, so it works also after sleep()
          dht.readSensor(true);
        
          // 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: ");
            Serial.println(temperature);
            #endif
          } else {
            // Increase no update counter if the temperature stayed the same
            nNoUpdatesTemp++;
          }
        
          // 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: ");
            Serial.println(humidity);
            #endif
          } else {
            // Increase no update counter if the humidity stayed the same
            nNoUpdatesHum++;
          }
          
          // 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;
            send(msgLight.set(light));
        
            #ifdef MY_DEBUG
            Serial.print("L: ");
            Serial.println(light);
            #endif
          } else {
            // Increase no update counter if the temperature stayed the same
            nNoUpdatesLight++;
          }
        }
       
      }
        
      
      berkseoB Offline
      berkseoB Offline
      berkseo
      wrote on last edited by
      #2

      @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.

      1 Reply Last reply
      0
      • rejoe2R Offline
        rejoe2R Offline
        rejoe2
        wrote on last edited by
        #3

        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 :grinning: .

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

        Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

        R 1 Reply Last reply
        0
        • rejoe2R rejoe2

          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 :grinning: .

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

          R Offline
          R Offline
          Ruric
          wrote on last edited by
          #4

          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?

          rejoe2R 1 Reply Last reply
          0
          • R Ruric

            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?

            rejoe2R Offline
            rejoe2R Offline
            rejoe2
            wrote on last edited by
            #5

            @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.

            Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            16

            Online

            11.7k

            Users

            11.2k

            Topics

            113.1k

            Posts


            Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • MySensors
            • OpenHardware.io
            • Categories
            • Recent
            • Tags
            • Popular