mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-15 01:46:38 +02:00
implemented #318 static IP support
This commit is contained in:
parent
ea991a7723
commit
ca8142e781
7 changed files with 96 additions and 2 deletions
|
@ -141,6 +141,14 @@ bool ahoywifi::setupStation(uint32_t timeout) {
|
||||||
}
|
}
|
||||||
|
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
|
if(mConfig->staticIp.ip[0] != 0) {
|
||||||
|
IPAddress ip(mConfig->staticIp.ip);
|
||||||
|
IPAddress mask(mConfig->staticIp.mask);
|
||||||
|
IPAddress dns(mConfig->staticIp.dns);
|
||||||
|
IPAddress gateway(mConfig->staticIp.gateway);
|
||||||
|
if(!WiFi.config(ip, gateway, mask, dns))
|
||||||
|
DPRINTLN(DBG_ERROR, F("failed to set static IP!"));
|
||||||
|
}
|
||||||
WiFi.begin(mSysCfg->stationSsid, mSysCfg->stationPwd);
|
WiFi.begin(mSysCfg->stationSsid, mSysCfg->stationPwd);
|
||||||
if(String(mSysCfg->deviceName) != "")
|
if(String(mSysCfg->deviceName) != "")
|
||||||
WiFi.hostname(mSysCfg->deviceName);
|
WiFi.hostname(mSysCfg->deviceName);
|
||||||
|
|
|
@ -126,6 +126,17 @@ typedef struct {
|
||||||
#pragma pack(pop) // restore original alignment from stack
|
#pragma pack(pop) // restore original alignment from stack
|
||||||
|
|
||||||
|
|
||||||
|
#pragma pack(push) // push current alignment to stack
|
||||||
|
#pragma pack(1) // set alignment to 1 byte boundary
|
||||||
|
typedef struct {
|
||||||
|
uint8_t ip[4]; // ip address
|
||||||
|
uint8_t mask[4]; // sub mask
|
||||||
|
uint8_t dns[4]; // dns
|
||||||
|
uint8_t gateway[4]; // standard gateway
|
||||||
|
} staticIp_t;
|
||||||
|
#pragma pack(pop) // restore original alignment from stack
|
||||||
|
|
||||||
|
|
||||||
#pragma pack(push) // push current alignment to stack
|
#pragma pack(push) // push current alignment to stack
|
||||||
#pragma pack(1) // set alignment to 1 byte boundary
|
#pragma pack(1) // set alignment to 1 byte boundary
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -160,8 +171,8 @@ typedef struct {
|
||||||
bool serialShowIv;
|
bool serialShowIv;
|
||||||
bool serialDebug;
|
bool serialDebug;
|
||||||
|
|
||||||
// static ip placeholder
|
// static ip
|
||||||
uint32_t staticIp[4];
|
staticIp_t staticIp;
|
||||||
} config_t;
|
} config_t;
|
||||||
#pragma pack(pop) // restore original alignment from stack
|
#pragma pack(pop) // restore original alignment from stack
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,21 @@
|
||||||
<label for="pwd">Password</label>
|
<label for="pwd">Password</label>
|
||||||
<input type="password" class="text" name="pwd" value="{PWD}"/>
|
<input type="password" class="text" name="pwd" value="{PWD}"/>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
<fieldset>
|
||||||
|
<legend class="des">Static IP (optional)</legend>
|
||||||
|
<p>
|
||||||
|
Leave fields blank for DHCP<br/>
|
||||||
|
The following fields are parsed in this format: 192.168.1.1
|
||||||
|
</p>
|
||||||
|
<label for="ipAddr">IP Address</label>
|
||||||
|
<input type="text" name="ipAddr" class="text" maxlength="15" />
|
||||||
|
<label for="ipMask">Submask</label>
|
||||||
|
<input type="text" name="ipMask" class="text" maxlength="15" />
|
||||||
|
<label for="ipDns">DNS</label>
|
||||||
|
<input type="text" name="ipDns" class="text" maxlength="15" />
|
||||||
|
<label for="ipGateway">Gateway</label>
|
||||||
|
<input type="text" name="ipGateway" class="text" maxlength="15" />
|
||||||
|
</fieldset>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button type="button" class="s_collapsible">Inverter</button>
|
<button type="button" class="s_collapsible">Inverter</button>
|
||||||
|
@ -92,6 +107,9 @@
|
||||||
<div class="s_content">
|
<div class="s_content">
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<legend class="des">Sunrise & Sunset</legend>
|
<legend class="des">Sunrise & Sunset</legend>
|
||||||
|
<p>
|
||||||
|
Latitude and Longitude must be set to be stored! decimal separator: '.' (dot)
|
||||||
|
</p>
|
||||||
<label for="sunLat">Latitude (decimal)</label>
|
<label for="sunLat">Latitude (decimal)</label>
|
||||||
<input type="text" class="text" name="sunLat"/>
|
<input type="text" class="text" name="sunLat"/>
|
||||||
<label for="sunLon">Longitude (decimal)</label>
|
<label for="sunLon">Longitude (decimal)</label>
|
||||||
|
@ -313,6 +331,12 @@
|
||||||
parseVersion(obj);
|
parseVersion(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseStaticIp(obj) {
|
||||||
|
for(var i of [["ipAddr", "ip"], ["ipMask", "mask"], ["ipDns", "dns"], ["ipGateway", "gateway"]])
|
||||||
|
if(null != obj[i[1]])
|
||||||
|
document.getElementsByName(i[0])[0].value = obj[i[1]];
|
||||||
|
}
|
||||||
|
|
||||||
function parseIv(obj) {
|
function parseIv(obj) {
|
||||||
for(var i = 0; i < obj.inverter.length; i++)
|
for(var i = 0; i < obj.inverter.length; i++)
|
||||||
ivHtml(obj.inverter[i], i);
|
ivHtml(obj.inverter[i], i);
|
||||||
|
@ -416,6 +440,7 @@
|
||||||
if(null != root) {
|
if(null != root) {
|
||||||
parseMenu(root["menu"]);
|
parseMenu(root["menu"]);
|
||||||
parseSys(root["system"]);
|
parseSys(root["system"]);
|
||||||
|
parseStaticIp(root["static_ip"]);
|
||||||
parseIv(root["inverter"]);
|
parseIv(root["inverter"]);
|
||||||
parseMqtt(root["mqtt"]);
|
parseMqtt(root["mqtt"]);
|
||||||
parseNtp(root["ntp"]);
|
parseNtp(root["ntp"]);
|
||||||
|
|
|
@ -330,6 +330,28 @@ void web::showSave(AsyncWebServerRequest *request) {
|
||||||
mProtected = (strlen(mConfig->password) > 0);
|
mProtected = (strlen(mConfig->password) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// static ip
|
||||||
|
if(request->arg("ipAddr") != "") {
|
||||||
|
request->arg("ipAddr").toCharArray(buf, SSID_LEN);
|
||||||
|
ip2Arr(mConfig->staticIp.ip, buf);
|
||||||
|
if(request->arg("ipMask") != "") {
|
||||||
|
request->arg("ipMask").toCharArray(buf, SSID_LEN);
|
||||||
|
ip2Arr(mConfig->staticIp.mask, buf);
|
||||||
|
}
|
||||||
|
if(request->arg("ipDns") != "") {
|
||||||
|
request->arg("ipDns").toCharArray(buf, SSID_LEN);
|
||||||
|
ip2Arr(mConfig->staticIp.dns, buf);
|
||||||
|
}
|
||||||
|
if(request->arg("ipGateway") != "") {
|
||||||
|
request->arg("ipGateway").toCharArray(buf, SSID_LEN);
|
||||||
|
ip2Arr(mConfig->staticIp.gateway, buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
memset(&mConfig->staticIp, 0, sizeof(staticIp_t));
|
||||||
|
|
||||||
|
|
||||||
// inverter
|
// inverter
|
||||||
Inverter<> *iv;
|
Inverter<> *iv;
|
||||||
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
|
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
|
||||||
|
|
|
@ -61,6 +61,15 @@ class web {
|
||||||
void onSerial(AsyncWebServerRequest *request);
|
void onSerial(AsyncWebServerRequest *request);
|
||||||
void onSystem(AsyncWebServerRequest *request);
|
void onSystem(AsyncWebServerRequest *request);
|
||||||
|
|
||||||
|
void ip2Arr(uint8_t ip[], char *ipStr) {
|
||||||
|
char *p = strtok(ipStr, ".");
|
||||||
|
uint8_t i = 0;
|
||||||
|
while(NULL != p) {
|
||||||
|
ip[i++] = atoi(p);
|
||||||
|
p = strtok(NULL, ".");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
AsyncWebServer *mWeb;
|
AsyncWebServer *mWeb;
|
||||||
AsyncEventSource *mEvts;
|
AsyncEventSource *mEvts;
|
||||||
bool mProtected;
|
bool mProtected;
|
||||||
|
|
|
@ -284,6 +284,17 @@ void webApi::getSerial(JsonObject obj) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
void webApi::getStaticIp(JsonObject obj) {
|
||||||
|
if(mConfig->staticIp.ip[0] != 0) {
|
||||||
|
obj[F("ip")] = ip2String(mConfig->staticIp.ip);
|
||||||
|
obj[F("mask")] = ip2String(mConfig->staticIp.mask);
|
||||||
|
obj[F("dns")] = ip2String(mConfig->staticIp.dns);
|
||||||
|
obj[F("gateway")] = ip2String(mConfig->staticIp.gateway);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void webApi::getMenu(JsonObject obj) {
|
void webApi::getMenu(JsonObject obj) {
|
||||||
obj["name"][0] = "Live";
|
obj["name"][0] = "Live";
|
||||||
|
@ -359,6 +370,7 @@ void webApi::getSetup(JsonObject obj) {
|
||||||
getPinout(obj.createNestedObject(F("pinout")));
|
getPinout(obj.createNestedObject(F("pinout")));
|
||||||
getRadio(obj.createNestedObject(F("radio")));
|
getRadio(obj.createNestedObject(F("radio")));
|
||||||
getSerial(obj.createNestedObject(F("serial")));
|
getSerial(obj.createNestedObject(F("serial")));
|
||||||
|
getStaticIp(obj.createNestedObject(F("static_ip")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -45,6 +45,7 @@ class webApi {
|
||||||
void getPinout(JsonObject obj);
|
void getPinout(JsonObject obj);
|
||||||
void getRadio(JsonObject obj);
|
void getRadio(JsonObject obj);
|
||||||
void getSerial(JsonObject obj);
|
void getSerial(JsonObject obj);
|
||||||
|
void getStaticIp(JsonObject obj);
|
||||||
|
|
||||||
void getMenu(JsonObject obj);
|
void getMenu(JsonObject obj);
|
||||||
void getIndex(JsonObject obj);
|
void getIndex(JsonObject obj);
|
||||||
|
@ -62,6 +63,12 @@ class webApi {
|
||||||
return (int)(value * 1000 + 0.5) / 1000.0;
|
return (int)(value * 1000 + 0.5) / 1000.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String ip2String(uint8_t ip[]) {
|
||||||
|
char str[16];
|
||||||
|
snprintf(str, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
|
||||||
|
return String(str);
|
||||||
|
}
|
||||||
|
|
||||||
AsyncWebServer *mSrv;
|
AsyncWebServer *mSrv;
|
||||||
app *mApp;
|
app *mApp;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue