SD WebInterface - wybór LAN/WLAN

edm
Posts: 677
Joined: Mon Jan 15, 2018 6:18 pm
Been thanked: 2 times

Post

lukfud wrote: Wed Dec 13, 2023 7:47 pm Finalna wersja szkicu dla max. 16 ściemniaczy z obsługą MCP, PCA i LAN'u ;)

Code: Select all

#define DEFAULT_DIMMERS_QTY 2
#define WLAN 0
#define LAN 1
#define STATUS_LED_GPIO 2
#define BUTTON_CFG_GPIO 12

#define DEFAULT_HOLD_TIME 400
#define DEFAULT_REPEAT_ON_HOLD 35
#define DEFAULT_MULTICLICK_TIME 350
#define DEFAULT_BUTTON_STEP 5
#define DEFAULT_FADE_EFFECT 500
#define DEFAULT_ITERATION_DELAY 750
#define DEFAULT_MIN_VALUE 0
#define DEFAULT_MAX_VALUE 100

#include <SuplaDevice.h>
#include <supla/clock/clock.h>

#include <supla/network/client.h>
#include <supla/network/esp_wifi.h>
Supla::ESPWifi *wifi = nullptr;
#ifdef ARDUINO_ARCH_ESP32
#include <supla/network/wt32_eth01.h>
Supla::WT32_ETH01 *eth = nullptr;
#include <HTTPUpdateServer.h>
HTTPUpdateServer httpUpdater;
#else
#include <ESP8266HTTPUpdateServer.h>
ESP8266HTTPUpdateServer httpUpdater;
#endif
#include <supla/network/esp_web_server.h>
Supla::EspWebServer suplaServer;

#include <supla/control/button.h>
Supla::Control::Button *button_[16] = {};
#include <supla/control/dimmer_leds.h>
Supla::Control::DimmerLeds *dimmer_[16] = {};
#include <supla/control/action_trigger.h>
Supla::Control::ActionTrigger *at_[16] = {};
#include <supla/control/virtual_relay.h>
Supla::Control::VirtualRelay *webSrvVRelay = nullptr;
Supla::Control::VirtualRelay *vrelay_[16] = {};
#include <supla/control/EXT_PCA9685.h>
Supla::Control::ExtPCA9685 *extpca = nullptr;
#include <supla/control/EXT_MCP23017.h>
Supla::Control::ExtMCP23017 *extmcp = nullptr;

#include <supla/device/status_led.h>
#ifdef ARDUINO_ARCH_ESP8266
Supla::Device::StatusLed statusLed(STATUS_LED_GPIO, true);
#else
Supla::Device::StatusLed statusLed(STATUS_LED_GPIO);
#endif
#include <supla/device/supla_ca_cert.h>

#include <supla/storage/eeprom.h>
Supla::Eeprom eeprom;
#include <supla/storage/littlefs_config.h>
Supla::LittleFsConfig configSupla(2048);

#include <supla/network/html/div.h>
#include <supla/network/html/h3_tag.h>
#include <supla/network/html/device_info.h>
#include <supla/network/html/protocol_parameters.h>
#include <supla/network/html/status_led_parameters.h>
#ifdef ARDUINO_ARCH_ESP8266
#include <supla/network/html/wifi_parameters.h>
#else
#include <supla/network/html/connection_settings.h>
#endif
#include <supla/network/html/custom_parameter.h>
#include <supla/network/html/custom_text_parameter.h>
#include <supla/network/html/select_input_parameter.h>

int8_t selectConnection = 0;
const char DEV_NAME[] = "dev_name";
char devName[30] = {};
const char HT_VALUE[] = "hold_time_value";
int32_t holdTimeValue = DEFAULT_HOLD_TIME;
const char ROH_VALUE[] = "repeat_on_hold_value";
int32_t repeatOnHoldValue = DEFAULT_REPEAT_ON_HOLD;
const char MCT_VALUE[] = "multiclick_time_value";
int32_t multiclickTimeValue = DEFAULT_MULTICLICK_TIME;
const char PWM_FREQ[] = "pwm_frequency";
int32_t pwmFrequency = 223;
const char BUTTON_STEP[] = "button_step";
int32_t btnStep = DEFAULT_BUTTON_STEP;
const char FADE_EFFECT[] = "fade_effect";
int32_t fadeEffect_ = DEFAULT_FADE_EFFECT;
const char ITERATION_DELAY[] = "iteration_delay";
int32_t iterationDelay = DEFAULT_ITERATION_DELAY;
int32_t minValue[16] = {};
int32_t maxValue[16] = {};
const char DIM_QTY[] = "dimmers_quantity";
int32_t dimmersQuantity = DEFAULT_DIMMERS_QTY;

