Variable not update in sleep node



  • Hi!
    I'm trying to make automatic loading of variables in new nodes.
    I have a variable called time_sleep. TypeSensor = Custom, TypeVariable = V_VAR5.
    When I connect a new node, I need to send the variable type to the MyController.
    If I give the command

    request (ID_TIME_SLEEP, S_CUSTOM, V_VAR5);
    

    In MyController, a new sensor with an undefined variable is created.
    In order to determine the type of the variable, I have to give a command

    send (msg_TIME_SLEEP.set (time_sleep)
    

    After that, even if I change the value of the time_sleep variable of type V_VAR5
    in MyController, a new value is not sent to my node.
    This does not happen, and the sketch works correctly if I do not first give the command

    request (ID_TIME_SLEEP, S_CUSTOM, V_VAR5);
    

    Here's my sketch

    /*******************************************************************
     * DEFINE SECTION
     ********************************************************************/
    // Enable debug prints
    //#define MY_DEBUG
    #define SER_DEBUG
    #ifdef SER_DEBUG
    #define DEBUG_SERIAL(x) Serial.begin(x)
    #define DEBUG_PRINT(x) Serial.print(x)
    #define DEBUG_PRINTLN(x) Serial.println(x)
    #else
    #define DEBUG_SERIAL(x)
    #define DEBUG_PRINT(x) 
    #define DEBUG_PRINTLN(x) 
    #endif
    #define ONE_DAY_TIME 86400000 //24 часа
    #define ONE_HOUR_TIME 3600000 //1 час
    
    #define MY_SMART_SLEEP_WAIT_DURATION_MS (30000ul) //время прослушки эфира до ухода в сон
    #define TIME_SLEEP_INIT 123456
    #define RSSI_REPORT_INTERVAL 180000
    #define BATTERY_REPORT_INTERVAL 600000
    //#define BATTERY_REPORT_INTERVAL ONE_DAY_TIME
    //#define MY_INDICATION_HANDLER
    // Enable and select radio type attached
    //#define MY_RADIO_NRF24
    //#define MY_RF24_PA_LEVEL (RF24_PA_MAX)
    //#define MY_RF24_CHANNEL (124)
    //#define MY_RF24_CHANNEL (125)
    #define MY_RADIO_RFM69
    #define MY_IS_RFM69HW
    #define MY_RFM69_NEW_DRIVER   // ATC on RFM69 works only with the new driver (not compatible with old=default driver)
    #define MY_RFM69_ATC_MODE_ENABLED
    #define MY_RFM69_ATC_TARGET_RSSI_DBM (-70)  // target RSSI -70dBm
    #define MY_RFM69_MAX_POWER_LEVEL_DBM (20)   // max. TX power 10dBm = 10mW
    //#define MY_DEBUG_VERBOSE_RFM69
    
    //**** Message signing settings
    //#define MY_RFM69_ENABLE_ENCRYPTION
    /*#define MY_SIGNING_SOFT
    #define MY_SIGNING_SOFT_RANDOMSEED_PIN 7
    #define MY_SIGNING_REQUEST_SIGNATURES*/
    
    #ifdef MY_RADIO_RFM69
    #define MY_SIGNAL_REPORT_ENABLED
    #endif
    
    // Set baud rate to same as optibot
    #define MY_BAUD_RATE 9600
    
    #define MY_NODE_ID 1
    #define SKETCH_NAME "Door Sensor"
    #define SKETCH_VERSION "1.0"
    
    #define ID_S_DOOR 		1   // Id of the sensor child
    #define ID_TIME_SLEEP  	2
    #define ID_RSSI		  	3
    
    #define PIN_S_DOOR  	3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define PIN_PWR   		8   // Pin for power sensors in sleep.
    #define VCC_MIN 		1.8
    #define VCC_MAX 		2.8
    
    #include <MySensors.h>
    #include <Vcc.h>
    Vcc vcc;
    
    /*
    //Отправить уровень батареи после стольких циклов передач.Максимальный интервал отчета также будет равен этому количеству дней.
    int BATTERY_REPORT_BY_IRT_CYCLE = 10; 
    // "false" will make the first loop disregard high output from HV-505 (from start-up) and make a battery report instead.  
    bool interruptReturn = false;
    */
    
    
    #ifdef MY_RADIO_RFM69
    MyMessage msg_RSSI(ID_RSSI,V_VAR5);//Создаем контейнер для измерителя RSSI
    #endif
    MyMessage msg_S_DOOR(ID_S_DOOR, V_TRIPPED);// Создаем контейнер для датчика
    MyMessage msg_S_DOOR_ARM(ID_S_DOOR, V_ARMED);// Создаем контейнер для датчика
    MyMessage msg_TIME_SLEEP(ID_TIME_SLEEP, V_VAR5);
    
    
    
    bool first_start;
    bool s_armed; //режим работы сенсора - armed, bypassed
    unsigned long time_sleep;
    unsigned long lastSendRSSI = 0;
    unsigned long lastSendBattery = 0;
    unsigned long millis_in_sleep = 0;
    unsigned long millis_total = 0;
    int sleep_returt;
    
    
    void setup()  
    {  
    	DEBUG_PRINTLN("Setup");
    	first_start = true;
    	s_armed = true;
    	time_sleep = TIME_SLEEP_INIT;
    	pinMode(PIN_PWR, OUTPUT);//Пин питающий датчики
    	digitalWrite(PIN_PWR, HIGH);
    	pinMode(PIN_S_DOOR, INPUT);      // Вход датчика
    	digitalWrite(PIN_S_DOOR, HIGH);  //подтяжка к плюсу для отладки
    }
    
    void presentation()  {
    	sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
    	present(ID_S_DOOR, S_DOOR, "Door Sensor");
    	present(ID_TIME_SLEEP, S_CUSTOM, "Time sleep");
    }
    
    void loop()     
    {
    	if(first_start){
    		first_start = false;
    		DEBUG_PRINTLN("Start");
    		send(msg_S_DOOR.set(false));
    		send(msg_S_DOOR_ARM.set(s_armed));
    		//send(msg_TIME_SLEEP.set(time_sleep));
    		//sendRSSIReport();
    		//sendBatteryReport();
    		request(ID_TIME_SLEEP, S_CUSTOM, V_VAR5); //запрос значения time_sleep
    
    	}	
    	millis_total = millis() + millis_in_sleep + MY_SMART_SLEEP_WAIT_DURATION_MS;
    	#ifdef MY_RADIO_RFM69
    	if (millis_total - lastSendRSSI > RSSI_REPORT_INTERVAL) {
    		lastSendRSSI = millis_total;
    		sendRSSIReport();
    	}
    	#endif
    	if (millis_total - lastSendBattery > BATTERY_REPORT_INTERVAL) {
    		lastSendBattery = millis_total;
    		sendBatteryReport(); 
    	}
    
    	//sendHeartbeat();
    	sleep_returt = sleep(digitalPinToInterrupt(PIN_S_DOOR),CHANGE,time_sleep,true);
    	if(sleep_returt != MY_WAKE_UP_BY_TIMER){ // проснулись по тревоге
    		send(msg_S_DOOR.set(true));
    		DEBUG_PRINTLN("ТРЕВОГА!!!");
    	}
    	else{
    		millis_in_sleep = millis_in_sleep + time_sleep;
    	}
    	DEBUG_PRINT("ts = ");DEBUG_PRINTLN(time_sleep);
    	if(time_sleep == 123456) send(msg_TIME_SLEEP.set(time_sleep)
    }
    
    /*********************FUNCTIONS**********************************/
    /*
    void receive(const MyMessage &message) {
        if (message.sensor == YOUR_SENSOR_ID) {
            if (message.type == V_TRIPPED) {
              //Do somthing here
            } else if (message.type == V_ARMED) {
              //Do somthing here
            }
        }
    }
    */
    
    //функция обработки входящих сообщений
    void receive(const MyMessage &message) {
        if (message.sensor == ID_S_DOOR) {		//проверяем, что именно этому сенсору
    		if (message.type == V_ARMED) {		//проверяем тип переменной
    			s_armed = message.getBool();	//получаем значение V_ARMED
    			DEBUG_PRINT("V_ARMED received: ");DEBUG_PRINTLN(s_armed);
    			if(s_armed){
    				DEBUG_PRINTLN("Power On");
    				//digitalWrite(PIN_PWR, HIGH); //включаем питание
    				digitalWrite(PIN_PWR, LOW); //для отладки
    			}
    			else{
    				DEBUG_PRINTLN("Power Off");
    				//digitalWrite(PIN_PWR, LOW); //отключаем питание	
    				digitalWrite(PIN_PWR, HIGH); //для отладки
    			}
            }
        }
    	else if (message.sensor == ID_TIME_SLEEP){
    		DEBUG_PRINT("reseived ID_TIME_SLEEP type var =");DEBUG_PRINTLN(message.type);
    		if (message.type == V_VAR5) {
    			time_sleep = message.getULong(); //получаем значение sleep_time
    			DEBUG_PRINT("time_sleep = ");DEBUG_PRINTLN(time_sleep);
    		}
    	}
    }
    //функция отправки RSSI если используем RFM69 радио
    
    #ifdef MY_RADIO_RFM69
    void sendRSSIReport() 
    {
    	int16_t rssiVal;
    	char rssiStr[14];
        rssiVal = transportGetSignalReport(SR_RX_RSSI); 
    //    snprintf(rssiStr,10,"rssi: %d%",rssiVal,"dBm");
    	sprintf (rssiStr, "rssi:%d dBm", rssiVal);
        send(msg_RSSI.set(rssiStr));
    	DEBUG_PRINTLN(rssiStr);
    	//DEBUG_PRINT("TxRssi = ");DEBUG_PRINTLN(transportGetSignalReport(SR_TX_RSSI));
    }
    #endif
    //функция отправки отчета состояния батареи
    void sendBatteryReport() 
    {
      float p = vcc.Read_Perc(VCC_MIN, VCC_MAX, true);
      int batteryPcnt = static_cast<int>(p);
      sendBatteryLevel(batteryPcnt);
      DEBUG_PRINT("Battery is: ");DEBUG_PRINTLN(batteryPcnt);
    }
    

    Help me figure out what I'm doing wrong? Why is not sending a new value to my node variable?
    And why initially from the command

    request (ID_TIME_SLEEP, S_CUSTOM, V_VAR5);
    

    in the MyController does not create a variable of type V_VAR5?

    Thanx!



  • I found the reason that the variable is not updated.
    In MyController, the node is added by default as not sleeping. It should be noted chek box with the name "Slepeeng" in the properties of the node.

    There were two questions:

    1. Whether it is possible to make so that on a command from a node
    request (ID_TIME_SLEEP, S_CUSTOM, V_VAR5);
    

    in MyController created a variable of the desired type?

    1. How it is possible from the side of a node to inform in MуController, that it sleeping?
      And how to process this message in MyController for the automatic check box "Node Slepping" in the node properties?

  • ADMIN

    @wanvo

    1. Whether it is possible to make so that on a command from a node

    Yes, it is possible. I see some error in your request command, should be as, for more information refer this, you h

    request (ID_TIME_SLEEP, V_VAR5);
    
    1. How it is possible from the side of a node to inform in MуController, that it sleeping?
      And how to process this message in MyController for the automatic check box "Node Slepping" in the node properties?

    For now, it is not possible to mention it as a sleeping node. Please add a request on GitHub.



  • @jkandasa said in Variable not update in sleep node:

    @wanvo

    1. Whether it is possible to make so that on a command from a node

    Yes, it is possible. I see some error in your request command, should be as, for more information refer this, you h

    request (ID_TIME_SLEEP, V_VAR5);
    
    1. How it is possible from the side of a node to inform in MуController, that it sleeping?
      And how to process this message in MyController for the automatic check box "Node Slepping" in the node properties?

    For now, it is not possible to mention it as a sleeping node. Please add a request on GitHub.

    1. Really everything works great! The variable of the required type is created.
      I inattentively read the documentation and added an extra parameter "S_CUSTOM".
      Many thanks for the advice!

    2. This request created. I will be very grateful if you can add this feature.


 

10
Online

2.2k
Users

302
Topics

1.8k
Posts

Looks like your connection to MYCONTROLLER.ORG was lost, please wait while we try to reconnect.