Hi,
i have similar problem with domoticz and mysensors.
Firstly, i can't get current dim level from domoticz on startup.
Secondly if i start arduino and controll it over domoticz only, dimming and on/off works. If i turn the rotary knob, domoticz commands won't work.
In serial monitor i see that new dim value has been received:
Serial.print("Fading level: ");
Serial.println(dimValue);
but output doesn't change. If i turn knob on rotary, value in domoticz changes.
I removed eeprom writes and rotary button. Only dims to off/on or domoticz commands off/on.
Domoticz 4.10171, Mysensors 2.3.1, ide 1.8.10
// Enable debug prints to serial monitor
#define MY_DEBUG
#define MY_RADIO_RF24
#define MY_NODE_ID 10
#define MY_REPEATER_FEATURE
#define MY_TRANSPORT_WAIT_READY_MS 10 //ajaviide ms, kuni läheb tööle ilma serverita
#include <MySensors.h>
#include <Encoder.h>
#define KNOB_ENC_PIN_1 5 // Rotary encoder input pin 1
#define KNOB_ENC_PIN_2 4 // Rotary encoder input pin 2
#define SEND_THROTTLE_DELAY 500 // Number of milliseconds before sending after user stops turning knob
#define CHILD_ID_Light 1
#define SN "DimmableLED"
#define SV "1.1"
#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)
int dimValue;
int fadeTo;
int fadeDelta;
bool changedByKnob = false;
bool sendDimValue = false;
unsigned long lastFadeStep;
unsigned long sendDimTimeout;
//char convBuffer[10];
static int16_t currentLevel ; // Current dim level...
MyMessage dimmerMsg(CHILD_ID_Light, V_DIMMER);
MyMessage lightMsg(CHILD_ID_Light, V_LIGHT);
Encoder knob(KNOB_ENC_PIN_1, KNOB_ENC_PIN_2);
/***
Dimmable LED initialization method
*/
void setup()
{
// Pull the gateway's current dim level - restore light level upon node power-up
request( CHILD_ID_Light, V_DIMMER);
// wait(3000);
}
void presentation()
{
// Register the LED Dimmable Light with the gateway
present(CHILD_ID_Light, S_DIMMER );
sendSketchInfo(SN, SV);
}
/***
Dimmable LED main processing loop
*/
void loop()
{
// Check if someone turned the rotary encode
checkRotaryEncoder();
// Fade light to new dim value
fadeStep();
}
void receive(const MyMessage &message)
{
if (message.type == V_STATUS || message.type == V_PERCENTAGE) {
// Retrieve the power or dim level from the incoming request message
fadeTo = atoi( message.data );
// Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
fadeTo *= ( message.type == V_LIGHT ? 100 : 1 );
// Clip incoming level to valid range of 0 to 100
fadeTo = fadeTo > 100 ? 100 : fadeTo;
fadeTo = fadeTo < 0 ? 0 : fadeTo;
//startFade();
Serial.print("New light level received: ");
Serial.println(fadeTo);
if (!changedByKnob)
knob.write(fadeTo << 1); //### need to multiply by two (using Shift left)
// Cancel send if user turns knob while message comes in
changedByKnob = false;
sendDimValue = false;
// Stard fading to new light level
startFade();
// // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
// send(lightMsg.set(dimValue > 0));
//
// // hek comment: Is this really nessesary?
// send( dimmerMsg.set(dimValue) );
}
}
void checkRotaryEncoder() {
long encoderValue = knob.read() >> 1 ; //### Divide by 2 (using shift right)
if (encoderValue > 100) {
encoderValue = 100;
knob.write(200); //### max value now 200 due to divide by 2
} else if (encoderValue < 0) {
encoderValue = 0;
knob.write(0);
}
if (encoderValue != fadeTo) {
fadeTo = encoderValue;
changedByKnob = true;
startFade();
}
}
void startFade() {
fadeDelta = ( fadeTo - dimValue ) < 0 ? -1 : 1;
lastFadeStep = millis();
}
// This method provides a graceful none-blocking fade up/down effect
void fadeStep() {
unsigned long currentTime = millis();
if ( dimValue != fadeTo && currentTime > lastFadeStep + FADE_DELAY) {
dimValue += fadeDelta;
analogWrite( LED_PIN, (int)(dimValue / 100. * 255 ) );
lastFadeStep = currentTime;
Serial.print("Fading level: ");
Serial.println(dimValue);
if (fadeTo == dimValue && changedByKnob) {
sendDimValue = true;
sendDimTimeout = currentTime;
}
}
// Wait a few millisecs before sending in new value (if user still turns the knob)
if (sendDimValue && currentTime > sendDimTimeout + SEND_THROTTLE_DELAY) {
// We're done fading.. send in new dim-value to controller.
// Send in new dim value with ack (will be picked up in incomingMessage)
send(dimmerMsg.set(dimValue)); // Send new dimmer value and request ack back
sendDimValue = false;
}
}```