enum default_actions {
  START_WEB_SERVER,
  STOP_WEB_SERVER,
  ENABLE_BUTTON_0,
  ENABLE_BUTTON_1,
  ENABLE_BUTTON_2,
  ENABLE_BUTTON_3,
  ENABLE_BUTTON_4,
  ENABLE_BUTTON_5,
  ENABLE_BUTTON_6,
  ENABLE_BUTTON_7,
  DISABLE_BUTTON_0,
  DISABLE_BUTTON_1,
  DISABLE_BUTTON_2,
  DISABLE_BUTTON_3,
  DISABLE_BUTTON_4,
  DISABLE_BUTTON_5,
  DISABLE_BUTTON_6,
  DISABLE_BUTTON_7
};

class addedActionsClass : public Supla::ActionHandler, public Supla::Element {
 public: addedActionsClass() {}
  void handleAction(int event, int action) {
    if (action == START_WEB_SERVER) {
      SuplaDevice.handleAction(0, Supla::START_LOCAL_WEB_SERVER);
    }
    if (action == STOP_WEB_SERVER) {
      SuplaDevice.handleAction(0, Supla::STOP_LOCAL_WEB_SERVER);
    }
    for (int i = 0; i < dimmersQuantity; i++) {
      if (action == ENABLE_BUTTON_0+i) {
        button_[i]->enableAction(-1, dimmer_[i], -1);
        SUPLA_LOG_INFO("# button[%d] enabled", i);
      }
      if (action == DISABLE_BUTTON_0+i) {
        button_[i]->disableAction(-1, dimmer_[i], -1);
        SUPLA_LOG_INFO("# button[%d] disabled", i);
      }
    }  
  }
};
addedActionsClass *custAct = new addedActionsClass;

