[SOLVED] GW reports wrong value after node restart



  • Hi guys

    First of all, I want to say thank you for such a great lib as MySensors 🙂
    I'm pretty new and experimenting.
    And possibly, there is some bug...
    Config: MySensorsESP8266MQTT gw + Domoticz + one node (merge batteryPowered + dimmableLight).
    There are two sensors in node: 0 - switch, 1 - dimmer.
    I know you want to see sources, here it is

    /*
       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.
    
     *******************************
    
       DESCRIPTION
    
       This is an example that demonstrates how to report the battery level for a sensor
       Instructions for measuring battery capacity on A0 are available here:
       http://www.mysensors.org/build/battery
    
    */
    
    
    
    // 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
    
    #include <MySensors.h>
    
    int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
    
    uint32_t SLEEP_TIME = 900000;  // sleep time between reads (seconds * 1000 milliseconds)
    int oldBatteryPcnt = 0;
    
    #define LED_PIN 3      // Arduino pin attached to MOSFET Gate pin
    #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
    
    static int16_t currentLevel = 0;  // Current dim level...
    
    MyMessage lightMsg(0, V_LIGHT);
    MyMessage dimmerMsg(1, V_DIMMER);
    
    void setup()
    {
      // use the 1.1 V internal reference
    #if defined(__AVR_ATmega2560__)
      analogReference(INTERNAL1V1);
    #else
      analogReference(INTERNAL);
    #endif
    
      request( 1, V_DIMMER );
    }
    
    void presentation()
    {
      present( 0, S_LIGHT );
      present( 1, S_DIMMER );
      sendSketchInfo("Battery dimmer", "1.1");
    }
    
    uint32_t readVcc() {
      auto admux = ADMUX;
    
      // Read 1.1V reference against AVcc
      // set the reference to Vcc and the measurement to the internal 1.1V reference
    #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
      ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
    #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
      ADMUX = _BV(MUX5) | _BV(MUX0);
    #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
      ADMUX = _BV(MUX3) | _BV(MUX2);
    #else
      ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
    #endif
    
      delay(2); // Wait for Vref to settle
      ADCSRA |= _BV(ADSC); // Start conversion
      while (bit_is_set(ADCSRA, ADSC)); // measuring
    
      uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH
      uint8_t high = ADCH; // unlocks both
    
      uint32_t result = (high << 8) | low;
    
      result = 1125300 / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
    
      ADMUX = admux;
    
      delay(2); // Wait for Vref to settle
    
      return result; // Vcc in millivolts
    }
    
    
    void receive(const MyMessage &message)
    {
      if (message.type == V_LIGHT || message.type == V_DIMMER) {
    
        //  Retrieve the power or dim level from the incoming request message
        int requestedLevel = atoi( message.data );
    
        // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
        requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );
    
        // Clip incoming level to valid range of 0 to 100
        requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
        requestedLevel = requestedLevel < 0   ? 0   : requestedLevel;
    
        Serial.print( "Changing level to " );
        Serial.print( requestedLevel );
        Serial.print( ", from " );
        Serial.println( currentLevel );
    
        fadeToLevel( requestedLevel );
    
        // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
        send(lightMsg.set(currentLevel > 0));
    
        // hek comment: Is this really nessesary?
        send( dimmerMsg.set(currentLevel) );
    
    
      }
    }
    
    /***
        This method provides a graceful fade up/down effect
    */
    void fadeToLevel( int toLevel )
    {
    
      int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
    
      while ( currentLevel != toLevel ) {
        currentLevel += delta;
        analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
        delay( FADE_DELAY );
      }
    }
    
    void loop()
    {
      static long lastMillis = millis() - 60001;
      if (millis() - lastMillis < 60000) {
        return;
      }
      lastMillis = millis();
    
    
      Serial.print("Voltage: ");
      Serial.println(readVcc());
    
      // get the battery Voltage
      int sensorValue = analogRead(BATTERY_SENSE_PIN);
    #ifdef MY_DEBUG
      Serial.println(sensorValue);
    #endif
    
      // 1M, 470K divider across battery and using internal ADC ref of 1.1V
      // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
      // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
      // 3.44/1023 = Volts per bit = 0.003363075
    
      int batteryPcnt = sensorValue / 10;
      batteryPcnt = 90;
    
    #ifdef MY_DEBUG
      float batteryV  = sensorValue * 0.003363075;
      Serial.print("Battery Voltage: ");
      Serial.print(batteryV);
      Serial.println(" V");
    
      Serial.print("Battery percent: ");
      Serial.print(batteryPcnt);
      Serial.println(" %");
    #endif
    
      if (oldBatteryPcnt != batteryPcnt) {
        // Power up radio after sleep
        sendBatteryLevel(batteryPcnt, true);
        oldBatteryPcnt = batteryPcnt;
      }
    }
    

    The problem is: when I turn to switch off in domoticz and restart the node, it goes back to the previous dimmer value.

    17526 GWT:IMQ:TOPIC=domoticz/out/MyMQTT/90/1/2/0/3, MSG RECEIVED
    17534 TSF:MSG:SEND,0-0-90-90,s=1,c=2,t=3,pt=0,l=2,sg=0,ft=0,st=OK:50 <-- set dimmer to 50
    18043 TSF:MSG:READ,90-90-0,s=0,c=1,t=2,pt=1,l=1,sg=0:1
    18048 GWT:TPS:TOPIC=domoticz/in/MyMQTT/90/0/1/0/2,MSG SENT
    18055 TSF:MSG:READ,90-90-0,s=1,c=1,t=3,pt=2,l=2,sg=0:50
    18060 GWT:TPS:TOPIC=domoticz/in/MyMQTT/90/1/1/0/3,MSG SENT
    66986 GWT:IMQ:TOPIC=domoticz/out/MyMQTT/0/0/3/0/18, MSG RECEIVED
    66992 GWT:TPS:TOPIC=domoticz/in/MyMQTT/0/255/3/0/22,MSG SENT
    174098 GWT:IMQ:TOPIC=domoticz/out/MyMQTT/0/0/3/0/18, MSG RECEIVED
    174104 GWT:TPS:TOPIC=domoticz/in/MyMQTT/0/255/3/0/22,MSG SENT
    281211 GWT:IMQ:TOPIC=domoticz/out/MyMQTT/0/0/3/0/18, MSG RECEIVED
    281217 GWT:TPS:TOPIC=domoticz/in/MyMQTT/0/255/3/0/22,MSG SENT
    387296 GWT:IMQ:TOPIC=domoticz/out/MyMQTT/0/0/3/0/18, MSG RECEIVED
    387302 GWT:TPS:TOPIC=domoticz/in/MyMQTT/0/255/3/0/22,MSG SENT
    465838 GWT:IMQ:TOPIC=domoticz/out/MyMQTT/90/1/1/0/2, MSG RECEIVED
    465838 GWT:IMQ:TOPIC=domoticz/out/MyMQTT/90/1/1/0/2, MSG RECEIVED <-- receive command from domoticz
    465846 TSF:MSG:SEND,0-0-90-90,s=1,c=1,t=2,pt=0,l=1,sg=0,ft=0,st=OK:0 <-- set dimmer to 0
    466355 TSF:MSG:READ,90-90-0,s=0,c=1,t=2,pt=1,l=1,sg=0:0  <-- confirm for switch
    466360 GWT:TPS:TOPIC=domoticz/in/MyMQTT/90/0/1/0/2,MSG SENT
    466367 TSF:MSG:READ,90-90-0,s=1,c=1,t=3,pt=2,l=2,sg=0:0  <-- confirm for dimmer
    466372 GWT:TPS:TOPIC=domoticz/in/MyMQTT/90/1/1/0/3,MSG SENT
    490926 GWT:IMQ:TOPIC=domoticz/out/MyMQTT/0/0/3/0/18, MSG RECEIVED
    490932 GWT:TPS:TOPIC=domoticz/in/MyMQTT/0/255/3/0/22,MSG SENT
    596418 TSF:MSG:READ,90-90-255,s=255,c=3,t=7,pt=0,l=0,sg=0:
    -- rebooting node
    596424 TSF:MSG:BC
    596425 TSF:MSG:FPAR REQ,ID=90
    596428 TSF:PNG:SEND,TO=0
    596430 TSF:CKU:OK
    596432 TSF:MSG:GWL OK
    596902 TSF:MSG:SEND,0-0-90-90,s=255,c=3,t=8,pt=1,l=1,sg=0,ft=0,st=OK:0
    598039 GWT:IMQ:TOPIC=domoticz/out/MyMQTT/0/0/3/0/18, MSG RECEIVED
    598045 GWT:TPS:TOPIC=domoticz/in/MyMQTT/0/255/3/0/22,MSG SENT
    598466 TSF:MSG:READ,90-90-0,s=255,c=3,t=24,pt=1,l=1,sg=0:1
    598471 TSF:MSG:PINGED,ID=90,HP=1
    598481 TSF:MSG:SEND,0-0-90-90,s=255,c=3,t=25,pt=1,l=1,sg=0,ft=0,st=OK:1
    598494 TSF:MSG:READ,90-90-0,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    598502 TSF:MSG:SEND,0-0-90-90,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    598508 TSF:MSG:READ,90-90-0,s=255,c=0,t=17,pt=0,l=5,sg=0:2.3.1
    598514 GWT:TPS:TOPIC=domoticz/in/MyMQTT/90/255/0/0/17,MSG SENT
    598521 TSF:MSG:READ,90-90-0,s=255,c=3,t=6,pt=1,l=1,sg=0:0
    598526 GWT:TPS:TOPIC=domoticz/in/MyMQTT/90/255/3/0/6,MSG SENT
    598654 GWT:IMQ:TOPIC=domoticz/out/MyMQTT/90/255/3/0/6, MSG RECEIVED
    598662 TSF:MSG:SEND,0-0-90-90,s=255,c=3,t=6,pt=0,l=1,sg=0,ft=0,st=OK:M
    598669 TSF:MSG:READ,90-90-0,s=0,c=0,t=3,pt=0,l=0,sg=0:
    598674 GWT:TPS:TOPIC=domoticz/in/MyMQTT/90/0/0/0/3,MSG SENT
    598680 TSF:MSG:READ,90-90-0,s=1,c=0,t=4,pt=0,l=0,sg=0:
    598685 GWT:TPS:TOPIC=domoticz/in/MyMQTT/90/1/0/0/4,MSG SENT
    598692 TSF:MSG:READ,90-90-0,s=255,c=3,t=11,pt=0,l=14,sg=0:Battery dimmer
    598698 GWT:TPS:TOPIC=domoticz/in/MyMQTT/90/255/3/0/11,MSG SENT
    598705 TSF:MSG:READ,90-90-0,s=255,c=3,t=12,pt=0,l=3,sg=0:1.1
    598711 GWT:TPS:TOPIC=domoticz/in/MyMQTT/90/255/3/0/12,MSG SENT
    598718 TSF:MSG:READ,90-90-0,s=255,c=3,t=26,pt=1,l=1,sg=0:2
    598730 TSF:MSG:SEND,0-0-90-90,s=255,c=3,t=27,pt=1,l=1,sg=0,ft=0,st=OK:1
    598740 TSF:MSG:READ,90-90-0,s=1,c=2,t=3,pt=0,l=0,sg=0:
    598745 GWT:TPS:TOPIC=domoticz/in/MyMQTT/90/1/2/0/3,MSG SENT
    598757 TSF:MSG:READ,90-90-0,s=255,c=3,t=0,pt=1,l=1,sg=0:90
    598762 TSF:MSG:ACK REQ
    598766 TSF:MSG:SEND,0-0-90-90,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:90
    598772 GWT:TPS:TOPIC=domoticz/in/MyMQTT/90/255/3/0/0,MSG SENT
    599267 GWT:IMQ:TOPIC=domoticz/out/MyMQTT/90/1/2/0/3, MSG RECEIVED
    599275 TSF:MSG:SEND,0-0-90-90,s=1,c=2,t=3,pt=0,l=2,sg=0,ft=0,st=OK:50 <-- here is a wrong value received
    599785 TSF:MSG:READ,90-90-0,s=0,c=1,t=2,pt=1,l=1,sg=0:1
    599790 GWT:TPS:TOPIC=domoticz/in/MyMQTT/90/0/1/0/2,MSG SENT
    599796 TSF:MSG:READ,90-90-0,s=1,c=1,t=3,pt=2,l=2,sg=0:50 <-- confirmed
    

    I appreciate any help with this problem. Thanks!



  • Well, seems like the problem is on domoticz side.
    I've change note to request switch state (instead of dimmer) from controller on boot.

    -- set dimmer to 31
    7/7/2019, 10:50:07 PMnode: 2be13ec4.72cdd2
    domoticz/out/MyMQTT/90/1/1/0/3 : msg.payload : string[2]
    "31"
    7/7/2019, 10:50:07 PMnode: 2be13ec4.72cdd2
    domoticz/out : msg.payload : string[265]
    "{↵ "Battery" : 255,↵ "Level" : 31,↵ "RSSI" : 12,↵ "description" : "",↵ "dtype" : "Light/Switch",↵ "id" : "0000005A",↵ "idx" : 2,↵ "name" : "Light",↵ "nvalue" : 2,↵ "stype" : "AC",↵ "svalue1" : "31",↵ "switchType" : "Dimmer",↵ "unit" : 1↵}↵"
    7/7/2019, 10:50:08 PMnode: 2be13ec4.72cdd2
    domoticz/in/MyMQTT/90/0/1/0/2 : msg.payload : string[1]
    "1"
    7/7/2019, 10:50:09 PMnode: 2be13ec4.72cdd2
    domoticz/in/MyMQTT/90/1/1/0/3 : msg.payload : string[2]
    "31"
    -- switch off
    7/7/2019, 10:50:10 PMnode: 2be13ec4.72cdd2
    domoticz/out/MyMQTT/90/1/1/0/2 : msg.payload : string[1]
    "0"
    7/7/2019, 10:50:11 PMnode: 2be13ec4.72cdd2
    domoticz/out : msg.payload : string[265]
    "{↵ "Battery" : 255,↵ "Level" : 31,↵ "RSSI" : 12,↵ "description" : "",↵ "dtype" : "Light/Switch",↵ "id" : "0000005A",↵ "idx" : 2,↵ "name" : "Light",↵ "nvalue" : 0,↵ "stype" : "AC",↵ "svalue1" : "31",↵ "switchType" : "Dimmer",↵ "unit" : 1↵}↵"
    -- confirm from node
    7/7/2019, 10:50:12 PMnode: 2be13ec4.72cdd2
    domoticz/in/MyMQTT/90/0/1/0/2 : msg.payload : string[1]
    "0"
    7/7/2019, 10:50:13 PMnode: 2be13ec4.72cdd2
    domoticz/in/MyMQTT/90/1/1/0/3 : msg.payload : string[1]
    "0"
    7/7/2019, 10:50:14 PMnode: 2be13ec4.72cdd2
    domoticz/out : msg.payload : string[248]
    "{↵ "Battery" : 90,↵ "RSSI" : 12,↵ "description" : "",↵ "dtype" : "Light/Switch",↵ "id" : "0000005A",↵ "idx" : 1,↵ "name" : "Light",↵ "nvalue" : 0,↵ "stype" : "AC",↵ "svalue1" : "100",↵ "switchType" : "On/Off",↵ "unit" : 0↵}↵"
    -- reboot node
    7/7/2019, 10:50:22 PMnode: 2be13ec4.72cdd2
    domoticz/in/MyMQTT/90/255/0/0/17 : msg.payload : string[5]
    "2.3.1"
    7/7/2019, 10:50:22 PMnode: 2be13ec4.72cdd2
    domoticz/in/MyMQTT/90/255/3/0/6 : msg.payload : string[1]
    "0"
    7/7/2019, 10:50:23 PMnode: 2be13ec4.72cdd2
    domoticz/out/MyMQTT/90/255/3/0/6 : msg.payload : string[1]
    "M"
    7/7/2019, 10:50:24 PMnode: 2be13ec4.72cdd2
    domoticz/in/MyMQTT/90/0/0/0/3 : msg.payload : string[0]
    ""
    7/7/2019, 10:50:25 PMnode: 2be13ec4.72cdd2
    domoticz/in/MyMQTT/90/1/0/0/4 : msg.payload : string[0]
    ""
    7/7/2019, 10:50:26 PMnode: 2be13ec4.72cdd2
    domoticz/in/MyMQTT/90/255/3/0/11 : msg.payload : string[14]
    "Battery dimmer"
    7/7/2019, 10:50:27 PMnode: 2be13ec4.72cdd2
    domoticz/in/MyMQTT/90/255/3/0/12 : msg.payload : string[3]
    "1.1"
    -- i assume this is request from node
    7/7/2019, 10:50:28 PMnode: 2be13ec4.72cdd2
    domoticz/in/MyMQTT/90/0/2/0/3 : msg.payload : string[0]
    ""
    7/7/2019, 10:50:29 PMnode: 2be13ec4.72cdd2
    domoticz/in/MyMQTT/90/255/3/0/0 : msg.payload : string[2]
    "90"
    -- here is switch 100(????) from domoticz
    7/7/2019, 10:50:30 PMnode: 2be13ec4.72cdd2
    domoticz/out/MyMQTT/90/0/2/0/3 : msg.payload : string[3]
    "100"
    7/7/2019, 10:50:31 PMnode: 2be13ec4.72cdd2
    domoticz/in/MyMQTT/90/0/1/0/2 : msg.payload : string[1]
    "1"
    7/7/2019, 10:50:32 PMnode: 2be13ec4.72cdd2
    domoticz/in/MyMQTT/90/1/1/0/3 : msg.payload : string[3]
    "100"
    7/7/2019, 10:50:32 PMnode: 2be13ec4.72cdd2
    domoticz/out : msg.payload : string[248]
    "{↵ "Battery" : 90,↵ "RSSI" : 12,↵ "description" : "",↵ "dtype" : "Light/Switch",↵ "id" : "0000005A",↵ "idx" : 1,↵ "name" : "Light",↵ "nvalue" : 1,↵ "stype" : "AC",↵ "svalue1" : "100",↵ "switchType" : "On/Off",↵ "unit" : 0↵}↵"
    7/7/2019, 10:50:32 PMnode: 2be13ec4.72cdd2
    domoticz/out : msg.payload : string[266]
    "{↵ "Battery" : 90,↵ "Level" : 100,↵ "RSSI" : 12,↵ "description" : "",↵ "dtype" : "Light/Switch",↵ "id" : "0000005A",↵ "idx" : 2,↵ "name" : "Light",↵ "nvalue" : 1,↵ "stype" : "AC",↵ "svalue1" : "100",↵ "switchType" : "Dimmer",↵ "unit" : 1↵}↵"
    7/7/2019, 10:51:22 PMnode: 2be13ec4.72cdd2
    domoticz/out/MyMQTT/0/0/3/0/18 : msg.payload : string[4]
    "PING"
    7/7/2019, 10:51:22 PMnode: 2be13ec4.72cdd2
    domoticz/in/MyMQTT/0/255/3/0/22 : msg.payload : string[7]
    "1899265"
    


  • Sorry guys, the problem is on my side:

    // instead of
      request( 0, S_LIGHT );
    // should be
      request( 0, V_LIGHT );
    

    I don't know why this mistake leads to such behavior, but now it's ok.


  • Mod

    Nice work @alex-kubrinsky, thanks for sharing the solution.


Log in to reply
 

Suggested Topics

  • 33
  • 3
  • 8
  • 17
  • 9
  • 5

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts