@ -114,6 +114,8 @@ void _sensorWebSocketSendData(JsonObject& root) {
for ( unsigned char i = 0 ; i < _magnitudes . size ( ) ; i + + ) {
for ( unsigned char i = 0 ; i < _magnitudes . size ( ) ; i + + ) {
sensor_magnitude_t magnitude = _magnitudes [ i ] ;
sensor_magnitude_t magnitude = _magnitudes [ i ] ;
if ( magnitude . type = = MAGNITUDE_EVENT ) continue ;
unsigned char decimals = _magnitudeDecimals ( magnitude . type ) ;
unsigned char decimals = _magnitudeDecimals ( magnitude . type ) ;
dtostrf ( magnitude . current , 1 - sizeof ( buffer ) , decimals , buffer ) ;
dtostrf ( magnitude . current , 1 - sizeof ( buffer ) , decimals , buffer ) ;
@ -446,7 +448,8 @@ void _sensorLoad() {
{
{
EventSensor * sensor = new EventSensor ( ) ;
EventSensor * sensor = new EventSensor ( ) ;
sensor - > setGPIO ( EVENTS_PIN ) ;
sensor - > setGPIO ( EVENTS_PIN ) ;
sensor - > setMode ( EVENTS_PIN_MODE ) ;
sensor - > setTrigger ( EVENTS_TRIGGER ) ;
sensor - > setPinMode ( EVENTS_PIN_MODE ) ;
sensor - > setDebounceTime ( EVENTS_DEBOUNCE ) ;
sensor - > setDebounceTime ( EVENTS_DEBOUNCE ) ;
sensor - > setInterruptMode ( EVENTS_INTERRUPT_MODE ) ;
sensor - > setInterruptMode ( EVENTS_INTERRUPT_MODE ) ;
_sensors . push_back ( sensor ) ;
_sensors . push_back ( sensor ) ;
@ -583,8 +586,17 @@ void _sensorLoad() {
}
}
void _sensorCallback ( unsigned char i , unsigned char type , const char * payload ) {
DEBUG_MSG_P ( PSTR ( " [SENSOR] Sensor #%u callback, type %u, payload: '%s' \n " ) , i , type , payload ) ;
void _sensorCallback ( unsigned char i , unsigned char type , double value ) {
DEBUG_MSG_P ( PSTR ( " [SENSOR] Sensor #%u callback, type %u, payload: '%s' \n " ) , i , type , String ( value ) . c_str ( ) ) ;
for ( unsigned char k = 0 ; k < _magnitudes . size ( ) ; k + + ) {
if ( ( _sensors [ i ] = = _magnitudes [ k ] . sensor ) & & ( type = = _magnitudes [ k ] . type ) ) {
_sensorReport ( k , value ) ;
return ;
}
}
}
}
void _sensorInit ( ) {
void _sensorInit ( ) {
@ -621,7 +633,7 @@ void _sensorInit() {
new_magnitude . min_change = 0 ;
new_magnitude . min_change = 0 ;
if ( type = = MAGNITUDE_DIGITAL ) {
if ( type = = MAGNITUDE_DIGITAL ) {
new_magnitude . filter = new MaxFilter ( ) ;
new_magnitude . filter = new MaxFilter ( ) ;
} else if ( type = = MAGNITUDE_EVENTS | | type = = MAGNITUDE_GEIGER_CPM | | type = = MAGNITUDE_GEIGER_SIEVERT ) { // For geiger counting moving average filter is the most appropriate if needed at all.
} else if ( type = = MAGNITUDE_COUNT | | type = = MAGNITUDE_GEIGER_CPM | | type = = MAGNITUDE_GEIGER_SIEVERT ) { // For geiger counting moving average filter is the most appropriate if needed at all.
new_magnitude . filter = new MovingAverageFilter ( ) ;
new_magnitude . filter = new MovingAverageFilter ( ) ;
} else {
} else {
new_magnitude . filter = new MedianFilter ( ) ;
new_magnitude . filter = new MedianFilter ( ) ;
@ -636,8 +648,8 @@ void _sensorInit() {
}
}
// Hook callback
// Hook callback
_sensors [ i ] - > onEvent ( [ i ] ( unsigned char type , const char * payload ) {
_sensorCallback ( i , type , payload ) ;
_sensors [ i ] - > onEvent ( [ i ] ( unsigned char type , double value ) {
_sensorCallback ( i , type , value ) ;
} ) ;
} ) ;
// Custom initializations
// Custom initializations
@ -855,6 +867,72 @@ void _sensorConfigure() {
}
}
void _sensorReport ( unsigned char index , double value ) {
sensor_magnitude_t magnitude = _magnitudes [ index ] ;
unsigned char decimals = _magnitudeDecimals ( magnitude . type ) ;
char buffer [ 10 ] ;
dtostrf ( value , 1 - sizeof ( buffer ) , decimals , buffer ) ;
# if BROKER_SUPPORT
brokerPublish ( magnitudeTopic ( magnitude . type ) . c_str ( ) , magnitude . local , buffer ) ;
# endif
# if MQTT_SUPPORT
mqttSend ( magnitudeTopicIndex ( index ) . c_str ( ) , buffer ) ;
# if SENSOR_PUBLISH_ADDRESSES
char topic [ 32 ] ;
snprintf ( topic , sizeof ( topic ) , " %s/%s " , SENSOR_ADDRESS_TOPIC , magnitudeTopic ( magnitude . type ) . c_str ( ) ) ;
if ( SENSOR_USE_INDEX | | ( _counts [ magnitude . type ] > 1 ) ) {
mqttSend ( topic , magnitude . global , magnitude . sensor - > address ( magnitude . local ) . c_str ( ) ) ;
} else {
mqttSend ( topic , magnitude . sensor - > address ( magnitude . local ) . c_str ( ) ) ;
}
# endif // SENSOR_PUBLISH_ADDRESSES
# endif // MQTT_SUPPORT
# if INFLUXDB_SUPPORT
if ( SENSOR_USE_INDEX | | ( _counts [ magnitude . type ] > 1 ) ) {
idbSend ( magnitudeTopic ( magnitude . type ) . c_str ( ) , magnitude . global , buffer ) ;
} else {
idbSend ( magnitudeTopic ( magnitude . type ) . c_str ( ) , buffer ) ;
}
# endif // INFLUXDB_SUPPORT
# if THINGSPEAK_SUPPORT
tspkEnqueueMeasurement ( index , buffer ) ;
# endif
# if DOMOTICZ_SUPPORT
{
char key [ 15 ] ;
snprintf_P ( key , sizeof ( key ) , PSTR ( " dczMagnitude%d " ) , index ) ;
if ( magnitude . type = = MAGNITUDE_HUMIDITY ) {
int status ;
if ( value > 70 ) {
status = HUMIDITY_WET ;
} else if ( value > 45 ) {
status = HUMIDITY_COMFORTABLE ;
} else if ( value > 30 ) {
status = HUMIDITY_NORMAL ;
} else {
status = HUMIDITY_DRY ;
}
char status_buf [ 5 ] ;
itoa ( status , status_buf , 10 ) ;
domoticzSend ( key , buffer , status_buf ) ;
} else {
domoticzSend ( key , 0 , buffer ) ;
}
}
# endif // DOMOTICZ_SUPPORT
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Public
// Public
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
@ -991,7 +1069,6 @@ void sensorLoop() {
double current ;
double current ;
double filtered ;
double filtered ;
char buffer [ 64 ] ;
// Pre-read hook
// Pre-read hook
_sensorPre ( ) ;
_sensorPre ( ) ;
@ -1027,7 +1104,7 @@ void sensorLoop() {
magnitude . filter - > add ( current ) ;
magnitude . filter - > add ( current ) ;
// Special case
// Special case
if ( magnitude . type = = MAGNITUDE_EVENTS ) {
if ( magnitude . type = = MAGNITUDE_COUNT ) {
current = magnitude . filter - > result ( ) ;
current = magnitude . filter - > result ( ) ;
}
}
@ -1037,6 +1114,7 @@ void sensorLoop() {
// Debug
// Debug
# if SENSOR_DEBUG
# if SENSOR_DEBUG
{
{
char buffer [ 64 ] ;
dtostrf ( current , 1 - sizeof ( buffer ) , _magnitudeDecimals ( magnitude . type ) , buffer ) ;
dtostrf ( current , 1 - sizeof ( buffer ) , _magnitudeDecimals ( magnitude . type ) , buffer ) ;
DEBUG_MSG_P ( PSTR ( " [SENSOR] %s - %s: %s%s \n " ) ,
DEBUG_MSG_P ( PSTR ( " [SENSOR] %s - %s: %s%s \n " ) ,
magnitude . sensor - > slot ( magnitude . local ) . c_str ( ) ,
magnitude . sensor - > slot ( magnitude . local ) . c_str ( ) ,
@ -1059,63 +1137,8 @@ void sensorLoop() {
if ( fabs ( filtered - magnitude . reported ) > = magnitude . min_change ) {
if ( fabs ( filtered - magnitude . reported ) > = magnitude . min_change ) {
_magnitudes [ i ] . reported = filtered ;
_magnitudes [ i ] . reported = filtered ;
dtostrf ( filtered , 1 - sizeof ( buffer ) , decimals , buffer ) ;
# if BROKER_SUPPORT
brokerPublish ( magnitudeTopic ( magnitude . type ) . c_str ( ) , magnitude . local , buffer ) ;
# endif
# if MQTT_SUPPORT
mqttSend ( magnitudeTopicIndex ( i ) . c_str ( ) , buffer ) ;
# if SENSOR_PUBLISH_ADDRESSES
char topic [ 32 ] ;
snprintf ( topic , sizeof ( topic ) , " %s/%s " , SENSOR_ADDRESS_TOPIC , magnitudeTopic ( magnitude . type ) . c_str ( ) ) ;
if ( SENSOR_USE_INDEX | | ( _counts [ magnitude . type ] > 1 ) ) {
mqttSend ( topic , magnitude . global , magnitude . sensor - > address ( magnitude . local ) . c_str ( ) ) ;
} else {
mqttSend ( topic , magnitude . sensor - > address ( magnitude . local ) . c_str ( ) ) ;
}
# endif // SENSOR_PUBLISH_ADDRESSES
# endif // MQTT_SUPPORT
# if INFLUXDB_SUPPORT
if ( SENSOR_USE_INDEX | | ( _counts [ magnitude . type ] > 1 ) ) {
idbSend ( magnitudeTopic ( magnitude . type ) . c_str ( ) , magnitude . global , buffer ) ;
} else {
idbSend ( magnitudeTopic ( magnitude . type ) . c_str ( ) , buffer ) ;
}
# endif // INFLUXDB_SUPPORT
# if THINGSPEAK_SUPPORT
tspkEnqueueMeasurement ( i , buffer ) ;
# endif
# if DOMOTICZ_SUPPORT
{
char key [ 15 ] ;
snprintf_P ( key , sizeof ( key ) , PSTR ( " dczMagnitude%d " ) , i ) ;
if ( magnitude . type = = MAGNITUDE_HUMIDITY ) {
int status ;
if ( filtered > 70 ) {
status = HUMIDITY_WET ;
} else if ( filtered > 45 ) {
status = HUMIDITY_COMFORTABLE ;
} else if ( filtered > 30 ) {
status = HUMIDITY_NORMAL ;
} else {
status = HUMIDITY_DRY ;
}
char status_buf [ 5 ] ;
itoa ( status , status_buf , 10 ) ;
domoticzSend ( key , buffer , status_buf ) ;
} else {
domoticzSend ( key , 0 , buffer ) ;
}
}
# endif // DOMOTICZ_SUPPORT
_sensorReport ( i , filtered ) ;
} // if (fabs(filtered - magnitude.reported) >= magnitude.min_change)
} // if (fabs(filtered - magnitude.reported) >= magnitude.min_change)
} // if (report_count == 0)
} // if (report_count == 0)