@Yveaux I solved the problem.
The issue was not my sketch, but rather my ESP8266 MQTT Gateway. I was using an older development version. After upgrading to version 2.0 everything started working. Thankfully my older nodes, using the older development version, are still working, I will upgrade them over time.
This is my final sketch:
#define MY_DEBUG
#define MY_RADIO_NRF24
#define MY_NODE_ID 300
#define SN "ACDimmer"
#define SV "1.0"
#define AC1_ID 1
#define FADE_DELAY 18 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
#include <TimerOne.h>
#include <SPI.h>
#include <MySensors.h>
MyMessage dimmerMsg(AC1_ID, V_DIMMER);
volatile int i=0; // Variable to use as a counter volatile as it is in an interrupt
volatile boolean zero_cross=0; // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = 4; // Output to Opto Triac
int intPin = 3;
int dim = 0; // Dimming level (0-128) 0 = on, 128 = 0ff
int inc=1; // counting up or down, 1=up, -1=down
int freqStep = 65; // This is the delay-per-brightness step in microseconds.
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long currentMillis = 0;
int currentLevel = 0;
int requestedLevel = 0;
void before() {
#ifdef MY_DEBUG
Serial.println("Dimmer Node Starting");
#endif
pinMode(AC_pin, OUTPUT); // Set the Triac pin as output
attachInterrupt(digitalPinToInterrupt(intPin), zero_cross_detect, RISING); // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
Timer1.initialize(freqStep); // Initialize TimerOne library for the freq we need
Timer1.attachInterrupt(dim_check, freqStep);
}
void setup() { // Begin setup
}
void presentation() {
present( AC1_ID, S_DIMMER );
sendSketchInfo(SN, SV);
}
void zero_cross_detect() {
zero_cross = true; // set the boolean to true to tell our dimming function that a zero cross has occured
i=0;
digitalWrite(AC_pin, LOW); // turn off TRIAC (and AC)
}
// Turn on the TRIAC at the appropriate time
void dim_check() {
if(zero_cross == true) {
if(i>=currentLevel) {
digitalWrite(AC_pin, HIGH); // turn on light
i=0; // reset time step counter
zero_cross = false; //reset zero cross detection
}
else {
i++; // increment time step counter
}
}
}
void loop() {
}
void receive(const MyMessage &message) {
#ifdef MY_DEBUG
Serial.print("Message: ");
Serial.println(message.type);
Serial.print("Message Data: ");
Serial.println(message.data);
#endif
if (message.type == 3) {
int requestedLevel = map(atoi( message.data ), 0, 100, 128, 0); //128 = off | 0 = ON
while (currentLevel != requestedLevel) {
if(currentLevel<=requestedLevel) inc=1;
if(currentLevel>=requestedLevel) inc=-1;
currentLevel+=inc;
#ifdef MY_DEBUG
Serial.print("Current Level:");
Serial.println(String(currentLevel));
#endif
//delay
currentMillis = previousMillis = millis();
while (currentMillis - previousMillis < FADE_DELAY) {
currentMillis = millis();
}
}
}
}
There is still one error though. When I try to set my own node id using #define MY_NODE_ID 300, I get the following error:
In file included from /home/sketchbook/libraries/MySensors/MySensors.h:253:0,
from /home/sketchbook/ACDimmer_Test/ACDimmer_Test.ino:11:
/home/sketchbook/libraries/MySensors/core/MyTransport.cpp: In function 'void stInitTransition()':
/home/sketchbook/libraries/MySensors/core/MyTransport.cpp:74:16: warning: large integer implicitly truncated to unsigned type [-Woverflow]
_nc.nodeId = MY_NODE_ID;
^
In file included from /home/sketchbook/libraries/MySensors/core/MyHwATMega328.cpp:22:0,
from /home/sketchbook/libraries/MySensors/MySensors.h:69,
from /home/sketchbook/ACDimmer_Test/ACDimmer_Test.ino:11:
/home/sketchbook/libraries/MySensors/core/MyHwATMega328.h:69:88: warning: large integer implicitly truncated to unsigned type [-Woverflow]
#define hwWriteConfig(__pos, __value) (eeprom_update_byte((uint8_t*)(__pos), (__value)))
^
/home/sketchbook/libraries/MySensors/core/MyTransport.cpp:76:5: note: in expansion of macro 'hwWriteConfig'
hwWriteConfig(EEPROM_NODE_ID_ADDRESS, MY_NODE_ID);
I was able to set my own node id's in the older development version but it does not seem to work now.
Thank you for all your help.
Mike