Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temp/Humidity sensor add offset for calibration #1539

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion fs_src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -763,10 +763,14 @@ <h1 id="head">Sensor</h1>
<option id="unit_2" value="2">&percnt;</option>
</select>
</div>
<div class="form-control">
<div class="form-control" id="update_interval_container">
<label for="update_interval">Update Interval:</label>
<input type="number" id="update_interval" min="1" max="1000" class="short"><span>&nbsp; s</span>
</div>
<div class="form-control">
<label for="offset">Offset:</label>
<input type="text" id="offset" class="short">
</div>
<div class="button-container">
<button id="save_btn">
<label><span id="save_spinner"></span>Save</label>
Expand Down
4 changes: 4 additions & 0 deletions fs_src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ function tsSetConfig(c) {
name: name,
unit: parseInt(el(c, "unit").value),
update_interval: parseInt(el(c, "update_interval").value),
offset: Math.round(parseFloat(el(c, "offset").value * 100)),
};
setComponentConfig(c, cfg, el(c, "save_spinner"));
}
Expand Down Expand Up @@ -793,7 +794,10 @@ function updateComponent(cd) {
}
updateInnerText(el(c, "value"), v);
selectIfNotModified(el(c, "unit"), cd.unit);
el("update_interval_container").style.display =
(cd.unit == 2) ? "none" : "block";
setValueIfNotModified(el(c, "update_interval"), cd.update_interval);
setValueIfNotModified(el(c, "offset"), cd.offset / 100.0);
break;
}
case Component_Type.kStatelessSwitch:
Expand Down
1 change: 1 addition & 0 deletions mos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ config_schema:
- ["ts.name", "s", "", {title: "Name of the sensor"}]
- ["ts.unit", "i", 0, {title: "Display unit of the Sensor"}] # 0 - Celsius, 1 - Farenheit
- ["ts.update_interval", "i", 1, {title: "Update Interval of the sensor in seconds, minimum 1s"}]
- ["ts.offset", "i", 0, {title: "Offset value can be used for calibration in unit*100 e.g. centigrade"}]

- ["sw", "o", {title: "Switch settings", abstract: true}]
- ["sw.name", "s", "", {title: "Name of the switch"}]
Expand Down
2 changes: 1 addition & 1 deletion src/shelly_hap_garage_door_opener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ void CreateHAPGDO(int id, Input *in_close, Input *in_open, Output *out_close,
Output *out_open, const struct mgos_config_gdo *gdo_cfg,
std::vector<std::unique_ptr<Component>> *comps,
std::vector<std::unique_ptr<mgos::hap::Accessory>> *accs,
HAPAccessoryServerRef *svr, bool to_pri_acc) {
HAPAccessoryServerRef *svr UNUSED_ARG, bool to_pri_acc) {
struct mgos_config_gdo *gdo2_cfg = (struct mgos_config_gdo *) gdo_cfg;

if (gdo_cfg->sensor_swap) {
Expand Down
19 changes: 11 additions & 8 deletions src/shelly_hap_humidity_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ Status HumiditySensor::SetConfig(const std::string &config_json,
struct mgos_config_ts cfg = *cfg_;
cfg.name = nullptr;
json_scanf(config_json.c_str(), config_json.size(),
"{name: %Q, unit: %d, update_interval: %d", &cfg.name, &cfg.unit,
&cfg.update_interval);
"{name: %Q, unit: %d, update_interval: %d, offset: %d", &cfg.name,
&cfg.unit, &cfg.update_interval, &cfg.offset);

mgos::ScopedCPtr name_owner((void *) cfg.name);
// Validation.
Expand All @@ -81,9 +81,12 @@ Status HumiditySensor::SetConfig(const std::string &config_json,
if (cfg_->unit != cfg.unit) {
cfg_->unit = cfg.unit;
}
if (cfg_->offset != cfg.offset) {
cfg_->offset = cfg.offset;
}
if (cfg_->update_interval != cfg.update_interval) {
cfg_->update_interval = cfg.update_interval;
hum_sensor_->StartUpdating(cfg_->update_interval * 1000);
// update interval is set via temperature sensor on DHT
}
return Status::OK();
}
Expand Down Expand Up @@ -114,15 +117,14 @@ Status HumiditySensor::Init() {
return kHAPError_Busy;
}
float temp = static_cast<float>(tempval.ValueOrDie());
*value = truncf(temp * 10) / 10;
*value = truncf((temp + cfg_->offset / 100) * 10) / 10;
return kHAPError_None;
},

true /* supports_notification */, nullptr /* write_handler */,
kHAPCharacteristicDebugDescription_CurrentRelativeHumidity);
AddChar(current_humidity_characteristic_);

hum_sensor_->StartUpdating(cfg_->update_interval * 1000);
return Status::OK();
}

