@ -24,8 +24,10 @@ Ticker inching;
// -----------------------------------------------------------------------------
void relayMQTT ( unsigned char id ) {
char buffer [ 10 ] ;
sprintf ( buffer , MQTT_RELAY_TOPIC , id ) ;
if ( id > = _relays . size ( ) ) return ;
String mqttGetter = getSetting ( " mqttGetter " , MQTT_USE_GETTER ) ;
char buffer [ strlen ( MQTT_RELAY_TOPIC ) + mqttGetter . length ( ) + 3 ] ;
sprintf ( buffer , " %s/%d%s " , MQTT_RELAY_TOPIC , id , mqttGetter . c_str ( ) ) ;
mqttSend ( buffer , ( char * ) ( relayStatus ( id ) ? " 1 " : " 0 " ) ) ;
}
@ -54,8 +56,10 @@ void relayWS() {
bool relayStatus ( unsigned char id ) {
# ifdef SONOFF_DUAL
if ( id > = 2 ) return false ;
return ( ( dualRelayStatus & ( 1 < < id ) ) > 0 ) ;
# else
if ( id > = _relays . size ( ) ) return false ;
return ( digitalRead ( _relays [ id ] ) = = HIGH ) ;
# endif
}
@ -87,6 +91,8 @@ void relayInching(unsigned char id) {
bool relayStatus ( unsigned char id , bool status , bool report ) {
if ( id > = _relays . size ( ) ) return false ;
bool changed = false ;
if ( relayStatus ( id ) ! = status ) {
@ -122,13 +128,17 @@ bool relayStatus(unsigned char id, bool status, bool report) {
}
bool relayStatus ( unsigned char id , bool status ) {
return relayStatus ( id , status , true ) ;
}
void relaySync ( unsigned char id ) {
if ( _relays . size ( ) > 1 ) {
recursive = true ;
byte relaySync = getSetting ( " relaySync " , String ( RELAY_SYNC ) ) . toInt ( ) ;
byte relaySync = getSetting ( " relaySync " , RELAY_SYNC ) . toInt ( ) ;
bool status = relayStatus ( id ) ;
// If RELAY_SYNC_SAME all relays should have the same state
@ -182,6 +192,7 @@ void relayRetrieve() {
}
void relayToggle ( unsigned char id ) {
if ( id > = _relays . size ( ) ) return ;
relayStatus ( id , ! relayStatus ( id ) ) ;
}
@ -193,36 +204,41 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
static bool isFirstMessage = true ;
String mqttSetter = getSetting ( " mqttSetter " , MQTT_USE_SETTER ) ;
String mqttGetter = getSetting ( " mqttGetter " , MQTT_USE_GETTER ) ;
bool sameSetGet = mqttGetter . compareTo ( mqttSetter ) = = 0 ;
if ( type = = MQTT_CONNECT_EVENT ) {
relayMQTT ( ) ;
mqttSubscribe ( " /relay/# " ) ;
char buffer [ strlen ( MQTT_RELAY_TOPIC ) + mqttSetter . length ( ) + 3 ] ;
sprintf ( buffer , " %s/+%s " , MQTT_RELAY_TOPIC , mqttSetter . c_str ( ) ) ;
mqttSubscribe ( buffer ) ;
}
if ( type = = MQTT_MESSAGE_EVENT ) {
// Match topic
if ( memcmp ( " /relay/ " , topic , 7 ) ! = 0 ) return ;
String t = String ( topic ) ;
if ( ! t . startsWith ( MQTT_RELAY_TOPIC ) ) return ;
if ( ! t . endsWith ( mqttSetter ) ) return ;
// If relayMode is not SAME avoid responding to a retained message
if ( isFirstMessage ) {
if ( sameSetGet & & isFirstMessage ) {
isFirstMessage = false ;
byte relayMode = getSetting ( " relayMode " , String ( RELAY_MODE ) ) . toInt ( ) ;
byte relayMode = getSetting ( " relayMode " , RELAY_MODE ) . toInt ( ) ;
if ( relayMode ! = RELAY_MODE_SAME ) return ;
}
// Get relay ID
unsigned int relayID = topic [ strlen ( topic ) - 1 ] - ' 0 ' ;
unsigned int relayID = topic [ strlen ( MQTT_RELAY_TOPIC ) + 1 ] - ' 0 ' ;
if ( relayID > = relayCount ( ) ) relayID = 0 ;
// Action to perform
if ( ( char ) payload [ 0 ] = = ' 0 ' ) {
relayStatus ( relayID , false , false ) ;
}
if ( ( char ) payload [ 0 ] = = ' 1 ' ) {
relayStatus ( relayID , true , false ) ;
}
if ( ( char ) payload [ 0 ] = = ' 2 ' ) {
unsigned int value = ( char ) payload [ 0 ] - ' 0 ' ;
if ( value = = 2 ) {
relayToggle ( relayID ) ;
} else {
relayStatus ( relayID , value > 0 , ! sameSetGet ) ;
}
}
@ -255,7 +271,7 @@ void relaySetup() {
# endif
EEPROM . begin ( 4096 ) ;
byte relayMode = getSetting ( " relayMode " , String ( RELAY_MODE ) ) . toInt ( ) ;
byte relayMode = getSetting ( " relayMode " , RELAY_MODE ) . toInt ( ) ;
for ( unsigned int i = 0 ; i < _relays . size ( ) ; i + + ) {
pinMode ( _relays [ i ] , OUTPUT ) ;