Skip to content

Commit

Permalink
Merge pull request #736 from MartinMueller2003/main
Browse files Browse the repository at this point in the history
Made AP WiFi channel configurable.
  • Loading branch information
forkineye authored Feb 5, 2024
2 parents 7e28201 + 09ccb49 commit a7f23da
Show file tree
Hide file tree
Showing 12 changed files with 4,689 additions and 3,093 deletions.
3 changes: 2 additions & 1 deletion ESPixelStick/src/ConstNames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const CN_PROGMEM char CN_activevalue [] = "activevalue";
const CN_PROGMEM char CN_addr [] = "addr";
const CN_PROGMEM char CN_advancedView [] = "advancedView";
const CN_PROGMEM char CN_allleds [] = "allleds";
const CN_PROGMEM char CN_APA102 [] = "APA102";
const CN_PROGMEM char CN_ap_channel [] = "ap_channel";
const CN_PROGMEM char CN_ap_fallback [] = "ap_fallback";
const CN_PROGMEM char CN_ap_timeout [] = "ap_timeout";
const CN_PROGMEM char CN_ap_reboot [] = "ap_reboot";
Expand Down Expand Up @@ -195,6 +195,7 @@ const CN_PROGMEM char CN_TimeRemaining [] = "TimeRemaining";
const CN_PROGMEM char CN_time_remaining [] = "time_remaining";
const CN_PROGMEM char CN_TLS3001 [] = "TLS3001";
const CN_PROGMEM char CN_TM1814 [] = "TM1814";
const CN_PROGMEM char CN_tsensortopic [] = "tsensortopic";
const CN_PROGMEM char CN_topic [] = "topic";
const CN_PROGMEM char CN_topicset [] = "topicset";
const CN_PROGMEM char CN_transitions [] = "transitions";
Expand Down
3 changes: 2 additions & 1 deletion ESPixelStick/src/ConstNames.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extern const CN_PROGMEM char CN_ActiveLow [];
extern const CN_PROGMEM char CN_addr [];
extern const CN_PROGMEM char CN_advancedView [];
extern const CN_PROGMEM char CN_allleds [];
extern const CN_PROGMEM char CN_APA102 [];
extern const CN_PROGMEM char CN_ap_channel [];
extern const CN_PROGMEM char CN_ap_fallback [];
extern const CN_PROGMEM char CN_ap_timeout [];
extern const CN_PROGMEM char CN_ap_reboot [];
Expand Down Expand Up @@ -204,6 +204,7 @@ extern const CN_PROGMEM char CN_TimeRemaining [];
extern const CN_PROGMEM char CN_time_remaining [];
extern const CN_PROGMEM char CN_TLS3001 [];
extern const CN_PROGMEM char CN_TM1814 [];
extern const CN_PROGMEM char CN_tsensortopic [];
extern const CN_PROGMEM char CN_topic [];
extern const CN_PROGMEM char CN_topicset [];
extern const CN_PROGMEM char CN_transitions [];
Expand Down
69 changes: 38 additions & 31 deletions ESPixelStick/src/input/InputMQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ c_InputMQTT::c_InputMQTT (c_InputMgr::e_InputChannelIds NewInputChannelId,

// Set LWT - Must be set before mqtt connect()
mqtt.setWill (lwtTopic.c_str(), 1, true, LWT_OFFLINE);
#ifdef SUPPORT_SENSOR_DS18B20
TemperatureSensorTopic = "Temperature1";
#endif // def SUPPORT_SENSOR_DS18B20

// DEBUG_END;
} // c_InputE131
Expand All @@ -67,7 +70,7 @@ c_InputMQTT::~c_InputMQTT ()
if (HasBeenInitialized)
{
// DEBUG_V ("");
mqtt.unsubscribe (String(topic + CN_slashset).c_str());
mqtt.unsubscribe (lwtTopic.c_str());
mqtt.disconnect (/*force = */ true);
mqttTicker.detach ();

Expand Down Expand Up @@ -120,14 +123,17 @@ void c_InputMQTT::GetConfig (JsonObject & jsonConfig)
// DEBUG_START;

// Serialize Config
jsonConfig[CN_user] = user;
jsonConfig[CN_password] = password;
jsonConfig[CN_topic] = topic;
jsonConfig[CN_clean] = CleanSessionRequired;
jsonConfig[CN_hadisco] = hadisco;
jsonConfig[CN_haprefix] = haprefix;
jsonConfig[CN_effects] = true;
jsonConfig[CN_play] = true;
jsonConfig[CN_user] = user;
jsonConfig[CN_password] = password;
jsonConfig[CN_topic] = topic;
#ifdef SUPPORT_SENSOR_DS18B20
jsonConfig[CN_tsensortopic] = TemperatureSensorTopic;
#endif // def SUPPORT_SENSOR_DS18B20
jsonConfig[CN_clean] = CleanSessionRequired;
jsonConfig[CN_hadisco] = hadisco;
jsonConfig[CN_haprefix] = haprefix;
jsonConfig[CN_effects] = true;
jsonConfig[CN_play] = true;

// DEBUG_END;

Expand Down Expand Up @@ -190,14 +196,17 @@ bool c_InputMQTT::SetConfig (ArduinoJson::JsonObject & jsonConfig)
// DEBUG_START;

String OldTopic = topic;
setFromJSON (ip, jsonConfig, CN_ip);
setFromJSON (port, jsonConfig, CN_port);
setFromJSON (user, jsonConfig, CN_user);
setFromJSON (password, jsonConfig, CN_password);
setFromJSON (topic, jsonConfig, CN_topic);
setFromJSON (CleanSessionRequired, jsonConfig, CN_clean);
setFromJSON (hadisco, jsonConfig, CN_hadisco);
setFromJSON (haprefix, jsonConfig, CN_haprefix);
setFromJSON (ip, jsonConfig, CN_ip);
setFromJSON (port, jsonConfig, CN_port);
setFromJSON (user, jsonConfig, CN_user);
setFromJSON (password, jsonConfig, CN_password);
setFromJSON (topic, jsonConfig, CN_topic);
#ifdef SUPPORT_SENSOR_DS18B20
setFromJSON (TemperatureSensorTopic, jsonConfig, CN_tsensortopic);
#endif // def SUPPORT_SENSOR_DS18B20
setFromJSON (CleanSessionRequired, jsonConfig, CN_clean);
setFromJSON (hadisco, jsonConfig, CN_hadisco);
setFromJSON (haprefix, jsonConfig, CN_haprefix);

validateConfiguration ();

Expand Down Expand Up @@ -362,7 +371,7 @@ void c_InputMQTT::onMqttDisconnect(AsyncMqttClientDisconnectReason reason)

//-----------------------------------------------------------------------------
void c_InputMQTT::onMqttMessage(
char* RcvTopic,
char* rcvTopic,
char* payload,
AsyncMqttClientMessageProperties properties,
uint32_t len,
Expand All @@ -371,27 +380,23 @@ void c_InputMQTT::onMqttMessage(
{
// DEBUG_START;

// Payload isn't null terminated
char* payloadString = (char*)malloc (len + 1);
memcpy (payloadString, payload, len);
payloadString[len] = 0x00;
String RcvTopic = String(rcvTopic);

do // once
{
// DEBUG_V (String (" topic: ") + String (topic));
// DEBUG_V (String ("RcvTopic: ") + String (RcvTopic));
// DEBUG_V (String (" payload: ") + String (payloadString));
// DEBUG_V (String (" l/i/t: ") + String (len) + " / " + String(index) + " / " + String(total));
// DEBUG_V (String (" len: ") + String (len) + " / " + String(index) + " / " + String(total));

if ((String (RcvTopic) != topic) &&
(String (RcvTopic) != topic + CN_slashset))
if (!RcvTopic.equals(topic) &&
!RcvTopic.equals(lwtTopic))
{
// DEBUG_V ("Not our topic");
return;
break;
}

DynamicJsonDocument rootDoc (1024);
DeserializationError error = deserializeJson (rootDoc, payloadString, len);
DeserializationError error = deserializeJson (rootDoc, payload, len);

// DEBUG_V ("Set new values");
if (error)
Expand Down Expand Up @@ -462,8 +467,6 @@ void c_InputMQTT::onMqttMessage(
// DEBUG_V ("");
} while (false);

free (payloadString);

// DEBUG_END;

} // onMqttMessage
Expand Down Expand Up @@ -692,7 +695,7 @@ void c_InputMQTT::publishHA()
JsonConfig[CN_name] = config.id;
JsonConfig[F ("schema")] = F ("json");
JsonConfig[F ("state_topic")] = topic;
JsonConfig[F ("command_topic")] = topic + CN_slashset;
JsonConfig[F ("command_topic")] = lwtTopic;
JsonConfig[F ("availability_topic")] = lwtTopic;
JsonConfig[F ("rgb")] = CN_true;

Expand Down Expand Up @@ -749,6 +752,10 @@ void c_InputMQTT::publishState()

mqtt.publish(topic.c_str(), 0, true, JsonConfigString.c_str());

#ifdef SUPPORT_SENSOR_DS18B20
mqtt.publish(TemperatureSensorTopic.c_str(), 0, true, String("92.3F").c_str());
#endif // def SUPPORT_SENSOR_DS18B20

// DEBUG_END;

} // publishState
Expand Down
3 changes: 3 additions & 0 deletions ESPixelStick/src/input/InputMQTT.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class c_InputMQTT : public c_InputCommon
String user;
String password;
String topic;
#ifdef SUPPORT_SENSOR_DS18B20
String TemperatureSensorTopic;
#endif // def SUPPORT_SENSOR_DS18B20
bool CleanSessionRequired = false;
String haprefix = "homeassistant";
bool hadisco = true;
Expand Down
17 changes: 16 additions & 1 deletion ESPixelStick/src/network/WiFiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ void c_WiFiDriver::GetConfig (JsonObject& json)
json[CN_StayInApMode] = StayInApMode;
json[CN_dhcp] = UseDhcp;
json[CN_sta_timeout] = sta_timeout;
json[CN_ap_channel] = ap_channelNumber;
json[CN_ap_fallback] = ap_fallbackIsEnabled;
json[CN_ap_timeout] = ap_timeout;
json[CN_ap_reboot] = RebootOnWiFiFailureToConnect;
Expand Down Expand Up @@ -463,6 +464,7 @@ bool c_WiFiDriver::SetConfig (JsonObject & json)
{
// DEBUG_START;

// DEBUG_V(String("ap_channelNumber: ") + String(ap_channelNumber));
bool ConfigChanged = false;

String sIp = ip.toString ();
Expand All @@ -475,6 +477,8 @@ bool c_WiFiDriver::SetConfig (JsonObject & json)
ConfigChanged |= setFromJSON (sNetmask, json, CN_netmask);
ConfigChanged |= setFromJSON (sGateway, json, CN_gateway);
ConfigChanged |= setFromJSON (UseDhcp, json, CN_dhcp);
bool chanChanged = setFromJSON (ap_channelNumber, json, CN_ap_channel);
ConfigChanged |= chanChanged;
ConfigChanged |= setFromJSON (sta_timeout, json, CN_sta_timeout);
ConfigChanged |= setFromJSON (ap_fallbackIsEnabled, json, CN_ap_fallback);
ConfigChanged |= setFromJSON (ap_timeout, json, CN_ap_timeout);
Expand All @@ -485,6 +489,16 @@ bool c_WiFiDriver::SetConfig (JsonObject & json)
// DEBUG_V ("gateway: " + gateway);
// DEBUG_V ("netmask: " + netmask);

// DEBUG_V(String("ap_channelNumber: ") + String(ap_channelNumber));
// String StateName;
// pCurrentFsmState->GetStateName(StateName);
// DEBUG_V(String(" CurrState: ") + StateName);

if(chanChanged & pCurrentFsmState == &fsm_WiFi_state_ConnectedToSta_imp)
{
// DEBUG_V("need to cycle the WiFi to move to a new channel");
WiFi.softAPdisconnect();
}
ip.fromString (sIp);
gateway.fromString (sGateway);
netmask.fromString (sNetmask);
Expand Down Expand Up @@ -792,7 +806,8 @@ void fsm_WiFi_state_ConnectingAsAP::Init ()
String Hostname;
NetworkMgr.GetHostname (Hostname);
String ssid = "ESPixelStick-" + String (Hostname);
WiFi.softAP (ssid.c_str ());
// DEBUG_V(String("ap_channelNumber: ") + String(pWiFiDriver->ap_channelNumber));
WiFi.softAP (ssid.c_str (), NULL, pWiFiDriver->ap_channelNumber);

pWiFiDriver->setIpAddress (WiFi.localIP ());
pWiFiDriver->setIpSubNetMask (WiFi.subnetMask ());
Expand Down
2 changes: 1 addition & 1 deletion ESPixelStick/src/network/WiFiDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class c_WiFiDriver

int ValidateConfig ();


#ifdef ARDUINO_ARCH_ESP8266
WiFiEventHandler wifiConnectHandler; // WiFi connect handler
WiFiEventHandler wifiDisconnectHandler; // WiFi disconnect handler
Expand All @@ -107,6 +106,7 @@ class c_WiFiDriver
IPAddress netmask = IPAddress ((uint32_t)0);
IPAddress gateway = IPAddress ((uint32_t)0);
bool UseDhcp = true;
uint8_t ap_channelNumber = 1;
bool ap_fallbackIsEnabled = true;
uint32_t ap_timeout = AP_TIMEOUT; ///< How long to wait in AP mode with no connection before rebooting
uint32_t sta_timeout = CLIENT_TIMEOUT; ///< Timeout when connection as client (station)
Expand Down
2 changes: 1 addition & 1 deletion ESPixelStick/src/output/OutputAPA102.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class c_OutputAPA102 : public c_OutputPixel
// functions to be provided by the derived class
virtual bool SetConfig (ArduinoJson::JsonObject & jsonConfig); ///< Set a new config in the driver
virtual void GetConfig (ArduinoJson::JsonObject & jsonConfig); ///< Get the current config used by the driver
void GetDriverName (String & sDriverName) { sDriverName = CN_APA102; }
void GetDriverName (String & sDriverName) { sDriverName = "APA102"; }
virtual void GetStatus (ArduinoJson::JsonObject & jsonStatus);
virtual void SetOutputBufferSize (uint32_t NumChannelsAvailable);

Expand Down
2 changes: 2 additions & 0 deletions gulpme.bat
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ IF !InstallGulp! == 1 (
echo Install gulp

call npm install
call npm install --global gulp-cli
call npm install --global
call npm install --save-dev gulp
call npm audit fix
) ELSE (
echo gulp is installed. Proceeding...
Expand Down
9 changes: 8 additions & 1 deletion html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,21 @@
title="When connecting as a client, this is the timeout in seconds.">
</div>
</div>
<div class="form-group" id="fg_staTimeout">
<div class="form-group" id="fg_apTimeout">
<label class="control-label col-sm-2" for="ap_timeout">AP Timeout</label>
<div class="col-sm-2">
<input type="number" class="form-control is-valid" id="ap_timeout" step="1" min="15"
max="240" value="0"
title="When waiting for a client to connect, this is the timeout in seconds.">
</div>
</div>
<div class="form-group" id="fg_apChannel">
<label class="control-label col-sm-2" for="ap_channel">AP WiFi Channel</label>
<div class="col-sm-2">
<input type="number" class="form-control is-valid" id="ap_channel" step="1" min="1" max="15"
title="Set the WiFi channel to be used in AP mode.">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<div class="checkbox"><label><input type="checkbox" id="dhcp"
Expand Down
6 changes: 6 additions & 0 deletions html/mqtt.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
<input type="text" class="form-control" id="haprefix" name="haprefix" maxlength="32" title="Prefix used for Home Assistant Discovery">
</div>
</div>
<div class="form-group" id="temperatureSensor">
<label class="control-label col-sm-2" for="tsensortopic">Temperature Sensor Suffix</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="tsensortopic" name="tsensortopic" maxlength="32" title="Suffix used for reporting Temperature">
</div>
</div>

<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
Expand Down
9 changes: 9 additions & 0 deletions html/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,14 @@ function ProcessInputConfig() {
$('#ecb_gpioid').val(Input_Config.ecb.id);
$('#ecb_polarity').val(Input_Config.ecb.polarity);
$('#ecb_longPress').val(Input_Config.ecb.long);

if ({}.hasOwnProperty.call(Input_Config.channels[1][2], "tsensortopic")) {
$("#temperatureSensor").removeClass("hidden");
}
else {
$("#temperatureSensor").addClass("hidden");
}

} // ProcessInputConfig

function ProcessModeConfigurationData(channelId, ChannelType, JsonConfig) {
Expand Down Expand Up @@ -1358,6 +1366,7 @@ function ExtractNetworkWiFiConfigFromHtmlPage() {
wifi.netmask = $('#network #wifi #netmask').val();
wifi.gateway = $('#network #wifi #gateway').val();
wifi.dhcp = $('#network #wifi #dhcp').prop('checked');
wifi.ap_channel = $('#network #wifi #ap_channel').val();
wifi.ap_fallback = $('#network #wifi #ap_fallback').prop('checked');
wifi.ap_reboot = $('#network #wifi #ap_reboot').prop('checked');
wifi.ap_timeout = $('#network #wifi #ap_timeout').val();
Expand Down
Loading

0 comments on commit a7f23da

Please sign in to comment.