MQTT Dimmer

I have set up an led dimmer based on Mysensors project with the MQTT client gateway.
It somehow works when i send an mqtt command like this
$ mosquitto_pub -t mygateway1-in/88/0/1/0/3 -m "10"

This is what i have in my items
Dimmer Dimmed_Light "Dimmer Light [%d %%]" <slider> { mqtt=">[mymosquitto:mygateway1-in/88/0/0/3:state:*:default]" }

Then a rule

`

 /**
* This is a demo rule which simulates a real dimmer by reacting to increase/decrease    commands
* and posting an updated state on the bus
*/ 
rule "Dimmed Light"
when
    Item Dimmed_Light received command
then
    var Number percent = 100
    if(Dimmed_Light.state instanceof DecimalType) percent = Dimmed_Light.state as DecimalType

    if(receivedCommand==INCREASE) percent = percent + 5
    if(receivedCommand==DECREASE) percent = percent - 5

    if(percent<0)   percent = 0
    if(percent>100) percent = 100

    postUpdate(Dimmed_Light, percent);
end

In sitemap i have - Slider item=Dimmed_Light

Somehow clicking on slider buttons does nothing. I don’e even see the messages when i subscribe to the mymosquitto:mygateway1-in channel. So seems the problem i have misconfiguration in the items string.
At the same time relay module works defined in items like this

Switch vent_vsr_high “Vent High” { mqtt=">[mymosquitto:mygateway1-in/55/3/1/0/2:command:ON:1],>[mymosquitto:mygateway1-in/55/3/1/0/2:command:OFF:0]" }```

Where di i go wrong on this one?

When you use “state” in the MQTT binding string, that tells the MQTT item binding to publish to the broker when the item’s state is updated. I think you would instead want to use command so that manipulating the widget in the sitemap, which sends commands, would cause the command to be sent to the MQTT broker.

But your rule is problematic, because it’s being triggered by a command to the item, but it’s then updating the state of the same item. This seems like you don’t really intend to have an item “talk to itself.”

Manipulating a Slider widget on a Dimmer item ought to (I think) send PercentType commands to the item, in which case there ought to be no need for a rule.

Hi,
I’ve successfully created a dimmer using a MQTT binding (OH2, HABpanel). Although it is working quite well, the binding (or openhab) is sending a command with the new value only when I release the slider (lifting the finger). Any chance to let the new values being send while I’m moving the slider with my finger?

Cheers
Pirx

dear Pirx

Is it possible that you can help me with creating a dimmer using MQTT binding For the (OH2,Habpannel)

Kind regard pepijn

Sure pepijn! What type of dimmer are you using?
Regards
Pirx

Daar pirx i al using an A-C dimmer wich i bought from eBay,
Thats works with arduino,
De dimmer module is a 8channel dimmer
Link:

https://m.ebay.com/itm/8CH-AC-LED-BULB-DIMMER-110V-220V-SMART-HOME-ARDUINO-RASPBERRY-PROGRAMMABLE/122631760038?_trkparms=aid%3D444000%26algo%3DSOI.DEFAULT%26ao%3D1%26asc%3D20171010182220%26meid%3Da99bdaa180be46b9955f80dd7452a972%26pid%3D100891%26rk%3D4%26rkt%3D12%26sd%3D112410704471&_trksid=p2056116.c100891.m5206

I has got a arduino sketch copyt from a RGB led strip but i do bot understand the sketch.
Hope so that you can help me
Kind regard pepijn

Did I got it right? The dimmer still not working and you need help to understand a sketch on Arduino? …Send me a link to that sketch, but I cannot promise anything :slight_smile:

1 Like

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h> // http://knolleary.net/arduino-client-for-mqtt/

// MAC Adresse des Ethernet Shields
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
// IP des MQTT Servers
byte server[] = { 192, 168, 1, 12 };
// Ethernet Client zur Kommunikation des MQTT Clients
EthernetClient ethClient;
EthernetClient Client;
IPAddress broker(192,168,1,12);
// MQTT Client zur Kommunikation mit dem Server
// Server - Variable des Types byte mit Serveradresse
// 1883 - Ist der Standard TCP Port
// callback - Function wird aufgerufen wen MQTT Nachrichten eintreffen. Am ende des Sketches
// ethClient - Angabe des Ethernet Clients
PubSubClient client(ethClient);

// Timervariable für eine Verzögerung. Als alternative zu delay was die verarbeitung anhält.
int timer = 0;
int EndTimer = 200;

