Updated Sensor API (markdown)

Michael Ingraham 2019-02-23 15:21:31 -05:00
parent c5786d1751
commit ed1a335bca
1 changed files with 16 additions and 16 deletions

@ -49,7 +49,7 @@ boolean Xsns<driverID>(byte callback_id) {
// Set return value to `false`
boolean result = false;
// Check if I<sup>2</sup>C interface mode
// Check if I2C interface mode
// if(i2c_flg) {
// Check which callback ID is called by Tasmota
@ -121,14 +121,14 @@ Unless your driver is specifically going to use the entire array of addresses pr
**I<sup>2</sup>C address auto-detection example**
```
#define MPR121_I<sup>2</sup>C_ADDR_1ST 0x5A /** 1st I<sup>2</sup>C address of sensor model **/
#define MPR121_I<sup>2</sup>C_ADDR_NUM 4 /** Number of sensors/I<sup>2</sup>C addresses **/
#define MPR121_I<sup>2</sup>C_ID_REG 0x5D /** Sensor model specific ID register **/
#define MPR121_I<sup>2</sup>C_ID_VAL 0x24 /** Sensor model specific ID register value **/
#define MPR121_I2C_ADDR_1ST 0x5A /** 1st I2C address of sensor model **/
#define MPR121_I2C_ADDR_NUM 4 /** Number of sensors/I2C addresses **/
#define MPR121_I2C_ID_REG 0x5D /** Sensor model specific ID register **/
#define MPR121_I2C_ID_VAL 0x24 /** Sensor model specific ID register value **/
/* Sensor data struct type declaration/default definition */
typedef struct {
bool connected = false; /** Status if sensor is connected at I<sup>2</sup>C address */
bool connected = false; /** Status if sensor is connected at I2C address */
bool running = false; /** Running state of sensor */
.
.
@ -136,20 +136,20 @@ typedef struct {
} mpr121;
// Declare array of sensor data structs
mpr121 mpr121[MPR121_I<sup>2</sup>C_ADDR_NUM];
mpr121 mpr121[MPR121_I2C_ADDR_NUM];
// Sensor specific init function
void mpr121_init() {
// Loop through I<sup>2</sup>C addresses
for (uint8_t i = 0; i < MPR121_I<sup>2</sup>C_ADDR_NUM); i++) {
// Loop through I2C addresses
for (uint8_t i = 0; i < MPR121_I2C_ADDR_NUM); i++) {
// Check if sensor is connected on I<sup>2</sup>C address
mpr121[i].connected = (MPR121_I<sup>2</sup>C_ID_VAL == I2cRead8(MPR121_I<sup>2</sup>C_ADDR_1ST + i, MPR121_I<sup>2</sup>C_ID_REG);
// Check if sensor is connected on I2C address
mpr121[i].connected = (MPR121_I2C_ID_VAL == I2cRead8(MPR121_I2C_ADDR_1ST + i, MPR121_I2C_ID_REG);
if(mpr121[i].connected) {
// Log sensor found
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_I<sup>2</sup>C "MPR121-%d " D_FOUND_AT " 0x%X"), i, MPR121_I<sup>2</sup>C_ADDR_1ST + i);
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_I2C "MPR121-%d " D_FOUND_AT " 0x%X"), i, MPR121_I2C_ADDR_1ST + i);
AddLog(LOG_LEVEL_INFO);
// Initialize sensor
@ -162,7 +162,7 @@ void mpr121_init() {
}
}
if(!(mpr121[0].connected || mpr121[1].connected || mpr121[2].connected || mpr121[3].connected)){
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_I<sup>2</sup>C "MPR121: No sensors found"));
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_I2C "MPR121: No sensors found"));
AddLog(LOG_LEVEL_INFO);
}
}
@ -172,13 +172,13 @@ void mpr121_init() {
* If a sensor needs an action which takes a long time, like more than 100mS, the action will be started here for a future follow-up. Using the uptime variable for testing like (uptime &1) will happen every 2 seconds. An example is the DS18B20 driver where readings (conversions they call it) can take up to 800mS from the initial request.
* If a sensor needed the previous action it is now time to gather the information and store it in a safe place to be used by FUNC_JSON_APPEND and/or FUNC_WEB_APPEND. Using the else function of the previous test (uptime &1) will happen every 2 seconds too but just 1 second later than the previous action.
* If a sensor does not respond for 10 times the sensor detection flag could be reset which will stop further processing until the sensor is re-detected. This is currently not being used actively as some users complain about disappearing sensors for whatever reason - Could be hardware related but easier to make Tasmota a little more flexible.
* Making re-dection of a sensor possible by executing this once every 100 seconds (94 == (uptime %100)) a re-attached sensor can be detected without a restart of Tasmota. The 94 given in this example should be different for every sensor driver to make sure not all sensors start detection at the same time. Using the drivers index number should be a good starting point.
* Making re-detection of a sensor possible by executing this once every 100 seconds (94 == (uptime %100)) a re-attached sensor can be detected without a restart of Tasmota. The 94 given in this example should be different for every sensor driver to make sure not all sensors start detection at the same time. Using the drivers index number should be a good starting point.
### FUNC_PREP_BEFORE_TELEPERIOD
NOTE: This callback ID is deprecated as sensors should prepare for more regular updates due to "realtime" rule execution. Use FUNC_EVERY_SECOND instead. See examples used in xsns_05_ds18x20.ino and xsns_09_bmp.ino where updated sensor data is stored in preparation to calls to FUNC_JSON_APPEND and FUNC_WEB_APPEND.
### FUNC_JSON_APPEND
This callback ID is called when `TELEPERIOD` is due to append telemetry data to the MQTT JSON string or at approximately every 2 seconds when a rule is checked, e.g.
This callback ID is called when [`TelePeriod`](Commands#teleperiod) is due to append telemetry data to the MQTT JSON string or at approximately every 2 seconds when a rule is checked, e.g.
```
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"MPR121%c\":{\"Button%i\":%i}}"), pS->id[i], j, BITC(i,j));
```
@ -251,7 +251,7 @@ ___
#### void AddLog(byte loglevel)
This function adds log messages stored in ``log_data`` to the local logging system, e.g.
```
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_I<sup>2</sup>C "MPR121(%c) " D_FOUND_AT " 0x%X"), pS->id[i], pS->i2c_addr[i]);
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_I2C "MPR121(%c) " D_FOUND_AT " 0x%X"), pS->id[i], pS->i2c_addr[i]);
AddLog(LOG_LEVEL_INFO);
```
#### void AddLogSerial(byte loglevel)