[SOLVED] Timers and Interrupts not being triggered
-
@Mike-Cayouette Maybe this helps
void setup() { // Begin setup Serial.begin( 115200 ); Serial.println("Dim Sketch started"); }@TheoL I tried that also but I thought that was not needed if MY_DEBUG was defined. In any case, event with Serial.begin( 115200 ) it still does not work.
I did notice the following 2 errors in the debug output, one after the other.
TSP:CHKUPL:FAIL (hops=255) !TSM:UPL:FAILbut I see my sensor registered on my MQTT server.
Thank you,
Mike
-
@TheoL I tried that also but I thought that was not needed if MY_DEBUG was defined. In any case, event with Serial.begin( 115200 ) it still does not work.
I did notice the following 2 errors in the debug output, one after the other.
TSP:CHKUPL:FAIL (hops=255) !TSM:UPL:FAILbut I see my sensor registered on my MQTT server.
Thank you,
Mike
@Mike-Cayouette I think you won't be getting any serial output from the println's because the node cannot get a handshake with the gateway. But I'm still not familiar with the new debug output.
-
@TheoL I tried that also but I thought that was not needed if MY_DEBUG was defined. In any case, event with Serial.begin( 115200 ) it still does not work.
I did notice the following 2 errors in the debug output, one after the other.
TSP:CHKUPL:FAIL (hops=255) !TSM:UPL:FAILbut I see my sensor registered on my MQTT server.
Thank you,
Mike
-
@Mike-Cayouette try putting the code suggested by @theol in the before() method instead of in setup()
@Yveaux There is still something odd happening. This is now my basic sketch
//#define MY_DEBUG #define MY_RADIO_NRF24 //#define MY_NODE_ID 300 #define SN "ACDimmer" #define SV "1.0" #include <SPI.h> #include <MySensors.h> unsigned long previousMillis = 0; unsigned long currentMillis = 0; int AC1_ID = 1; int SET_DELAY = 300; MyMessage dimmerMsg(AC1_ID, V_DIMMER); void before() { Serial.begin( 115200 ); Serial.println("Dim Sketch started"); } void setup() { // Begin setup } void presentation() { // Register the LED Dimmable Light with the gateway present( AC1_ID, S_DIMMER ); sendSketchInfo(SN, SV); } void loop() { Serial.println("Loop"); if (currentMillis - previousMillis > SET_DELAY) { send( dimmerMsg.set("Message From Dimmer") ); previousMillis = millis(); } currentMillis = millis(); }I get the output that is in the before() method, but what I have in the loop is not output at all. Its as if loop is not running at all.
Mike
-
@Yveaux There is still something odd happening. This is now my basic sketch
//#define MY_DEBUG #define MY_RADIO_NRF24 //#define MY_NODE_ID 300 #define SN "ACDimmer" #define SV "1.0" #include <SPI.h> #include <MySensors.h> unsigned long previousMillis = 0; unsigned long currentMillis = 0; int AC1_ID = 1; int SET_DELAY = 300; MyMessage dimmerMsg(AC1_ID, V_DIMMER); void before() { Serial.begin( 115200 ); Serial.println("Dim Sketch started"); } void setup() { // Begin setup } void presentation() { // Register the LED Dimmable Light with the gateway present( AC1_ID, S_DIMMER ); sendSketchInfo(SN, SV); } void loop() { Serial.println("Loop"); if (currentMillis - previousMillis > SET_DELAY) { send( dimmerMsg.set("Message From Dimmer") ); previousMillis = millis(); } currentMillis = millis(); }I get the output that is in the before() method, but what I have in the loop is not output at all. Its as if loop is not running at all.
Mike
-
@Mike-Cayouette that's because the mysensors stack doesn't start (e.g radio fails to initialize, or parent can not be found)
That is probably also causing setup() not to be called and your timer not being registered!@Yveaux I find it hard to believe that the radio is not initialized. My MQTT server receives the following when I have the node running:
mymqtt-out/44/255/3/0/24I modified the sketch and add the following receive method
void receive(const MyMessage &message) { Serial.print("Message: "); Serial.println(message.type); Serial.print("Message Data: "); Serial.println(message.data); }when I send a payload of 50 to mymqtt-in/44/1/1/0/3, the serial output shows the following:
Dim Sketch started Message: 3 Message Data: 50I would think if the radio is not initialized this would not be possible.
My debug output is below:
Dim Sketch started Starting sensor (RNNNA-, 2.0.0) TSM:INIT TSM:RADIO:OK TSP:ASSIGNID:OK (ID=44) TSM:FPAR TSP:MSG:SEND 44-44-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc: TSP:MSG:READ 0-0-44 s=255,c=3,t=8,pt=1,l=1,sg=0:0 TSP:MSG:FPAR RES (ID=0, dist=0) TSP:MSG:PAR OK (ID=0, dist=1) TSM:FPAR:OK TSM:ID TSM:CHKID:OK (ID=44) TSM:UPL TSP:PING:SEND (dest=0) TSP:MSG:SEND 44-44-0-0 s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=ok:1 TSP:CHKUPL:FAIL (hops=255) !TSM:UPL:FAIL TSM:FPAR TSP:MSG:SEND 44-44-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc: TSP:MSG:READ 0-0-44 s=255,c=3,t=8,pt=1,l=1,sg=0:0 TSP:MSG:FPAR RES (ID=0, dist=0) TSP:MSG:PAR OK (ID=0, dist=1) TSM:FPAR:OK TSM:IDI remember in the past when the radio would not initialize it said radio init failed or something like that.
Thank you,
Mike
-
@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
-
@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
-
@Yveaux I find it hard to believe that the radio is not initialized. My MQTT server receives the following when I have the node running:
mymqtt-out/44/255/3/0/24I modified the sketch and add the following receive method
void receive(const MyMessage &message) { Serial.print("Message: "); Serial.println(message.type); Serial.print("Message Data: "); Serial.println(message.data); }when I send a payload of 50 to mymqtt-in/44/1/1/0/3, the serial output shows the following:
Dim Sketch started Message: 3 Message Data: 50I would think if the radio is not initialized this would not be possible.
My debug output is below:
Dim Sketch started Starting sensor (RNNNA-, 2.0.0) TSM:INIT TSM:RADIO:OK TSP:ASSIGNID:OK (ID=44) TSM:FPAR TSP:MSG:SEND 44-44-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc: TSP:MSG:READ 0-0-44 s=255,c=3,t=8,pt=1,l=1,sg=0:0 TSP:MSG:FPAR RES (ID=0, dist=0) TSP:MSG:PAR OK (ID=0, dist=1) TSM:FPAR:OK TSM:ID TSM:CHKID:OK (ID=44) TSM:UPL TSP:PING:SEND (dest=0) TSP:MSG:SEND 44-44-0-0 s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=ok:1 TSP:CHKUPL:FAIL (hops=255) !TSM:UPL:FAIL TSM:FPAR TSP:MSG:SEND 44-44-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc: TSP:MSG:READ 0-0-44 s=255,c=3,t=8,pt=1,l=1,sg=0:0 TSP:MSG:FPAR RES (ID=0, dist=0) TSP:MSG:PAR OK (ID=0, dist=1) TSM:FPAR:OK TSM:IDI remember in the past when the radio would not initialize it said radio init failed or something like that.
Thank you,
Mike
-