Reporting an independent float switch and relay



  • I've got some code which uses an arduino to control a relay (12v motor) based on a float switch position. It is essentially "standalone" right now and works fine. I've copied the code below.

    I'd like to convert it to be of use via mySensors so that it can report its activity to the gateway but continues to act independently.

    I've tried to take the example mySensor code for a relay actuator, and inserted what I thought to be the relevant sections for presenting itself to the gw (and removed the parts on receiving commands) - and while the code compiles and loads - the unit does not function.

    I've wired up the radio - but not got a gw or controller turned on at the moment - but I was expecting the unit to operate regardless.

    Is this possible? If so can someone point me in the right direction please. Many thanks

    /***********************************************************************************
     * Rollermat control
     *
     *
     *  Wiring
     *  Pin 3 - Float Switch
     *  Pin 4 - Relay to 12v motor
     *  Pin 7 - Green LED 
     *  Pin 8 - Red LED
     *
     *  Control Behavior:
     *    If the float switch is floating (i.e. rollermat is clogged) then turn on the pump and green led
     *    If the float switch is not floating (i.e. rollermat is clean) turn off the pump and green LED and turn on the red LED
     *
     **********************************************************************************
    */
    
    
    
    //define the input/output pins
    #define FLOAT_SWITCH_PIN 3
    #define MOTOR_1_PIN 4
    #define LED_PIN 7
    #define LED_RED 8
    #define CHILD_ID 1   // Id of the sensor child
    
    
    
    
    
    //setup runs once
    void setup()
    {
      //setup input pins for float switch 
      
      pinMode(FLOAT_SWITCH_PIN, INPUT);
      
      
      //setup output pins for relay and LED board
      pinMode(MOTOR_1_PIN, OUTPUT);
      pinMode(LED_PIN, OUTPUT);
      pinMode(LED_RED, OUTPUT);
    
    }
    
    
    
    //loop() runs indefinitely 
    void loop()
    {
     
    
    
      // LOW corresponds to the float switch being at its highest point (i.e. rollermat is clogged)
      if(digitalRead(FLOAT_SWITCH_PIN) == LOW)
      {
         digitalWrite(MOTOR_1_PIN, HIGH); //turn on the motor
         digitalWrite(LED_PIN, LOW);    //turn on the Green LED
         digitalWrite(LED_RED, HIGH);    //turn of the Red LED
      }
      
      //otherwise the float switch is HIGH
      // HIGH corresponds to the float switch being at its lowest point (i.e. rollermat is clean and water is flowing)
      else
      {
         digitalWrite(MOTOR_1_PIN, LOW); //turn off the pump
         digitalWrite(LED_PIN, HIGH);    //turn off the Green LED
         digitalWrite(LED_RED, LOW);    //turn on the Red LED
      }
    }```

  • Mod

    @stkilda you'll need to use

    #define 	MY_TRANSPORT_WAIT_READY_MS (3000)
    

    where 3000 is the timeout value in milliseconds. The default value is 0, which means wait forever.
    Documentation: https://www.mysensors.org/apidocs/group__RoutingNodeSettingGrpPub.html#ga45b4f1c868b8cb5d7396cdfb4939559c

    You can search the forum for MY_TRANSPORT_WAIT_READY_MS to see earlier discussions on how to use this feature.



  • Thanks. But I am still not getting any joy. Here is the full sketch. When I comment out all the ySensor related lines the sketch works. As before, at the moment I have no controller or gw running - but as far as I can see this should not prevent the relay and LED from operating. I'm new to this so perhaps am making a basic error. Thanks.

    
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    
    // Enable repeater functionality for this node
    //#define MY_REPEATER_FEATURE
    
    #include <MyConfig.h>
    #include <MySensors.h>
    #include <SPI.h>
    
    #define RELAY_PIN 4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    
    #define FLOAT_SWITCH_PIN 3 //FLOAT SWITCH PIN
    
    #define LED_GRN_PIN 7
    #define LED_RED_PIN 8
    #define MY_TRANSPORT_WAIT_READY_MS (3000)
    #define CHILD_ID 1
    #define RELAY_ON 1
    #define RELAY_OFF 0
    
    
    MyMessage msg(CHILD_ID, V_STATUS);
    uint8_t value = RELAY_ON;
    
    void setup()
    {
        pinMode(RELAY_PIN, OUTPUT);
        pinMode(FLOAT_SWITCH_PIN, INPUT);
        // Set relay to last known state (using eeprom storage)
        pinMode(LED_GRN_PIN, OUTPUT);
        pinMode(LED_RED_PIN, OUTPUT);
    }
    
    void presentation()
    {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Rollermat", "3.0");
      
        // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID, S_BINARY);
      
    }
    
    
    void loop()
    {
    
    
    
      // LOW corresponds to the float switch being at its highest point (i.e. rollermat is clogged)
      if(digitalRead(FLOAT_SWITCH_PIN) == LOW)
      {
         digitalWrite(RELAY_PIN, RELAY_ON); //turn on the motor
         digitalWrite(LED_GRN_PIN, LOW);    //turns on the Green LED
         digitalWrite(LED_RED_PIN, HIGH);    //turns off the Red LED
      }
      
      //otherwise the float switch is HIGH
      // HIGH corresponds to the float switch being at its lowest point (i.e. rollermat is clean and water is flowing)
      else
      {
         digitalWrite(RELAY_PIN, RELAY_OFF); //turns off the pump
         digitalWrite(LED_GRN_PIN, HIGH);    //turns off the Green LED
         digitalWrite(LED_RED_PIN, LOW);    //turns on the Red LED
      }
      
        value = value == RELAY_ON ? RELAY_OFF:RELAY_ON;
        send(msg.set(value));
    }
    


  • Update: I booted up the gateway and controller. The above sensor/actuator sketch then operated normally. I then powered down the gateway - and the sensor/actuator continued to operate independently. I'm guessing that it needed to present itself at least once.


  • Mod

    @stkilda yes. The debug log probably showed that the node was trying to get a node id from the controller. With no controller, MY_NODE_ID must be defined.



  • Ha. Another rookie error - the sketch is sending the current state on every loop so it's flooding the gateway. I need to modify so it only sends when it changes state. Can you point me to an example that does this and I will adjust to my sketch?



  • Success. stole some code from a post a while ago: No concept of how efficient this is, but hey...

    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-2018 Sensnology AB
       Full contributor list: https://github.com/mysensors/MySensors/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
    */
    
    
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    
    // Enable repeater functionality for this node
    //#define MY_REPEATER_FEATURE
    
    #include <MyConfig.h>
    #include <MySensors.h>
    #include <SPI.h>
    
    #define RELAY_PIN 4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    
    #define FLOAT_SWITCH_PIN 3 //FLOAT SWITCH PIN
    
    #define LED_GRN_PIN 7
    #define LED_RED_PIN 8
    #define MY_TRANSPORT_WAIT_READY_MS (3000)
    #define CHILD_ID 4
    #define RELAY_ON 1
    #define RELAY_OFF 0
    
    boolean lastSensorState;
    unsigned long lastUpdate;
    
    MyMessage msg(CHILD_ID, V_STATUS);
    
    void setup()
    {
        pinMode(RELAY_PIN, OUTPUT);
        pinMode(FLOAT_SWITCH_PIN, INPUT);
        // Define LED
        pinMode(LED_GRN_PIN, OUTPUT);
        pinMode(LED_RED_PIN, OUTPUT);
    }
    
    void presentation()
    {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Rollermat", "3.0");
      
        // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID, S_BINARY);
      
    }
    
    
    void loop()
    {
    
    
    
      // LOW corresponds to the float switch being at its highest point (i.e. rollermat is clogged)
      if(digitalRead(FLOAT_SWITCH_PIN) == LOW)
      {
         digitalWrite(RELAY_PIN, RELAY_ON); //turn on the motor
         digitalWrite(LED_GRN_PIN, LOW);    //turns on the Green LED
         digitalWrite(LED_RED_PIN, HIGH);    //turns off the Red LED
      }
      
      //otherwise the float switch is HIGH
      // HIGH corresponds to the float switch being at its lowest point (i.e. rollermat is clean and water is flowing)
      else
      {
         digitalWrite(RELAY_PIN, RELAY_OFF); //turns off the pump
         digitalWrite(LED_GRN_PIN, HIGH);    //turns off the Green LED
         digitalWrite(LED_RED_PIN, LOW);    //turns on the Red LED
      }
      
      boolean sensorState = digitalRead(FLOAT_SWITCH_PIN);
      if (sensorState != lastSensorState)
      {
        #ifdef DEBUG
          digitalWrite(FLOAT_SWITCH_PIN,sensorState? HIGH : LOW); 
          Serial.println(sensorState? "Motor On" : "Motor Off");
        #endif
        send(msg.set(sensorState?"1":"0"));  // Update gateway on change of state
        lastSensorState = sensorState;
      }
      
    }
    
    
    

Log in to reply
 

Suggested Topics

  • 8
  • 7
  • 1
  • 44
  • 2
  • 1

12
Online

11.4k
Users

11.1k
Topics

112.7k
Posts