Skip to content

Commit

Permalink
Version 1.4.3
Browse files Browse the repository at this point in the history
- Bugfix: Static IP option was not available during AP mode
- Bugfix: Domoticz IDX option field displayed wrong
  • Loading branch information
arjenhiemstra committed Jan 3, 2021
1 parent 7007cd9 commit dd1bb16
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 28 deletions.
78 changes: 65 additions & 13 deletions software/NRG_itho_wifi/04_JS_UI.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ function startWebsock(websocketServerLocation){
$('#mqtt_idx, #label-mqtt_idx').hide();
if($radios.is(':checked') === false) {
$radios.filter('[value=' + x.mqtt_domoticz_active + ']').prop('checked', true);
$('#mqtt_idx, #label-mqtt_idx').show();
}
radio("mqtt_domoticz", x.mqtt_domoticz_active);
$('#mqtt_idx').val(x.mqtt_idx);
Expand All @@ -70,6 +69,7 @@ function startWebsock(websocketServerLocation){
}
else if (f.remotes) {
let x = f.remotes;
$('#RemotesTable').empty();
buildHtmlTable('#RemotesTable', x);
}
else if (f.wifiscanresult) {
Expand Down Expand Up @@ -105,6 +105,16 @@ function startWebsock(websocketServerLocation){
i2cstatus = 'unknown status';
}
$('#i2cstat').html(i2cstatus);
if(x.itho_llm > 0) {
$('#itho_llm').removeClass();
$('#itho_llm').addClass("pure-button button-success");
$('#itho_llm').text("On " + x.itho_llm + "s");
}
else {
$('#itho_llm').removeClass();
$('#itho_llm').addClass("pure-button button-secondary");
$('#itho_llm').text("Off");
}
}
else if (f.messagebox) {
let x = f.messagebox;
Expand All @@ -124,6 +134,9 @@ function startWebsock(websocketServerLocation){
console.log('websock open');
document.getElementById("layout").style.opacity = 1;
document.getElementById("loader").style.display = "none";
if (lastPageReq !== "") {
update_page(lastPageReq);
}
};
websock.onclose = function(a) {
console.log('websock close');
Expand Down Expand Up @@ -212,7 +225,28 @@ $(document).ready(function() {
}
}));
update_page('itho');
}
}
else if ($(this).attr('id') == 'itho_llm') {
websock.send('{\"itho_llm\":true}');
}
else if ($(this).attr('id') == 'itho_remove_remote') {
var selected = $('input[name=\'optionsRemotes\']:checked').val();
if (selected == null) {
alert("Please select a remote.")
}
else {
websock.send('{\"itho_remove_remote\":' + selected + '}');
}
}
else if ($(this).attr('id') == 'itho_update_remote') {
var selected = $('input[name=\'optionsRemotes\']:checked').val();
if (selected == null) {
alert("Please select a remote.");
}
else {
websock.send('{\"itho_update_remote\":' + selected + ', \"value\":\"' + $('#name_remote-' + selected).val() + '\"}');
}
}
else if ($(this).attr('id') == 'resetwificonf') {
if (confirm("This will reset the wifi config to factory default, are you sure?")) {
websock.send('{\"resetwificonf\":true}');
Expand Down Expand Up @@ -325,11 +359,7 @@ var mqtt_cmd_topic_tmp = "";
function radio(origin, state) {
if (origin == "dhcp") {
if (on_ap == true) {
$('#renew, #ip, #subnet, #gateway, #dns1, #dns2, #port').prop('readonly', true);
$('#option-dhcp-on, #option-dhcp-off').prop('disabled', true);
}
else if (state == 'on') {
if (state == 'on') {
$('#ip, #subnet, #gateway, #dns1, #dns2').prop('readonly', true);
$('#renew, #port').prop('readonly', false);
$('#option-dhcp-on, #option-dhcp-off').prop('disabled', false);
Expand Down Expand Up @@ -370,7 +400,15 @@ function radio(origin, state) {
}
else {
$('#RemotesTable').hide();
}
}
}
else if (origin == "remote") {
$('[id^=name_remote-]').each(function(index, item){
$(item).prop('readonly', true);
if (index == state) {
$(item).prop('readonly', false);
}
});
}
}
Expand Down Expand Up @@ -431,7 +469,9 @@ function updateSlider(value) {
}
//function to load html main content
var lastPageReq = "";
function update_page(page) {
lastPageReq = page;
$('#main').empty();
if (page == 'index') { $('#main').append(html_index); }
Expand Down Expand Up @@ -475,7 +515,6 @@ window.onload = function(){
toggleClass(menuLink, active);
}
menuLink.onclick = function(e) {
console.log(e + " menu link clicked, link: " + $(this).attr('href'));
toggleAll(e);
};
content.onclick = function(e) {
Expand Down Expand Up @@ -573,15 +612,25 @@ function returnWifiSecSVG(secVal) {
return returnString;
}
var remotesCount;
function buildHtmlTable(selector, jsonVar) {
var columns = addAllColumnHeaders(jsonVar, selector);
var headerTbody$ = $('<tbody>');
for (var i = 0; i < jsonVar.length; i++) {
remotesCount = jsonVar.length;
for (var i = 0; i < remotesCount; i++) {
var row$ = $('<tr>');
row$.append($('<td>').html('<input type=\'radio\' id=\'option-select_remote-' + i + '\' name=\'optionsRemotes\' onchange=\"radio(\'remote\', '+i+')\" value=\'' + i + '\'/>'));
for (var colIndex = 0; colIndex < columns.length; colIndex++) {
var cellValue = jsonVar[i][columns[colIndex]].toString();
if (cellValue == null) cellValue = '';
row$.append($('<td>').html(cellValue));
if (cellValue == "0,0,0,0,0,0,0,0") cellValue = "empty slot";
if (colIndex == 2) {
row$.append($('<td>').html('<input type=\'text\' id=\'name_remote-' + i + '\' value=\'' + cellValue + '\' readonly=\'\' />'));
}
else {
row$.append($('<td>').html(cellValue));
}
}
headerTbody$.append(row$);
}
Expand All @@ -592,6 +641,8 @@ function addAllColumnHeaders(jsonVar, selector) {
var columnSet = [];
var headerThead$ = $('<thead>');
var headerTr$ = $('<tr>');
headerTr$.append($('<th>').html('select'));
for (var i = 0; i < jsonVar.length; i++) {
var rowHash = jsonVar[i];
for (var key in rowHash) {
Expand All @@ -603,6 +654,7 @@ function addAllColumnHeaders(jsonVar, selector) {
}
headerThead$.append(headerTr$);
$(selector).append(headerThead$);
return columnSet;
}
Expand Down Expand Up @@ -788,8 +840,8 @@ var html_mqttsetup = `
<input id="option-mqtt_domoticz-off" type="radio" name="option-mqtt_domoticz_active" onchange='radio("mqtt_domoticz", "off")' value="off"> off
</div>
<div class="pure-control-group">
<label id="label-mqtt_idx" for="mqtt_idx">Device IDX</label>
<input id="mqtt_idx" maxlength="5" type="text">
<label id="label-mqtt_idx" for="mqtt_idx" style="display: none;">Device IDX</label>
<input id="mqtt_idx" maxlength="5" type="text" style="display: none;">
</div>
<br>
<div class="pure-controls">
Expand Down
46 changes: 45 additions & 1 deletion software/NRG_itho_wifi/06_Websock_func.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ void jsonWsSend(const char* rootName) {
JsonObject nested = root.createNestedObject(rootName);
systemConfig.get(nested);
}
#if ESP32
else if (strcmp(rootName, "ithoremotes") == 0) {
// Create an object at the root
JsonObject obj = root.to<JsonObject>(); // Fill the object
remotes.get(obj);
}
#endif
size_t len = measureJson(root);
AsyncWebSocketMessageBuffer * buffer = ws.makeBuffer(len); // creates a buffer (len + 1) for you.
if (buffer) {
Expand Down Expand Up @@ -79,6 +86,9 @@ void jsonSystemstat() {
systemstat["itho_low"] = systemConfig.itho_low;
systemstat["itho_medium"] = systemConfig.itho_medium;
systemstat["itho_high"] = systemConfig.itho_high;
#if ESP32
systemstat["itho_llm"] = remotes.getllModeTime();
#endif
systemstat["i2cstat"] = i2cstat;

char buffer[512];
Expand Down Expand Up @@ -175,10 +185,44 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
systemConfig.get_itho_settings = true;
jsonWsSend("systemsettings");
sysStatReq = true;
}
}
#if ESP32
else if (msg.startsWith("{\"ithoremotes")) {
jsonWsSend("ithoremotes");
}
else if (msg.startsWith("{\"itho_llm")) {
toggleRemoteLLmode();
}
else if (msg.startsWith("{\"itho_remove_remote")) {
bool parseOK = false;
int number = (msg.substring(22)).toInt();
if (number == 0) {
if (strcmp(msg.substring(22, 23).c_str(), "0") == 0) {
parseOK = true;
}
}
else if (number > 0 && number < MAX_NUMBER_OF_REMOTES) {
parseOK = true;
}
if (parseOK) {
remotes.removeRemote(remotes.getRemoteIDbyIndex(number));
saveRemotes = true;
sendRemotes = true;
}
}
else if (msg.startsWith("{\"itho_update_remote")) {
StaticJsonDocument<512> root;
DeserializationError error = deserializeJson(root, msg.c_str());
if (!error) {
uint8_t index = root["itho_update_remote"].as<unsigned int>();
char remoteName[32];
strlcpy(remoteName, root["value"] | "", sizeof(remoteName));
remotes.updateRemoteName(index, remoteName);
saveRemotes = true;
sendRemotes = true;
}
}
#endif
else if (msg.startsWith("{\"reboot")) {
shouldReboot = true;
}
Expand Down
72 changes: 72 additions & 0 deletions software/NRG_itho_wifi/08_Config.ino
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,75 @@ bool resetSystemConfig() {
return true;
}
}
#if ESP32

uint16_t serializeRemotes(const IthoRemote &remotes, Print& dst) {
DynamicJsonDocument doc(1000+(MAX_NUMBER_OF_REMOTES*300));
// Create an object at the root
JsonObject root = doc.to<JsonObject>(); // Fill the object
remotes.get(root);
// Serialize JSON to file
return serializeJson(doc, dst) > 0;
}


bool saveRemotesConfig() {

return saveFileRemotes("/remotes.json", remotes);

}

bool saveFileRemotes(const char *filename, const IthoRemote &remotes) { // Open file for writing
File file = SPIFFS.open(filename, "w");
if (!file) {
//Serial.println(F("Failed to create remotes file"));
return false;
}
// Serialize JSON to file
bool success = serializeRemotes(remotes, file);
if (!success) {
//Serial.println(F("Failed to serialize remotes"));
return false;
}
//Serial.println(F("File saved"));
return true;
}

bool deserializeRemotes(Stream &src, IthoRemote &remotes) {

DynamicJsonDocument doc(1000+(MAX_NUMBER_OF_REMOTES*300));

// Parse the JSON object in the file
DeserializationError err = deserializeJson(doc, src);
if (err) {
//Serial.println(F("Failed to deserialize json"));
return false;
}

doc.shrinkToFit();
remotes.set(doc.as<JsonObject>());
return true;
}

bool loadRemotesConfig() {

return loadFileRemotes("/remotes.json", remotes);

}

bool loadFileRemotes(const char *filename, IthoRemote &remotes) { // Open file for reading
File file = SPIFFS.open(filename, "r");
// This may fail if the file is missing
if (!file) {
//Serial.println(F("Failed to open config file")); return false;
}
// Parse the JSON object in the file
bool success = deserializeRemotes(file, remotes);
// This may fail if the JSON is invalid
if (!success) {
//Serial.println(F("Failed to deserialize configuration"));
return false;
}
return true;
}
#endif
30 changes: 27 additions & 3 deletions software/NRG_itho_wifi/09_init_code.ino
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ char* hostName() {
// Do a little work to get a unique-ish name. Append the
// last two bytes of the MAC (HEX'd):
uint8_t mac[6];
#if ESP8266
WiFi.softAPmacAddress(mac);
#endif
#if ESP32
esp_read_mac(mac, ESP_MAC_WIFI_STA);
#endif

sprintf(hostName, "%s%02x%02x", espName, mac[6 - 2], mac[6 - 1]);

Expand Down Expand Up @@ -145,12 +150,20 @@ bool connectWiFiSTA()
#endif

int i = 0;
#if defined(ESP8266)
while (wifi_station_get_connect_status() != STATION_GOT_IP && i < 31) {
delay(1000);
#else
while ((WiFi.status() != WL_CONNECTED) && i < 31) {
#endif
delay(1000);
//Serial.print(".");
++i;
}
#if defined(ESP8266)
if (wifi_station_get_connect_status() != STATION_GOT_IP && i >= 30) {
#else
if ((WiFi.status() != WL_CONNECTED) && i >= 30) {
#endif
//delay(1000);
//Serial.println("");
//Serial.println("Couldn't connect to network :( ");
Expand Down Expand Up @@ -229,12 +242,13 @@ void logWifiInfo() {
logInput("WiFi info:");

const char* const modes[] = { "NULL", "STA", "AP", "STA+AP" };
const char* const phymodes[] = { "", "B", "G", "N" };

#if defined(ESP8266)
sprintf(wifiBuff, "Mode:%s", modes[wifi_get_opmode()]);
logInput(wifiBuff);
strcpy(wifiBuff, "");


const char* const phymodes[] = { "", "B", "G", "N" };
sprintf(wifiBuff, "PHY mode:%s", phymodes[(int) wifi_get_phy_mode()]);
logInput(wifiBuff);
strcpy(wifiBuff, "");
Expand Down Expand Up @@ -265,6 +279,16 @@ void logWifiInfo() {
sprintf(wifiBuff, "SSID (%d):%s", strlen(ssid), ssid);
logInput(wifiBuff);
strcpy(wifiBuff, "");
#else

sprintf(wifiBuff, "Mode:%s", modes[WiFi.getMode()]);
logInput(wifiBuff);
strcpy(wifiBuff, "");

sprintf(wifiBuff, "Status:%d", WiFi.status());
logInput(wifiBuff);
strcpy(wifiBuff, "");
#endif

IPAddress ip = WiFi.localIP();
sprintf(wifiBuff, "IP:%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
Expand Down
Loading

0 comments on commit dd1bb16

Please sign in to comment.