mysprinkler for mysensor and home assistant not working together



  • first of all i must admit that i'm quite a noob. so please forgive if my questions seem stupid.

    i built the irrigation system board, and it seems to work electronically. i also built a serial gateway with the same 3.3v arduino pro mini that i used for the irrigation controller.

    the serial gateway is connected to the usb (with a serial - rs232 converter which also powers the arduino and the nrf2401 with 3.3v). i added the gateway to configuration.yaml

    mysensors:
      gateways:
        - device: '/dev/ttyUSB0'
          baud_rate: 38400
          persistence_file: 'home/homeassistant/mysensors.json'
      optimistic: false
      persistence: true
      retain: true
      version: '2.0'
    

    the sensor.yaml has this added:

      - platform: mysensors
        port: "/dev/ttyUSB0"
    

    the serial debug of the irrigation controller looks like that:

    0 MCO:BGN:INIT NODE,CP=RNNNA--,VER=2.1.1
    4 TSM:INIT
    4 TSF:WUR:MS=0
    12 TSM:INIT:TSP OK
    14 TSF:SID:OK,ID=2
    16 TSM:FPAR
    86 TSF:MSG:SEND,2-2-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    946 TSF:MSG:READ,0-0-2,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    952 TSF:MSG:FPAR OK,ID=0,D=1
    2095 TSM:FPAR:OK
    2095 TSM:ID
    2097 TSM:ID:OK
    2099 TSM:UPL
    2107 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    2162 TSF:MSG:READ,0-0-2,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    2168 TSF:MSG:PONG RECV,HP=1
    2170 TSM:UPL:OK
    2172 TSM:READY:ID=2,PAR=0,DIS=1
    2179 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    2240 TSF:MSG:READ,0-0-2,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    2250 TSF:MSG:SEND,2-2-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.1.1
    2265 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    2400 TSF:MSG:READ,0-0-2,s=255,c=3,t=6,pt=0,l=1,sg=0:M
    2410 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=11,pt=0,l=11,sg=0,ft=0,st=OK:MySprinkler
    2426 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:2.2
    2437 TSF:MSG:SEND,2-2-0-0,s=0,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
    2447 TSF:MSG:SEND,2-2-0-0,s=1,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
    2490 TSF:MSG:SEND,2-2-0-0,s=2,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
    2535 TSF:MSG:SEND,2-2-0-0,s=3,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
    2584 TSF:MSG:SEND,2-2-0-0,s=4,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
    2590 MCO:REG:REQ
    2609 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
    2756 TSF:MSG:READ,0-0-2,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    2762 MCO:PIM:NODE REG=1
    2764 MCO:BGN:STP
    Initialising...
    Turning All Valves Off...
    Checking EEPROM for stored date:
    Retreiving last run time from EEPROM...
    Sensor Presentation Complete
    Ready...
    3389 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=1,pt=0,l=0,sg=0,ft=0,st=OK:
    Requesting time from Gateway:
    3467 TSF:MSG:READ,0-0-2,s=255,c=3,t=1,pt=0,l=10,sg=0:1515859257
    Time value received and updated...
    Clock updated....
    Sensor's time currently set to: 04:00pm
    1/13/2018
    Saturday 
    Calling for Valve 1 Data...
    4403 TSF:MSG:SEND,2-2-0-0,s=1,c=2,t=24,pt=0,l=0,sg=0,ft=0,st=OK:
    4462 TSF:MSG:SEND,2-2-0-0,s=1,c=2,t=24,pt=0,l=0,sg=0,ft=0,st=OK:
    4526 TSF:MSG:SEND,2-2-0-0,s=1,c=2,t=24,pt=0,l=0,sg=0,ft=0,st=OK:
    4587 TSF:MSG:SEND,2-2-0-0,s=1,c=2,t=24,pt=0,l=0,sg=0,ft=0,st=OK:```
    

    there seems to be some communication between the hub and the sensor which is ok. but i don't see any mysensor sensors in home assistant and i don't understand what the log errors below mean, or how to get rid of them.

    Jan 13 14:01:13 hassbian hass[2451]: 2018-01-13 14:01:13 DEBUG (Thread-13) [mysensors] n:0 c:255 t:3 s:9 p:TSF:MSG:SEND,0-0-2-2,s=255,c=3,t=6,pt=0,l=1,sg=0,ft=0,st=OK:M#0
    Jan 13 14:01:14 hassbian hass[2451]: 2018-01-13 14:01:14 DEBUG (Thread-13) [mysensors] n:0 c:255 t:3 s:9 p:TSF:MSG:READ,2-2-0,s=255,c=3,t=11,pt=0,l=11,sg=0:MySprinkler#0
    Jan 13 14:01:14 hassbian hass[2451]: 2018-01-13 14:01:14 DEBUG (Thread-13) [homeassistant.components.mysensors] Node update: node 2 child 255#0
    Jan 13 14:01:14 hassbian hass[2451]: 2018-01-13 14:01:14 DEBUG (Thread-13) [homeassistant.components.mysensors] Not a child update for node 2#0
    Jan 13 14:01:14 hassbian hass[2451]: 2018-01-13 14:01:14 DEBUG (Thread-13) [mysensors] Handle queue with call <bound method SerialGateway.logic of SerialGateway>(('2;255;3;0;11;MySprinkler\n',), {}) took 0.200 seconds#0
    Jan 13 14:01:14 hassbian hass[2451]: 2018-01-13 14:01:14 DEBUG (Thread-13) [mysensors] n:0 c:255 t:3 s:9 p:TSF:MSG:READ,2-2-0,s=255,c=3,t=12,pt=0,l=3,sg=0:2.2#0
    Jan 13 14:01:14 hassbian hass[2451]: 2018-01-13 14:01:14 DEBUG (Thread-13) [homeassistant.components.mysensors] Node update: node 2 child 255#0
    Jan 13 14:01:14 hassbian hass[2451]: 2018-01-13 14:01:14 DEBUG (Thread-13) [homeassistant.components.mysensors] Not a child update for node 2#0
    Jan 13 14:01:14 hassbian hass[2451]: 2018-01-13 14:01:14 DEBUG (Thread-13) [mysensors] n:0 c:255 t:3 s:9 p:TSF:MSG:READ,2-2-0,s=0,c=0,t=3,pt=0,l=0,sg=0:#0
    Jan 13 14:01:14 hassbian hass[2451]: #033[33m2018-01-13 14:01:14 WARNING (Thread-13) [mysensors] child_id 0 already exists in children of node 2, cannot add child#0
    Jan 13 14:01:14 hassbian hass[2451]: 2018-01-13 14:01:14 DEBUG (Thread-13) [mysensors] n:0 c:255 t:3 s:9 p:TSF:MSG:READ,2-2-0,s=1,c=0,t=3,pt=0,l=0,sg=0:#0
    Jan 13 14:01:14 hassbian hass[2451]: #033[33m2018-01-13 14:01:14 WARNING (Thread-13) [mysensors] child_id 1 already exists in children of node 2, cannot add child#0
    Jan 13 14:01:14 hassbian hass[2451]: 2018-01-13 14:01:14 DEBUG (Thread-13) [mysensors] n:0 c:255 t:3 s:9 p:TSF:MSG:READ,2-2-0,s=2,c=0,t=3,pt=0,l=0,sg=0:#0
    Jan 13 14:01:14 hassbian hass[2451]: #033[33m2018-01-13 14:01:14 WARNING (Thread-13) [mysensors] child_id 2 already exists in children of node 2, cannot add child#0
    Jan 13 14:01:14 hassbian hass[2451]: 2018-01-13 14:01:14 DEBUG (Thread-13) [mysensors] n:0 c:255 t:3 s:9 p:TSF:MSG:READ,2-2-0,s=3,c=0,t=3,pt=0,l=0,sg=0:#0
    Jan 13 14:01:14 hassbian hass[2451]: #033[33m2018-01-13 14:01:14 WARNING (Thread-13) [mysensors] child_id 3 already exists in children of node 2, cannot add child#0
    Jan 13 14:01:14 hassbian hass[2451]: 2018-01-13 14:01:14 DEBUG (Thread-13) [mysensors] n:0 c:255 t:3 s:9 p:TSF:MSG:READ,2-2-0,s=4,c=0,t=3,pt=0,l=0,sg=0:#0
    Jan 13 14:01:14 hassbian hass[2451]: #033[33m2018-01-13 14:01:14 WARNING (Thread-13) [mysensors] child_id 4 already exists in children of node 2, cannot add child#0
    Jan 13 14:01:14 hassbian hass[2451]: 2018-01-13 14:01:14 DEBUG (Thread-13) [mysensors] n:0 c:255 t:3 s:9 p:TSF:MSG:READ,2-2-0,s=255,c=3,t=26,pt=1,l=1,sg=0:2#0```
    

    this is my
    /home/homeassistant/mysensors.json

    {"2": {"sketch_version": "2.2", "battery_level": 0, "protocol_version": "2.1.1", "sketch_name": "MySprinkler", "children": {"0": {"id": 0, "description": "", "type": 3, "values": {}}, "1": {"id": 1, "description": "", "type": 3, "values": {}}, "2": {"id": 2, "description": "", "type": 3, "values": {}}, "3": {"id": 3, "description": "", "type": 3, "values": {}}, "4": {"id": 4, "description": "", "type": 3, "values": {}}}, "sensor_id": 2, "type": 17}}
    

  • Plugin Developer

    You need to send initial values for each device. That's what's missing.

    Also remove the mysensors sensor platform from your configuration yaml. That's not needed.



  • @martinhjelmare said in mysprinkler for mysensor and home assistant not working together:

    You need to send initial values for each device. That's what's missing.

    Also remove the mysensors sensor platform from your configuration yaml. That's not needed.

    thanks martin. i read that, but i have no idea how to send those initial values?
    is this something that i have to implement in the sensor's code?

    https://home-assistant.io/components/mysensors/#presentation says:

    Send at least one initial value per V_TYPE. In version 2.0 of MySensors this has to be done in the loop function.

    is it something like that?

        Serial.println("Sending initial value");
        send(msg.set(state?RELAY_ON:RELAY_OFF));
    

    if that is the case, i assume it's missing in the irrigation controller's code. correct?


  • Plugin Developer

    @johannes-schoeller

    Yes, something like that.

    I'm not familiar with the irrigation controller code. But your log is missing the output about sending values, so I assume it's missing from the sketch.



  • thanks that did work. basically it worked the very next day w/o changing anything. it just did send initial values. so problem solved.

    but i have another issue. now that everything is working so far, and i see the switch automatically added in my home assistant dashboard, i can use them and click them. this is seen by the sensor and it acts appropriately.

    but how the irrigation controller is meant to be used is: it ASKS the hub for data. this data is a) time (works!) and b) duration to activate each zone on button press.

    so it has 4 irrigation zones, each of them ca be assigned 2 values, one is how long it's turned on when activated alone, and one how long it's turned on when you run all zones, one after the other.

    but i don't know how to set those values in home assistant so i can send them back to the sensor.

    this is what the sensor looks like in home assistant:

    0_1515951958517_Capture.JPG



  • @martinhjelmare said in mysprinkler for mysensor and home assistant not working together:

    Also remove the mysensors sensor platform from your configuration yaml. That's not needed.

    you mean this form configuration.yaml:

    mysensors:
      gateways:
        - device: '/dev/ttyUSB0'
          baud_rate: 38400
          persistence_file: 'home/homeassistant/mysensors.json'
      optimistic: false
      persistence: true
      retain: true
      version: '2.0'
    

    or this from sensors.yaml:

    - platform: mysensors
        port: "/dev/ttyUSB0"
    

    EDIT: got is sorted out myself. NOT the configuration part is wrong, but the sensors.yaml part is too much. i removed it, and it works fine. thank you!!



  • i investigated further and now i know that the sensor is requesting VAR_1 to VAR_3 and afaik this is not working with home assistant.
    any ideas how to circumvent this? i need to set the value in HA and then the sensor is requesting it. how would this be accomplished in HA?


  • Plugin Developer

    @johannes-schoeller

    Currently you can hack it by adding those value types and values to the JSON persistence file, keeping the structure. It's not a real solution, but should work. They will get represented as sensor entities in home assistant. If you want to avoid entity_id clashes the V_VAR1 and V_VAR3 should be placed on different children.

    I have on the roadmap to add an option for inital values in the mysensors home assistant config section.



  • @johannes-schoeller
    hi im currently struggling with the same problem, i dont suppose you found a solution? i tried to do what martinhjelmare said but obviously i did it wrong. any help would be apreciated anyone. at the moment i have the mysprinkler for mysensor showing in home assistant and if i try start one of the relays it just goes to next relay but doesnt start. it shows on the display 0 sec and i cant find how to set how to put in time for the relays as im a noob lol. cheers



  • @darren-herd said in mysprinkler for mysensor and home assistant not working together:

    @johannes-schoeller
    hi im currently struggling with the same problem, i dont suppose you found a solution? i tried to do what martinhjelmare said but obviously i did it wrong. any help would be apreciated anyone. at the moment i have the mysprinkler for mysensor showing in home assistant and if i try start one of the relays it just goes to next relay but doesnt start. it shows on the display 0 sec and i cant find how to set how to put in time for the relays as im a noob lol. cheers

    i wish i would. i now 3d printed a case for it, got it all working so far, but can't make it announce the sensors in HA. no idea, why it worked before, and what's not working now ...



  • @johannes-schoeller did you find a solution? I am struggling to add a water meter pulse sensor to HA UI. Thank you



  • Hi,

    You can fix variable with sending data. In the firmware, you must add this function for place value in this V_VARx

    So, place this function into your setup firmware.

    Where in this case

    MyMessage var1valve(CHILD_ID_SPRINKLER, V_VAR1);
    MyMessage var2valve(CHILD_ID_SPRINKLER, V_VAR2);
    

    This loop repeat for each relay (i) and you can set any value at the end (1) for one minutes and (5) for five minutes.

    That's all.

    void goInitiateValveTime() {
      for (byte i = 0; i <= NUMBER_OF_VALVES; i++)
      {
        send(var1valve.setSensor(i).set(1));
        wait(50);
        send(var2valve.setSensor(i).set(5));
      }
      wait(50);
      lastValve = -1;
    }
    

    Refresh Home Assistant just after run one time your script and value appear in your persistence file.

    Regars,



  • @martinhjelmare did this get added?

    "add an option for inital values in the mysensors home assistant config section"



  • @diltech i need help with where to put this in my firmware



  • @diltech for reference here is my code

    /*
    MySprinkler for MySensors
    
    Arduino Multi-Zone Sprinkler Control
    
    May 31, 2015
    
    *** Version 2.0
    
    *** Upgraded to http://MySensors.org version 1.4.1
    *** Expanded for up to 16 Valves
    *** Setup for active low relay board or comment out #define ACTIVE_LOW to switch to active high
    *** Switch to bitshift method vs byte arrays
    *** Changed RUN_ALL_ZONES Vera device to 0 (was highest valve)
    *** Added optional LCD display featuring remaining time, date last ran & current time
    *** Features 'raindrop' and 'clock' icons which indicate sensor is updating valve data and clock respectively
    *** Added single pushbutton menu to manually select which program to run (All Zones or a Single Zone)
    *** Added option of naming your Zones programmatically or with Vera (V_VAR3 used to store names)
    
    Utilizing your Vera home automation controller and the MySensors.org gateway you can
    control up to a sixteen zone irrigation system with only three digital pins.  This sketch
    will create NUMBER_OF_VALVES + 1 devices on your Vera controller
    
    This sketch features the following:
    
    * Allows you to cycle through All zones (RUN_ALL_ZONES) or individual zone (RUN_SINGLE_ZONE) control.
    * Use the 0th controller to activate RUN_ALL_ZONES (each zone in numeric sequence 1 to n)
      using Variable1 as the "ON" time in minutes in each of the vera devices created.
    * Use the individual zone controller to activate a single zone.  This feature uses
      Variable2 as the "ON" time for each individual device/zone.
    * Connect according to pinout below and uses Shift Registers as to allow the MySensors
      standard radio configuration and still leave available digital pins
    * Turning on any zone will stop the current process and begin that particular process.
    * Turning off any zone will stop the current process and turn off all zones.
    * To push your new time intervals for your zones, simply change the variable on your Vera and
      your arduino will call to Vera once a minute and update accordingly.  Variables will also be
      requested when the device is first powered on.
    * Pushbutton activation to RUN_ALL_ZONES, RUN_SINGLE_ZONE or halt the current program
    * LED status indicator
    
    PARTS LIST:
    Available from the MySensors store - http://www.mysensors.org/store/
    * Relays (8 channel)
    * Female Pin Header Connector Strip
    * Prototype Universal Printed Circuit Boards (PCB)
    * NRF24L01 Radio
    * Arduino (I used a Pro Mini)
    * FTDI USB to TTL Serial Adapter
    * Capacitors (10uf and .1uf)
    * 3.3v voltage regulator
    * Resistors (270, 1K & 10K)
    * Female Dupont Cables
    * 1602 LCD (with I2C Interface)
    * LED
    * Push button
    * Shift Register (SN74HC595)
    * 2 Pole 5mm Pitch PCB Mount Screw Terminal Block
    * 3 Pole 5mm Pitch PCB Mount Screw Terminal Block
    * 22-24 gauge wire or similar (I used Cat5/Cat6 cable)
    * 18 gauge wire (for relay)
    * Irrigation Power Supply (24-Volt/750 mA Transformer)
    
    
    INSTRUCTIONS:
    
    * A step-by-step setup video is available here: http://youtu.be/l4GPRTsuHkI
    * After assembling your arduino, radio, decoupling capacitors, shift register(s), status LED, pushbutton LCD (I2C connected to
      A4 and A5) and relays, and load the sketch.
    * Following the instructions at https://MySensors.org include the device to your MySensors Gateway.
    * Verify that each new device has a Variable1, Variable2 and Variable3. Populate data accordingly with whole minutes for
      the RUN_ALL_ZONES routine (Variable1) and the RUN_SINGLE_ZONE routines (Variable 2).  The values entered for times may be zero and
      you may use the defaulet zone names by leaving Variable3 blank.
    * Once you have entered values for each zone and each variable, save the settings by pressing the red save button on your Vera.
    * Restart your arduino; verify the settings are loaded into your arduino with the serial monitor; the array will be printed
      on the serial monitor.
    * Your arduino should slow-flash, indicating that it is in ready mode.
    * There are multiple debug serial prints that can be monitored to assure that it is operating properly.
    * ***THIS SHOULD NO LONGER BE NEEDED*** The standard MySensors library now works. https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads for the I2C library, or use yours
    
    Contributed by Jim (BulldogLowell@gmail.com) with much contribution from Pete (pete.will@mysensors.org) and is released to the public domain
    */
    //
    
    // Enable debug prints
    //#define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #define MY_NODE_ID 1  // Set this to fix your Radio ID or use Auto
    
    #include <Wire.h>
    #include <TimeLib.h>
    #include <SPI.h>
    #include <MySensors.h>
    #include <LiquidCrystal.h>
    #include <LiquidCrystal_I2C.h>
    
    
    #define NUMBER_OF_VALVES 2  // Change this to set your valve count up to 16.
    #define VALVE_RESET_TIME 7500UL   // Change this (in milliseconds) for the time you need your valves to hydraulically reset and change state
    #define VALVE_TIMES_RELOAD 300000UL  // Change this (in milliseconds) for how often to update all valves data from the controller (Loops at value/number valves)
                                         // ie: 300000 for 8 valves produces requests 37.5seconds with all valves updated every 5mins 
    
    #define SKETCH_NAME "MySprinkler"
    #define SKETCH_VERSION "2.2"
    //
    #define CHILD_ID_SPRINKLER 0
    //
    #define ACTIVE_LOW // comment out this line if your relays are active high
    //
    // #define DEBUG_ON   // comment out to supress serial monitor output
    //
    #ifdef ACTIVE_LOW
    #define BITSHIFT_VALVE_NUMBER ~(1U << (valveNumber-1))
    #define ALL_VALVES_OFF 0xFFFF
    #else
    #define BITSHIFT_VALVE_NUMBER (1U << (valveNumber-1))
    #define ALL_VALVES_OFF 0U
    #endif
    //
    #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
    //
    typedef enum {
      STAND_BY_ALL_OFF, RUN_SINGLE_ZONE, RUN_ALL_ZONES, CYCLE_COMPLETE, ZONE_SELECT_MENU
    }
    SprinklerStates;
    //
    SprinklerStates state = STAND_BY_ALL_OFF;
    SprinklerStates lastState;
    byte menuState = 0;
    unsigned long menuTimer;
    byte countDownTime = 10;
    //
    int allZoneTime [NUMBER_OF_VALVES + 1];
    int valveSoloTime [NUMBER_OF_VALVES + 1];
    int valveNumber;
    int lastValve;
    unsigned long startMillis;
    const int ledPin = 5;
    const int waterButtonPin = 3;
    bool buttonPushed = false;
    bool showTime = true;
    bool clockUpdating = false;
    bool recentUpdate = true;
    int allVars[] = {V_VAR1, V_VAR2, V_VAR3};
    const char *dayOfWeek[] = {
      "Null", "Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "
    };
    // Name your Zones here or use Vera to edit them by adding a name in Variable3...
    String valveNickName[17] = {
      "All Zones", "Zone 1", "Zone 2", "Zone 3", "Zone 4", "Zone 5", "Zone 6", "Zone 7", "Zone 8", "Zone 9", "Zone 10", "Zone 11", "Zone 12", "Zone 13", "Zone 14", "Zone 15", "Zone 16"
    };
    //
    time_t lastTimeRun = 0;
    //Setup Shift Register...
    const int latchPin = 8;
    const int clockPin = 4;
    const int dataPin  = 7;
    const int outputEnablePin = 6;
    //
    byte clock[8] = {0x0, 0xe, 0x15, 0x17, 0x11, 0xe, 0x0}; // fetching time indicator
    byte raindrop[8] = {0x4, 0x4, 0xA, 0xA, 0x11, 0xE, 0x0,}; // fetching Valve Data indicator
    // Set the pins on the I2C chip used for LCD connections:
    //                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
    LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address to 0x27
    //
    MyMessage msg1valve(CHILD_ID_SPRINKLER, V_LIGHT);
    MyMessage var1valve(CHILD_ID_SPRINKLER, V_VAR1);
    MyMessage var2valve(CHILD_ID_SPRINKLER, V_VAR2);
    
    bool receivedInitialValue = false;
    bool inSetup = true;
    //
    void setup()
    {
      DEBUG_PRINTLN(F("Initialising..."));
      pinMode(latchPin, OUTPUT);
      pinMode(clockPin, OUTPUT);
      pinMode(dataPin, OUTPUT);
      pinMode(ledPin, OUTPUT);
      pinMode(outputEnablePin, OUTPUT);
      digitalWrite (outputEnablePin, LOW);
      pinMode(waterButtonPin, INPUT_PULLUP);
      //pinMode(waterButtonPin, INPUT);
      attachInterrupt(digitalPinToInterrupt(waterButtonPin), PushButton, RISING); //May need to change for your Arduino model
      digitalWrite (ledPin, HIGH);
      DEBUG_PRINTLN(F("Turning All Valves Off..."));
      updateRelays(ALL_VALVES_OFF);
      //delay(5000);
      lcd.begin(16, 2); //(16 characters and 2 line display)
      lcd.clear();
      lcd.backlight();
      lcd.createChar(0, clock);
      lcd.createChar(1, raindrop);
      //
      //check for saved date in EEPROM
      DEBUG_PRINTLN(F("Checking EEPROM for stored date:"));
      delay(500);
      if (loadState(0) == 0xFF) // EEPROM flag
      {
        DEBUG_PRINTLN(F("Retreiving last run time from EEPROM..."));
        for (int i = 0; i < 4 ; i++)
        {
          lastTimeRun = lastTimeRun << 8;
          lastTimeRun = lastTimeRun | loadState(i + 1); // assemble 4 bytes into an ussigned long epoch timestamp
        }
      }
    
      DEBUG_PRINTLN(F("Sensor Presentation Complete"));
      //
      digitalWrite (ledPin, LOW);
      DEBUG_PRINTLN(F("Ready..."));
      //
      lcd.setCursor(0, 0);
      lcd.print(F(" Syncing Time  "));
      lcd.setCursor(15, 0);
      lcd.write(byte(0));
      lcd.setCursor(0, 1);
      int clockCounter = 0;
      while (timeStatus() == timeNotSet && clockCounter < 21)
      {
        requestTime();
        DEBUG_PRINTLN(F("Requesting time from Gateway:"));
        wait(1000);
        lcd.print(".");
        clockCounter++;
        if (clockCounter > 16)
        {
          DEBUG_PRINTLN(F("Failed initial clock synchronization!"));
          lcd.clear();
          lcd.print(F("  Failed Clock  "));
          lcd.setCursor(0, 1);
          lcd.print(F(" Syncronization "));
          wait(2000);
          break;
        }
      }
      //
      //Update valve data when first powered on 
      for (byte i = 1; i <= NUMBER_OF_VALVES; i++)
      {
        lcd.clear();
        goGetValveTimes();
      }
      lcd.clear();
      inSetup = false;
    }
    
    
    void presentation()  
    { 
      sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
      for (byte i = 0; i <= NUMBER_OF_VALVES; i++)
      {
        present(i, S_LIGHT);
      }
    }
    
    //
    void loop()
    {
      updateClock();
      updateDisplay();
      goGetValveTimes();
      //
      if (buttonPushed)
      {
        menuTimer = millis();
        DEBUG_PRINTLN(F("Button Pressed"));
        if (state == STAND_BY_ALL_OFF)
        {
          state = ZONE_SELECT_MENU;
          menuState = 0;
        }
        else if (state == ZONE_SELECT_MENU)
        {
          menuState++;
          if (menuState > NUMBER_OF_VALVES)
          {
            menuState = 0;
          }
        }
        else
        {
          state = STAND_BY_ALL_OFF;
        }
        buttonPushed = false;
      }
      if (state == STAND_BY_ALL_OFF)
      {
        slowToggleLED ();
        if (state != lastState)
        {
          updateRelays(ALL_VALVES_OFF);
          DEBUG_PRINTLN(F("State Changed... all Zones off"));
          for (byte i = 0; i <= NUMBER_OF_VALVES; i++)
          {
            wait(50);
            send(msg1valve.setSensor(i).set(false), false);
          }
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print(F("** Irrigation **"));
          lcd.setCursor(0,1);
          lcd.print(F("**   Halted   **"));
          wait(2000);
          lastValve = -1;
        }
      }
      //
      else if (state == RUN_ALL_ZONES)
      {
        if (lastValve != valveNumber)
        {
          for (byte i = 0; i <= NUMBER_OF_VALVES; i++)
          {
            if (i == 0 || i == valveNumber)
            {
              send(msg1valve.setSensor(i).set(true), false);
            }
            else
            {
              send(msg1valve.setSensor(i).set(false), false);
            }
            wait(50);
          }
        }
        lastValve = valveNumber;
        fastToggleLed();
        if (state != lastState)
        {
          valveNumber = 1;
          updateRelays(ALL_VALVES_OFF);
          DEBUG_PRINTLN(F("State Changed, Running All Zones..."));
        }
        unsigned long nowMillis = millis();
        if (nowMillis - startMillis < VALVE_RESET_TIME)
        {
          updateRelays(ALL_VALVES_OFF);
        }
        else if (nowMillis - startMillis < (allZoneTime[valveNumber] * 60000UL))
        {
          updateRelays(BITSHIFT_VALVE_NUMBER);
        }
        else
        {
          DEBUG_PRINTLN(F("Changing Valves..."));
          updateRelays(ALL_VALVES_OFF);
          startMillis = millis();
          valveNumber++;
          if (valveNumber > NUMBER_OF_VALVES)
          {
            state = CYCLE_COMPLETE;
            startMillis = millis();
            lastValve = -1;
            lastTimeRun = now();
            saveDateToEEPROM(lastTimeRun);
            for (byte i = 0; i <= NUMBER_OF_VALVES; i++)
            {
              send(msg1valve.setSensor(i).set(false), false);
              wait(50);
            }
            DEBUG_PRINT(F("State = "));
            DEBUG_PRINTLN(state);
          }
        }
      }
      //
      else if (state == RUN_SINGLE_ZONE)
      {
        fastToggleLed();
        if (state != lastState)
        {
          for (byte i = 0; i <= NUMBER_OF_VALVES; i++)
          {
            if (i == 0 || i == valveNumber)
            {
              send(msg1valve.setSensor(i).set(true), false);
            }
            else
            {
              send(msg1valve.setSensor(i).set(false), false);
            }
            wait(50);
          }
          DEBUG_PRINTLN(F("State Changed, Single Zone Running..."));
          DEBUG_PRINT(F("Zone: "));
          DEBUG_PRINTLN(valveNumber);
        }
        unsigned long nowMillis = millis();
        if (nowMillis - startMillis < VALVE_RESET_TIME)
        {
          updateRelays(ALL_VALVES_OFF);
        }
        else if (nowMillis - startMillis < (valveSoloTime [valveNumber] * 60000UL))
        {
          updateRelays(BITSHIFT_VALVE_NUMBER);
        }
        else
        {
          updateRelays(ALL_VALVES_OFF);
          for (byte i = 0; i <= NUMBER_OF_VALVES; i++)
          {
            send(msg1valve.setSensor(i).set(false), false);
          }
          state = CYCLE_COMPLETE;
          startMillis = millis();
          DEBUG_PRINT(F("State = "));
          DEBUG_PRINTLN(state);
        }
        lastTimeRun = now();
      }
      else if (state == CYCLE_COMPLETE)
      {
        if (millis() - startMillis < 30000UL)
        {
          fastToggleLed();
        }
        else
        {
          state = STAND_BY_ALL_OFF;
        }
      }
      if (state == ZONE_SELECT_MENU)
      {
        displayMenu();
      } 
      else 
      {
        lastState = state;
      }
    }
    //
    void displayMenu(void)
    {
      static byte lastMenuState = -1;
      static int lastSecond;
      if (menuState != lastMenuState)
      {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print(valveNickName[menuState]);
        lcd.setCursor(0, 1);
        lcd.print(F("Starting"));
        DEBUG_PRINT(valveNickName[menuState]);
        Serial.print(F(" Starting Shortly"));
      }
      int thisSecond = (millis() - menuTimer) / 1000UL;
      if (thisSecond != lastSecond && thisSecond < 8)
      {
        lcd.print(F("."));
        Serial.print(".");
      }
      lastSecond = thisSecond;
      if (millis() - menuTimer > 10000UL)
      {
        startMillis = millis();
        if (menuState == 0)
        {
          valveNumber = 1;
          state = RUN_ALL_ZONES;
        }
        else
        {
          valveNumber = menuState;
          state = RUN_SINGLE_ZONE;
        }
      }
      else
      {
    
      }
      lastMenuState = menuState;
    }
    //
    void updateRelays(int value)
    {
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, highByte(value));
      shiftOut(dataPin, clockPin, MSBFIRST, lowByte(value));
      digitalWrite(latchPin, HIGH);
    }
    //
    void PushButton() //interrupt with debounce
    {
      static unsigned long last_interrupt_time = 0;
      unsigned long interrupt_time = millis();
      if (interrupt_time - last_interrupt_time > 200)
      {
        buttonPushed = true;
      }
      last_interrupt_time = interrupt_time;
    }
    //
    void fastToggleLed()
    {
      static unsigned long fastLedTimer;
      if (millis() - fastLedTimer >= 100UL)
      {
        digitalWrite(ledPin, !digitalRead(ledPin));
        fastLedTimer = millis ();
      }
    }
    //
    void slowToggleLED ()
    {
      static unsigned long slowLedTimer;
      if (millis() - slowLedTimer >= 1250UL)
      {
        digitalWrite(ledPin, !digitalRead(ledPin));
        slowLedTimer = millis ();
      }
    }
    //
    void receive(const MyMessage &message)
    {
      bool zoneTimeUpdate = false;
      if (message.isAck())
      {
        DEBUG_PRINTLN(F("This is an ack from gateway"));
      }
      for (byte i = 0; i <= NUMBER_OF_VALVES; i++)
      {
        if (message.sensor == i)
        {
          if (message.type == V_LIGHT)
          {
            int switchState = atoi(message.data);
            if (switchState == 0)
            {
              state = STAND_BY_ALL_OFF;
              DEBUG_PRINTLN(F("Recieved Instruction to Cancel..."));
            }
            else
            {
              if (i == 0)
              {
                state = RUN_ALL_ZONES;
                valveNumber = 1;
                DEBUG_PRINTLN(F("Recieved Instruction to Run All Zones..."));
              }
              else
              {
                state = RUN_SINGLE_ZONE;
                valveNumber = i;
                DEBUG_PRINT(F("Recieved Instruction to Activate Zone: "));
                DEBUG_PRINTLN(i);
              }
            }
            startMillis = millis();
          }
          else if (message.type == V_VAR1)
          {
            int variable1 = 5; // atoi(message.data);// RUN_ALL_ZONES time <-- this is what was added " 5; //" as the controller won't talk to the node for VAR_V -->
            DEBUG_PRINT(F("Recieved variable1 valve:"));
            DEBUG_PRINT(i);
            DEBUG_PRINT(F(" = "));
            DEBUG_PRINTLN(variable1);
            if (variable1 != allZoneTime[i])
            {
              allZoneTime[i] = variable1;
    
              zoneTimeUpdate = true;
            }
            receivedInitialValue = true;
          }
          else if (message.type == V_VAR2)
          {
            int variable2 = 5; // atoi(message.data);// RUN_SINGLE_ZONE time <-- this is whats changed " 5; //" as the controller won't talk to the node for VAR_V -->
            DEBUG_PRINT(F("Recieved variable2 valve:"));
            DEBUG_PRINT(i);
            DEBUG_PRINT(F(" = "));
            DEBUG_PRINTLN(variable2);
            if (variable2 != valveSoloTime[i])
            {
              valveSoloTime[i] = variable2;
              zoneTimeUpdate = true;
            }
            receivedInitialValue = true;
          }
          else if (message.type == V_VAR3)
          {
            String newMessage = String(message.data);
            if (newMessage.length() == 0) 
            {
              DEBUG_PRINT(F("No Name Recieved for zone "));
              DEBUG_PRINTLN(i);
              break;
            }
            if (newMessage.length() > 16)
            {
              newMessage.substring(0, 16);
            }
            valveNickName[i] = "";
            valveNickName[i] += newMessage;
            DEBUG_PRINT(F("Recieved variable3 valve: "));
            DEBUG_PRINT(i);
            DEBUG_PRINT(F(" = "));
            DEBUG_PRINTLN(valveNickName[i]);
          }
          receivedInitialValue = true;
        }
      }
      if (zoneTimeUpdate)
      {
        //
        DEBUG_PRINTLN(F("New Zone Times Recieved..."));
        for (byte i = 0; i <= NUMBER_OF_VALVES; i++)
        {
          if (i != 0)
          {
            DEBUG_PRINT(F("Zone "));
            DEBUG_PRINT(i);
            DEBUG_PRINT(F(" individual time: "));
            DEBUG_PRINT(valveSoloTime[i]);
            DEBUG_PRINT(F(" group time: "));
            DEBUG_PRINT(allZoneTime[i]);
            DEBUG_PRINT(F(" name: "));
            DEBUG_PRINTLN(valveNickName[i]);
            recentUpdate = true;
          }
        }
      }
      else
      {
        recentUpdate = false;
      }
    }
    //
    void updateDisplay()
    {
      static unsigned long lastUpdateTime;
      static bool displayToggle = false;
      //static byte toggleCounter = 0;
      static SprinklerStates lastDisplayState;
      if (state != lastDisplayState || millis() - lastUpdateTime >= 3000UL)
      {
        displayToggle = !displayToggle;
        switch (state) {
          case STAND_BY_ALL_OFF:
            //
            fastClear();
            lcd.setCursor(0, 0);
            if (displayToggle)
            {
              lcd.print(F("  System Ready "));
              if (clockUpdating)
              {
                lcd.setCursor(15, 0);
                lcd.write(byte(0));
              }
              lcd.setCursor(0, 1);
              lcd.print(hourFormat12() < 10 ? F(" ") : F(""));
              lcd.print(hourFormat12());
              lcd.print(minute() < 10 ? F(":0") : F(":"));
              lcd.print(minute());
              lcd.print(isAM() ? F("am") : F("pm"));
              lcd.print(month() < 10 ? F(" 0") : F(" "));
              lcd.print(month());
              lcd.print(day() < 10 ? F("/0") : F("/"));
              lcd.print(day());
              lcd.print(F("/"));
              lcd.print(year() % 100);
            }
            else
            {
              lcd.print(F("  Last Watered "));
              if (clockUpdating)
              {
                lcd.setCursor(15, 0);
                lcd.write(byte(0));
              }
              lcd.setCursor(0, 1);
              lcd.print(dayOfWeek[weekday(lastTimeRun)]);
              lcd.setCursor(11, 1);
              lcd.print(month(lastTimeRun) < 10 ? F(" ") : F(""));
              lcd.print(month(lastTimeRun));
              lcd.print(day(lastTimeRun) < 10 ? F("/0") : F("/"));
              lcd.print(day(lastTimeRun));
            }
            break;
          case RUN_SINGLE_ZONE:
            //
            fastClear();
            lcd.setCursor(0, 0);
            if (displayToggle)
            {
              lcd.print(F("Single Zone Mode"));
              lcd.setCursor(0, 1);
              lcd.print(F(" Zone:"));
              if (valveNumber < 10) lcd.print(F("0"));
              lcd.print(valveNumber);
              lcd.print(F(" Active"));
            }
            else
            {
              lcd.print(F(" Time Remaining "));
              lcd.setCursor(0, 1);
              if (valveSoloTime[valveNumber] == 0)
              {
                lcd.print(F(" No Valve Time "));
              }
              else
              {
                unsigned long timeRemaining = (valveSoloTime[valveNumber] * 60) - ((millis() - startMillis) / 1000);
                lcd.print(timeRemaining / 60 < 10 ? "   0" : "   ");
                lcd.print(timeRemaining / 60);
                lcd.print("min");
                lcd.print(timeRemaining % 60 < 10 ? " 0" : " ");
                lcd.print(timeRemaining % 60);
                lcd.print("sec  ");
              }
            }
            break;
          case RUN_ALL_ZONES:
            //
            fastClear();
            lcd.setCursor(0, 0);
            if (displayToggle)
            {
              lcd.print(F(" All-Zone  Mode "));
              lcd.setCursor(0, 1);
              lcd.print(F(" Zone:"));
              if (valveNumber < 10) lcd.print(F("0"));
              lcd.print(valveNumber);
              lcd.print(F(" Active "));
            }
            else
            {
              lcd.print(F(" Time Remaining "));
              lcd.setCursor(0, 1);
              int timeRemaining = (allZoneTime[valveNumber] * 60) - ((millis() - startMillis) / 1000);
              lcd.print((timeRemaining / 60) < 10 ? "   0" : "   ");
              lcd.print(timeRemaining / 60);
              lcd.print("min");
              lcd.print(timeRemaining % 60 < 10 ? " 0" : " ");
              lcd.print(timeRemaining % 60);
              lcd.print("sec  ");
            }
            break;
          case CYCLE_COMPLETE:
            //
            if (displayToggle)
            {
              lcd.setCursor(0, 0);
              lcd.print(F(" Watering Cycle "));
              lcd.setCursor(0, 1);
              lcd.print(F("    Complete    "));
            }
            else
            {
              int totalTimeRan = 0;
              for (int i = 1; i < NUMBER_OF_VALVES + 1; i++)
              {
                totalTimeRan += allZoneTime[i];
              }
              lcd.setCursor(0, 0);
              lcd.print(F(" Total Time Run "));
              lcd.setCursor(0, 1);
              lcd.print(totalTimeRan < 10 ? "   0" : "   ");
              lcd.print(totalTimeRan);
              lcd.print(" Minutes   ");
            }
          default:
            // what of ZONE_SELECT_MENU?
            break;
        }
        lastUpdateTime = millis();
      }
      lastDisplayState = state;
    }
    void receiveTime(time_t newTime)
    {
      DEBUG_PRINTLN(F("Time value received and updated..."));
      int lastSecond = second();
      int lastMinute = minute();
      int lastHour = hour();
      setTime(newTime);
      if (((second() != lastSecond) || (minute() != lastMinute) || (hour() != lastHour)) || showTime)
      {
        DEBUG_PRINTLN(F("Clock updated...."));
        DEBUG_PRINT(F("Sensor's time currently set to:"));
        DEBUG_PRINT(hourFormat12() < 10 ? F(" 0") : F(" "));
        DEBUG_PRINT(hourFormat12());
        DEBUG_PRINT(minute() < 10 ? F(":0") : F(":"));
        DEBUG_PRINT(minute());
        DEBUG_PRINTLN(isAM() ? F("am") : F("pm"));
        DEBUG_PRINT(month());
        DEBUG_PRINT(F("/"));
        DEBUG_PRINT(day());
        DEBUG_PRINT(F("/"));
        DEBUG_PRINTLN(year());
        DEBUG_PRINTLN(dayOfWeek[weekday()]);
        showTime = false;
      }
      else
      {
        DEBUG_PRINTLN(F("Sensor's time did NOT need adjustment greater than 1 second."));
      }
      clockUpdating = false;
    }
    void fastClear()
    {
      lcd.setCursor(0, 0);
      lcd.print(F("                "));
      lcd.setCursor(0, 1);
      lcd.print(F("                "));
    }
    //
    void updateClock()
    {
      static unsigned long lastVeraGetTime;
      if (millis() - lastVeraGetTime >= 3600000UL) // updates clock time and gets zone times from vera once every hour
      {
        DEBUG_PRINTLN(F("Requesting time and valve data from Gateway..."));
        lcd.setCursor(15, 0);
        lcd.write(byte(0));
        clockUpdating = true;
        requestTime();
        lastVeraGetTime = millis();
      }
    }
    //
    void saveDateToEEPROM(unsigned long theDate)
    {
      DEBUG_PRINTLN(F("Saving Last Run date"));
      if (loadState(0) != 0xFF)
      {
        saveState(0, 0xFF); // EEPROM flag for last date saved stored in EEPROM (location zero)
      }
      //
      for (int i = 1; i < 5; i++)
      {
        saveState(5 - i, byte(theDate >> 8 * (i - 1))); // store epoch datestamp in 4 bytes of EEPROM starting in location one
      }
    }
    //
    void goGetValveTimes()
    {
      static unsigned long valveUpdateTime;
      static byte valveIndex = 1;
      if (inSetup || millis() - valveUpdateTime >= VALVE_TIMES_RELOAD / NUMBER_OF_VALVES) // update each valve once every 5 mins (distributes the traffic)
      {
        if (inSetup) {
          lcd.print(F(" Updating  "));
          lcd.setCursor(0, 1);
          lcd.print(F(" Valve Data: "));
          lcd.print(valveIndex);
        }
        bool flashIcon = false;
        DEBUG_PRINT(F("Calling for Valve "));
        DEBUG_PRINT(valveIndex);
        DEBUG_PRINTLN(F(" Data..."));
        for (int a = 0; a < (sizeof(allVars)/sizeof(int)); a++) {
          receivedInitialValue = false;
          byte timeout = 10;
          while (!receivedInitialValue && timeout > 0)
          {
            lcd.setCursor(15, 0);
            flashIcon = !flashIcon;
            flashIcon ? lcd.write(byte(1)) : lcd.print(F(" "));
            request(valveIndex, allVars[a]);
            wait(50);
            timeout--;
          }
        }
        valveUpdateTime = millis();
        valveIndex++;
        if (valveIndex > NUMBER_OF_VALVES)
        {
          valveIndex = 1;
        }
      }
    }
    


  • has anyone got any ideas? has anyone read this?


  • Mod

    @MadKezza this topic has been viewed 3k times.


  • Hardware Contributor

    I have added a sollution to V_VAR problem in both my rain and power pulse meter (check these threads). As said above, Homeassistant will not give an answer to these requests if there are no value in the mysensors json file. Either add this in the hardware code or as I did, request a couple of times to make sure you get the value if there is one. Count these requests and set a upper limit, like five or ten reuqest. Once your each this limit, send the value 0 to HA and this will be updated in the json file and start from there.


Log in to reply
 

Suggested Topics

  • 1
  • 1
  • 17
  • 8
  • 3
  • 1

54
Online

11.4k
Users

11.1k
Topics

112.7k
Posts