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



  • 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);
                }
    
    

  • Contest Winner

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



  • 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


  • Contest Winner

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



  • @TheoL thx for your feedback.



  • 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;```


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



  • Hi @tlpeter , cna share one example?

    Regards.



  • 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);
    

Log in to reply
 

Suggested Topics

  • 1
  • 1
  • 2
  • 3
  • 1
  • 3

61
Online

11.4k
Users

11.1k
Topics

112.7k
Posts