Expand All @@ -137,11 +139,12 @@ StatusOr<std::string> HumiditySensor::GetInfo() const {
StatusOr<std::string> HumiditySensor::GetInfoJSON() const {
std::string res = mgos::JSONPrintStringf(
"{id: %d, type: %d, name: %Q, unit: %d, "
"update_interval: %d, ",
id(), type(), cfg_->name, 2, cfg_->update_interval);
"update_interval: %d, offset: %d, ",
id(), type(), cfg_->name, 2, cfg_->update_interval, cfg_->offset);
auto tempval = hum_sensor_->GetHumidity();
if (tempval.ok()) {
mgos::JSONAppendStringf(&res, "value: %.1f", tempval.ValueOrDie());
mgos::JSONAppendStringf(&res, "value: %.1f",
tempval.ValueOrDie() + cfg_->offset / 100);
} else {
mgos::JSONAppendStringf(&res, "error: %.1f", tempval.ValueOrDie());
}
Expand Down
17 changes: 11 additions & 6 deletions src/shelly_hap_temperature_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ Status TemperatureSensor::SetConfig(const std::string &config_json,
struct mgos_config_ts cfg = *cfg_;
cfg.name = nullptr;
json_scanf(config_json.c_str(), config_json.size(),
"{name: %Q, unit: %d, update_interval: %d", &cfg.name, &cfg.unit,
&cfg.update_interval);
"{name: %Q, unit: %d, update_interval: %d, offset: %d", &cfg.name,
&cfg.unit, &cfg.update_interval, &cfg.offset);

mgos::ScopedCPtr name_owner((void *) cfg.name);
// Validation.
Expand All @@ -84,6 +84,9 @@ Status TemperatureSensor::SetConfig(const std::string &config_json,
cfg_->update_interval = cfg.update_interval;
temp_sensor_->StartUpdating(cfg_->update_interval * 1000);
}
if (cfg_->offset != cfg.offset) {
cfg_->offset = cfg.offset;
}
return Status::OK();
}

Expand Down Expand Up @@ -113,7 +116,7 @@ Status TemperatureSensor::Init() {
return kHAPError_Busy;
}
float temp = static_cast<float>(tempval.ValueOrDie());
*value = truncf(temp * 10) / 10;
*value = truncf((temp + cfg_->offset / 100) * 10) / 10;
return kHAPError_None;
},

Expand Down Expand Up @@ -149,11 +152,13 @@ StatusOr<std::string> TemperatureSensor::GetInfo() const {
StatusOr<std::string> TemperatureSensor::GetInfoJSON() const {
std::string res = mgos::JSONPrintStringf(
"{id: %d, type: %d, name: %Q, unit: %d, "
"update_interval: %d, ",
id(), type(), cfg_->name, cfg_->unit, cfg_->update_interval);
"update_interval: %d, offset: %d, ",
id(), type(), cfg_->name, cfg_->unit, cfg_->update_interval,
cfg_->offset);
auto tempval = temp_sensor_->GetTemperature();
if (tempval.ok()) {
mgos::JSONAppendStringf(&res, "value: %.1f", tempval.ValueOrDie());
mgos::JSONAppendStringf(&res, "value: %.1f",
tempval.ValueOrDie() + cfg_->offset / 100);
} else {
mgos::JSONAppendStringf(&res, "error: %.1f", tempval.ValueOrDie());
}
Expand Down
33 changes: 18 additions & 15 deletions src/shelly_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,11 @@ void CreateHAPSensors(std::vector<std::unique_ptr<TempSensor>> *sensors,

if (ts->getType() == TS_HUM) { // can only be one shares config, as same
// update interval but no unit settable
i++;
ts_cfg = ts_cfgs[i];
shelly::hap::CreateHAPHumiditySensor(j++, (HumidityTempSensor *) ts,
ts_cfg, comps, accs, svr);
break; // max 1 DHT sensor
}
}
}
Expand Down Expand Up @@ -758,28 +761,28 @@ void InitApp() {

// Initialize accessory server.
HAPAccessoryServerOptions server_options = {
.maxPairings = kHAPPairingStorage_MinElements,
.maxPairings = kHAPPairingStorage_MinElements,
#if HAP_IP
.ip =
{
.transport = &kHAPAccessoryServerTransport_IP,
.ip =
{
.transport = &kHAPAccessoryServerTransport_IP,
#ifndef __clang__
.available = 0,
.available = 0,
#endif
.accessoryServerStorage = &s_ip_storage,
},
.accessoryServerStorage = &s_ip_storage,
},
#endif
#if HAP_BLE
.ble =
{
.transport = nullptr,
.ble =
{
.transport = nullptr,
#ifndef __clang__
.available = 0,
.available = 0,
#endif
.accessoryServerStorage = nullptr,
.preferredAdvertisingInterval = 0,
.preferredNotificationDuration = 0,
},
.accessoryServerStorage = nullptr,
.preferredAdvertisingInterval = 0,
.preferredNotificationDuration = 0,
},
#endif
};
static struct HAPPlatformMFiTokenAuth s_mfi_auth;
Expand Down
2 changes: 2 additions & 0 deletions src/shelly_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Status OutputPin::SetStatePWM(float duty, const char *source) {
}
return Status::OK();
#else
(void) duty;
(void) source;
return Status::UNIMPLEMENTED();
#endif
}
Expand Down