From dd9215b8e57227d5391b9c4fad9256278ad1d1a4 Mon Sep 17 00:00:00 2001 From: "Deomid \"rojer\" Ryabkov" Date: Mon, 2 Aug 2021 01:18:06 +0100 Subject: [PATCH] WIP Shelly Plug US support --- Makefile | 8 +++-- mos.yml | 31 ++++++++++++++++++ src/ShellyPlugUS1/shelly_init.cpp | 52 +++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 src/ShellyPlugUS1/shelly_init.cpp diff --git a/Makefile b/Makefile index d608037a..a6364e0d 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,7 @@ MAKEFLAGS += --warn-undefined-variables --no-builtin-rules .PHONY: build check-format format release upload \ - Shelly1 Shelly1L Shelly1PM Shelly25 Shelly2 ShellyColorBulb ShellyDuo ShellyI3 ShellyPlug ShellyPlugS ShellyPlus1 ShellyPlus1PM ShellyPlusI4 ShellyRGBW2 ShellyVintage ShellyU ShellyU25 ShellyUDuo ShellyURGBW2 ShellyUNI -.SUFFIXES: + Shelly1 Shelly1L Shelly1PM Shelly25 Shelly2 ShellyColorBulb ShellyDuo ShellyI3 ShellyPlug ShellyPlugS ShellyPlugUS1 ShellyPlus1 ShellyPlus1PM ShellyPlusI4 ShellyRGBW2 ShellyVintage ShellyU ShellyU25 ShellyUDuo ShellyURGBW2 ShellyUNI MOS ?= mos # Build locally by default if Docker is available. @@ -27,7 +26,7 @@ ifneq "$(VERBOSE)$(V)" "00" MOS_BUILD_FLAGS_FINAL += --verbose endif -build: Shelly1 Shelly1L Shelly1PM Shelly25 Shelly2 ShellyColorBulb ShellyDuo ShellyI3 ShellyPlug ShellyPlugS ShellyPlus1 ShellyPlus1PM ShellyPlusI4 ShellyRGBW2 ShellyVintage ShellyU ShellyU25 ShellyURGBW2 ShellyUNI +build: Shelly1 Shelly1L Shelly1PM Shelly25 Shelly2 ShellyColorBulb ShellyDuo ShellyI3 ShellyPlug ShellyPlugS ShellyPlugUS1 ShellyPlus1 ShellyPlus1PM ShellyPlusI4 ShellyRGBW2 ShellyVintage ShellyU ShellyU25 ShellyURGBW2 ShellyUNI release: $(MAKE) build CLEAN=1 RELEASE=1 @@ -64,6 +63,9 @@ ShellyPlug: build-ShellyPlug ShellyPlugS: build-ShellyPlugS @true +ShellyPlugUS1: build-ShellyPlugUS1 + @true + ShellyPlus1: PLATFORM=esp32 ShellyPlus1: build-ShellyPlus1 @true diff --git a/mos.yml b/mos.yml index 4dd29fc4..011c1e82 100644 --- a/mos.yml +++ b/mos.yml @@ -561,6 +561,37 @@ conds: - ["bl0937.power_coeff", "f", 0, {title: "BL0937 counts -> watts conversion coefficient"}] - ["bl0937.power_coeff", 1.64358469] # (16 + 1010 + 1935) / (9.55 + 617 + 1175) + - when: build_vars.MODEL == "ShellyPlugUS" + apply: + name: shelly-plug-u1 + libs: + - location: https://github.com/mongoose-os-libs/mongoose + variant: esp8266-nossl + build_vars: + FLASH_SIZE: 2097152 + FS_SIZE: 262144 + BOOT_CONFIG_ADDR: 0x7000 + MGOS_ROOT_FS_TYPE: SPIFFS + cdefs: + LED_GPIO: 2 # Blue, 0 red. + LED_ON: 0 + BTN_GPIO: 13 + BTN_DOWN: 0 + PRODUCT_HW_REV: '"1.0"' + STOCK_FW_MODEL: '"SHPLG-U1"' + MG_ENABLE_SSL: 0 + config_schema: + - ["device.id", "shellyplugu1-??????"] + - ["shelly.name", "shellyplugu1-??????"] + - ["wifi.ap.ssid", "shellyplugu1-??????"] + - ["sw1", "sw", {title: "Plug settings"}] + - ["sw1.name", "Shelly Plug US"] + - ["sw1.in_mode", -1] + - ["sw1.svc_type", 1] # Outlet + - ["sw1.initial_state", 2] + - ["sw1.state_led_en", 1] + - ["bl0937.power_coeff", 1.64358469] # Same as ShellyPlugS + - when: build_vars.MODEL == "ShellyPlus1" apply: name: Plus1 diff --git a/src/ShellyPlugUS1/shelly_init.cpp b/src/ShellyPlugUS1/shelly_init.cpp new file mode 100644 index 00000000..4d98c813 --- /dev/null +++ b/src/ShellyPlugUS1/shelly_init.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) Shelly-HomeKit Contributors + * All rights reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "shelly_main.hpp" +#include "shelly_output.hpp" +#include "shelly_pm_bl0937.hpp" +#include "shelly_temp_sensor_ntc.hpp" + +namespace shelly { + +static OutputPin *s_led_out = nullptr; + +void CreatePeripherals(std::vector> *inputs, + std::vector> *outputs, + std::vector> *pms, + std::unique_ptr *sys_temp) { + outputs->emplace_back(new OutputPin(1, 15, 1)); + s_led_out = new OutputPin(99, 0, 0); // Red LED. + std::unique_ptr pm( + new BL0937PowerMeter(1, 5 /* CF */, 14 /* CF1 */, 12 /* SEL */, 2)); + const Status &st = pm->Init(); + if (st.ok()) { + pms->emplace_back(std::move(pm)); + } else { + const std::string &s = st.ToString(); + LOG(LL_ERROR, ("PM init failed: %s", s.c_str())); + } + sys_temp->reset(new TempSensorSDNT1608X103F3950(0, 3.3f, 33000.0f)); +} + +void CreateComponents(std::vector> *comps, + std::vector> *accs, + HAPAccessoryServerRef *svr) { + CreateHAPSwitch(1, mgos_sys_config_get_sw1(), nullptr, comps, accs, svr, + true /* to_pri_acc */, s_led_out); +} + +} // namespace shelly