MQTT dimmer message



  • I've read through the topic on the MQTT dimmer structure...
    But still stuck.
    I have the following code

    /**
     * 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-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/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.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - February 15, 2014 - Bruce Lacey
     * Version 1.1 - August 13, 2014 - Converted to 1.4 (hek) 
     *
     * DESCRIPTION
     * This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad 
     * <henrik.ekblad@gmail.com> Vera Arduino Sensor project.  
     * Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches.
     * 
     * The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip.  
     * The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected
     * to the LED negative terminal and the MOSFET Source pin is connected to ground.  
     *
     * This sketch is extensible to support more than one MOSFET/PWM dimmer per circuit.
     * http://www.mysensors.org/build/dimmer
     */
    
    #define SN "DimmableLED"
    #define SV "1.1"
    
    #include <MySensor.h> 
    #include <SPI.h>
    
    #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)
    
    MySensor gw;
    
    static int currentLevel = 0;  // Current dim level...
    MyMessage dimmerMsg(0, V_DIMMER);
    MyMessage lightMsg(0, V_LIGHT);
    
    
    /***
     * Dimmable LED initialization method
     */
    void setup()  
    { 
      Serial.println( SN ); 
      gw.begin( incomingMessage,88,false) ;
      
      // Register the LED Dimmable Light with the gateway
      gw.present( 0, S_DIMMER );
      
      gw.sendSketchInfo(SN, SV);
      // Pull the gateway's current dim level - restore light level upon sendor node power-up
      gw.request( 0, V_DIMMER );
    }
    
    /***
     *  Dimmable LED main processing loop 
     */
    void loop() 
    {
      gw.process();
    }
    
    
    
    void incomingMessage(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...
        gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));
    
        // hek comment: Is this really nessesary?
        gw.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 );
      }
    }
    

    Am i correct that the mqtt message should look like to switch off?
    $ mosquitto_pub -t mygateway1-in/88/0/0/0/3 -m "0"



  • This post is deleted!


  • Puttin an LM1117 instead od the mosfet was the main cause. Didn't check the markings.

    Still having a problem of message construction.
    $ mosquitto_pub -t mygateway1-in/88/0/0/0/3 -m "100"
    Changing level to 100, from 1
    $ mosquitto_pub -t mygateway1-in/88/0/0/0/3 -m "0"
    Changing level to 0, from 100

    Seems working. Now
    $ mosquitto_pub -t mygateway1-in/88/0/0/0/3 -m "5"
    Changing level to 100, from 0
    $ mosquitto_pub -t mygateway1-in/88/0/0/0/3 -m "50"
    Changing level to 100, from 100

    Where is my mistake?


  • Plugin Developer

    @moskovskiy82

    Hi!

    The third digit from the left in the topic is the command type. That should be a 1 for a set message. 0 means presentation.

    So change "88/0/0/0/3" to "88/0/1/0/3".



  • Thank you!

    Done but still some problems...
    mosquitto_pub -t mygateway1-in/88/0/1/0/3 -m "10"

    read: 0-0-88 s=0,c=1,t=3,pt=0,l=2,sg=0:10
    Changing level to 100, from 0
    send: 88-88-0-0 s=0,c=1,t=2,pt=2,l=2,sg=0,st=ok:1
    send: 88-88-0-0 s=0,c=1,t=3,pt=2,l=2,sg=0,st=ok:100
    

    $ mosquitto_pub -t mygateway1-in/88/0/1/0/3 -m "5"

    read: 0-0-88 s=0,c=1,t=3,pt=0,l=1,sg=0:5
    Changing level to 100, from 100
    send: 88-88-0-0 s=0,c=1,t=2,pt=2,l=2,sg=0,st=ok:1
    send: 88-88-0-0 s=0,c=1,t=3,pt=2,l=2,sg=0,st=ok:100```

  • Plugin Developer

    What version of the mysensors library are you using for this node? There was a bug in 1.5.2 to 1.5.3 connected to payload values.

    You can try adding some debug prints in the incoming method and print the message data before and after doing the atoi conversion.



  • I am using an MQTT client gateway which was compiled frome the dev branch - so i guess it was 2.0

    For the sensors i currently use a 1.5.3 version.

    Fot me it's just seems weird that the arduino sees the correct value but still sets the wrong value


  • Plugin Developer

    @moskovskiy82 looks like you have disabled repeater mode. I guess to receive commands we should enable repeater mode.

      // The third argument enables repeater mode.
      gw.begin( incomingMessage,88,true) ;
    

  • Plugin Developer

    gw.process is called in the loop, so messages should be processed even with repeater mode turned off.

    I suggest upgrading the library to 1.5.4 for the nodes, as the bug regarding payloads was solved with that version.


 

215
Online

8.4k
Users

9.3k
Topics

97.9k
Posts