Help Please "Rain Water Tank"



  • I have a problem with my sketch.
    Can somebody help me?

    // Enable debug prints
    #define MY_DEBUG
    #define MY_NODE_ID 103
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    
    #define MY_REPEATER_FEATURE
    
    #include <SPI.h>
    #include <MySensors.h>  
    #include <NewPing.h>
    
    #define CHILD_ID_V 0
    #define CHILD_ID_D 1
    #define TRIGGER_PIN  6  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN     5  // Arduino pin tied to echo pin on the ultrasonic sensor.
    #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
    
    #define RELAY_PIN  4  // Arduino Digital I/O pin number for first relay
    #define NUMBER_OF_RELAYS 1 // 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
    
    //constant to calculate the volume of the tank
    #define high_water_level 230 //tank depth from the lowest point to the max water level
    #define distance_sensor 10 //distance in cm between the max water level and the sensor
    #define tank_volume 15000 //tank volume when it is fitted to the max water level
    unsigned long SLEEP_TIME = 3000; // Sleep time between reads (in milliseconds)
    
    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    MyMessage volume_msg(CHILD_ID_V, V_VOLUME);
    MyMessage distance_msg(CHILD_ID_D, V_DISTANCE);
    int lastDist;
    bool metric = true;
    
    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);
        }
    }
    
    void setup()  
    { 
      metric = getControllerConfig().isMetric;
    }
    
    void presentation()
    {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Rain Water Tank", "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);
        }
      
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_V, S_WATER);
      present(CHILD_ID_D, S_DISTANCE);
    }
    
    void loop()      
    { 
      int dist = metric?sonar.ping_cm():sonar.ping_in();
      Serial.print("Volume ");
      Serial.print(tank_level("liters"));
      Serial.println("L");
      Serial.print("Ping: ");
      Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
      Serial.println(metric?" cm":" in");
    
      if (dist != lastDist) {
          send(distance_msg.set(dist));
          lastDist = dist;
      }
    
      send(volume_msg.set(tank_level("liters")));
      sleep(SLEEP_TIME);
    }
    
    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());
        }
    }
    
    //function to measure and convert the tank volume
    int tank_level(String unit)
    {
      int level_liters = 0,level_percent = 0, echo_us = 0, echo_cm = 0, water_level_cm = 0;
      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;
      }
    }

  • Mod

    @wuffi0815 probably. But it would be a whole lot easier if you told us what problem you are experiencing. We don't have many psychics here.



  • @mfalkvidd Nobody could see that coming, precisely underlining your response..... 👌



  • I want to know the filling level of the tank and drive a pump.

    Should everything also run in fhem.

    Components: Arduino; HC-SR04; 10 A 1 Channel Arduino Compatible Relay Module



  • @wuffi0815 You skipped right past "But it would be a whole lot easier if you told us what problem you are experiencing." and headed out into the desert....



  • The distance is not displayed. And goes into a loop.
    Sometimes I have to refresh 2-3x

    no Connect: !TSF:MSG:SEND,102-103-0-0,s=1,c=1,t=35,pt=2,l=2,sg=0,ft=5,st=NACK:2457



  • @wuffi0815 "The distance is not displayed. And goes into a loop.
    Sometimes I have to refresh 2-3x"
    ?
    That's all? Oh well, seems simple really, you just change A for B, and give C a slight squeeze, but only on the left side, then give it a wallop with a cotton bud, almost guaranteed to fix it....
    Taking the mickey aside, please give details, unless the complete absence of information is deliberate...


  • Admin

    Please keep a nice tone folks.

    You seem to present sensors with the same id twice. CHILD_ID_D=1 (S_DISTANCE) and in the loop sensor starts with id 1 also (S_BINARY).



  • Thank You Is working!
    From time to time you overlook small errors with great effect. 🙂



  • Works fine so far but the relay can not be controlled after about 30min.

    Can it be that my arduino nano pro is too weak?

    // Enable debug prints
    #define MY_DEBUG
    #define MY_NODE_ID 103
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    
    #define MY_REPEATER_FEATURE
    
    #include <SPI.h>
    #include <MySensors.h>  
    #include <NewPing.h>
    
    #define CHILD_ID_V 2
    #define CHILD_ID_D 3
    #define TRIGGER_PIN  6  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN     5  // Arduino pin tied to echo pin on the ultrasonic sensor.
    #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
    
    #define RELAY_PIN  4  // Arduino Digital I/O pin number for first relay
    #define NUMBER_OF_RELAYS 1 // 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
    
    //constant to calculate the volume of the tank
    #define high_water_level 230 //tank depth from the lowest point to the max water level
    #define distance_sensor 10 //distance in cm between the max water level and the sensor
    #define tank_volume 15000 //tank volume when it is fitted to the max water level
    unsigned long SLEEP_TIME = 36000; // Sleep time between reads (in milliseconds)3600000 = 1h
    
    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    MyMessage volume_msg(CHILD_ID_V, V_VOLUME);
    MyMessage distance_msg(CHILD_ID_D, V_DISTANCE);
    int lastDist;
    bool metric = true;
    
    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);
        }
    }
    
    void setup()  
    { 
      metric = getControllerConfig().isMetric;
    }
    
    void presentation()
    {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Rain Water Tank", "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);
        }
      
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_V, S_WATER);
      present(CHILD_ID_D, S_DISTANCE);
    }
    
    void loop()      
    { 
      int dist = metric?sonar.ping_cm():sonar.ping_in();
      Serial.print("Volume ");
      Serial.print(tank_level("liters"));
      Serial.println("L");
      Serial.print("Ping: ");
      Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
      Serial.println(metric?" cm":" in");
    
      if (dist != lastDist) {
          send(distance_msg.set(dist));
          lastDist = dist;
      }
    
      send(volume_msg.set(tank_level("liters")));
      sleep(SLEEP_TIME);
    }
    
    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());
        }
    }
    
    //function to measure and convert the tank volume
    int tank_level(String unit)
    {
      int level_liters = 0,level_percent = 0, echo_us = 0, echo_cm = 0, water_level_cm = 0;
      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;
      }
    }
    


  • Provide a detailed sketch/circuit and then I am sure we can find the answer. It might be that your power circuit is not adequate, but it is difficult to ascertain until one can review the diagram.



  • Sorry for my bad drawing skills.
    0_1516210628692_Diagram.jpg



  • So what is giving 5v to this?


 

484
Online

7.9k
Users

8.7k
Topics

93.6k
Posts