[solved] MySensorWrapper - Wrapping sensor libraries for MySensors 2.0.x - need help



  • Hello all,

    I'm trying to write a some classes to help me wrapping some sensors in a user friendly way.
    The goal is to keep the .ino file as simple as possible so I can easily manage different sketches with different hardware configuration.
    And also to avoid updating all my sketches each time I want to update a single parameter or a sensor configuration.

    To achieve that I'm using VisualMicro as IDE and I've created a header file where a put all the #defines that are common to all my sketches and class files where I have my wrapper.

    The problem is that I don't know what I need to do to have access to MySensors methods like present or the to MyMessage object from the wrapper. So I would like to have your help on that 🙂
    My C++ skils for the moment are limited and I don't even know if that is possible.

    So here is my .ino file (MySensorGeneric.ino):

    /**
    * Name:		MySensorGeneric.ino
    * Created:	20-Aug-16 22:47:51
    * Author:	Daniel
    *
    *******************************
    *
    * REVISION HISTORY
    * Version 0.1 - Development
    *
    */
    
    #include "MySensorConfig.h"
    #include <MySensors.h>
    #include "MySensorWrapperBH1750.h"
    
    #define CHILD_ID_LIGHT 2
    
    MySensorWrapperBH1750 lightSensor(CHILD_ID_LIGHT);
    
    void setup()
    {
      lightSensor.Setup();
    }
    
    void presentation() {
      sendSketchInfo("Light Lux Sensor", "0.11");
      
      lightSensor.Present();
    }
    
    void loop()
    {
      lightSensor.UpdateSensorValue();
      sleep(SLEEP_TIME);
    }
    

    Here the header file with the #defines to share between sketches (MySensorConfig.h):

    // MySensorConfig.h
    
    #ifndef _MYSENSORCONFIG_h
    #define _MYSENSORCONFIG_h
    
    #define MY_BAUD_RATE 9600
    #define MY_RADIO_NRF24
    
    #define SLEEP_TIME 5000
    
    #endif
    

    And finally my wrapper class files (MySensorWrapperBH1750.h and MySensorWrapperBH1750.cpp):

    // MySensorWrapperBH1750.h
    
    #ifndef _MYSENSORWRAPPERBH1750_h
    #define _MYSENSORWRAPPERBH1750_h
    
    #include <BH1750.h>
    
    #if defined(ARDUINO) && ARDUINO >= 100
    	#include "arduino.h"
    #else
    	#include "WProgram.h"
    #endif
    
    class MySensorWrapperBH1750
    {
    public:
      MySensorWrapperBH1750(uint8_t sensor_id);
      ~MySensorWrapperBH1750();
      void Setup();
      void Present();
      void UpdateSensorValue();  
    
    private:
      uint8_t _sensor_id;
      uint16_t _lastValue;
      BH1750 _sensor;
      MyMessage _msg;
    };
    
    #endif
    
    /**
    * Name:		MySensorWrapperBH1750
    * Created:	20-Aug-16 22:47:51
    * Author:	Daniel
    * Version 0.1 - Draft
    *
    *******************************
    *
    * MySensors Wrapper for BH1750 sensor.
    * Wiring (I2C):
    * Sensor -> Arduino
    *  GND   -> GND
    *  ADD   -> NC
    *  SDA   -> A4
    *  SCL   -> A5
    *  VCC   -> +5V
    *
    */
    
    #include "MySensorWrapperBH1750.h"
    
    MySensorWrapperBH1750::MySensorWrapperBH1750(uint8_t sensor_id)
    {
      _sensor_id = sensor_id;
      MyMessage _msg(_sensor_id, V_LEVEL);
    }
    
    MySensorWrapperBH1750::~MySensorWrapperBH1750()
    {
    }
    
    void MySensorWrapperBH1750::Setup()
    {
      // TODO: Set initialization mode
      // Default mode:
      // Start measurement at 1lx resolution. Measurement time is approx 120ms.
      // #define BH1750_CONTINUOUS_HIGH_RES_MODE  0x10
      // Start measurement at 1lx resolution. Measurement time is approx 120ms.
      // Device is automatically set to Power Down after measurement.
      // #define BH1750_ONE_TIME_HIGH_RES_MODE  0x20
      _sensor.begin();
    }
    
    void MySensorWrapperBH1750::Present()
    {
      present(_sensor_id, S_LIGHT_LEVEL);
    }
    
    void MySensorWrapperBH1750::UpdateSensorValue()
    {
      // Fetch light in Lux from BH1750 sensor
      uint16_t newValue = _sensor.readLightLevel();// Get Lux value
      if (isnan(newValue)) {
        #ifdef MY_DEBUG
            Serial.println("Failed reading Light from BH1750");
        #endif
      }
      else if (newValue != _lastValue) {
        #ifdef MY_DEBUG
          Serial.print("Lux: ");
          Serial.println(newValue);
        #endif
        send(_msg.set(newValue));
        _lastValue = newValue;
      }
    }
    

    I've attached the VisualMicro project is you're interested:
    MySensorGeneric.zip

    Sorry for the long post and the noob questions.

    Thank you all for the help


  • Contest Winner

    @Daniel-Oliveira I think I've just included <MySensors.h> in the past. And that was all I needed to be able to acces the MySensors methods. Not sure if it still works in MySensors 2.0.



  • @TheoL said:

    @Daniel-Oliveira I think I've just included <MySensors.h> in the past. And that was all I needed to be able to acces the MySensors methods. Not sure if it still works in MySensors 2.0.

    Thank you for the quick reply,

    If I add #include <MySensors.h> the to the MySensorWrapperBH1750.h file apparently VisualMicro tries to insert/compile MySensors a second time and I get this error:

    MySensors.h: 253:4: error: #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless
       #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless
    Error compiling project sources
    

    Here the complete output:

    Compiling 'MySensorGeneric' for 'Arduino Nano w/ ATmega328'
    Build folder: file:///C:/Users/Daniel/AppData/Local/Temp/VMicroBuilds/MySensorGeneric/nano_atmega328
    Summary: Header=1 Prototypes=4 Imports=1
    Additional Defines: MY_DEBUG;
    Architecture Tools: C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2/bin/
    Sketchbook: file:\\\C:\Users\Daniel\Documents\Arduino
    Sketch Include Paths
    Include Path 'C:\Users\Daniel\Documents\Arduino\projects\MySensorGeneric'
    Core Include Paths
    Include Path 'C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino'
    Include Path 'C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs'
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\MySensorGeneric.cpp" -o "nul"
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\MySensorGeneric.cpp" -o "nul"
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src" "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src\SPI.cpp" -o "nul"
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\MySensorGeneric.cpp" -o "nul"
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750\utility" "C:\Users\Daniel\Documents\Arduino\libraries\BH1750\BH1750.cpp" -o "nul"
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750\utility" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire\src" "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire\src\Wire.cpp" -o "nul"
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750\utility" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire\src" "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire\src\utility\twi.c" -o "nul"
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750\utility" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire\src" "C:\Users\Daniel\Documents\Arduino\libraries\BH1750\BH1750.cpp" -o "nul"
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire\src" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\MySensorGeneric.cpp" -o "nul"
     
    Building core ...
     
    Building libraries ...
    Using library BH1750 version 0.0.0 in folder C:\Users\Daniel\Documents\Arduino\libraries\BH1750
    Using previously compiled file: C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\BH1750\BH1750.cpp.o
    Using library MySensors version 2.0.1-beta in folder C:\Users\Daniel\Documents\Arduino\libraries\MySensors
    Using library SPI version 1.0 in folder C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI
    Using library Wire version 1.0 in folder C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire
     
    Building project code ...
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire\src" -I"C:\Program Files (x86)\Arduino\libraries" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries" -I"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\ywun0ect.dgd\Micro Platforms\default\debuggers" -I"C:\Users\Daniel\Documents\Arduino\libraries" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\MySensorGeneric.cpp" -o "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\MySensorGeneric.cpp.o" -DMY_DEBUG
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire\src" -I"C:\Program Files (x86)\Arduino\libraries" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries" -I"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\ywun0ect.dgd\Micro Platforms\default\debuggers" -I"C:\Users\Daniel\Documents\Arduino\libraries" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\MySensorWrapperBH1750.cpp" -o "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\MySensorWrapperBH1750.cpp.o" -DMY_DEBUG
     
    MySensorWrapperBH1750.h:14: In file included from
    MySensorWrapperBH1750.cpp:20: from
     
    MySensors.h: 253:4: error: #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless
       #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless
    Error compiling project sources
    

    Regards


  • Contest Winner

    @Daniel-Oliveira Make sure that you have all the defines in your main sketch before including <MySensors.h>. And also include your own libraries as the last ones. So #include <MySensorWrapperBH1750.h> should be your last include in your main sketch.

    I think that will work.



  • @TheoL said:

    @Daniel-Oliveira Make sure that you have all the defines in your main sketch before including <MySensors.h>. And also include your own libraries as the last ones. So #include <MySensorWrapperBH1750.h> should be your last include in your main sketch.

    I think that will work.

    Thank you once more for the help, but that is exactly what I have, and still no luck 😞


  • Hardware Contributor

    Hello,

    I think your problem is you are including MySensors library before the configuration is visible to the controller. The error you have means it has started to compile the MySensors.h before it checked the MySensorConfig.h and saw the #define MY_RADIO_NRF24

    You should try to remove the #ifndef _MYSENSORCONFIG_h from the MySensorConfig.h file and put it in each file where you need to include MySensors.h, and inside this condition include both MySensorConfig.h and MySensors.h :

    #ifndef _MYSENSORCONFIG_h
     #include "MySensorConfig.h"
     #include <MySensors.h>
    #endif
    

    and your "MySensorConfig.h becomes :

    // MySensorConfig.h
    
    #define _MYSENSORCONFIG_h
    
    #define MY_BAUD_RATE 9600
    #define MY_RADIO_NRF24
    
    #define SLEEP_TIME 5000
    


  • @Nca78 said:

    Hello,

    I think your problem is you are including MySensors library before the configuration is visible to the controller. The error you have means it has started to compile the MySensors.h before it checked the MySensorConfig.h and saw the #define MY_RADIO_NRF24

    You should try to remove the #ifndef _MYSENSORCONFIG_h from the MySensorConfig.h file and put it in each file where you need to include MySensors.h, and inside this condition include both MySensorConfig.h and MySensors.h :

    #ifndef _MYSENSORCONFIG_h
     #include "MySensorConfig.h"
     #include <MySensors.h>
    #endif
    

    and your "MySensorConfig.h becomes :

    // MySensorConfig.h
    
    #define _MYSENSORCONFIG_h
    
    #define MY_BAUD_RATE 9600
    #define MY_RADIO_NRF24
    
    #define SLEEP_TIME 5000
    

    Hi and thank you for the help.

    Things apparently improved but still not there.

    Should I put the include of the MySensorConfig.h and MySensors.h as you mentioned also in the .ino file?
    And when a I have a class adding it to the header file is enough or should I also add it to the .cpp?

    Anyway I have problems in the linking stage apparently, do you have a clue?

    Compiling 'MySensorGeneric' for 'Arduino Nano w/ ATmega328'
    Build folder: file:///C:/Users/Daniel/AppData/Local/Temp/VMicroBuilds/MySensorGeneric/nano_atmega328
    Summary: Header=1 Prototypes=4 Imports=1
    Additional Defines: MY_DEBUG;
    Architecture Tools: C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2/bin/
    Sketchbook: file:\\\C:\Users\Daniel\Documents\Arduino
    Sketch Include Paths
    Include Path 'C:\Users\Daniel\Documents\Arduino\projects\MySensorGeneric'
    Core Include Paths
    Include Path 'C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino'
    Include Path 'C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs'
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\MySensorGeneric.cpp" -o "nul"
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\MySensorGeneric.cpp" -o "nul"
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src" "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src\SPI.cpp" -o "nul"
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\MySensorGeneric.cpp" -o "nul"
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750\utility" "C:\Users\Daniel\Documents\Arduino\libraries\BH1750\BH1750.cpp" -o "nul"
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750\utility" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire\src" "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire\src\Wire.cpp" -o "nul"
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750\utility" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire\src" "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire\src\utility\twi.c" -o "nul"
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750\utility" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire\src" "C:\Users\Daniel\Documents\Arduino\libraries\BH1750\BH1750.cpp" -o "nul"
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2\bin\avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=106010 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\variants\eightanaloginputs" -I"C:\Users\Daniel\Documents\Arduino\libraries\MySensors" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI\src" -I"C:\Users\Daniel\Documents\Arduino\libraries\BH1750" -I"C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire\src" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\MySensorGeneric.cpp" -o "nul"
     
    Building core ...
     
    Building libraries ...
    Using library BH1750 version 0.0.0 in folder C:\Users\Daniel\Documents\Arduino\libraries\BH1750
    Using previously compiled file: C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\BH1750\BH1750.cpp.o
    Using library MySensors version 2.0.1-beta in folder C:\Users\Daniel\Documents\Arduino\libraries\MySensors
    Using library SPI version 1.0 in folder C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI
    Using library Wire version 1.0 in folder C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire
     
    Building project code ...
    Using previously compiled file: C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\MySensorGeneric.cpp.o
    Using previously compiled file: C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\MySensorWrapperBH1750.cpp.o
     
    Linking it all together ...
    "C:\Users\Daniel\AppData\Local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2/bin/avr-gcc" -w -Os -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p -o "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328/MySensorGeneric.ino.elf" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\MySensorGeneric.cpp.o" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\Wire\Wire.cpp.o" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\Wire\utility\twi.c.o" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\SPI\SPI.cpp.o" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\BH1750\BH1750.cpp.o" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328\MySensorWrapperBH1750.cpp.o" "C:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328/core.a" "-LC:\Users\Daniel\AppData\Local\Temp\VMicroBuilds\MySensorGeneric\nano_atmega328" -lm
     
    MySensorWrapperBH1750.cpp.o (symbol from plugin)*: In function wakeUp1()
    (.text+0x0)*: multiple definition of wakeUp1()
    MySensorGeneric.cpp.o (symbol from plugin)*: (.text+0x0): first defined here
     
    MySensorWrapperBH1750.cpp.o (symbol from plugin)*: In function wakeUp1()
    (.text+0x0)*: multiple definition of _wakeUp1Interrupt
    MySensorGeneric.cpp.o (symbol from plugin)*: (.text+0x0): first defined here
     
    // and similiar erros another 100 times
     
    collect2.exe*: error: ld returned 1 exit status
    
    Error compiling for board Arduino Nano w/ ATmega328
    

    Thank you all for the help



  • Hi @tekka , @ericvdb and @Stuart-Middleton ,

    As VisualMicro users can you kindly take a look as this thread?

    Thank you in advance



  • @Daniel-Oliveira said:

    Hi @tekka , @ericvdb and @Stuart-Middleton ,

    As VisualMicro users can you kindly take a look as this thread?

    Thank you in advance

    Bump 🙂



  • @Daniel-Oliveira That seems to me an issue with the build process.
    I think you better point this out on the VisualMicro Forum



  • @ericvdb said:

    @Daniel-Oliveira That seems to me an issue with the build process.
    I think you better point this out on the VisualMicro Forum

    Hi,

    Thank you for the tip, and I bring news.

    Indeed I open a thread and after trying the suggested (that in fact was already suggested here) I found that I?m able to do what I want with another library, for example Bounce2.

    If I add #include "Bounce.h" to both my .ino file and to my MySensorWrapperBH1750.h it works flawlessly.
    In the other hand if I include have the MySensorWrapperBH1750.h like this:

    #define MY_BAUD_RATE 9600
    #define MY_RADIO_NRF24
    #define MY_DEBUG
    
    #include <MySensors.h>
    // and then my class
    

    And I include ONLY the MySensorWrapperBH1750.h in my .ino file I get the same error.
    What I understand from here is that even including MySensors library only once some how it is included twice when compiling.
    Does MySonsors lack somehow a "multiple inclusion protection"?

    Regards,


  • Admin

    You can only include MySensors.h once from your sketch. It drags in .cpp files.

    So if you need MySensors things in your class, just include the stuff you need. I.e. MyMessage.h.



  • @hek said:

    You can only include MySensors.h once from your sketch. It drags in .cpp files.

    So if you need MySensors things in your class, just include the stuff you need. I.e. MyMessage.h.

    Hi @hek

    As usual you are a live saviour, in this case MySensor projects saviour 🙂

    Because I want to access present() and send() functions I had to include the MySensorsCore.h and then add to the project (source files) the Version.h and the MyEepromAdresses.h to be able to compile.

    I'll do some tests and report back.

    Cheers



  • Hi!

    I accomplish to do what I wanted thanks to the help of you all, so I'm sharing the results 🙂

    I ended up with this very simple MySensorGeneric.ino that is able to manage a DHT and a BH1750

    /**
    * Name:		MySensorGeneric.ino
    * Created:	20-Aug-16 22:47:51
    * Author:	Daniel
    *
    *******************************
    *
    * REVISION HISTORY
    * Version 0.1 - Development
    *
    */
    
    #include "MySensorConfig.h"
    #include <MySensors.h>
    
    #include "MySensorWrapperDHT.h"
    #include "MySensorWrapperBH1750.h"
    
    #define CHILD_ID_LIGHT 2
    #define CHILD_ID_TEMP  1
    #define CHILD_ID_HUM   0
    #define HUMIDITY_SENSOR_DIGITAL_PIN 3
    
    MySensorWrapperBH1750 SensorBH1750(CHILD_ID_LIGHT);
    MySensorWrapperDHT    SensorDHT(CHILD_ID_TEMP, CHILD_ID_HUM, HUMIDITY_SENSOR_DIGITAL_PIN);
    
    void setup()
    {
      SensorBH1750.Setup();
      SensorDHT.Setup();
    }
    
    void presentation() {
      sendSketchInfo("My Generic Sensor", "0.11");
      SensorBH1750.Present();
      SensorDHT.Present();
    }
    
    void loop()
    {
      SensorBH1750.UpdateSensorValue();
      SensorDHT.UpdateSensorValue();
      wait(SLEEP_TIME);
    }
    

    I've created a class to manage the BH1750:
    MySensorWrapperBH1750.h

    // MySensorWrapperBH1750.h
    
    #ifndef _MYSENSORWRAPPERBH1750_h
    #define _MYSENSORWRAPPERBH1750_h
    
    //Need to add to project Version.h and MyEepromAdresses.h
    #include "MySensorsCore.h"
    
    #include <BH1750.h>
    
    class MySensorWrapperBH1750
    {
    public:
      MySensorWrapperBH1750(uint8_t sensor_id);
      ~MySensorWrapperBH1750();
      void Setup();
      void Present();
      void UpdateSensorValue();  
    
    private:
      uint8_t _sensor_id;
      uint16_t _lastValue;
      BH1750 _sensor;
      MyMessage _msg;
    };
    
    #endif
    
    
    

    MySensorWrapperBH1750.cpp

    /**
    * Name:		MySensorWrapperBH1750
    * Created:	20-Aug-16 22:47:51
    * Author:	Daniel
    * Version 0.1 - Draft
    *
    *******************************
    *
    * MySensors Wrapper for BH1750 sensor.
    * Wiring (I2C):
    * Sensor -> Arduino
    *  GND   -> GND
    *  ADD   -> NC
    *  SDA   -> A4
    *  SCL   -> A5
    *  VCC   -> +5V
    *
    */
    
    
    #include "MySensorWrapperBH1750.h"
    
    MySensorWrapperBH1750::MySensorWrapperBH1750(uint8_t sensor_id)
    {
      _sensor_id = sensor_id;
      MyMessage _msg(_sensor_id, V_LEVEL);
    }
    
    MySensorWrapperBH1750::~MySensorWrapperBH1750()
    {
    }
    
    void MySensorWrapperBH1750::Setup()
    {
      // TODO: Set initialization mode
      // Default mode:
      // Start measurement at 1lx resolution. Measurement time is approx 120ms.
      // #define BH1750_CONTINUOUS_HIGH_RES_MODE  0x10
      // Start measurement at 1lx resolution. Measurement time is approx 120ms.
      // Device is automatically set to Power Down after measurement.
      // #define BH1750_ONE_TIME_HIGH_RES_MODE  0x20
    
      _sensor.begin();
    }
    
    void MySensorWrapperBH1750::Present()
    {
      present(_sensor_id, S_LIGHT_LEVEL);
    }
    
    void MySensorWrapperBH1750::UpdateSensorValue()
    {
      // Fetch light in Lux from BH1750 sensor
      uint16_t newValue = _sensor.readLightLevel();// Get Lux value
      if (isnan(newValue)) {
        #ifdef MY_DEBUG
            Serial.println("Failed reading Light from BH1750");
        #endif
      }
      else if (newValue != _lastValue) {
        #ifdef MY_DEBUG
          Serial.print("Lux: ");
          Serial.println(newValue);
        #endif
        send(_msg.set(newValue));
        _lastValue = newValue;
      }
    }
    

    And for the DHT I've created the following class:
    MySensorWrapperDHT.h

    // MySensorWrapperDHT.h
    
    #ifndef _MYSENSORWRAPPERDHT_h
    #define _MYSENSORWRAPPERDHT_h
    
    //Need to add to project Version.h and MyEepromAdresses.h
    #include "MySensorsCore.h"
    
    #include "DHT.h"
    
    class MySensorWrapperDHT
    {
    public:
      MySensorWrapperDHT(uint8_t sensor_temp_id, uint8_t sensor_humd_id, uint8_t sensor_pin = 3);
      ~MySensorWrapperDHT();
      void Setup();
      void Present();
      void UpdateSensorValue();
    
    private:
      uint8_t _sensorTempId;
      uint8_t _sensorHumdId;
      uint8_t _sensorPin;
      DHT _sensor;
      MyMessage _msgTemp;
      MyMessage _msgHumd;
      float _lastTempValue;
      float _lastHumdValue;
    };
    
    #endif
    
    

    MySensorWrapperDHT.cpp

    /**
    * Name:		MySensorWrapperDHT
    * Created:	20-Aug-16 22:47:51
    * Author:	Daniel
    * Version 0.1 - Draft
    *
    *******************************
    *
    * MySensors Wrapper for DHT sensor.
    * Wiring ():
    * Sensor -> Arduino
    *  GND   -> GND
    *  OUT   -> D3
    *  VCC   -> +5V
    *
    * NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
    * to 3.3V instead of 5V!
    * Connect pin 2 of the sensor to whatever your DHTPIN is
    * Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
    * https://github.com/adafruit/DHT-sensor-library/blob/master/examples/DHTtester/DHTtester.ino
    */
    
    #include "MySensorWrapperDHT.h"
    
    MySensorWrapperDHT::MySensorWrapperDHT(uint8_t sensor_temp_id, uint8_t sensor_humd_id, uint8_t sensor_pin)
    {
      _sensorTempId = sensor_temp_id;
      _sensorHumdId = sensor_humd_id;
      _sensorPin = sensor_pin;
      MyMessage _msgTemp(_sensorTempId, V_TEMP);
      MyMessage _msgHumd(_sensorHumdId, V_HUM);
    }
    
    MySensorWrapperDHT::~MySensorWrapperDHT()
    {
    }
    
    void MySensorWrapperDHT::Setup()
    {
      _sensor.setup(_sensorPin, DHT::DHT22);
    }
    
    void MySensorWrapperDHT::Present()
    {
      present(_sensorTempId, S_TEMP);
      present(_sensorHumdId, S_HUM);
    }
    
    void MySensorWrapperDHT::UpdateSensorValue()
    {
      // TODO: really needed?
      wait(_sensor.getMinimumSamplingPeriod());
      
      // Reading temperature or humidity takes about 250 milliseconds!
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      float currentTempValue = _sensor.getTemperature();
      float currentHumdValue = _sensor.getHumidity();
    
      // Check if any reads failed and exit early (to try again).
      if (isnan(currentTempValue) || isnan(currentHumdValue) ){
        Serial.println("Failed to read from DHT sensor!");
      }
      else {
        // Update temperature from DHT sensor
        if (currentTempValue != _lastTempValue) {
          _lastTempValue = currentTempValue;
          send(_msgTemp.set(currentTempValue, true));
          #ifdef MY_DEBUG
            Serial.print("Temp: ");
            Serial.println(currentTempValue);
          #endif
        }
        // Update humidity from DHT sensor
        if (currentHumdValue != _lastHumdValue) {
          _lastHumdValue = currentHumdValue;
          send(_msgHumd.set(currentHumdValue, 1));
          #ifdef MY_DEBUG
            Serial.print("Hum: ");
            Serial.println(currentHumdValue);
          #endif
        }
      }
    }
    

    And to my surprise the footprint after compiling is even smaller, 18,374 bytes (59%) of program storage space and 913 bytes (44%) of dynamic memory with a single .ino versus 17,748 bytes (58%) of program storage space and 857 bytes (42%) of dynamic memory the above project in VisualMicro. Both results with MY_DEBUG defined.

    Next step is to create classes to manage switches, relays and LEDs.

    Once again thank you all for the help and fell free to share your opinions about the way I'm trying to do things.

    Best regards



Suggested Topics

14
Online

11.4k
Users

11.1k
Topics

112.7k
Posts