Browse Source

Option to publish address via MQTT for any sensor

i18n
Xose Pérez 7 years ago
parent
commit
170f0d543c
17 changed files with 92 additions and 17 deletions
  1. +3
    -4
      code/espurna/config/sensors.h
  2. +8
    -8
      code/espurna/sensor.ino
  3. +5
    -0
      code/espurna/sensors/AnalogSensor.h
  4. +1
    -1
      code/espurna/sensors/BMX280Sensor.h
  5. +6
    -3
      code/espurna/sensors/BaseSensor.h
  6. +5
    -0
      code/espurna/sensors/DHTSensor.h
  7. +1
    -1
      code/espurna/sensors/DallasSensor.h
  8. +5
    -0
      code/espurna/sensors/DigitalSensor.h
  9. +7
    -0
      code/espurna/sensors/ECH1560Sensor.h
  10. +8
    -0
      code/espurna/sensors/EmonADS1X15Sensor.h
  11. +5
    -0
      code/espurna/sensors/EmonAnalogSensor.h
  12. +5
    -0
      code/espurna/sensors/EventSensor.h
  13. +7
    -0
      code/espurna/sensors/HLW8012Sensor.h
  14. +7
    -0
      code/espurna/sensors/I2CSensor.h
  15. +7
    -0
      code/espurna/sensors/MHZ19Sensor.h
  16. +7
    -0
      code/espurna/sensors/PMSX003Sensor.h
  17. +5
    -0
      code/espurna/sensors/V9261FSensor.h

+ 3
- 4
code/espurna/config/sensors.h View File

@ -36,6 +36,9 @@
#define HUMIDITY_DRY 2
#define HUMIDITY_WET 3
#define SENSOR_PUBLISH_ADDRESSES 0 // Publish sensor addresses
#define SENSOR_ADDRESS_TOPIC "address" // Topic to publish sensor addresses
//--------------------------------------------------------------------------------
// Sensor ID
// These should remain over time, do not modify them, only add new ones at the end
@ -164,10 +167,6 @@
#define DALLAS_RESOLUTION 9 // Not used atm
#define DALLAS_READ_INTERVAL 2000 // Force sensor read & cache every 2 seconds
#define DALLAS_PUBLISH_ADDRESSES 0 // Publish sensor addresses
#define DALLAS_ADDRESS_TOPIC "address" // Topic to publish sensor addresses
//------------------------------------------------------------------------------
// DHTXX temperature/humidity sensor
// Enable support by passing DHT_SUPPORT=1 build flag


+ 8
- 8
code/espurna/sensor.ino View File

@ -641,15 +641,15 @@ void sensorLoop() {
mqttSend(_magnitudeTopic(magnitude.type).c_str(), buffer);
}
#if DALLAS_SUPPORT && DALLAS_PUBLISH_ADDRESSES
if (magnitude.sensor->getID() == SENSOR_DALLAS_ID) {
if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) {
mqttSend(DALLAS_ADDRESS_TOPIC, magnitude.global, (((DallasSensor *) magnitude.sensor)->getAddress(magnitude.local)).c_str());
} else {
mqttSend(DALLAS_ADDRESS_TOPIC, (((DallasSensor *) magnitude.sensor)->getAddress(magnitude.local)).c_str());
}
#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 // DALLAS_SUPPORT && DALLAS_PUBLISH_ADDRESSES
#endif // SENSOR_PUBLISH_ADDRESSES
#endif // MQTT_SUPPORT


+ 5
- 0
code/espurna/sensors/AnalogSensor.h View File

