Happy Standby Node :)



  • turn off your gateway and try..

    bool flag_find_parent = 0;
    bool flag_find_parent_process = 0;
    bool q = 0;
    bool flag_nogateway_mode = 0;
    bool flag_no_present = 0;
    bool flag_update_transport_param = 0;
    bool Ack_HB;
    bool Ack_FP;
    bool bin = 0;
    
    byte mypar;
    byte myid;
    byte mydis;
    byte err_delivery_beat;
    byte friend_node = 10;
    
    word mtwr;
    
    unsigned long interval_hb = 0;
    unsigned long interval_friend_node = 0;
    unsigned long interval_find_parent = 0;
    
    unsigned int  sec_interval_hb = 15000;
    unsigned int  sec_interval_friend_node = 10000;
    unsigned int  sec_interval_find_parent = 5000;
    
    #define MY_TRANSPORT_WAIT_READY_MS (mtwr)
    #define MY_DEBUG
    #define MY_RADIO_NRF24
    #define CHILD_ID 0
    #define CHILD_ID_1 1
    
    #include <MySensors.h>
    
    MyMessage msg(CHILD_ID, V_TEMP);
    MyMessage msg_to_friend(CHILD_ID_1, V_STATUS);
    
    
    void before() {
      if (hwReadConfig(EEPROM_NODE_ID_ADDRESS) == 0) {
        hwWriteConfig(EEPROM_NODE_ID_ADDRESS, 255);
      }
    
      if (hwReadConfig(EEPROM_NODE_ID_ADDRESS) == 255) {
        mtwr = 0;
      } else {
        mtwr = 30000;
      }
    }
    
    
    void presentation()
    {
      sendSketchInfo("Happy Standby Node", "1.01");
      present(CHILD_ID, S_TEMP);
    }
    
    
    void setup() {
      myid = getNodeId();
      if (isTransportReady() == true) {
        mydis = _transportConfig.distanceGW;
        mypar = _transportConfig.parentNodeId;
        loadState(103);
        if (mydis != loadState(103))
        {
          saveState(103, mydis);
        }
        if (mypar != loadState(101))
        {
          saveState(101, mypar);
        }
      }
      if (isTransportReady() == false)
      {
        CORE_DEBUG(PSTR("MyS: STANDBY TRANSPORT MODE\n"));
        no_present();
        happy_node_mode();
        gateway_fail();
        err_delivery_beat = 3;
      }
      interval_friend_node = millis();
      interval_hb = millis();
    }
    
    
    void loop() {
      if (isTransportReady() == true) {
        if (millis() - interval_friend_node > sec_interval_friend_node)    {
          _transportConfig.parentNodeId = friend_node;
          bin = !bin;
          bool friend_sent = send(msg_to_friend.setDestination(friend_node).set(bin));
          if (friend_sent == 1) {
            _transportConfig.parentNodeId = mypar;
          } else {
            _transportSM.failedUplinkTransmissions = 0;
            _transportConfig.parentNodeId = mypar;
          }
          interval_friend_node = millis();
        }
        if (flag_nogateway_mode == 0) {
          if (flag_find_parent_process == 1) {
            flag_update_transport_param = 1;
            flag_find_parent_process = 0;
            CORE_DEBUG(PSTR("MyS: STANDART TRANSPORT MODE IS RESTORED\n"));
          }
          if (millis() - interval_hb > sec_interval_hb)    {
            send_to_gateway();
            interval_hb = millis();
          }
        }
    
        if (flag_update_transport_param == 1) {
          CORE_DEBUG(PSTR("MyS: UPDATE TRANSPORT CONFIGURATION\n"));
          mydis = _transportConfig.distanceGW;
          mypar = _transportConfig.parentNodeId;
          loadState(103);
          if (mydis != loadState(103))
          {
            saveState(103, mydis);
          }
          if (mypar != loadState(101))
          {
            saveState(101, mypar);
          }
          flag_update_transport_param = 0;
          if (flag_no_present == 1) {
            CORE_DEBUG(PSTR("MyS: SEND PRESENTATION AFTER RESTORING STANDART TRANSPORT MODE \n"));
            presentNode();
            flag_no_present = 0;
            wait(1000);
            send_to_gateway();
          } else {
            CORE_DEBUG(PSTR("MyS: SEND LITTLE PRESENT:) WITH PARENT ID IN CONTRILER AFTER RESTORING STANDART TRANSPORT MODE \n"));
            present_only_parent();
            wait(1000);
            send_to_gateway();
          }
        }
      }
    
      if (_transportSM.failureCounter > 0) {
        happy_node_mode();
        gateway_fail();
      }
    
      if (flag_nogateway_mode == 1) {
        if (millis() - interval_find_parent > sec_interval_find_parent)    {
          check_parent();
          interval_find_parent = millis();
        }
      }
    
      if (q == 1) {
        if (_msg.sensor == 255) {
          if (mGetCommand(_msg) == 3) {
            if (_msg.type == 8) {
              Ack_FP = 1;
              CORE_DEBUG(PSTR("MyS: PARENT RESPONSE FOUND\n"));
            }
          }
        }
    
        if (Ack_FP == 1) {
          CORE_DEBUG(PSTR("MyS: FIND PARENT PROCESS\n"));
          q = 0;
          Ack_FP = 0;
          transportSwitchSM(stParent);
          flag_nogateway_mode = 0;
          flag_find_parent_process = 1;
        } else {
          _transportSM.findingParentNode = false;
          q = 0;
          CORE_DEBUG(PSTR("MyS: PARENT RESPONSE NOT FOUND\n"));
          _transportSM.failedUplinkTransmissions = 0;
        }
      }
    }
    
    void receive(const MyMessage & message) {
      if (message.isAck()) {
        if (message.sensor == 0) {
          if (message.type == 0) {
            Ack_HB = 1;
          }
        }
      }
    }
    
    
    void update_parent() {
      _sendRoute(build(_msgTmp, 0, NODE_SENSOR_ID, 3, 6).set(mypar));
      wait(1000, 3, 6);
    }
    
    
    void no_present() {
      _coreConfig.presentationSent = true;
      _coreConfig.nodeRegistered = true;
      flag_no_present = 1;
    }
    
    
    void happy_node_mode() {
      _transportConfig.nodeId = myid;
      _transportConfig.parentNodeId = loadState(101);
      _transportConfig.distanceGW = loadState(103);
      _transportSM.findingParentNode = false;
      _transportSM.transportActive = true;
      _transportSM.uplinkOk = true;
      _transportSM.pingActive = false;
      transportSwitchSM(stReady);
      mydis = _transportConfig.distanceGW;
      mypar = _transportConfig.parentNodeId;
      _transportSM.failureCounter = 0;
    }
    
    
    void gateway_fail() {
      flag_nogateway_mode = 1;
      flag_update_transport_param = 0;
      interval_find_parent = millis();
    }
    
    
    void check_parent() {
      q = 1;
      _transportSM.findingParentNode = true;
      CORE_DEBUG(PSTR("MyS: SEND FIND PARENT REQUEST, WAIT RESPONSE\n"));
      _sendRoute(build(_msg, 255, NODE_SENSOR_ID, 3, 7).set(""));
      wait(3000, 3, 8);
    }
    
    
    void present_only_parent() {
      _sendRoute(build(_msgTmp, 0, NODE_SENSOR_ID, 3, 6).set(mypar));
      wait(1000, 3, 6);
    }
    
    
    void send_to_gateway () {
      send(msg.set(25.0 + random(0, 30) / 10.0, 2), 1);
      wait(2000, 1, 0);
    
      if (Ack_HB == 0) {
        if (err_delivery_beat < 3) {
          err_delivery_beat++;
        }
        if (err_delivery_beat == 2) {
          gateway_fail();
          CORE_DEBUG(PSTR("MyS: LOST GATEWAY MODE\n"));
        }
        _transportSM.failedUplinkTransmissions = 0;
      } else {
        Ack_HB = 0;
        err_delivery_beat = 0;
        if (flag_nogateway_mode == 1) {
          flag_nogateway_mode = 0;
        }
      }
    }
    
    


  • could you provide a quick summary of what this does?




Log in to reply
 

Suggested Topics

  • 1
  • 3
  • 10
  • 6
  • 5
  • 1

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts