Browse Source

implemented #318 static IP support

pull/421/head
lumapu 2 years ago
parent
commit
ca8142e781
  1. 8
      tools/esp8266/ahoywifi.cpp
  2. 15
      tools/esp8266/defines.h
  3. 25
      tools/esp8266/html/setup.html
  4. 22
      tools/esp8266/web.cpp
  5. 9
      tools/esp8266/web.h
  6. 12
      tools/esp8266/webApi.cpp
  7. 7
      tools/esp8266/webApi.h

8
tools/esp8266/ahoywifi.cpp

@ -141,6 +141,14 @@ bool ahoywifi::setupStation(uint32_t timeout) {
}
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);
if(String(mSysCfg->deviceName) != "")
WiFi.hostname(mSysCfg->deviceName);

15
tools/esp8266/defines.h

@ -126,6 +126,17 @@ typedef struct {
#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(1) // set alignment to 1 byte boundary
typedef struct {
@ -160,8 +171,8 @@ typedef struct {
bool serialShowIv;
bool serialDebug;
// static ip placeholder
uint32_t staticIp[4];
// static ip
staticIp_t staticIp;
} config_t;
#pragma pack(pop) // restore original alignment from stack

25
tools/esp8266/html/setup.html

@ -57,6 +57,21 @@
<label for="pwd">Password</label>
<input type="password" class="text" name="pwd" value="{PWD}"/>
</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>
<button type="button" class="s_collapsible">Inverter</button>
@ -92,6 +107,9 @@
<div class="s_content">
<fieldset>
<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>
<input type="text" class="text" name="sunLat"/>
<label for="sunLon">Longitude (decimal)</label>
@ -313,6 +331,12 @@
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) {
for(var i = 0; i < obj.inverter.length; i++)
ivHtml(obj.inverter[i], i);
@ -416,6 +440,7 @@
if(null != root) {
parseMenu(root["menu"]);
parseSys(root["system"]);
parseStaticIp(root["static_ip"]);
parseIv(root["inverter"]);
parseMqtt(root["mqtt"]);
parseNtp(root["ntp"]);

22
tools/esp8266/web.cpp

@ -330,6 +330,28 @@ void web::showSave(AsyncWebServerRequest *request) {
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<> *iv;
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {

9
tools/esp8266/web.h

@ -61,6 +61,15 @@ class web {
void onSerial(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;
AsyncEventSource *mEvts;
bool mProtected;

12
tools/esp8266/webApi.cpp

@ -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) {
obj["name"][0] = "Live";
@ -359,6 +370,7 @@ void webApi::getSetup(JsonObject obj) {
getPinout(obj.createNestedObject(F("pinout")));
getRadio(obj.createNestedObject(F("radio")));
getSerial(obj.createNestedObject(F("serial")));
getStaticIp(obj.createNestedObject(F("static_ip")));
}

7
tools/esp8266/webApi.h

@ -45,6 +45,7 @@ class webApi {
void getPinout(JsonObject obj);
void getRadio(JsonObject obj);
void getSerial(JsonObject obj);
void getStaticIp(JsonObject obj);
void getMenu(JsonObject obj);
void getIndex(JsonObject obj);
@ -62,6 +63,12 @@ class webApi {
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;
app *mApp;

Loading…
Cancel
Save