Skip to content

Commit

Permalink
New commands:
Browse files Browse the repository at this point in the history
 - MEASure:VSENSORS?
 - MEASure:VSENSORx:HUMidity?
 - MEASure:VSENSORx:PREssure?
  • Loading branch information
tjko committed Sep 18, 2024
1 parent 0a9e094 commit 9fa215d
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 3 deletions.
41 changes: 41 additions & 0 deletions commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ Fanpico supports following commands:
* [MEASure:SENSORx?](#measuresensorx)
* [MEASure:SENSORx:Read?](#measuresensorxread)
* [MEASure:SENSORx:TEMP?](#measuresensorxtemp)
* [MEASure:VSENSORS?](#measurevsensors)
* [MEASure:VSENSORx?](#measurevsensorx)
* [MEASure:VSENSORx:HUMidity?](#measurevsensorxhumidity)
* [MEASure:VSENSORx:PREssure?](#measurevsensorxpressure)
* [MEASure:VSENSORx:Read?](#measurevsensorxread)
* [MEASure:VSENSORx:TEMP?](#measurevsensorxtemp)
* [Read?](#read)
Expand Down Expand Up @@ -1463,6 +1466,26 @@ MEAS:SENSOR1:TEMP?
25
```


#### MEASure:VSENSORS?
Return all measurements for all virtual sensors.

Format: sensor,temperature_C,humidity_%,pressure_hPa

Example:
```
MEAS:VSENSORS?
vsensor1,"vsensor1",24.4,0,0
vsensor2,"vsensor2",24.9,0,0
vsensor3,"vsensor3",24.8,43,0
vsensor4,"vsensor4",26.5,0,997
vsensor5,"vsensor5",25.1,0,991
vsensor6,"vsensor6",0.0,0,0
vsensor7,"vsensor7",0.0,0,0
vsensor8,"vsensor8",0.0,0,0
```


#### MEASure:VENSORx?
Return current temperature (C) measured by the sensor.

Expand All @@ -1472,6 +1495,24 @@ MEAS:VSENSOR1?
25
```

#### MEASure:VSENSORx:HUMidity?
Return current humidity (%) measured by the sensor.

Example:
```
MEAS:VSENSOR1:HUM?
45
```

#### MEASure:VSENSORx:PREssure?
Return current pressure (hPa) measured by the sensor.

Example:
```
MEAS:VSENSOR1:PRE?
1013
```

#### MEASure:VSENSORx:Read?
Return current temperature (C) measured by the sensor.

Expand Down
59 changes: 59 additions & 0 deletions src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,24 @@ int cmd_read(const char *cmd, const char *args, int query, struct prev_cmd_t *pr
return 0;
}

int cmd_vsensors_read(const char *cmd, const char *args, int query, struct prev_cmd_t *prev_cmd)
{
int i;

if (!query)
return 1;

for (i = 0; i < VSENSOR_COUNT; i++) {
printf("vsensor%d,\"%s\",%.1lf,%.0f,%0.0f\n", i+1,
conf->vsensors[i].name,
st->vtemp[i],
st->vhumidity[i],
st->vpressure[i]);
}

return 0;
}

int cmd_fan_name(const char *cmd, const char *args, int query, struct prev_cmd_t *prev_cmd)
{
int fan;
Expand Down Expand Up @@ -2011,6 +2029,44 @@ int cmd_vsensor_temp(const char *cmd, const char *args, int query, struct prev_c
return 1;
}

int cmd_vsensor_humidity(const char *cmd, const char *args, int query, struct prev_cmd_t *prev_cmd)
{
int sensor;
float d;

if (!query)
return 1;

sensor = get_prev_cmd_index(prev_cmd, 0) - 1;
if (sensor >= 0 && sensor < VSENSOR_COUNT) {
d = st->vhumidity[sensor];
log_msg(LOG_DEBUG, "vsensor%d humidity = %f%%", sensor + 1, d);
printf("%.0f\n", d);
return 0;
}

return 1;
}

int cmd_vsensor_pressure(const char *cmd, const char *args, int query, struct prev_cmd_t *prev_cmd)
{
int sensor;
float d;

if (!query)
return 1;

sensor = get_prev_cmd_index(prev_cmd, 0) - 1;
if (sensor >= 0 && sensor < VSENSOR_COUNT) {
d = st->vpressure[sensor];
log_msg(LOG_DEBUG, "vsensor%d pressure = %fhPa", sensor + 1, d);
printf("%.0f\n", d);
return 0;
}

return 1;
}

int cmd_vsensor_filter(const char *cmd, const char *args, int query, struct prev_cmd_t *prev_cmd)
{
int sensor;
Expand Down Expand Up @@ -3019,6 +3075,8 @@ const struct cmd_t sensor_commands[] = {
};

const struct cmd_t vsensor_commands[] = {
{ "HUMidity", 3, NULL, cmd_vsensor_humidity },
{ "PREssure", 3, NULL, cmd_vsensor_pressure },
{ "Read", 1, NULL, cmd_vsensor_temp },
{ "TEMP", 4, NULL, cmd_vsensor_temp },
{ 0, 0, 0, 0 }
Expand All @@ -3029,6 +3087,7 @@ const struct cmd_t measure_commands[] = {
{ "MBFAN", 5, mbfan_commands, cmd_mbfan_read },
{ "Read", 1, NULL, cmd_read },
{ "SENSOR", 6, sensor_commands, cmd_sensor_temp },
{ "VSENSORS", 8, NULL, cmd_vsensors_read },
{ "VSENSOR", 7, vsensor_commands, cmd_vsensor_temp },
{ 0, 0, 0, 0 }
};
Expand Down
2 changes: 2 additions & 0 deletions src/fanpico.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ static void clear_state(struct fanpico_state *s)
s->vtemp[i] = 0.0;
s->vtemp_prev[i] = 0.0;
s->vtemp_updated[i] = from_us_since_boot(0);
s->vpressure[i] = 0.0;
s->vhumidity[i] = 0.0;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/fanpico.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ struct fanpico_config {
#endif
/* Non-config items */
float vtemp[VSENSOR_MAX_COUNT];
float vhumidity[VSENSOR_MAX_COUNT];
float vpressure[VSENSOR_MAX_COUNT];
absolute_time_t vtemp_updated[VSENSOR_MAX_COUNT];
void *i2c_context[VSENSOR_MAX_COUNT];
};
Expand All @@ -291,6 +293,8 @@ struct fanpico_state {
float temp[SENSOR_MAX_COUNT];
float temp_prev[SENSOR_MAX_COUNT];
float vtemp[VSENSOR_MAX_COUNT];
float vhumidity[VSENSOR_MAX_COUNT];
float vpressure[VSENSOR_MAX_COUNT];
absolute_time_t vtemp_updated[VSENSOR_MAX_COUNT];
float vtemp_prev[VSENSOR_MAX_COUNT];
float onewire_temp[ONEWIRE_MAX_COUNT];
Expand Down
8 changes: 5 additions & 3 deletions src/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,16 +566,18 @@ int i2c_read_temps(struct fanpico_config *config)

res = i2c_get_measurement(v->i2c_type, config->i2c_context[i], &temp, &pressure, &humidity);
if (res == 0) {
if (pressure > 0.0 || humidity > 0.0 ) {
if (pressure > 0.0)
pressure /= 100.0;
if (pressure >= 0.0 || humidity >= 0.0 ) {
log_msg(LOG_DEBUG, "vsensor%d: temp=%0.4fC, pressure=%0.2fhPa, humidity=%0.2f%%",
i + 1, temp, pressure, humidity);
} else {
log_msg(LOG_DEBUG, "vsensor%d: temperature %0.4f C", i + 1, temp);
}
mutex_enter_blocking(config_mutex);
config->vtemp[i] = temp;
if (pressure >= 0.0)
config->vpressure[i] = pressure;
if (humidity >= 0.0)
config->vhumidity[i] = humidity;
config->vtemp_updated[i] = get_absolute_time();
mutex_exit(config_mutex);
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ double get_vsensor(uint8_t i, struct fanpico_config *config,
if (absolute_time_diff_us(config->vtemp_updated[i], state->vtemp_updated[i]) != 0) {
t = config->vtemp[i];
state->vtemp_updated[i] = config->vtemp_updated[i];
state->vpressure[i] = config->vpressure[i];
state->vhumidity[i] = config->vhumidity[i];
}
/* Check if should reset temperature back to default due to lack of updates... */
if (s->timeout > 0 && t != s->default_temp) {
Expand Down

0 comments on commit 9fa215d

Please sign in to comment.