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. Development
  3. Happy Standby Node :)

Happy Standby Node :)

Scheduled Pinned Locked Moved Development
3 Posts 2 Posters 1.9k Views 2 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.
  • berkseoB Offline
    berkseoB Offline
    berkseo
    wrote on last edited by berkseo
    #1

    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;
        }
      }
    }
    
    
    1 Reply Last reply
    0
    • CrankyCoderC Offline
      CrankyCoderC Offline
      CrankyCoder
      wrote on last edited by
      #2

      could you provide a quick summary of what this does?

      Home Automation Tinkerer
      www.CrankyCoder.net

      Controller: HomeAssistant in Kubernetes
      Gateway: MQTTClientGateway
      MySensors: 2.3

      berkseoB 1 Reply Last reply
      0
      • CrankyCoderC CrankyCoder

        could you provide a quick summary of what this does?

        berkseoB Offline
        berkseoB Offline
        berkseo
        wrote on last edited by
        #3

        @crankycoder https://translate.google.com/translate?sl=ru&tl=en&js=y&prev=_t&hl=ru&ie=UTF-8&u=http%3A%2F%2Fmysensors.ru%2Fforum%2Fviewtopic.php%3Ff%3D5%26t%3D380&edit-text=

        1 Reply Last reply
        1

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        5

        Online

        12.0k

        Users

        11.2k

        Topics

        113.4k

        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