@ -42,6 +42,11 @@ class AnalogSensor : public BaseSensor {
return description();
};
// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) {
return String("0");
}
// Type for slot # index
unsigned char type(unsigned char index) {
_error = SENSOR_ERROR_OK;


+ 1
- 1
code/espurna/sensors/BMX280Sensor.h View File

@ -150,7 +150,7 @@ class BMX280Sensor : public I2CSensor {
void getConfig(JsonObject& root) {
root["sensor_id"] = _sensor_id;
root["address"] = getAddress();
root["address"] = _address;
};
void setConfig(JsonObject& root) {


+ 6
- 3
code/espurna/sensors/BaseSensor.h View File

@ -46,6 +46,12 @@ class BaseSensor {
// Descriptive name of the sensor
virtual String description() {}
// Address of the sensor (it could be the GPIO or I2C address)
virtual String address(unsigned char index) {}
// Descriptive name of the slot # index
virtual String slot(unsigned char index) {};
// Type for slot # index
virtual unsigned char type(unsigned char index) {}
@ -61,9 +67,6 @@ class BaseSensor {
// Load the configuration manifest
static void manifest(JsonArray& root) {};
// Descriptive name of the slot # index
virtual String slot(unsigned char index) {};
// Sensor ID
unsigned char getID() { return _sensor_id; };


+ 5
- 0
code/espurna/sensors/DHTSensor.h View File

@ -95,6 +95,11 @@ class DHTSensor : public BaseSensor {
return description();
};
// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) {
return String(_gpio);
}
// Type for slot # index
unsigned char type(unsigned char index) {
_error = SENSOR_ERROR_OK;


+ 1
- 1
code/espurna/sensors/DallasSensor.h View File

@ -171,7 +171,7 @@ class DallasSensor : public BaseSensor {
}
// Address of the device
String getAddress(unsigned char index) {
String address(unsigned char index) {
char buffer[20] = {0};
if (index < _count) {
uint8_t * address = _devices[index].address;


+ 5
- 0
code/espurna/sensors/DigitalSensor.h View File

@ -72,6 +72,11 @@ class DigitalSensor : public BaseSensor {
return description();
};
// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) {
return String(_gpio);
}
// Type for slot # index
unsigned char type(unsigned char index) {
_error = SENSOR_ERROR_OK;


+ 7
- 0
code/espurna/sensors/ECH1560Sensor.h View File

@ -87,6 +87,13 @@ class ECH1560Sensor : public BaseSensor {
return description();
};
// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) {
char buffer[6];
snprintf(buffer, sizeof(buffer), "%i:%i", _clk, _miso);
return String(buffer);
}
// Type for slot # index
unsigned char type(unsigned char index) {
_error = SENSOR_ERROR_OK;


+ 8
- 0
code/espurna/sensors/EmonADS1X15Sensor.h View File

@ -203,6 +203,14 @@ class EmonADS1X15Sensor : public EmonSensor {
return String(buffer);
}
// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) {
char buffer[10];
unsigned char channel = getChannel(index % _ports);
snprintf(buffer, sizeof(buffer), "0x%02X:%i", _address, channel);
return String(buffer);
}
// Type for slot # index
unsigned char type(unsigned char index) {
if (index < _count) {


+ 5
- 0
code/espurna/sensors/EmonAnalogSensor.h View File

@ -59,6 +59,11 @@ class EmonAnalogSensor : public EmonSensor {
return String("EMON @ ANALOG @ GPIO0");
}
// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) {
return String("0");
}
// Type for slot # index
unsigned char type(unsigned char index) {
_error = SENSOR_ERROR_OK;


+ 5
- 0
code/espurna/sensors/EventSensor.h View File

@ -86,6 +86,11 @@ class EventSensor : public BaseSensor {
return description();
};
// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) {
return String(_gpio);
}
// Type for slot # index
unsigned char type(unsigned char index) {
_error = SENSOR_ERROR_OK;


+ 7
- 0
code/espurna/sensors/HLW8012Sensor.h View File

@ -167,6 +167,13 @@ class HLW8012Sensor : public BaseSensor {
return description();
};
// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) {
char buffer[10];
snprintf(buffer, sizeof(buffer), "%i:%i:%i", _sel, _cf, _cf1);
return String(buffer);
}
// Type for slot # index
unsigned char type(unsigned char index) {
_error = SENSOR_ERROR_OK;


+ 7
- 0
code/espurna/sensors/I2CSensor.h View File

@ -28,6 +28,13 @@ class I2CSensor : public BaseSensor {
return description();
};
// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) {
char buffer[5];
snprintf(buffer, sizeof(buffer), "0x%02X", _address);
return String(buffer);
}
protected:
// Specific for I2C sensors


+ 7
- 0
code/espurna/sensors/MHZ19Sensor.h View File

@ -94,6 +94,13 @@ class MHZ19Sensor : public BaseSensor {
return description();
};
// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) {
char buffer[6];
snprintf(buffer, sizeof(buffer), "%i:%i", _pin_rx, _pin_tx);
return String(buffer);
}
// Type for slot # index
unsigned char type(unsigned char index) {
_error = SENSOR_ERROR_OK;


+ 7
- 0
code/espurna/sensors/PMSX003Sensor.h View File

@ -97,6 +97,13 @@ class PMSX003Sensor : public BaseSensor {
return String();
}
// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) {
char buffer[6];
snprintf(buffer, sizeof(buffer), "%i:%i", _pin_rx, _pin_tx);
return String(buffer);
}
// Type for slot # index
unsigned char type(unsigned char index) {
if (index < _count) {


+ 5
- 0
code/espurna/sensors/V9261FSensor.h View File

@ -82,6 +82,11 @@ class V9261FSensor : public BaseSensor {
return description();
};
// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) {
return String(_pin_rx);
}
// Loop-like method, call it in your main loop
void tick() {
_read();


Loading…
Cancel
Save