Help in data send to Domoticz



  • Hi
    I'm using arduino UNO gateway + mysensors + Domoticz controller to send and receive data like "DHT", "soil moisture" and "relay state"
    I got problem about reading temp and humidity; both shows in one block and the humidity doesn't update anymore. how can I set them to different blocks?
    and my second problem is about soil moisture sensor, when I want to add "event" to automating relay by soil moisture data, I receive "nil value" error for event and something about global variable.
    this is my code please anyone could help me I'll be grateful.
    thanks in advance.

    #define MY_DEFAULT_LED_BLINK_PERIOD 500
    #define MY_WITH_LEDS_BLINKING_INVERSE
    #define MY_DEFAULT_RX_LED_PIN  8  // Receive led pin
    #define MY_DEFAULT_TX_LED_PIN  7  // the PCB, on board LED
    #include <MySensors.h>
    #include <SPI.h>
    #include <DHT.h>
    DHT dht;
    #define DHTTYPE DHT22
    #define DHT_DATA_PIN 3
    #define SENSOR_TEMP_OFFSET 0
    
    const int buzzer = 5;
    static const uint8_t FORCE_UPDATE_N_READS = 10;
    
    
    #define MY_RADIO_NRF24
    #define MY_RF24_PA_LEVEL RF24_PA_HIGH
    
    
    #define MY_REPEATER_FEATURE
    
    #define RELAY_PIN 4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 1 // Total number of attached relays
    #define RELAY_ON 0  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
    
    #define CHILD_ID_SOIL 3
    MyMessage msg(CHILD_ID_SOIL, V_LEVEL);
    uint32_t SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds)
    int sensorValue;
    int val;
    int soilPin = A0;
    int soilPower = 6;
    
    #define CHILD_ID_HUM 1
    #define CHILD_ID_TEMP 2
    float lastTemp;
    float lastHum;
    uint8_t nNoUpdatesTemp;
    uint8_t nNoUpdatesHum;
    bool metric = true;
    
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    
    static const uint64_t UPDATE_INTERVAL = 5000;
    
    
    void before()
    {
    	for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
    		// Then set relay pins in output mode
    		pinMode(pin, OUTPUT);
    		// Set relay to last known state (using eeprom storage)
    		digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
    	}
    }
    
    void setup()
    {
     pinMode(buzzer, OUTPUT);
      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(soilPower, OUTPUT);//Set D6 as an OUTPUT
    digitalWrite(soilPower, LOW);
    }
    
    void presentation()
    {
    	// Send the sketch version information to the gateway and Controller
    	sendSketchInfo("Relay", "1.0");
    	for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
    		// Register all sensors to gw (they will be created as child devices)
    		present(sensor, S_BINARY);
        
        sendSketchInfo("Humidity and Temperature", "1.1");
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_HUM, S_HUM);
      present(CHILD_ID_TEMP, S_TEMP);
    
      
      sendSketchInfo("Soil Moisture Sensor", "1.0");
      present(CHILD_ID_SOIL, S_MOISTURE);
      
        metric = getControllerConfig().isMetric;
    
    	}
    }
    
    
    void loop()
    {
    
      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;
        if (!metric) {
          temperature = dht.toFahrenheit(temperature);
        }
        // Reset no updates counter
        nNoUpdatesTemp = 0;
        temperature += SENSOR_TEMP_OFFSET;
        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++;
      }
    
      
      sensorValue = readSoil();
    send(msg.set(sensorValue));
    
    
      // Sleep for a while to save energy
      sleep(UPDATE_INTERVAL); 
    
    }
    
    void receive(const MyMessage &message)
    {
    	// We only expect one type of message from controller. But we better check anyway.
    	if (message.type==V_STATUS) {
    		// Change relay state
    		digitalWrite(message.sensor-1+RELAY_PIN, message.getBool()?RELAY_ON:RELAY_OFF);
    		// Store state in eeprom
    		saveState(message.sensor, message.getBool());
    		// Write some debug info
    		Serial.print("Incoming change for sensor:");
    		Serial.print(message.sensor);
    		Serial.print(", New status: ");
    		Serial.println(message.getBool());
        tone(buzzer, 850); // Send 1KHz sound signal...
      delay(500);        // ...for 1 sec
      noTone(buzzer);     // Stop sound...
      delay(500);  
    	}
    }
    int readSoil()
    {
    digitalWrite(soilPower, HIGH);
    delay(10);//wait 10 milliseconds
    val = analogRead(soilPin);
    digitalWrite(soilPower, LOW);
    return val;//send current moisture value
    }
    

  • Mod

    Do some debug on the node and on gateway and check if data is actually sent from the node and received by gateway. Also use wait instead of sleep if you are using it as a repeater and relay actuator.



Suggested Topics

  • 11
  • 1
  • 51
  • 3
  • 1
  • 2

1
Online

11.4k
Users

11.1k
Topics

112.7k
Posts