Skip to content

Commit

Permalink
1PM: do not read calib data
Browse files Browse the repository at this point in the history
  • Loading branch information
markirb committed Jan 11, 2025
1 parent c8bf834 commit b89e5fb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 56 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ Reverting to stock firmware is also possible [see here](https://github.com/mongo

### Plus devices

| |[+1]|[+1Mini]|[+1PMMini]|[+1PM]|[+2PM]|+i4 [AC]/[DC]|[+Plug S|[+Uni]
|- |- |- |- |- |- |- |- |-
|Switch & Co.<sup>1</sup> |✓ |✓ |✓ |✓ |✓ |✗ |✓ |✓
|Stateless Input<sup>2</sup> |✓ |✓ |✓ |✓ |✓ |✓ |✗ |✓
|Sensors<sup>3</sup> |✓ |✓ |✓ |✓ |✓ |✓ |✗ |✓
|Garage door opener |✓ |✓ |✓ |✓ |✓ |✗ |✗ |✓
|Roller shutter mode |✗ |✗ |✗ |✗ |✓ |✗ |✗ |✗
|Power measurement |✗ |✗ |- |✓ |✓ |✗ |✓ |✗
|Temperature/Humidity measurement<sup>4</sup>|✓ |✗ |✗ |✓ |✓ |✓ |✗ |✓
| |[+1]|[+1Mini]|[+1PMMini]|[+1PM]|[+2PM]|+i4 [AC]/[DC]|[+Plug S]|[+Uni]
|- |- |- |- |- |- |- |- |-
|Switch & Co.<sup>1</sup> |✓ |✓ |✓ |✓ |✓ |✗ |✓ |✓
|Stateless Input<sup>2</sup> |✓ |✓ |✓ |✓ |✓ |✓ |✗ |✓
|Sensors<sup>3</sup> |✓ |✓ |✓ |✓ |✓ |✓ |✗ |✓
|Garage door opener |✓ |✓ |✓ |✓ |✓ |✗ |✗ |✓
|Roller shutter mode |✗ |✗ |✗ |✗ |✓ |✗ |✗ |✗
|Power measurement |✗ |✗ |- |✓ |✓ |✗ |✓ |✗
|Temperature/Humidity measurement<sup>4</sup>|✓ |✗ |✗ |✓ |✓ |✓ |✗ |✓

### Light Controllers

Expand Down
6 changes: 3 additions & 3 deletions mos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -797,9 +797,9 @@ conds:
RELAY1_GPIO: 5
SWITCH1_GPIO: 10
ADC_GPIO: 3
ADDON_OUT_GPIO: -1
ADDON_IN_GPIO: -1
ADDON_DIG_GPIO: -1
ADDON_OUT_GPIO: -1 #most likely: 8
ADDON_IN_GPIO: -1 #most likely: 21
ADDON_DIG_GPIO: -1 #most likely: 9
config_schema:
- ["device.id", "Shelly1Gen3-??????"]
- ["shelly.name", "Shelly1Gen3-??????"]
Expand Down
44 changes: 0 additions & 44 deletions src/ShellyPlus1PM/shelly_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,60 +40,16 @@ namespace shelly {
static std::unique_ptr<Onewire> s_onewire;
static std::vector<std::unique_ptr<TempSensor>> sensors;

static constexpr const char *kNVSPartitionName = "shelly";
static constexpr const char *kNVSNamespace = "shelly";
static constexpr const char *kAPowerCoeffNVSKey = "Pm0.apower";

static StatusOr<float> ReadPowerCoeff() {
esp_err_t err = ESP_OK;
auto fh = nvs::open_nvs_handle_from_partition(
kNVSPartitionName, kNVSNamespace, NVS_READONLY, &err);
if (fh == nullptr) {
return mgos::Errorf(STATUS_NOT_FOUND, "No NVS factory data! err %d", err);
}
size_t size = 0;
err = fh->get_item_size(nvs::ItemType::SZ, kAPowerCoeffNVSKey, size);
if (err != ESP_OK) {
return mgos::Errorf(STATUS_NOT_FOUND, "No power calibration data!");
}
char *buf = (char *) calloc(1, size + 1); // NUL at the end.
if (buf == nullptr) {
return mgos::Errorf(STATUS_RESOURCE_EXHAUSTED, "Out of memory");
}
mgos::ScopedCPtr buf_owner(buf);
err = fh->get_string(kAPowerCoeffNVSKey, buf, size);
if (err != ESP_OK) {
return mgos::Errorf(STATUS_RESOURCE_EXHAUSTED, "Failed to read key: %d",
err);
}
float apc = atof(buf);
LOG(LL_DEBUG, ("Factory apower calibration value: %f", apc));
return apc;
}

void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
std::vector<std::unique_ptr<Output>> *outputs,
std::vector<std::unique_ptr<PowerMeter>> *pms,
std::unique_ptr<TempSensor> *sys_temp) {
nvs_flash_init_partition(kNVSPartitionName);
outputs->emplace_back(new OutputPin(1, RELAY1_GPIO, 1));
auto *in = new InputPin(1, SWITCH1_GPIO, 1, MGOS_GPIO_PULL_NONE, true);
in->AddHandler(std::bind(&HandleInputResetSequence, in, LED_GPIO, _1, _2));
in->Init();
inputs->emplace_back(in);

// Read factory calibration data but only if the value is default.
// If locally adjusted, do not override.
if (mgos_sys_config_get_bl0937_0_apower_scale() ==
mgos_sys_config_get_default_bl0937_0_apower_scale()) {
auto apcs = ReadPowerCoeff();
if (apcs.ok()) {
mgos_sys_config_set_bl0937_0_apower_scale(apcs.ValueOrDie());
} else {
auto ss = apcs.status().ToString();
LOG(LL_ERROR, ("Error reading factory calibration data: %s", ss.c_str()));
}
}
#ifndef UART_TX_GPIO
std::unique_ptr<PowerMeter> pm(
new BL0937PowerMeter(1, 5 /* CF */, 18 /* CF1 */, 23 /* SEL */, 2,
Expand Down

0 comments on commit b89e5fb

Please sign in to comment.