void setup() {
  Serial.begin(115200);
  new Supla::Clock;

  Supla::Storage::Init();

#ifdef ARDUINO_ARCH_ESP32
  if (Supla::Storage::ConfigInstance()->getInt8(
                      Supla::Html::ConnectionSettingsTag, &selectConnection)) {
    SUPLA_LOG_DEBUG("# Param[%s]: %d", Supla::Html::ConnectionSettingsTag,
                                                             selectConnection);
  } else {
    SUPLA_LOG_DEBUG("# Param[%s]: is not set",
                                           Supla::Html::ConnectionSettingsTag);
  }
#endif

  if (Supla::Storage::ConfigInstance()->getString(DEV_NAME, devName, 30)) {
    SUPLA_LOG_DEBUG("# Param[%s]: %s", DEV_NAME, devName);
  } else {
    SUPLA_LOG_DEBUG("# Param[%s] is not set", DEV_NAME);
  }

  if (Supla::Storage::ConfigInstance()->getInt32(DIM_QTY, &dimmersQuantity)) {
    if (dimmersQuantity < 1 || dimmersQuantity > 16) {
      dimmersQuantity = DEFAULT_DIMMERS_QTY;
      Supla::Storage::ConfigInstance()->setInt32(DIM_QTY, dimmersQuantity);
    }
    SUPLA_LOG_DEBUG("# Param[%s]: %d", DIM_QTY, dimmersQuantity);
  } else {
    SUPLA_LOG_DEBUG("# Param[%s]: is not set", DIM_QTY);
  }
  
  if (Supla::Storage::ConfigInstance()->getInt32(HT_VALUE, &holdTimeValue)) {
    if (holdTimeValue <= 0 || holdTimeValue > 1000) {
      holdTimeValue = DEFAULT_HOLD_TIME;
      Supla::Storage::ConfigInstance()->setInt32(HT_VALUE, holdTimeValue);
    }
    SUPLA_LOG_DEBUG("# Param[%s]: %d", HT_VALUE, holdTimeValue);
  } else {
    SUPLA_LOG_DEBUG("# Param[%s] is not set", HT_VALUE);
  }

  if (Supla::Storage::ConfigInstance()->getInt32(ROH_VALUE,
                                                         &repeatOnHoldValue)) {
    if (repeatOnHoldValue <= 0 || repeatOnHoldValue > 150) {
      repeatOnHoldValue = DEFAULT_REPEAT_ON_HOLD;
      Supla::Storage::ConfigInstance()->setInt32(ROH_VALUE, repeatOnHoldValue);
    }
    SUPLA_LOG_DEBUG("# Param[%s]: %d", ROH_VALUE, repeatOnHoldValue);
  } else {
    SUPLA_LOG_DEBUG("# Param[%s] is not set", ROH_VALUE);
  }

  if (Supla::Storage::ConfigInstance()->getInt32(MCT_VALUE,
                                                       &multiclickTimeValue)) {
    if (multiclickTimeValue <= 0 || multiclickTimeValue > 1500) {
      multiclickTimeValue = DEFAULT_MULTICLICK_TIME;
      Supla::Storage::ConfigInstance()->setInt32(MCT_VALUE,
                                                          multiclickTimeValue);
    }
    SUPLA_LOG_DEBUG("# Param[%s]: %d", MCT_VALUE, multiclickTimeValue);
  } else {
    SUPLA_LOG_DEBUG("# Param[%s] is not set", MCT_VALUE);
  }

  if (Supla::Storage::ConfigInstance()->getInt32(BUTTON_STEP, &btnStep)) {
    if (btnStep <= 0 || btnStep > 20) {
      btnStep = DEFAULT_BUTTON_STEP;
      Supla::Storage::ConfigInstance()->setInt32(BUTTON_STEP, btnStep);
    }
    SUPLA_LOG_DEBUG("# Param[%s]: %d", BUTTON_STEP, btnStep);
  } else {
    SUPLA_LOG_DEBUG("# Param[%s] is not set", BUTTON_STEP);
  }

  if (Supla::Storage::ConfigInstance()->getInt32(FADE_EFFECT, &fadeEffect_)) {
    if (fadeEffect_ <= 0 || fadeEffect_ > 1000) {
      fadeEffect_ = DEFAULT_FADE_EFFECT;
      Supla::Storage::ConfigInstance()->setInt32(FADE_EFFECT, fadeEffect_);
    }
    SUPLA_LOG_DEBUG("# Param[%s]: %d", FADE_EFFECT, fadeEffect_);
  } else {
    SUPLA_LOG_DEBUG("# Param[%s] is not set", FADE_EFFECT);
  }

  if (Supla::Storage::ConfigInstance()->getInt32(ITERATION_DELAY,
                                                            &iterationDelay)) {
    if (iterationDelay <= 0 || iterationDelay > 5000) {
      iterationDelay = DEFAULT_ITERATION_DELAY;
      Supla::Storage::ConfigInstance()->setInt32(ITERATION_DELAY,
                                                               iterationDelay);
    }
    SUPLA_LOG_DEBUG("# Param[%s]: %d", ITERATION_DELAY, iterationDelay);
  } else {
    SUPLA_LOG_DEBUG("# Param[%s] is not set", ITERATION_DELAY);
  }

  for (int i = 0; i < dimmersQuantity; i++) {
    char minBuffer[10], maxBuffer[10];
    snprintf(minBuffer, sizeof(minBuffer), "min_%d", i);
    snprintf(maxBuffer, sizeof(maxBuffer), "max_%d", i);
    if (Supla::Storage::ConfigInstance()->getInt32(minBuffer, &minValue[i])) {
      if (minValue[i] < 0 || minValue[i] >= 50) {
        minValue[i] = DEFAULT_MIN_VALUE;
        Supla::Storage::ConfigInstance()->setInt32(minBuffer, minValue[i]);
      } 
      SUPLA_LOG_DEBUG("# Param[%s]: %d", minBuffer, minValue[i]);
    } else {
      SUPLA_LOG_DEBUG("# Param[%s] is not set, setting default value: %d",
                                                 minBuffer, DEFAULT_MIN_VALUE);
      minValue[i] = DEFAULT_MIN_VALUE;
      Supla::Storage::ConfigInstance()->setInt32(minBuffer, minValue[i]);
    }
    if (Supla::Storage::ConfigInstance()->getInt32(maxBuffer, &maxValue[i])) {
      if (maxValue[i] <= 50 || maxValue[i] > 100) {
        maxValue[i] = DEFAULT_MAX_VALUE;
        Supla::Storage::ConfigInstance()->setInt32(maxBuffer, maxValue[i]);
      }
      SUPLA_LOG_DEBUG("# Param[%s]: %d", maxBuffer, maxValue[i]);
    } else {
      SUPLA_LOG_DEBUG("# Param[%s] is not set, setting default value: %d",
                                                 maxBuffer, DEFAULT_MAX_VALUE);
      maxValue[i] = DEFAULT_MAX_VALUE;
      Supla::Storage::ConfigInstance()->setInt32(maxBuffer, maxValue[i]);
    }
  }

  if (Supla::Storage::ConfigInstance()->getInt32(PWM_FREQ, &pwmFrequency)) {
    if (pwmFrequency <= 24 || pwmFrequency > 1526) {
      pwmFrequency = 223;
      Supla::Storage::ConfigInstance()->setInt32(PWM_FREQ, pwmFrequency);
    }
    SUPLA_LOG_DEBUG("# Param[%s]: %d", PWM_FREQ, pwmFrequency);
  } else {
    SUPLA_LOG_DEBUG("# Param[%s] is not set", PWM_FREQ);
  }

  if (selectConnection == WLAN) {
    wifi = new Supla::ESPWifi;
  } else {
#ifdef ARDUINO_ARCH_ESP32
    eth = new Supla::WT32_ETH01(1);
#endif
  }
  extmcp = new Supla::Control::ExtMCP23017(0x20);
  extpca = new Supla::Control::ExtPCA9685();
  extpca->setPWMFrequency(pwmFrequency);

  int pcaChannel[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  int mcpChannel[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

  for (int i = 0; i < dimmersQuantity; i++) {
    dimmer_[i] = new Supla::Control::DimmerLeds(extpca, pcaChannel[i]);
    dimmer_[i]->setStep(btnStep);
    dimmer_[i]->setFadeEffectTime(fadeEffect_);
    dimmer_[i]->setMinMaxIterationDelay(iterationDelay);
    at_[i] = new Supla::Control::ActionTrigger();
    button_[i] = new Supla::Control::Button(extmcp, mcpChannel[i], true, true);
    button_[i]->setSwNoiseFilterDelay(100);
    button_[i]->setHoldTime(holdTimeValue);
    button_[i]->repeatOnHoldEvery(repeatOnHoldValue);
    button_[i]->setMulticlickTime(multiclickTimeValue);
    button_[i]->addAction(Supla::ITERATE_DIM_W, dimmer_[i], Supla::ON_HOLD);
    button_[i]->addAction(Supla::TOGGLE, dimmer_[i], Supla::ON_CLICK_1);
    at_[i]->setRelatedChannel(dimmer_[i]);
    at_[i]->attach(button_[i]);
    minValue[i] = map(minValue[i], 0, 100, 0, 1023);
    maxValue[i] = map(maxValue[i], 0, 100, 0, 1023);
    dimmer_[i]->setBrightnessLimits(minValue[i], maxValue[i]);
  }
  button_[0]->addAction(Supla::ENTER_CONFIG_MODE_OR_RESET_TO_FACTORY,
                                         SuplaDevice, Supla::ON_CLICK_3, true);
  webSrvVRelay = new Supla::Control::VirtualRelay(32);
  webSrvVRelay->setDefaultStateOff();
  webSrvVRelay->getChannel()->setDefault(SUPLA_CHANNELFNC_POWERSWITCH);
  webSrvVRelay->addAction(START_WEB_SERVER, custAct, Supla::ON_TURN_ON);
  webSrvVRelay->addAction(STOP_WEB_SERVER, custAct, Supla::ON_TURN_OFF);
  for (int i = 0; i < dimmersQuantity; i++) {
    vrelay_[i] = new Supla::Control::VirtualRelay(32);
    vrelay_[i]->setDefaultStateRestore();
    vrelay_[i]->getChannel()->setDefault(SUPLA_CHANNELFNC_POWERSWITCH);
    vrelay_[i]->addAction(DISABLE_BUTTON_0+i, custAct, Supla::ON_TURN_ON);
    vrelay_[i]->addAction(ENABLE_BUTTON_0+i, custAct, Supla::ON_TURN_OFF);
  }
  auto buttonCfg = new Supla::Control::Button(BUTTON_CFG_GPIO, true, true);
  buttonCfg->configureAsConfigButton(&SuplaDevice);

  new Supla::Html::DeviceInfo(&SuplaDevice);
#ifdef ARDUINO_ARCH_ESP8266
  new Supla::Html::WifiParameters;
#else
  new Supla::Html::ConnectionSettings;
#endif
  new Supla::Html::ProtocolParameters;
  new Supla::Html::CustomTextParameter(DEV_NAME, "Device name", 30);
  new Supla::Html::CustomParameter(DIM_QTY, "Dimmers quantity",
                                                          DEFAULT_DIMMERS_QTY);
  new Supla::Html::StatusLedParameters;
  new Supla::Html::DivEnd();
  new Supla::Html::DivBegin("box");
  new Supla::Html::H3Tag("Parameters Adjustment");
  new Supla::Html::CustomParameter(HT_VALUE, "Hold time (ms)",
                                                            DEFAULT_HOLD_TIME);
  new Supla::Html::CustomParameter(ROH_VALUE, "Repeat on hold (ms)",
                                                       DEFAULT_REPEAT_ON_HOLD);
  new Supla::Html::CustomParameter(MCT_VALUE, "Multiclick time (ms)",
                                                      DEFAULT_MULTICLICK_TIME);
  new Supla::Html::CustomParameter(BUTTON_STEP, "Button step (ms)",
                                                          DEFAULT_BUTTON_STEP);
  new Supla::Html::CustomParameter(FADE_EFFECT, "Fade effect (ms)",
                                                          DEFAULT_FADE_EFFECT);
  new Supla::Html::CustomParameter(ITERATION_DELAY, "Iteration delay (ms)",
                                                      DEFAULT_ITERATION_DELAY);
  for (int i = 0; i < dimmersQuantity; i++) {
    char minBuffer[10], maxBuffer[10], labelBuff1[25], labelBuff2[25];
    snprintf(minBuffer, sizeof(minBuffer), "min_%d", i);
    snprintf(maxBuffer, sizeof(maxBuffer), "max_%d", i);
    snprintf(labelBuff1, sizeof(labelBuff1), "Min.%d brightness (%%)", i+1);
    snprintf(labelBuff2, sizeof(labelBuff2), "Max.%d brightness (%%)", i+1);
    new Supla::Html::CustomParameter(minBuffer, labelBuff1);
    new Supla::Html::CustomParameter(maxBuffer, labelBuff2, 100);
  }
  new Supla::Html::CustomParameter(PWM_FREQ, "PWM frequency (Hz)", 223);

  httpUpdater.setup(suplaServer.getServerPtr(), "/update");
  SuplaDevice.setName(devName);
  SuplaDevice.setSuplaCACert(suplaCACert);
  SuplaDevice.setSupla3rdPartyCACert(supla3rdCACert);
  SuplaDevice.begin();
  SuplaDevice.getSrpcLayer()->client->setDebugLogs(false);
};

void loop() {
  SuplaDevice.iterate();
};
Chcę trochę zmienić ustawienia dimmera. Głównie zależy mi na tym żeby naciskając i przytrzymując przycisk zewnętrzny (zwierny) rozjaśnianie światła i ściemnianie odbywało się wolnej niż mam obecnie. Teraz jest tak, że zwłaszcza przy rozjaśnianiu odbywa się to bardzo szybko i trudno jest uchwycić właściwy moment, pożądaną jasność światła. Przy ściemnianiu jest wolniej, dobrze.
Poprawiło się trochę po zmianie opcji Button step (ms) z 5 na 1, ale nie wiem czy to jedyna możliwość?.
Proszę opisać chociaż po 1 zdaniu do czego służą poniższe opcje. Niektóre niby wiem, ale pamięć zawodzi.

Code: Select all

Hold time (ms) 400
Repeat on hold (ms) 35
Multiclick time (ms) 350
Button step (ms) 1
Fade effect (ms) 500
Iteration delay (ms) 750
Wziąłem udział w Supla Offline Party 2023 :D

Return to “Projekty użytkowników”