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. Skech using DHT11/LDR and SoilMoisture from itead. (Solved solution inside)

Skech using DHT11/LDR and SoilMoisture from itead. (Solved solution inside)

Scheduled Pinned Locked Moved Development
9 Posts 3 Posters 1.9k Views 3 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.
  • Tagore.PDET Offline
    Tagore.PDET Offline
    Tagore.PDE
    wrote on last edited by Tagore.PDE
    #1

    This is good? i think is not, maybe someone can check?
    Regards.

    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    //Including nescessary libraries
    #include <SPI.h>
    #include <MySensors.h> 
    #include <DHT.h>  
    
    //Defining child ID's and sensor pins
    //#define MY_NODE_ID 2
    #define LIGHTLEVEL 0
    #define HUMIDITY 1
    #define TEMPERATURE 2
    #define SOILHUMDITY 3 //agregado soil
    #define HUMIDITY_SENSOR_DIGITAL_PIN 3
    #define LIGHT_SENSOR_ANALOG_PIN A0
    #define ANALOG_INPUT_SOIL_SENSOR A1 // agregado soil
    DHT dht;
    
    //Defining values to store sensor information
    int LightLevel = 0;
    int lastLightLevel;
    int lastSoilValue = -1; // agregado soil
    float lastTemp;
    float lastHum;
    boolean metric = true; 
    
    
    //Defining sleep and wait times
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)(30 seconds)
    #define MESSAGEWAIT 500
    
    MyMessage LightMsg(LIGHTLEVEL, V_LIGHT_LEVEL);
    MyMessage msgHum(HUMIDITY, V_HUM);
    MyMessage msgTemp(TEMPERATURE, V_TEMP);
    MyMessage sHum(SOILHUMDITY, V_HUM); //agregado soil
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Sensor Pot", "1.0");
      wait(MESSAGEWAIT);
    
      // Register all sensors to gateway (they will be created as child devices)
      present(LIGHTLEVEL, S_LIGHT_LEVEL);
      wait(MESSAGEWAIT);
      present(HUMIDITY, S_HUM);
      wait(MESSAGEWAIT);
      present(TEMPERATURE, S_TEMP);
      wait(MESSAGEWAIT);
      present(SOILHUMDITY, S_HUM);
    }
    
    void setup() {
       dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
       pinMode(ANALOG_INPUT_SOIL_SENSOR, INPUT);
    }
    
    void loop() {
                delay(dht.getMinimumSamplingPeriod());
                float temperature = dht.getTemperature();
                if (isnan(temperature)) {
                                        Serial.println("Failed reading temperature from DHT");
                                        } else if (temperature != lastTemp) {
                                                                              lastTemp = 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);
                                                                                              }
    
                                                    int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23; 
                                                    Serial.println(lightLevel);
                                                    if (lightLevel != lastLightLevel) {
                                                                                      send(LightMsg.set(lightLevel));
                                                                                      lastLightLevel = lightLevel;
                                                                                      }
    
                                                                                        // Read analog soil value
                                          int soilValue = analogRead(ANALOG_INPUT_SOIL_SENSOR);
    
                                          if (soilValue != lastSoilValue) {
                                           Serial.print("Soil Value: ");
                                           Serial.println(soilValue);
                                           send(sHum.set(soilValue));
                                            }
                sleep(SLEEP_TIME);
                }
    
    
    TheoLT 1 Reply Last reply
    0
    • Tagore.PDET Tagore.PDE

      This is good? i think is not, maybe someone can check?
      Regards.

      // Enable debug prints to serial monitor
      #define MY_DEBUG 
      
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      //#define MY_RADIO_RFM69
      
      //Including nescessary libraries
      #include <SPI.h>
      #include <MySensors.h> 
      #include <DHT.h>  
      
      //Defining child ID's and sensor pins
      //#define MY_NODE_ID 2
      #define LIGHTLEVEL 0
      #define HUMIDITY 1
      #define TEMPERATURE 2
      #define SOILHUMDITY 3 //agregado soil
      #define HUMIDITY_SENSOR_DIGITAL_PIN 3
      #define LIGHT_SENSOR_ANALOG_PIN A0
      #define ANALOG_INPUT_SOIL_SENSOR A1 // agregado soil
      DHT dht;
      
      //Defining values to store sensor information
      int LightLevel = 0;
      int lastLightLevel;
      int lastSoilValue = -1; // agregado soil
      float lastTemp;
      float lastHum;
      boolean metric = true; 
      
      
      //Defining sleep and wait times
      unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)(30 seconds)
      #define MESSAGEWAIT 500
      
      MyMessage LightMsg(LIGHTLEVEL, V_LIGHT_LEVEL);
      MyMessage msgHum(HUMIDITY, V_HUM);
      MyMessage msgTemp(TEMPERATURE, V_TEMP);
      MyMessage sHum(SOILHUMDITY, V_HUM); //agregado soil
      
      void presentation()  {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("Sensor Pot", "1.0");
        wait(MESSAGEWAIT);
      
        // Register all sensors to gateway (they will be created as child devices)
        present(LIGHTLEVEL, S_LIGHT_LEVEL);
        wait(MESSAGEWAIT);
        present(HUMIDITY, S_HUM);
        wait(MESSAGEWAIT);
        present(TEMPERATURE, S_TEMP);
        wait(MESSAGEWAIT);
        present(SOILHUMDITY, S_HUM);
      }
      
      void setup() {
         dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
         pinMode(ANALOG_INPUT_SOIL_SENSOR, INPUT);
      }
      
      void loop() {
                  delay(dht.getMinimumSamplingPeriod());
                  float temperature = dht.getTemperature();
                  if (isnan(temperature)) {
                                          Serial.println("Failed reading temperature from DHT");
                                          } else if (temperature != lastTemp) {
                                                                                lastTemp = 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);
                                                                                                }
      
                                                      int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23; 
                                                      Serial.println(lightLevel);
                                                      if (lightLevel != lastLightLevel) {
                                                                                        send(LightMsg.set(lightLevel));
                                                                                        lastLightLevel = lightLevel;
                                                                                        }
      
                                                                                          // Read analog soil value
                                            int soilValue = analogRead(ANALOG_INPUT_SOIL_SENSOR);
      
                                            if (soilValue != lastSoilValue) {
                                             Serial.print("Soil Value: ");
                                             Serial.println(soilValue);
                                             send(sHum.set(soilValue));
                                              }
                  sleep(SLEEP_TIME);
                  }
      
      
      TheoLT Offline
      TheoLT Offline
      TheoL
      Contest Winner
      wrote on last edited by
      #2

      @Tagore.PDE I don't think it's appropriate to post a sketch and ask if it's a good sketch. Please be more specific. Like do you have errors? Does something not work?

      1 Reply Last reply
      0
      • Tagore.PDET Offline
        Tagore.PDET Offline
        Tagore.PDE
        wrote on last edited by
        #3

        Sorry about that, maybe the Controller ( Domoticz ) is not taking the soilmoisture correctly.
        I see that he send 3 digits number, like this:

        0_1471456262327_domoticz capture 2.PNG

        But domoticz show me like this:

        0_1471456192724_domoticz capture.PNG

        So maybe is confused? or ai m confused?

        Regards

        TheoLT 1 Reply Last reply
        0
        • Tagore.PDET Tagore.PDE

          Sorry about that, maybe the Controller ( Domoticz ) is not taking the soilmoisture correctly.
          I see that he send 3 digits number, like this:

          0_1471456262327_domoticz capture 2.PNG

          But domoticz show me like this:

          0_1471456192724_domoticz capture.PNG

          So maybe is confused? or ai m confused?

          Regards

          TheoLT Offline
          TheoLT Offline
          TheoL
          Contest Winner
          wrote on last edited by
          #4

          @Tagore.PDE I had a quick peak at your sketch. You're presenting the soil moisture sensor as a humidity sensor. That way Domoticz treats it as a humidity temp sensor.

          If you want to be able to see the moisture level you could present it as a light level

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

          But maybe @mfalkvidd has a better solution. Not sure what he uses for his office plant monitoring.

          I just used the digitial output of the soil moisture sensor. Because I was only interested in the fact that my plants are thirsty.

          Hope this helps.

          1 Reply Last reply
          0
          • Tagore.PDET Offline
            Tagore.PDET Offline
            Tagore.PDE
            wrote on last edited by
            #5

            @TheoL thx for your feedback.

            1 Reply Last reply
            0
            • Tagore.PDET Offline
              Tagore.PDET Offline
              Tagore.PDE
              wrote on last edited by
              #6

              For the record, to send in %, next code:

               int soilValue = analogRead(ANALOG_INPUT_SOIL_SENSOR); 
                soilValue = constrain(soilValue, 420, 1023); 
                soil = map(soilValue, 420, 1023, 100, 0);
                if (soil != lastSoilValue) {
                  send(msgHumSoil.set(soil)); // Send in percent
                  lastSoilValue = soil;```
              1 Reply Last reply
              0
              • tlpeterT Offline
                tlpeterT Offline
                tlpeter
                wrote on last edited by
                #7

                I use V_LEVEL and S_MOISTURE for a moisture sensor.
                The percentage part is something i am going to try for sure.

                1 Reply Last reply
                1
                • Tagore.PDET Offline
                  Tagore.PDET Offline
                  Tagore.PDE
                  wrote on last edited by
                  #8

                  Hi @tlpeter , cna share one example?

                  Regards.

                  1 Reply Last reply
                  0
                  • Tagore.PDET Offline
                    Tagore.PDET Offline
                    Tagore.PDE
                    wrote on last edited by Tagore.PDE
                    #9

                    I think i get it, @tlpeter :

                    MyMessage msgHumSoil(SOILHUMDITY, V_LEVEL); 
                    
                    void presentation()  {
                      // Send the sketch version information to the gateway and Controller
                      sendSketchInfo("Sensor POT", "1.1");
                      wait(MESSAGEWAIT);
                    
                      // Register all sensors to gateway (they will be created as child devices)
                      present(SOILHUMDITY, S_MOISTURE);
                    
                    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.0k

                    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