@mfalkvidd
I made some more investigation and sortet my code a little bit. The problem still exists.
FHEM sends the next commant to my node when the node sends I_PRE_SLEEP_NOTIFICATION.
But in this situation the smartSleep counter is already counting down.
The node receives the value of 1 but goes to sleep right after that. After Next wakeup the node runs through the loop and stays awake.
In my opinion there should be 2 solutions for that problem
- FHEM sends new values after I_POST_SLEEP_NOTIFICATION
- in mysensors there is a method to cancel smartSleep countdown
// ################### Debugging #####################
#define MY_DEBUG
#define SER_DEBUG
#define MY_RF24_PA_LEVEL RF24_PA_LOW
#define MY_RADIO_RF24
#define MY_RF24_CHANNEL 96
#define MY_TRANSPORT_WAIT_READY_MS (5000ul)
#define MY_TRANSPORT_SANITY_CHECK
#define MY_NODE_ID 110
#define MY_PARENT_NODE_ID 0 //without this passive node broadcasts everything to parent 255 (dont know what happens, if 2 repeater receive this at the same time)
// #define MY_PARENT_NODE_IS_STATIC
// #define MY_PASSIVE_NODE
// ################### Node Spezifisch #####################
#define SKETCH_VER "1.0-002" // Sketch version
#define SKETCH_NAME "LEDSwitch" // Optional child sensor name
#define SLEEP_TIME 13333
#define TOGGLE_BUTTON 3
#include <MySensors.h>
#ifdef SER_DEBUG
#define DEBUG_SERIAL(x) Serial.begin(x)
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#else
#define DEBUG_SERIAL(x)
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#endif
#define LED_PWM_PIN 5
#define CHILD_SINGLE_LED_SWITCH 51
#define CHILD_SINGLE_LED_SWITCH_TEXT (F("Switch"))
#define MAX_LED_LEVEL 1
#define LED_LEVEL_EEPROM 0
#define BLINK_DELAY 180
bool justReceived = false;
uint8_t lastLevel = 1;
uint32_t wakeupTime = 0;
uint32_t waitBeforeNextSleep = 5000;
volatile uint8_t currentLevel = 0; // Current LED level 0 or 1
MyMessage msgSwitchState (CHILD_SINGLE_LED_SWITCH, V_STATUS); //51
void preHwInit()
{
DEBUG_SERIAL(MY_BAUD_RATE);
pinMode(LED_PWM_PIN, OUTPUT); // sets the pin as output
pinMode(TOGGLE_BUTTON, INPUT_PULLUP); // sets the pin as output
DEBUG_PRINTLN("preHwInit done");
}
void before()
{
digitalWrite( LED_PWM_PIN, currentLevel );
}
void setup()
{
DEBUG_PRINTLN("Setup");
}
void presentation()
{
sendSketchInfo(SKETCH_NAME, SKETCH_VER );
present(CHILD_SINGLE_LED_SWITCH, S_BINARY, CHILD_SINGLE_LED_SWITCH_TEXT);
}
void loop()
{
uint32_t currentTime = millis();
if (justReceived)//
{
DEBUG_PRINTLN("justReceived");
justReceived = false;
}
if (lastLevel != currentLevel)
{
lastLevel = currentLevel;
setLED(currentLevel);
}
if ((currentTime - wakeupTime) > waitBeforeNextSleep)
{
DEBUG_PRINTLN("_");
if (currentLevel == 0)
{
DEBUG_PRINTLN("prepare to sleep");
int8_t wakeupReason = sleep(digitalPinToInterrupt(TOGGLE_BUTTON), FALLING , SLEEP_TIME, true);
if (wakeupReason == digitalPinToInterrupt(TOGGLE_BUTTON))
{
currentLevel = 1;
DEBUG_PRINTLN("LED on after wake by ButtonIRQ");
}
wakeupTime = millis();
DEBUG_PRINT("leaving sleep: wakeup reason: ");
DEBUG_PRINTLN(wakeupReason);
}
}
else
{
DEBUG_PRINT(".");
}
}
void receive(const MyMessage &message)
{
DEBUG_PRINT( "receive: " );
DEBUG_PRINTLN( message.getByte() );
if (message.type == V_STATUS)
{
justReceived = true;
uint8_t requestedLevel = message.getByte();
if (requestedLevel == 0)
{
currentLevel=0;
}
else
{
currentLevel=1;
}
}
}
void setLED(uint8_t newLevel)
{
DEBUG_PRINT( "setLED: " );
DEBUG_PRINTLN( newLevel );
if (newLevel == 0)
{
analogWrite(LED_PWM_PIN,0);
}
else
{
analogWrite(LED_PWM_PIN,newLevel<<3);
}
send( msgSwitchState.set(currentLevel) );
}
15:06:44.843 -> setLED: 0
15:06:44.843 -> 3224 TSF:MSG:SEND,110-110-0-0,s=51,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:0
15:06:44.843 -> prepare to sleep
15:06:44.843 -> 3230 MCO:SLP:MS=13333,SMS=1,I1=1,M1=2,I2=255,M2=255
15:06:44.843 -> 3238 TSF:MSG:SEND,110-110-0-0,s=255,c=3,t=32,pt=5,l=4,sg=0,ft=0,st=OK:500
15:06:45.346 -> 3745 TSF:TDI:TSL
15:07:00.209 -> 3746 MCO:SLP:WUP=-1
15:07:00.209 -> 3748 TSF:TRI:TSB
15:07:00.209 -> 3757 TSF:MSG:SEND,110-110-0-0,s=255,c=3,t=33,pt=5,l=4,sg=0,ft=0,st=OK:13333
15:07:00.209 -> leaving sleep: wakeup reason: -1
15:07:00.209 -> prepare to sleep
15:07:00.209 -> 3764 MCO:SLP:MS=13333,SMS=1,I1=1,M1=2,I2=255,M2=255
15:07:00.209 -> 3774 TSF:MSG:SEND,110-110-0-0,s=255,c=3,t=32,pt=5,l=4,sg=0,ft=0,st=OK:500
15:07:00.275 -> 3817 TSF:MSG:READ,0-0-110,s=51,c=1,t=2,pt=0,l=1,sg=0:1
15:07:00.275 -> receive: 1
15:07:00.711 -> 4281 TSF:TDI:TSL
15:07:15.543 -> 4282 MCO:SLP:WUP=-1
15:07:15.543 -> 4284 TSF:TRI:TSB
15:07:15.577 -> 4292 TSF:MSG:SEND,110-110-0-0,s=255,c=3,t=33,pt=5,l=4,sg=0,ft=0,st=OK:13333
15:07:15.577 -> leaving sleep: wakeup reason: -1
15:07:15.577 -> justReceived
15:07:15.577 -> setLED: 1
15:07:15.577 -> 4301 TSF:MSG:SEND,110-110-0-0,s=51,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1