Pls Help: Sensor value shows in Domoticz Hardware TAB, but wont show on Switches



  • Please Help needed I have a Node with Sonar sensor and an digitalRead(A0) value from Node to Gateway. The value shows on Domoticz Hardware TAB, but wont show on Switches. I have stayed up all night looking for solutions without much luck. I sketch is as follows.

    // Example sketch showing how to control physical relays. 
    // This example will remember relay state even after power failure.
    
    // Enable debug prints
    #define MY_DEBUG ON
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    #define MY_RF24_PA_LEVEL RF24_PA_HIGH // Options MAX to reach the furthest 
    #define MY_NODE_ID 4  // Set this to fix your Radio ID or use AUTO or 1
    #define MY_REGISTRATION_FEATURE // Force registration
    #define MY_REGISTRATION_RETRIES 5
    
    #include <Wire.h>
    #include <TimeLib.h>
    #include <SPI.h>
    #include <MySensors.h>
    #include <NewPing.h>
    
    // For Debug
    #ifdef DEBUG_ON
    #define DEBUG_PRINT(x)   Serial.print(x)
    #define DEBUG_PRINTLN(x) Serial.println(x)
    #else
    #define DEBUG_PRINT(x)
    #define DEBUG_PRINTLN(x)
    #define SERIAL_START(x)
    #endif
    
    #define RELAY_PIN 2  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 5 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    #define SKETCH_NAME "Generator Node"
    #define SKETCH_VERSION "1.3.1"
    #define CHILD_ID 0
    
    MyMessage msgNEPA(0, V_STATUS);
    MyMessage msg(CHILD_ID, V_STATUS);
    MyMessage msgTank1(1, V_VOLUME);
    MyMessage msgTank2(8, V_VOLUME);
    
    boolean sensorState, lastSensorState;
    
    //constant for the sonar HC-SR04 sensor
    #define TRIGGER_PIN  8
    #define ECHO_PIN     7
    #define MAX_DISTANCE 300 //distance max measured
    
    //constant to calculate the volume of the tank
    #define high_water_level 30 //tank depth from the lowest point to the max water level
    #define distance_sensor 10 //was 30 = distance in cm between the max water level and the sensor
    #define tank_volume 1000 //tank volume (10000 default) when it is fitted to the max water level
    unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds)
    
    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    
    int lastDist;
    bool metric = true;
    
    void setup()
    {
      for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
        // Then set relay pins to output mode
        pinMode(pin, OUTPUT);
        // Set relay to last known state (using eeprom storage)
        digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
        DEBUG_PRINT(F("Initialised Relay: "));
        DEBUG_PRINT(F(sensor));
        DEBUG_PRINT(F(" at pin "));
        DEBUG_PRINT(F(pin));
        DEBUG_PRINT(F(" ("));
        DEBUG_PRINT(F(loadState(sensor)));
        DEBUG_PRINTLN(F(")"));
      }
        pinMode(A0, INPUT); // Then set etra sensor pins to input mode
        pinMode(A1, INPUT);   // Float Sensor for Gen fuel Tank
        pinMode(A2, INPUT);   // Spare Sensor Readout
        DEBUG_PRINTLN(F("Initialize additional Sensor: "));
    }
    
    void presentation() { 
    sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
    
    //  Manually present the additional 3 sensors
        present(0, S_BINARY);
        pinMode(A0, INPUT); // Then set relay pins in output mode  
        boolean savedState = loadState(A0); // Set relay to last known state (using eeprom storage)
        digitalWrite(A0, savedState?RELAY_ON:RELAY_OFF);
        DEBUG_PRINT(F("Presented Relay at : "));
        DEBUG_PRINTLN(F(A0));
        send(msg.set(savedState? 0 : 1));
    
    // Fetch relay status   
    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);
        pinMode(pin, OUTPUT); // Then set relay pins in output mode  
        savedState = loadState(pin); // Set relay to last known state (using eeprom storage)
        digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF);
        DEBUG_PRINT(F("Presented Relay st : "));
        DEBUG_PRINTLN(F(pin));
        send(msg.set(savedState? 0 : 1));
      }
        
    //    DEBUG_PRINTLN(F("Sensor Presentation Complete"));
    
    //  Manually present the additional 2 sensors
        present(6, S_WATER);
        pinMode(A1, INPUT); // Then set relay pins in output mode  
        savedState = loadState(A1); // Set relay to last known state (using eeprom storage)
        digitalWrite(A1, savedState?RELAY_ON:RELAY_OFF);
        DEBUG_PRINT(F("Presented Relay at : "));
        DEBUG_PRINTLN(F(A1));
        send(msg.set(savedState? 0 : 1));
    }
    void loop() 
    {
    // Alway process incoming messages whenever possible
    // Sleep until interrupt comes in on motion sensor. Send update every two minute.
    //  sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
    
      Serial.print("Fuel Level: ");
      Serial.print(tank_level("liters"));
      Serial.print("L");
      Serial.print("(");
      Serial.print(tank_level("percent"));
      Serial.println("%)");
      send(msgTank1.set(tank_level("liters")));
    
    if(digitalRead(A0) == LOW){
      DEBUG_PRINTLN(F("There is no Light"));
       Serial.println(sensorState? "Light On" : "Light Off");
       send(msgNEPA.set(sensorState?"0":"0"));  // Update gateway on change of state
      }
    else
      {
      DEBUG_PRINTLN(F("Up NEPA!"));
      Serial.println(sensorState? "Light Off" : "Light On");
      send(msgNEPA.set(sensorState?"1":"1"));  // Update gateway on change of state
      }
      wait(2000); // use for debuging
    }
    void receive(const MyMessage &message){
    // Change relay state if message is received
    if (digitalRead(A0) == HIGH){
    // Sensor says tank is full do something
    //  DEBUG_PRINTLN(F("Float Sensor High, 'Tank is Full'"));
    //  digitalWrite(3, LOW);  // Switch off tank pump
      saveState(message.sensor, message.getBool());
      bool send(MyMessage &msgNEPA, bool ack);
    
      }
      if (message.type == V_STATUS){
    //   digitalWrite(message.sensor, !digitalRead(message.sensor));
         if (message.getBool() == 0) {
         digitalWrite(message.sensor, LOW);
         // Store state in eeprom
         saveState(message.sensor, message.getBool());
          }
         else //getBool() == 1
         digitalWrite(message.sensor, HIGH);   
         // Store state in eeprom
         saveState(message.sensor, message.getBool());
         
    // digitalWrite(message.sensor-1+RELAY_PIN, message.getBool()?RELAY_ON:RELAY_OFF);
    
    // Write some debug info
       Serial.print("Incoming change for sensor ID: ");
       Serial.print(message.sensor);
       Serial.print(", New status: ");
       Serial.println(message.getBool());
        } 
      }
    
    int tank_level(String unit)   //function to measure and convert the tank volume
    {
    
      int level_liters = 0,level_percent = 0, echo_us = 0, echo_cm = 0, water_level_cm = 0;
    //  int level_liters,level_percent, echo_us, echo_cm, water_level_cm;
      echo_us = sonar.ping_median(); //return the median time of 5measures between the ping and the echo in microseconds
      echo_cm = echo_us / US_ROUNDTRIP_CM; //convert the echo time to distance in cm.
      water_level_cm = high_water_level - (echo_cm - distance_sensor);
      level_liters = water_level_cm * (tank_volume / high_water_level);
      level_percent = (water_level_cm * 100) / high_water_level;
      
      if (unit == "liters")
      {
        return level_liters;
      }
      else if (unit == "percent")
      {
        return level_percent;
      }
      else
      {
        return 0;
      }
    }
    

    0_1559969259570_Hardware Values.png
    0_1559969279829_Switches.png


  • Mod

    @eme look at the debug log from the node and the gateway.

    My guess is that they will show messages with st=fail, showing that the message didn't get through.

    If you haven't already, see https://forum.mysensors.org/topic/666/debug-faq-and-how-ask-for-help/ for a list of the most common problems and a troubleshooting flowchart.



  • @eme
    I am not sure if Domoticz can know, what you want.
    I am not MySensors expert, but there are some weird things in your code:

    #define CHILD_ID 0
    
    MyMessage msgNEPA(0, V_STATUS);
    MyMessage msg(CHILD_ID, V_STATUS);
    

    msg and msgNEPA are identical, but you use it in different ways ....

    MyMessage msgTank1(1, V_VOLUME);
    

    For CHILD ID 1 type of message V_VOLUME, but in loop:

    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);
        pinMode(pin, OUTPUT); // Then set relay pins in output mode  
        savedState = loadState(pin); // Set relay to last known state (using eeprom storage)
        digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF);
        DEBUG_PRINT(F("Presented Relay st : "));
        DEBUG_PRINTLN(F(pin));
        send(msg.set(savedState? 0 : 1));
      }
    

    You present CHILID ID 1 like S_BINARY

    And then you set to this binary sensor volume value:

    send(msgTank1.set(tank_level("liters")));
    

    And in the same presentation loop you send value only to child id 0, because msg.set send to ID 0



  • Did you activate the child(ren) through the "Setup" > "Devices" list? If yes, the arrow on the child(ren) should be oriented to the left side. If not, activate them, giving a name. You'll then see them on the associated tabs. Note that device type depends on the type sent during presentation (so don't expect being able to declare device as a switch if you don't use the right child type)



  • In addition to the remark on declaring sensor to Domoticz, the sketch itself seems to me a bit confusing.

    Normally, you should do setup into setup function (stuff like pin mode, even eventually reading saved config and setting relay in correct state), presentation should contain only sendsketchinfo() & present(), loop should do the repetitive job.

    You should avoid blocking the loop, especially if you want to receive messages (as you did).

    You're using relay from pin 2 to 6 but sensor 1 to 5, but when receiving an order to set sensor x, you set pin x (instead of x+1). Result may not exactly be what you want/imagine.

    You're turning pump off only when receiving a message (as the test is done here), instead in the loop.

    You're using digitalWrite() on some analog pin (A1) 😉

    You're sending udpdates every 2 seconds, while sending either changes or data at larger interval may fit better.

    In addition, you may also want to simplify:
    send(msgNEPA.set(sensorState?"0":"0")) by send(msgNEPA.set(0)) (in addition, this will send numeric value instead of string, don't think that sending a string is what you want)

    and

    send(msgNEPA.set(sensorState?"1":"1")) by send(msgNEPA.set(1))

    May I suggest you explain what you have, and what you want to do, for us to guide you setting the sensors properly?


Log in to reply
 

Suggested Topics

  • 5
  • 3
  • 1
  • 8
  • 1
  • 2

14
Online

11.4k
Users

11.1k
Topics

112.7k
Posts