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. My Project
  3. Help Please "Rain Water Tank"

Help Please "Rain Water Tank"

Scheduled Pinned Locked Moved My Project
13 Posts 6 Posters 2.6k Views 4 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.
  • W Wuffi0815

    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;
      }
    }
    mfalkviddM Offline
    mfalkviddM Offline
    mfalkvidd
    Mod
    wrote on last edited by mfalkvidd
    #2

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

    zboblamontZ 1 Reply Last reply
    1
    • mfalkviddM mfalkvidd

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

      zboblamontZ Offline
      zboblamontZ Offline
      zboblamont
      wrote on last edited by
      #3

      @mfalkvidd Nobody could see that coming, precisely underlining your response..... :ok_hand:

      1 Reply Last reply
      0
      • W Offline
        W Offline
        Wuffi0815
        wrote on last edited by Wuffi0815
        #4

        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

        zboblamontZ 1 Reply Last reply
        0
        • W Wuffi0815

          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

          zboblamontZ Offline
          zboblamontZ Offline
          zboblamont
          wrote on last edited by
          #5

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

          1 Reply Last reply
          0
          • W Offline
            W Offline
            Wuffi0815
            wrote on last edited by Wuffi0815
            #6

            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

            zboblamontZ 1 Reply Last reply
            0
            • W Wuffi0815

              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

              zboblamontZ Offline
              zboblamontZ Offline
              zboblamont
              wrote on last edited by
              #7

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

              1 Reply Last reply
              0
              • hekH Offline
                hekH Offline
                hek
                Admin
                wrote on last edited by
                #8

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

                1 Reply Last reply
                3
                • W Offline
                  W Offline
                  Wuffi0815
                  wrote on last edited by
                  #9

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

                  1 Reply Last reply
                  0
                  • W Offline
                    W Offline
                    Wuffi0815
                    wrote on last edited by Wuffi0815
                    #10

                    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;
                      }
                    }
                    
                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Junglejake
                      wrote on last edited by
                      #11

                      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.

                      1 Reply Last reply
                      0
                      • W Offline
                        W Offline
                        Wuffi0815
                        wrote on last edited by
                        #12

                        Sorry for my bad drawing skills.
                        0_1516210628692_Diagram.jpg

                        1 Reply Last reply
                        0
                        • skywatchS Offline
                          skywatchS Offline
                          skywatch
                          wrote on last edited by
                          #13

                          So what is giving 5v to this?

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          32

                          Online

                          11.7k

                          Users

                          11.2k

                          Topics

                          113.1k

                          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