// Pins des DIMMER
int dim1=4;
int dim2=5;
int dim3=6;
int dim4=7;
int dim5=8;
int dim6=9;
int dim7=10;
int dim8=11;
// Übermittelte Farbwerte
int dim1Value = 0;
int dim2Value = 0;
int dim3Value = 0;
int dim4Value = 0;
int dim5Value = 0;
int dim6Value = 0;
int dim7Value = 0;
int dim8Value = 0;
// Gerade gesetzte Farbe
int CURdim1Value = 0;
int CURdim2Value = 0;
int CURdim3Value = 0;
int CURdim4Value = 0;
int CURdim5Value = 0;
int CURdim6Value = 0;
int CURdim7Value = 0;
int CURdim8Value = 0;
void setup()
{
// Setzen der PINS als Ausgang
pinMode(dim8, OUTPUT);
pinMode(dim7, OUTPUT);
pinMode(dim6, OUTPUT);
pinMode(dim5, OUTPUT);
pinMode(dim4, OUTPUT);
pinMode(dim3, OUTPUT);
pinMode(dim2, OUTPUT);
pinMode(dim1, OUTPUT);

// Bei start Farbe Blau setzen
analogWrite(dim1, 0);
analogWrite(dim2, 0);
analogWrite(dim3, 0);
analogWrite(dim4, 0);
analogWrite(dim5, 0);
analogWrite(dim6, 0);
analogWrite(dim7, 0);
analogWrite(dim8, 0);
// Initialisierung des Ethernets
if (Ethernet.begin(mac) == 0) {

// Wenn DHCP fehlschlägt dann rot setzen und aufhören
analogWrite(dim1, 0);
analogWrite(dim2, 0);
analogWrite(dim3, 0);
analogWrite(dim4, 0);
analogWrite(dim5, 0);
analogWrite(dim6, 0);
analogWrite(dim7, 100);
analogWrite(dim8, 100);
while (true);
}
else {
// Wenn DHCP OK ist dann grün setzen
dim2Value = 100;
dim7Value = 100
}

}

void loop()
{
// Aufbau der Verbindung mit MQTT falls diese nicht offen ist.
if (!client.connected()) {
client.connect(“arduinoClient”);
// Abonieren von Nachrichten mit dem angegebenen Topics
client.subscribe("/dd16/dimmer/1/#");
client.subscribe("/dd16/dimmer/2/#");
client.subscribe("/dd16/dimmer/3/#");
client.subscribe("/dd16/dimmer/4/#");
client.subscribe("/dd16/dimmer/5/#");
client.subscribe("/dd16/dimmer/6/#");
client.subscribe("/dd16/dimmer/7/#");
client.subscribe("/dd16/dimmer/8/#");
// Alternative Abonierung aller Topics unter /openHAB/Nachtlicht
// mqttClient.subscribe("/openHAB/Nachtlicht/#");
}

if (timer <= EndTimer) timer++;
else {
timer = 0;

if (dim1Value < CURdim1Value) CURdim1Value–;
else if (dim1Value > CURdim1Value) CURdim1Value++;

if (dim2Value < CURdim2Value) CURdim2Value–;
else if (dim2Value > CURdim2Value) CURdim2Value++;

if (dim3Value < CURdim3Value) CURdim3Value–;
else if (dim3Value > CURdim3Value) CURdim3Value++;

if (dim4Value < CURdim4Value) CURdim4Value–;
else if (dim3Value > CURdim3Value) CURdim3Value++;

if (dim5Value < CURdim5Value) CURdim5Value–;
else if (dim5Value > CURdim5Value) CURdim5Value++;

if (dim6Value < CURdim6Value) CURdim6Value–;
else if (dim6Value > CURdim6Value) CURdim6Value++;

if (dim7Value < CURdim7Value) CURdim7Value–;
else if (dim7Value > CURdim7Value) CURdim7Value++;

if (dim8Value < CURdim8Value) CURdim8Value–;
else if (dim8Value > CURdim8Value) CURdim8Value++;
}

analogWrite(dim1, CURdim1Value);
analogWrite(dim2, CURdim2Value);
analogWrite(dim3, CURdim3Value);
analogWrite(dim4, CURdim4Value);
analogWrite(dim5, CURdim5Value);
analogWrite(dim6, CURdim6Value);
analogWrite(dim7, CURdim7Value);
analogWrite(dim8, CURdim8Value);
client.loop(); // Schleife für MQTT

}

