New scketch for water level + control valves



  • Hi,

    As the title already mentioned, I would like to build a sensor which will measure the waterlevel and send it to my gateway. If the waterlevel is sufficient, my gateway has to send the signal to open the valve and start the pump.
    I would like to use a combination of 2 scripts (Relay and distance sensor)

    I have tested the sketch, but it only returns a distance when I press a button in Domoticz.

    The sketch I've used:

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     * 
     * DESCRIPTION
     * Example sketch showing how to control physical relays. 
     * This example will remember relay state after power failure.
     * http://www.mysensors.org/build/relay
     */ 
    
    #include <MySigningNone.h>
    #include <MyTransportNRF24.h>
    #include <MyTransportRFM69.h>
    #include <MyHwATMega328.h>
    #include <MySensor.h>
    #include <SPI.h>
    #include <NewPing.h>
    
    
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 4 // 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 CHILD_ID 1
    #define TRIGGER_PIN  8  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN     7  // Arduino pin tied to echo pin on the ultrasonic sensor.
    #define MAX_DISTANCE 300 // Maximum distance we want to ping 
    unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds)
    
    
    
    // NRFRF24L01 radio driver (set low transmit power by default) 
    MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);  
    //MyTransportRFM69 radio;
    // Message signing driver (none default)
    //MySigningNone signer;
    // Select AtMega328 hardware profile
    MyHwATMega328 hw;
    // Construct MySensors library
    
    MySensor gw(radio, hw);
    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 
    MyMessage msg(CHILD_ID, V_DISTANCE);
    int lastDist;
    boolean metric = true; 
    
    
    void setup()  
    {   
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, AUTO, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay", "1.0");
    
      // Fetch relay status
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        gw.present(sensor, S_LIGHT);
        gw.present(CHILD_ID, S_DISTANCE);
        boolean metric = gw.getConfig().isMetric;
    
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
    
      }
    }
    
    
    void loop() 
    {
      // Alway process incoming messages whenever possible
      gw.process();
      int dist = metric?sonar.ping_cm():sonar.ping_in();
      Serial.print("Ping: ");
      Serial.print(dist); // Convert ping time to distance in cm and print result 
      Serial.println(metric?" cm":" in");
    
      if (dist != lastDist) {
          gw.send(msg.set(dist));
          lastDist = dist;
    
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.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());
       } 
      gw.sleep(SLEEP_TIME);
    }
    

    Please could you help me, to combine the sketches and help me with my learnig curve 😉


  • Contest Winner

    Hello @Naitsirhc,

    I've put the correct tags around your sketch. I'm currently @work so I'm not able to have a in depth look at your sketch.


  • Hardware Contributor

    @Naitsirhc Hi!
    Have a look at https://forum.mysensors.org/topic/4651/my-basement-flooding-alarm/. This is exactly what @GertSanders posted some days ago. There is a .ino file with code attached at the bottom of the post.

    The problem with your code might be that you only send values when it has changed. So if the value is the same all the time (no flood) it will not send any new value.

    if (dist != lastDist) {
    

    Also check serial log to see that its not 0 or any other error value all the time coming from distance sensor.

    Another tip might be adding a wait(50); after or before reading the distance and see if that makes any difference.



  • Thank You all,

    I will try the sketch (and try to understand it....LOL)
    I need only need to measure the water level once or twice a day. So I will figure it out how it has to be done.

    Thanks again



  • Hi back again,

    Tried to upload the sketch. unfortunatly the copiler returns an error:
    'sendSketchInfo' was not declared in this scope
    Googled for an answer. And found an solution, for installing a new library.
    I installed:
    MySensors 2.0.0
    Mysensors_development
    But it doesn't work.

    Can you help me please



  • Did a check with my other sketches a adapted the basement fooding alarm sketch:

    /*
      Ultrasonic Sensor - based on a MySensors example (actuator)
    
      modified 30 august 2016
      version 1.2
      by Gert Sanders
    
      Uses 2.0.0-dev version of the library
    
    */
    
    // Enable debug prints to serial monitor
    
    //#define MY_DEBUG
    //#define MY_DEBUG_VERBOSE
    
    //#define MY_NODE_ID 123
    #define MY_NODE_ID 32
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    // possible values: RF24_PA_LOW (is default on gateway), RF24_PA_MED, RF24_PA_HIGH, RF24_PA_MAX (is default on nodes)
    #define MY_RF24_PA_LEVEL RF24_PA_MAX
    
    // RF channel for the sensor net, 0-127
    
    #define MY_RF24_CHANNEL     80  // just my choice
    
    
    //RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps
    #define MY_RF24_DATARATE      RF24_250KBPS
    
    
    // defines for sensors
    #define PUMP          1
    #define WATERLEVEL    2
    
    
    // Enable repeater functionality for this node
    //#define MY_REPEATER_FEATURE
    
    
    // All includes
    #include <MySensor.h>
    #include <NewPing.h>
    #include <SPI.h>
    
    // Pin definitions and other defines
    #define TRIGGER_PIN  5  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN     6  // Arduino pin tied to echo pin on the ultrasonic sensor.
    #define RELAYPIN     7
    //#define LEDPIN       8
    
    #define PumpOFFLimit    2
    #define PumpONLimit     12
    
    #define MAX_DISTANCE 255 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
    
    #define NUMBEROFSAMPLES 100
    
    
    // constants
    
    
    // variables
    boolean  PumpStatus = false;
    boolean  oldPumpStatus = false;
    
    unsigned int uS = 0;
    byte Distance = 0;
    byte LastMeasuredDistance = 0;
    byte MaxMeasuredDistance = 0;
    
    
    
    // Instantiate objects
    MySensor gw;
    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    MyMessage PumpMsg(PUMP, V_LIGHT);
    MyMessage DistanceMsg(WATERLEVEL, V_DISTANCE);
    
    
    
    void presentation()
    {
       gw.begin();
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("BasementAlarmUltrasonic", "1.1");
      gw.wait(500); // delay needed by Raspi/domotics
      gw.present(PUMP, S_LIGHT);
      gw.wait(500); // delay needed by Raspi/domotics
      gw.present(WATERLEVEL, S_DISTANCE);
      gw.wait(500); // delay needed by Raspi/domotics
    }
    
    
    
    
    
    // supporting functions.
    //void blipled()
    //{
    //  digitalWrite(LEDPIN, !PumpStatus);   // turn the LED on (HIGH is the voltage level)
    //  wait(50);              // wait for a bit
    //  digitalWrite(LEDPIN, PumpStatus);    // turn the LED off by making the voltage HIGH
    //  wait(150);              // wait for a bit
    // digitalWrite(LEDPIN, !PumpStatus);   // turn the LED on (HIGH is the voltage level)
    //  wait(50);              // wait for a bit
    //  digitalWrite(LEDPIN, PumpStatus);    // turn the LED off by making the voltage HIGH
    //}
    
    
    
    
    
    
    void receive(const MyMessage & message)
    {
    
    
    #ifdef MY_DEBUG
      // Write some debug info
      Serial.print(F("Incoming change for sensor:"));
      Serial.print(message.sensor);
    #endif
    
    
      if (message.type == V_LIGHT)
      {
        // Change relay state
        oldPumpStatus = PumpStatus;
        PumpStatus = message.getBool();
        digitalWrite(RELAYPIN, PumpStatus ? HIGH : LOW);
        // Store state in eeprom
        saveState(PUMP, PumpStatus);
    
    #ifdef MY_DEBUG
        Serial.print(F("Changing PUMP status to : "));
        Serial.println(PumpStatus);
    #endif
    
      }
    
    
    }
    
    
    
    
    /////////////////////////////////////////////////////////////////////////
    // the setup function runs once when you press reset or power the board
    /////////////////////////////////////////////////////////////////////////
    void setup()
    {
      PumpStatus = false;
      oldPumpStatus = false;
    
      // initialize digital LEDpin as an output.
    //  pinMode(LEDPIN, OUTPUT);
      //digitalWrite(LEDPIN, HIGH);    // turn the LED on by making the voltage HIGH
    
      // initialize digital Pumppin as an output.
      pinMode(RELAYPIN, OUTPUT);
      digitalWrite(RELAYPIN, LOW);    // turn the LED off by making the voltage LOW
    
      PumpStatus = loadState(PUMP);
      digitalWrite(RELAYPIN, PumpStatus);
      send(PumpMsg.set(PumpStatus == true ? 1 : 0));
      wait(500);
    
      MaxMeasuredDistance = loadState(WATERLEVEL);
    
    //  digitalWrite(LEDPIN, LOW);                   // turn the LED off by making the voltage LOW
    
    }
    
    
    
    
    
    
    
    
    
    
    ///////////////////////////////////////////////////////
    // the loop function runs over and over again forever
    ///////////////////////////////////////////////////////
    
    void loop()
    {
      blipled();
    
      uS = sonar.ping_median(NUMBEROFSAMPLES); // Send ping, get ping time in microseconds (uS).
    
      Distance = uS / US_ROUNDTRIP_CM;
    
      if (Distance >= MaxMeasuredDistance)
      {
        MaxMeasuredDistance = Distance;
        if (Distance >= loadState(WATERLEVEL))
        {
          saveState(WATERLEVEL, Distance);
        }
      }
    
      if (Distance != LastMeasuredDistance)
      {
        if (send(DistanceMsg.set((MaxMeasuredDistance - Distance), 0)))
        {
          LastMeasuredDistance = Distance;
          wait(500);
        }
      }
    
    #ifdef MY_DEBUG
      Serial.print("Ping: ");
      Serial.print(Distance); // Convert ping time to distance in cm and print result (0 = outside set distance range)
      Serial.print("cm");
      Serial.print("    Max: ");
      Serial.print(MaxMeasuredDistance); // Convert ping time to distance in cm and print result (0 = outside set distance range)
      Serial.println("cm");
    #endif
    
    
      if ((MaxMeasuredDistance - Distance) >= PumpONLimit)
      {
        SetPumpOn();
      }
    
      if ((MaxMeasuredDistance - Distance) <= PumpOFFLimit)
      {
        SetPumpOff();
      }
    
      if (oldPumpStatus != PumpStatus)
      {
    
        if (send(PumpMsg.set(PumpStatus == true ? 1 : 0)))
        {
          oldPumpStatus = PumpStatus;
          wait(500); // delay needed by Raspi/domotics
        }
      }
    
    
    }
    
    
    
    
    void SetPumpOn()
    {
    
      digitalWrite(RELAYPIN, HIGH);
      oldPumpStatus = PumpStatus;
      PumpStatus = true;
      saveState(PUMP, PumpStatus);
    
    }
    
    
    
    void SetPumpOff()
    {
    
      digitalWrite(RELAYPIN, LOW);
      oldPumpStatus = PumpStatus;
      PumpStatus = false;
      saveState(PUMP, PumpStatus);
    
    }
    

    I turned of the LED pin (I don't need it)

    The compiler does his job till the last line:
    saveState(PUMP, PumpStatus);

    which returns:
    exit status 1
    'saveState' was not declared in this scope

    What can I do to make this error disappear

    Thanks in advance


  • Contest Winner

    @Naitsirhc Your running MySensors 1.5 ??? try:

      gw.saveState(WATERLEVEL, Distance);
    


  • Hi

    First of all, thanks for all the answers.

    Started from skratch again. Downloaded the new arduino IDE.Some Libraries (NewPing and Mysensors) and finaly the sketch compiled 😄
    I uploaded it to the Nano and then expected to see something on the serial monitor....

    Too bad...Oh right. removed slashes for MyDebug and MyDebug_Verbose...

    Presto.

    Unfortunatly I see in serial monitor:

    Starting sensor (RNNNA-, 2.0.0)
    TSM:INIT
    TSM:RADIO:OK
    TSP:ASSIGNID:OK (ID=32)
    TSM:FPAR
    TSP:MSG:SEND 32-32-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSM:FPAR
    TSP:MSG:SEND 32-32-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSM:FPAR
    TSP:MSG:SEND 32-32-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSM:FPAR
    TSP:MSG:SEND 32-32-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    !TSM:FPAR:FAIL
    !TSM:FAILURE
    TSM:PDT
    TSM:INIT
    

    Too bad. What have I done wrong? Is it possible to see the distance in serial monitor and to see if a relay turned on or off?

    EDIT:
    Upgraded my Gateway also to 2.0

    Stil no succes 😞

    Output from Gateway:

    0;255;3;0;9;Starting gateway (RNNGA-, 2.0.0)
    0;255;3;0;9;TSM:INIT
    0;255;3;0;9;TSM:RADIO:OK
    0;255;3;0;9;TSM:GW MODE
    0;255;3;0;9;TSM:READY
    0;255;3;0;14;Gateway startup complete.
    0;255;0;0;18;2.0.0
    0;255;3;0;11;GateWay
    0;255;3;0;12;1.0
    0;255;3;0;9;No registration required
    0;255;3;0;9;Init complete, id=0, parent=0, distance=0, registration=1
    0;255;3;0;9;TSP:SANCHK:OK
    0;255;3;0;9;TSP:SANCHK:OK
    0;255;3;0;9;TSP:SANCHK:OK
    0;255;3;0;9;TSP:SANCHK:OK
    0;255;3;0;9;TSP:SANCHK:OK
    


  • update:

    Adapted the old distance sensor sketch to the 2.0 library and upladed it and connected to my PC. Also connected the gateway 2.0 to raspberry (Domoticz).....
    Allright it works....I do get distance in my serial monitor and in domoticz.

    Now try to upload the basement alarm sketch...See what happens...


Log in to reply
 

Suggested Topics

16
Online

11.4k
Users

11.1k
Topics

112.7k
Posts