// ===========================================================
// Callback Funktion von MQTT. Die Funktion wird aufgerufen
// wenn ein Wert empfangen wurde.
// ===========================================================
void callback(char* topic, byte* payload, unsigned int length) {
// Zähler
int i = 0;
// Hilfsvariablen für die Convertierung der Nachricht in ein String
char message_buff[100];

// Kopieren der Nachricht und erstellen eines Bytes mit abschließender \0
for(i=0; i<length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = ‘\0’;

// Konvertierung der nachricht in ein String
String msgString = String(message_buff);

// Überprüfung des Topis und setzen der Farbe je nach übermittelten Topic
if (String(topic) == “/dd16/dimmer/1”) dim1Value = round(msgString.toInt() * 2.55);
if (String(topic) == “/dd16/dimmer/2”) dim2Value = round(msgString.toInt() * 2.55);
if (String(topic) == “/dd16/dimmer/3”) dim3Value = round(msgString.toInt() * 2.55);
if (String(topic) == “/dd16/dimmer/4”) dim4Value = round(msgString.toInt() * 2.55);
if (String(topic) == “/dd16/dimmer/5”) dim5Value = round(msgString.toInt() * 2.55);
if (String(topic) == “/dd16/dimmer/6”) dim6Value = round(msgString.toInt() * 2.55);
if (String(topic) == “/dd16/dimmer/7”) dim7Value = round(msgString.toInt() * 2.55);
if (String(topic) == “/dd16/dimmer/8”) dim8Value = round(msgString.toInt() * 2.55);
}

Dear pirx,
I have upload the sketch to this topic :slight_smile:
I hope that you can help me understanding the sketch and that you can help me to make a right config for openhab2

I understand roughly the code. I assume someone has attached two RGB lights to the dimmer!? The code is repeatedly reading the input (which is fed by OpenHab sending RGB values via MQTT) and adjusting the dimmer in a slow way…by increasing/decreasing the dimmer values by +1/-1 every 200 cycles.
My questions are now, where are you staying with your knowledge?

  1. are you beginner to Arduino programming at all?
  2. do you understand the MQTT communication?
  3. can you setup MQTT on Arduino?
  4. same on OpenHAB?
1 Like

Yes i a beginner with the arduino
I can understand Some things of the sketch
And i am able to make the Connection between the raspberry pi where My openhab2 runs and i have already setup the mossquito and the binding.

I do not understand what i have to send over the Mqtt server to dimm the light.

hope you can help me further
Sorry for My bad english

Pepijn, @Pirx is right about the code, seems though that the code you have just is a demo code… that is also not suitable for your dimmer
I know that dimmer quite well, I have built similar ones, some 5-6 years ago. The workings I explained here.
In short the working is as follows: It has a zero cross detector based around the PC817 optocoupler, giving a signal on the SYNC pin on your board.
Arduino interrupt is triggered by that sync. (either by falling edge or rising edge) Then the arduino waits a specific amount of time before it triggers the triac (through he MOC3023 optocoupler). The longer it waits…the less bright the lamp will burn.

Suppose you have a grid frequency of 50Hz. That will have 2 zerocrossings per wave, making the period within to trigger the TRIAC 10mS (8.33 for 60 Hz).
If you would ignite the TRIAC immediately after the zerocross, it will be ON during the full sinewave, if you wait (max 10mS) it will be ON during a smaller part of the sinewave and therefore be less bright.
10mS is 10.000uS. if you would take 100uS steps, you could set the level with 100 steps, making it suitable for an openhab slider

It is called leading edge dimmer because it cuts the leading edge of the sinewave in dimming. Dimmers like that are also called ‘phase cutting dimmers’ or ‘phase angle control dimmers’. There are also ‘trailing edge’ dimmers but TRIACs cant do that

With regard to your code… that does not seem like it really belongs to that specific dimmer, as it does analogwrites, which one would expect to belong to a PWM circuit, not a TRIAC circuit, so i am not surprised it isnt working. Yes it would fit an RGB strip but that is something completely different. Also the SPI is not what u need and the pin assignments suggest it is for an ArduinoMega.
You say you have a working Arduino code. Why not use that?, just add the MQTT connectivity

In yr MQTT you then send the value with which you want to delay the ignition of the TRIAC. Understand though that if you use 8 channels, you have to keep track of 8 ignition times, Seems complicated but can be done. Ofcourse when you only dim one channel it is less complicated.
I presume you are dutch?

Pepijn, I’d follow Kees suggestions as he knows that dimmer. Furthermore the PIN-allocation will work on MEGA and DUE boards, but not on UNO!

Before sending anything over MQTT always try to get something work without it. If you succeed, than do the next step and add external communication.

To your last question: if this code would be correct for that dimmer (wich is not), then the item definition in openhab should look like this one:

Dimmer My_RGB_Stripe1_Color1         "RGB Stripe1 Color1"   {mqtt=">[localbroker:/dd16/dimmer/1:command:*:default]"}
Dimmer My_RGB_Stripe1_Color2         "RGB Stripe1 Color2"   {mqtt=">[localbroker:/dd16/dimmer/2:command:*:default]"}

…and so on. You can test it for instance by creating some sliders with HabPanel. The values must be between 0 and 100 ! BTW: your Arduino code translates it to a full range of analogWrite (pin, 0…255) by multiplying with 2.55.

Last, but not least: don’t worry about your English! :slight_smile:

Pirx

2 Likes

very true, but just to be complete/avoid confusion, his board will not work with PWM

2 Likes

Hi, kees thats true i am dutch.
Thanks for your explanation!

i am trying to understand it! :smiley:

Don’t hesitate to ask further advice if you need it.
Your english is fine but if you think it hampers you in understanding or asking, just drop me a direct message in Dutch.
i will post any public-important results here in English then.

OK, my back and forth with Pepijn by pm has led me to write a tutorial on how to use a 1 channel TRIAC dimmer

1 Like