Nodo Telnet support, logging changed

This commit is contained in:
esp8266nu
2015-07-12 15:15:35 +00:00
parent ad51cc37f1
commit 79fe89116a
7 changed files with 509 additions and 230 deletions
+126 -43
View File
@@ -18,6 +18,8 @@ boolean Domoticz_getData(int idx, float *data)
Serial.println(F("connection failed"));
return false;
}
if (connectionFailures)
connectionFailures--;
// We now create a URI for the request
String url = F("/json.htm?type=devices&rid=");
@@ -69,6 +71,9 @@ boolean sendData(byte sensorType, int idx, byte varIndex)
case 2:
Domoticz_sendDataMQTT(sensorType, idx, varIndex);
break;
case 3:
NodoTelnet_sendData(sensorType, idx, varIndex);
break;
}
}
//#endif
@@ -83,8 +88,8 @@ boolean Domoticz_sendData(byte sensorType, int idx, byte varIndex)
char host[20];
sprintf(host, "%u.%u.%u.%u", Settings.Controller_IP[0], Settings.Controller_IP[1], Settings.Controller_IP[2], Settings.Controller_IP[3]);
sprintf(log, "%s%s", "connecting to ", host);
addLog(log);
sprintf(log, "%s%s", "HTTP : connecting to ", host);
addLog(LOG_LEVEL_DEBUG,log);
if (printToWeb)
{
printWebString += log;
@@ -95,11 +100,13 @@ boolean Domoticz_sendData(byte sensorType, int idx, byte varIndex)
if (!client.connect(host, Settings.ControllerPort))
{
connectionFailures++;
addLog((char*)"connection failed");
addLog(LOG_LEVEL_ERROR,(char*)"HTTP : connection failed");
if (printToWeb)
printWebString += F("connection failed<BR>");
return false;
}
if (connectionFailures)
connectionFailures--;
// We now create a URI for the request
String url = F("/json.htm?type=command&param=udevice&idx=");
@@ -137,7 +144,7 @@ boolean Domoticz_sendData(byte sensorType, int idx, byte varIndex)
}
url.toCharArray(log, 79);
addLog(log);
addLog(LOG_LEVEL_DEBUG_MORE,log);
if (printToWeb)
{
printWebString += log;
@@ -155,42 +162,116 @@ boolean Domoticz_sendData(byte sensorType, int idx, byte varIndex)
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG_MORE)
{
sprintf(log,"C1 WS %u FM %u",WiFi.status(),FreeMem());
Serial.println(log);
}
String line = client.readStringUntil('\n');
if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG_MORE)
{
sprintf(log,"C2 WS %u FM %u",WiFi.status(),FreeMem());
Serial.println(log);
}
line.toCharArray(log,79);
addLog(LOG_LEVEL_DEBUG_MORE,log);
if (line.substring(0, 15) == "HTTP/1.1 200 OK")
{
addLog((char*)"Succes!");
addLog(LOG_LEVEL_DEBUG,(char*)"HTTP : Succes!");
if (printToWeb)
printWebString += F("Success<BR>");
success = true;
}
delay(1);
}
addLog((char*)"closing connection");
addLog(LOG_LEVEL_DEBUG,(char*)"HTTP : closing connection");
if (printToWeb)
printWebString += F("closing connection<BR>");
#ifdef ESP_EASY
if (Settings.UDPPort != 0)
{
IPAddress broadcastIP(255, 255, 255, 255);
portTX.beginPacket(broadcastIP, Settings.UDPPort);
String message = F("IDXEvent ");
message += idx;
message += ",";
message += UserVar[varIndex - 1];
char str[80];
str[0] = 0;
message.toCharArray(str, 79);
Serial.print("UDP >: ");
Serial.println(str);
portTX.write(str);
portTX.endPacket();
}
#endif
client.flush();
client.stop();
return success;
}
/*********************************************************************************************\
* Send data to Domoticz using http url querystring
\*********************************************************************************************/
boolean NodoTelnet_sendData(byte sensorType, int var, byte varIndex)
{
char log[80];
boolean success = false;
char host[20];
sprintf(host, "%u.%u.%u.%u", Settings.Controller_IP[0], Settings.Controller_IP[1], Settings.Controller_IP[2], Settings.Controller_IP[3]);
sprintf(log, "%s%s", "TELNT: connecting to ", host);
addLog(LOG_LEVEL_DEBUG,log);
if (printToWeb)
{
printWebString += log;
printWebString += "<BR>";
}
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, Settings.ControllerPort))
{
connectionFailures++;
addLog(LOG_LEVEL_ERROR,(char*)"TELNT: connection failed");
if (printToWeb)
printWebString += F("connection failed<BR>");
return false;
}
if (connectionFailures)
connectionFailures--;
float value = UserVar[varIndex - 1];
// We now create a URI for the request
String url = F("variableset ");
url += var;
url += ",";
url += value;
url += "\n";
Serial.println("Sending enter");
client.print(" \n");
unsigned long timer = millis() + 200;
while (!client.available() && millis() < timer)
delay(1);
timer = millis() + 1000;
while (client.available() && millis() < timer && !success)
{
String line = client.readStringUntil('\n');
Serial.println(line);
if (line.substring(0, 20) == "Enter your password:")
{
success = true;
Serial.println("Password request ok");
}
delay(1);
}
Serial.println("Sending pw");
client.println(Settings.ControllerPassword);
delay(100);
while (client.available())
Serial.write(client.read());
Serial.println("Sending cmd");
client.print(url);
delay(10);
while (client.available())
Serial.write(client.read());
addLog(LOG_LEVEL_DEBUG,(char*)"TELNT: closing connection");
if (printToWeb)
printWebString += F("closing connection<BR>");
client.stop();
return success;
}
/*********************************************************************************************\
* Syslog client
\*********************************************************************************************/
@@ -203,8 +284,7 @@ void syslog(char *message)
char str[80];
str[0] = 0;
sprintf(str, "<7>ESP Unit: %u : %s", Settings.Unit, message);
Serial.print("SYSLG > ");
Serial.println(str);
addLog(LOG_LEVEL_DEBUG,str);
portTX.write(str);
portTX.endPacket();
}
@@ -367,13 +447,16 @@ boolean Domoticz_sendDataMQTT(byte sensorType, int idx, byte varIndex)
root.printTo(json, sizeof(json));
Serial.print("MQTT : ");
Serial.println(json);
addLog(json);
addLog(LOG_LEVEL_DEBUG,json);
if (!MQTTclient.publish("domoticz/in", json))
{
Serial.println(F("MQTT publish failed"));
MQTTConnect();
connectionFailures++;
}
else
if (connectionFailures)
connectionFailures--;
}
struct NodeStruct
@@ -396,7 +479,7 @@ boolean nodeVariableCopy(byte var, byte unit)
if (Nodes[unit].ip[0] == 0)
{
strcpy(log, "Remote Node unknown");
addLog(log);
addLog(LOG_LEVEL_DEBUG,log);
if (printToWeb)
{
printWebString += log;
@@ -408,7 +491,7 @@ boolean nodeVariableCopy(byte var, byte unit)
sprintf(host, "%u.%u.%u.%u", Nodes[unit].ip[0], Nodes[unit].ip[1], Nodes[unit].ip[2], Nodes[unit].ip[3]);
sprintf(log, "%s%s", "connecting to ", host);
addLog(log);
addLog(LOG_LEVEL_DEBUG,log);
if (printToWeb)
{
printWebString += log;
@@ -419,11 +502,13 @@ boolean nodeVariableCopy(byte var, byte unit)
if (!client.connect(host, 80))
{
connectionFailures++;
addLog((char*)"connection failed");
addLog(LOG_LEVEL_ERROR,(char*)"HTTP : connection failed");
if (printToWeb)
printWebString += F("connection failed<BR>");
return false;
}
if (connectionFailures)
connectionFailures--;
// We now create a URI for the request
String url = F("/?cmd=variableset%20");
@@ -432,7 +517,7 @@ boolean nodeVariableCopy(byte var, byte unit)
url += value;
url.toCharArray(log, 79);
addLog(log);
addLog(LOG_LEVEL_DEBUG,log);
if (printToWeb)
{
printWebString += log;
@@ -453,13 +538,13 @@ boolean nodeVariableCopy(byte var, byte unit)
String line = client.readStringUntil('\n');
if (line.substring(0, 15) == "HTTP/1.1 200 OK")
{
addLog((char*)"Succes!");
addLog(LOG_LEVEL_DEBUG,(char*)"HTTP : Succes!");
if (printToWeb)
printWebString += F("Success<BR>");
success = true;
}
}
addLog((char*)"closing connection");
addLog(LOG_LEVEL_DEBUG,(char*)"HTTP : closing connection");
if (printToWeb)
printWebString += F("closing connection<BR>");
@@ -477,14 +562,14 @@ void checkUDP()
if (packetSize)
{
char packetBuffer[128];
Serial.print("UDP " );
Serial.print(packetSize);
Serial.print(" < ");
//Serial.print("UDP " );
//Serial.print(packetSize);
//Serial.print(" < ");
int len = portRX.read(packetBuffer, 128);
if (packetBuffer[0] != 255)
{
packetBuffer[len] = 0;
Serial.println(packetBuffer);
addLog(LOG_LEVEL_DEBUG,packetBuffer);
#ifdef ESP_CONNEXIO
ExecuteLine(packetBuffer, VALUE_SOURCE_SERIAL);
#endif
@@ -518,11 +603,9 @@ void checkUDP()
sprintf(macaddress, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
char ipaddress[16];
sprintf(ipaddress, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
Serial.print(macaddress);
Serial.print(",");
Serial.print(ipaddress);
Serial.print(",");
Serial.println(unit);
char log[80];
sprintf(log, "UDP : %s,%s,%u", macaddress,ipaddress,unit);
addLog(LOG_LEVEL_DEBUG,log);
}
}
}
+81 -50
View File
@@ -14,14 +14,14 @@
* Additional information about licensing can be found at : http://www.gnu.org/licenses
\*************************************************************************************************************************/
// This file incorporates work covered by the following copyright and permission notice:
// This file incorporates work covered by the following copyright and permission notice:
/****************************************************************************************************************************\
* Arduino project "Nodo" © Copyright 2010..2015 Paul Tonkes
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
* Arduino project "Nodo" © Copyright 2010..2015 Paul Tonkes
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You received a copy of the GNU General Public License along with this program in file 'License.txt'.
*
@@ -69,9 +69,10 @@
#define DEFAULT_PULSE1_IDX 0 // Enter IDX of your virtual Pulse counter
#define DEFAULT_SWITCH1_IDX 0 // Enter IDX of your switch device
#define DEFAULT_PROTOCOL 1 // Protocol used for controller communications
// 1 = Domoticz HTTP
// 2 = Domoticz MQTT
#define UNIT 1
// 1 = Domoticz HTTP
// 2 = Domoticz MQTT
// 3 = Nodo Telnet
#define UNIT 0
// ********************************************************************************
// DO NOT CHANGE ANYTHING BELOW THIS LINE
@@ -96,11 +97,18 @@
#define ESP_PROJECT_PID 2015050101L
#define ESP_EASY
#define VERSION 1
#define BUILD 10
#define VERSION 2
#define BUILD 11
#define REBOOT_ON_MAX_CONNECTION_FAILURES 30
#define LOG_LEVEL_ERROR 1
#define LOG_LEVEL_INFO 2
#define LOG_LEVEL_DEBUG 3
#define LOG_LEVEL_DEBUG_MORE 4
#define CMD_REBOOT 89
#define CMD_WIFI_DISCONNECT 135
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ESP8266WebServer.h>
@@ -114,7 +122,7 @@
PubSubClient MQTTclient("");
// LCD
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
// WebServer
ESP8266WebServer WebServer(80);
@@ -160,14 +168,19 @@ struct SettingsStruct
byte Debug;
char Name[26];
byte SyslogLevel;
byte SerialLogLevel;
byte WebLogLevel;
unsigned long BaudRate;
char ControllerUser[26];
char ControllerPassword[26];
} Settings;
struct LogStruct
{
unsigned long timeStamp;
char Message[80];
}Logging[10];
int logcount=-1;
} Logging[10];
int logcount = -1;
boolean printToWeb = false;
String printWebString = "";
@@ -180,33 +193,38 @@ unsigned long timerwd;
unsigned int NC_Count = 0;
unsigned int C_Count = 0;
boolean AP_Mode = false;
boolean cmd_disconnect = false;
byte cmd_within_mainloop = 0;
unsigned long connectionFailures;
unsigned long wdcounter=0;
unsigned long wdcounter = 0;
unsigned long pulseCounter1=0;
byte switch1state=0;
unsigned long pulseCounter1 = 0;
byte switch1state = 0;
void setup()
{
Serial.begin(9600);
Serial.print(F("\nINIT : Booting Build nr:"));
Serial.println(BUILD);
EEPROM.begin(1024);
LoadSettings();
// if different version, eeprom settings structure has changed. Full Reset needed
// on a fresh ESP module eeprom values are set to 255. Version results into -1 (signed int)
Serial.println(Settings.PID);
Serial.println(Settings.Version);
if (Settings.Version != VERSION || Settings.PID != ESP_PROJECT_PID)
{
Serial.begin(9600); // Initialiseer de seriele poort
Serial.println(Settings.PID);
Serial.println(Settings.Version);
Serial.println(F("INIT : Incorrect PID or version!"));
delay(10000);
ResetFactory();
}
Serial.begin(Settings.BaudRate);
Serial.print(F("\nINIT : Booting Build nr:"));
Serial.println(BUILD);
if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG_MORE)
Serial.setDebugOutput(true);
hardwareInit();
WifiAPconfig();
@@ -217,15 +235,15 @@ void setup()
// setup UDP
if (Settings.UDPPort != 0)
portRX.begin(Settings.UDPPort);
// Setup timers
timer = millis() + 30000; // startup delay 30 sec
timer100ms = millis() + 100; // timer for periodic actions 10 x per/sec
timer1s = millis() + 1000; // timer for periodic actions once per/sec
timerwd = millis() + 30000; // timer for watchdog once per 30 sec
// Setup LCD display
lcd.init(); // initialize the lcd
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.print("ESP Easy");
@@ -236,7 +254,7 @@ void setup()
syslog((char*)"Boot");
sendSysInfoUDP(3);
Serial.println(F("INIT : Boot OK"));
addLog((char*)"Boot");
addLog(LOG_LEVEL_INFO,(char*)"Boot");
}
void loop()
@@ -244,16 +262,29 @@ void loop()
WebServer.handleClient();
MQTTclient.loop();
if (Serial.available())
serial();
checkUDP();
if (cmd_disconnect == true)
checkUDP();
if (cmd_within_mainloop != 0)
{
cmd_disconnect = false;
WifiDisconnect();
switch (cmd_within_mainloop)
{
case CMD_WIFI_DISCONNECT:
{
WifiDisconnect();
break;
}
case CMD_REBOOT:
{
ESP.reset();
break;
}
}
cmd_within_mainloop = 0;
}
// Watchdog trigger
@@ -262,9 +293,9 @@ void loop()
wdcounter++;
timerwd = millis() + 30000;
char str[40];
str[0]=0;
str[0] = 0;
Serial.print("WD : ");
sprintf(str,"Uptime %u ConnectFailures %u FreeMem %u", wdcounter/2, connectionFailures, FreeMem());
sprintf(str, "Uptime %u ConnectFailures %u FreeMem %u", wdcounter / 2, connectionFailures, FreeMem());
Serial.println(str);
syslog(str);
sendSysInfoUDP(1);
@@ -292,22 +323,22 @@ void loop()
SensorSend();
}
if(connectionFailures > REBOOT_ON_MAX_CONNECTION_FAILURES)
if (connectionFailures > REBOOT_ON_MAX_CONNECTION_FAILURES)
{
Serial.println(F("Too many connection failures"));
delayedReboot(60);
}
if (Settings.Switch1 != 0)
{
byte state1 = digitalRead(Settings.Pin_wired_in_1);
if (state1 != switch1state)
{
switch1state = state1;
UserVar[11 - 1] = state1;
sendData(10, Settings.Switch1, 11);
delay(100);
}
{
switch1state = state1;
UserVar[11 - 1] = state1;
sendData(10, Settings.Switch1, 11);
delay(100);
}
}
delay(10);
}
@@ -338,7 +369,7 @@ void SensorSend()
if (Settings.DHT > 0)
{
dht(Settings.DHTType, 2, 2); // read dht on wiredout 2, store to var 2 (and 3)
if (!isnan(UserVar[2-1]) && (UserVar[3-1] > 0))
if (!isnan(UserVar[2 - 1]) && (UserVar[3 - 1] > 0))
sendData(2, Settings.DHT, 2);
}
@@ -362,15 +393,15 @@ void SensorSend()
if (Settings.Pulse1 > 0)
{
float value=0;
float value = 0;
if (Domoticz_getData(Settings.Pulse1, &value))
{
Serial.print(F("Current Value:"));
Serial.println(value);
Serial.print(F("Delta Value:"));
Serial.println(pulseCounter1);
value=(value + pulseCounter1)*100;
pulseCounter1=0;
value = (value + pulseCounter1) * 100;
pulseCounter1 = 0;
Serial.print(F("New Value:"));
Serial.println(value);
UserVar[9 - 1] = value; // store pulsecount to var 9
+16 -11
View File
@@ -17,21 +17,26 @@ float ul2float(unsigned long ul)
return f;
}
void addLog(char *line)
void addLog(byte loglevel, char *line)
{
Serial.println(line);
if (Settings.SyslogLevel == 3)
syslog(line);
if (loglevel <= Settings.SerialLogLevel)
Serial.println(line);
logcount++;
if (logcount > 9)
logcount=0;
Logging[logcount].timeStamp = millis();
strcpy(Logging[logcount].Message,line);
Logging[logcount].Message[79]=0;
if (loglevel <= Settings.SyslogLevel)
syslog(line);
if (loglevel <= Settings.WebLogLevel)
{
logcount++;
if (logcount > 9)
logcount = 0;
Logging[logcount].timeStamp = millis();
strncpy(Logging[logcount].Message, line, 80);
Logging[logcount].Message[79] = 0;
}
}
void delayedReboot(int rebootDelay)
{
while (rebootDelay !=0 )
+6
View File
@@ -443,6 +443,12 @@ void ResetFactory(void)
Settings.Debug = 0;
strcpy(Settings.Name, DEFAULT_NAME);
Settings.SyslogLevel = 0;
Settings.SerialLogLevel = 4;
Settings.WebLogLevel = 0;
Settings.BaudRate = 115200;
Settings.ControllerUser[0] = 0;
Settings.ControllerPassword[0] = 0;
Save_Settings();
WifiDisconnect();
ESP.reset();
+218 -78
View File
@@ -14,35 +14,38 @@ void WebServerInit()
#endif
WebServer.on("/log", handle_log);
WebServer.on("/tools", handle_tools);
WebServer.on("/i2cscanner", handle_i2cscanner);
WebServer.on("/wifiscanner", handle_wifiscanner);
WebServer.begin();
}
void addMenu(String& str)
{
// Inline style definitions
str += F("<style>");
str += F("h1 {font-family:sans-serif; font-size:16pt; font-style:normal; border: 1px solid #333; color: #ffffff; background: #27f;}");
str += F("button {border: 1px solid #000; color: #000; background: #ddd; }");
str += F("* {font-family:sans-serif; font-size:12pt;}");
str += F("h1 {font-size:16pt; border:1px solid #333; color:#ffffff; background:#27f;}");
str += F(".button-link {padding:5px 10px; background:#5bf; color:#fff; border-radius:4px; border:solid 1px #258; text-decoration:none}");
str += F(".button-link:hover {background:#369;}");
str += F("</style>");
str += F("<h1>Welcome to ESP ");
#ifdef ESP_CONNEXIO
str += F("Connexio : ");
#endif
#ifdef ESP_EASY
str += F("Easy : ");
#endif
#ifdef ESP_CONNEXIO
str += F("Connexio : ");
#endif
#ifdef ESP_EASY
str += F("Easy : ");
#endif
str += Settings.Name;
str += F("</h1><button onClick=\"window.location='' \">Main</button>");
str += F("<button onClick=\"window.location='config' \">Config</button>");
str += F("<button onClick=\"window.location='devices' \">Devices</button>");
str += F("<button onClick=\"window.location='hardware' \">Hardware</button>");
#ifdef ESP_CONNEXIO
str += F("<button onClick=\"window.location='eventlist' \">Eventlist</button>");
#endif
str += F("<button onClick=\"window.location='log' \">Log</button>");
str += F("<button onClick=\"window.location='tools' \">Tools</button>");
str += "</table>";
str += "<BR><BR>";
str += F("</h1><a class=\"button-link\" href=\".\">Main</a>");
str += F("<a class=\"button-link\" href=\"config\">Config</a>");
str += F("<a class=\"button-link\" href=\"devices\">Devices</a>");
str += F("<a class=\"button-link\" href=\"hardware\">Hardware</a>");
#ifdef ESP_CONNEXIO
str += F("<a class=\"button-link\" href=\"eventlist\">Eventlist</a>");
#endif
str += F("<a class=\"button-link\" href=\"log\">Log</a>");
str += F("<a class=\"button-link\" href=\"tools\">Tools</a><BR><BR>");
}
void addFooter(String& str)
@@ -55,15 +58,17 @@ void addFooter(String& str)
//********************************************************************************
void handle_root() {
int freeMem = ESP.getFreeHeap();
Serial.print(F("HTTP : Webrequest : "));
if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG)
Serial.print(F("HTTP : Webrequest : "));
String webrequest = WebServer.arg("cmd");
webrequest.replace("%20", " ");
Serial.println(webrequest);
if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG)
Serial.println(webrequest);
char command[80];
command[0] = 0;
webrequest.toCharArray(command, 79);
if (strcasecmp(command, "wifidisconnect") != 0)
if ((strcasecmp(command, "wifidisconnect") != 0) && (strcasecmp(command, "reboot") != 0))
{
String reply = "";
addMenu(reply);
@@ -152,12 +157,22 @@ void handle_root() {
}
else
{
// have to disconnect from within the main loop
// have to disconnect or reboot from within the main loop
// because the webconnection is still active at this point
// disconnect would result into a crash/reboot...
Serial.println(F("WIFI : Disconnecting..."));
// disconnect here could result into a crash/reboot...
if (strcasecmp(command, "wifidisconnect") == 0)
{
Serial.println(F("WIFI : Disconnecting..."));
cmd_within_mainloop = CMD_WIFI_DISCONNECT;
}
if (strcasecmp(command, "reboot") == 0)
{
Serial.println(F(" : Rebooting..."));
cmd_within_mainloop = CMD_REBOOT;
}
WebServer.send(200, "text/html", "OK");
cmd_disconnect = true;
}
}
@@ -167,7 +182,8 @@ void handle_root() {
void handle_config() {
char tmpstring[26];
Serial.println(F("HTTP : Webconfig : "));
if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG)
Serial.println(F("HTTP : Webconfig"));
String name = WebServer.arg("name");
String ssid = WebServer.arg("ssid");
@@ -175,6 +191,8 @@ void handle_config() {
String controllerip = WebServer.arg("controllerip");
String controllerport = WebServer.arg("controllerport");
String protocol = WebServer.arg("protocol");
String controlleruser = WebServer.arg("controlleruser");
String controllerpassword = WebServer.arg("controllerpassword");
String ip = WebServer.arg("ip");
String espip = WebServer.arg("espip");
String espgateway = WebServer.arg("espgateway");
@@ -184,6 +202,9 @@ void handle_config() {
String syslogip = WebServer.arg("syslogip");
String sysloglevel = WebServer.arg("sysloglevel");
String udpport = WebServer.arg("udpport");
String serialloglevel = WebServer.arg("serialloglevel");
String webloglevel = WebServer.arg("webloglevel");
String baudrate = WebServer.arg("baudrate");
if (ssid[0] != 0)
{
@@ -197,6 +218,10 @@ void handle_config() {
controllerip.toCharArray(tmpstring, 25);
str2ip(tmpstring, Settings.Controller_IP);
Settings.ControllerPort = controllerport.toInt();
controlleruser.toCharArray(tmpstring, 25);
strcpy(Settings.ControllerUser, tmpstring);
controllerpassword.toCharArray(tmpstring, 25);
strcpy(Settings.ControllerPassword, tmpstring);
Settings.Protocol = protocol.toInt();
Settings.IP_Octet = ip.toInt();
espip.toCharArray(tmpstring, 25);
@@ -212,6 +237,9 @@ void handle_config() {
str2ip(tmpstring, Settings.Syslog_IP);
Settings.UDPPort = udpport.toInt();
Settings.SyslogLevel = sysloglevel.toInt();
Settings.SerialLogLevel = serialloglevel.toInt();
Settings.WebLogLevel = webloglevel.toInt();
Settings.BaudRate = baudrate.toInt();
Save_Settings();
LoadSettings();
}
@@ -234,22 +262,15 @@ void handle_config() {
reply += F("'><TR><TD>Unit nr:<TD><input type='text' name='unit' value='");
reply += Settings.Unit;
reply += F("'><TR><TD>Controller IP:<TD><input type='text' name='controllerip' value='");
char str[20];
sprintf(str, "%u.%u.%u.%u", Settings.Controller_IP[0], Settings.Controller_IP[1], Settings.Controller_IP[2], Settings.Controller_IP[3]);
reply += str;
reply += F("'><TR><TD>Controller Port:<TD><input type='text' name='controllerport' value='");
reply += Settings.ControllerPort;
reply += F("'><TR><TD>Protocol:");
byte choice = Settings.Protocol;
String options[3];
String options[4];
options[0] = F("");
options[1] = F("Domoticz HTTP");
options[2] = F("Domoticz MQTT");
options[3] = F("Nodo Telnet");
reply += F("<TD><select name='protocol'>");
for (byte x = 0; x < 3; x++)
for (byte x = 0; x < 4; x++)
{
reply += F("<option value='");
reply += x;
@@ -262,7 +283,27 @@ void handle_config() {
}
reply += F("</select>");
reply += F("<TR bgcolor='#55bbff'><TD>Optional Settings<TD><TR><TD>Fixed IP Octet:<TD><input type='text' name='ip' value='");
reply += F("<TR><TD>Controller IP:<TD><input type='text' name='controllerip' value='");
char str[20];
sprintf(str, "%u.%u.%u.%u", Settings.Controller_IP[0], Settings.Controller_IP[1], Settings.Controller_IP[2], Settings.Controller_IP[3]);
reply += str;
reply += F("'><TR><TD>Controller Port:<TD><input type='text' name='controllerport' value='");
reply += Settings.ControllerPort;
if (Settings.Protocol == 9999)
{
reply += F("'><TR><TD>Controller User:<TD><input type='text' name='controlleruser' value='");
reply += Settings.ControllerUser;
}
if (Settings.Protocol == 3)
{
reply += F("'><TR><TD>Controller Password:<TD><input type='text' name='controllerpassword' value='");
reply += Settings.ControllerPassword;
}
reply += F("'><TR bgcolor='#55bbff'><TD>Optional Settings<TD><TR><TD>Fixed IP Octet:<TD><input type='text' name='ip' value='");
reply += Settings.IP_Octet;
reply += F("'><TR><TD>ESP IP:<TD><input type='text' name='espip' value='");
@@ -288,7 +329,16 @@ void handle_config() {
reply += F("'><TR><TD>UDP port:<TD><input type='text' name='udpport' value='");
reply += Settings.UDPPort;
reply += F("'><TR><TD><TD><input type='submit' value='Submit'>");
reply += F("'><TR><TD>Serial log Level:<TD><input type='text' name='serialloglevel' value='");
reply += Settings.SerialLogLevel;
reply += F("'><TR><TD>Web log Level:<TD><input type='text' name='webloglevel' value='");
reply += Settings.WebLogLevel;
reply += F("'><TR><TD>Baud Rate:<TD><input type='text' name='baudrate' value='");
reply += Settings.BaudRate;
reply += F("'><TR><TD><TD><input class=\"button-link\" type='submit' value='Submit'>");
reply += F("</table></form>");
addFooter(reply);
WebServer.send(200, "text/html", reply);
@@ -299,7 +349,8 @@ void handle_config() {
//********************************************************************************
void handle_devices() {
Serial.println(F("HTTP : Webdevices : "));
if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG)
Serial.println(F("HTTP : Webdevices"));
String boardtype = WebServer.arg("boardtype");
String sensordelay = WebServer.arg("delay");
@@ -325,14 +376,12 @@ void handle_devices() {
Settings.Analog = analog.toInt();
Settings.Pulse1 = pulse1.toInt();
Settings.Switch1 = switch1.toInt();
Save_Settings();
#ifdef ESP_CONNEXIO
struct NodoEventStruct TempEvent;
ClearEvent(&TempEvent);
for (byte x = 1; x < 50; x++)
Eventlist_Write(x, &TempEvent, &TempEvent);
EEPROM.commit();
byte x = 1;
while (Eventlist_Write(x++, &TempEvent, &TempEvent)) delay(1);
char cmd[80];
sprintf(cmd, "eventlistwrite; boot %u; TimerSet 1,%u", Settings.Unit, Settings.Delay);
@@ -365,6 +414,7 @@ void handle_devices() {
eventAddVarSend(7, 1, Settings.Analog);
}
#endif
Save_Settings();
}
String reply = "";
@@ -391,7 +441,7 @@ void handle_devices() {
reply += F("'><TR><TD>Switch:<TD><input type='text' name='switch1' value='");
reply += Settings.Switch1;
reply += F("'><TR><TD><TD><input type='submit' value='Submit'>");
reply += F("'><TR><TD><TD><input class=\"button-link\" type='submit' value='Submit'>");
reply += F("</table></form>");
addFooter(reply);
WebServer.send(200, "text/html", reply);
@@ -406,6 +456,8 @@ void eventAddVarSend(byte var, byte sensortype, int idx)
strcpy(strProtocol, "HTTP");
if (Settings.Protocol == 2)
strcpy(strProtocol, "MQTT");
if (Settings.Protocol == 3)
strcpy(strProtocol, "TELNET");
sprintf(cmd, "eventlistwrite; Timer 1; VariableSend %u,%s,%u,%u", var, strProtocol, sensortype, idx);
ExecuteLine(cmd, VALUE_SOURCE_SERIAL);
}
@@ -424,7 +476,8 @@ void eventAddTimer(char* event)
//********************************************************************************
void handle_hardware() {
Serial.println(F("HTTP : Hardware : "));
if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG)
Serial.println(F("HTTP : Hardware"));
String boardtype = WebServer.arg("boardtype");
@@ -505,7 +558,7 @@ void handle_hardware() {
}
reply += F("</select>");
reply += F("<TR><TD><TD><input type='submit' value='Submit'><TR><TD>");
reply += F("<TR><TD><TD><input class=\"button-link\" type='submit' value='Submit'><TR><TD>");
switch (Settings.BoardType)
{
@@ -631,7 +684,8 @@ void handle_json() {
// Web Interface eventlist page
//********************************************************************************
void handle_eventlist() {
Serial.print(F("HTTP : Eventlist request : "));
if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG)
Serial.println(F("HTTP : Eventlist request"));
char *TempString = (char*)malloc(80);
String reply = "";
@@ -644,16 +698,15 @@ void handle_eventlist() {
struct NodoEventStruct TempEvent;
ClearEvent(&TempEvent);
for (byte x = 1; x < 50; x++)
Eventlist_Write(x, &TempEvent, &TempEvent);
EEPROM.commit();
byte x = 1;
while (Eventlist_Write(x++, &TempEvent, &TempEvent)) delay(1);
String eventlist = WebServer.arg("eventlist");
eventlist.replace("%0D%0A", "\n");
int NewLineIndex = eventlist.indexOf('\n');
byte limit = 0;
byte messagecode = 0;
while (NewLineIndex > 0 && limit < 10)
while ((NewLineIndex > 0) && (limit < EventlistMax))
{
limit++;
String line = eventlist.substring(0, NewLineIndex);
@@ -664,7 +717,8 @@ void handle_eventlist() {
//line = line.substring(SemiColonIndex+1);
String strCommand = F("eventlistwrite;");
strCommand += line;
Serial.println(strCommand);
if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG)
Serial.println(strCommand);
strCommand.toCharArray(TempString, 79);
messagecode = ExecuteLine(TempString, VALUE_SOURCE_SERIAL);
if (messagecode > 0)
@@ -677,6 +731,7 @@ void handle_eventlist() {
eventlist = eventlist.substring(NewLineIndex + 1);
NewLineIndex = eventlist.indexOf('\n');
}
EEPROM.commit();
}
reply += F("<form method='post'>");
@@ -691,7 +746,7 @@ void handle_eventlist() {
reply += F("</textarea>");
reply += F("<TR><TD><TD><input type='submit' value='Submit'>");
reply += F("<TR><TD><TD><input class=\"button-link\" type='submit' value='Submit'>");
reply += F("</table></form>");
addFooter(reply);
WebServer.send(200, "text/html", reply);
@@ -703,7 +758,8 @@ void handle_eventlist() {
// Web Interface log page
//********************************************************************************
void handle_log() {
Serial.println(F("HTTP : Log request : "));
if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG)
Serial.println(F("HTTP : Log request"));
char *TempString = (char*)malloc(80);
@@ -712,21 +768,23 @@ void handle_log() {
reply += F("<script language='JavaScript'>function RefreshMe(){window.location = window.location}setTimeout('RefreshMe()', 3000);</script>");
reply += F("<table bgcolor='#ddeeff'><tr bgcolor='#55bbff'><td>Log<TR><TD>");
byte counter = logcount;
do
if (logcount != -1)
{
counter++;
if (counter > 9)
counter = 0;
if (Logging[counter].timeStamp > 0)
byte counter = logcount;
do
{
reply += Logging[counter].timeStamp;
reply += " : ";
reply += Logging[counter].Message;
reply += "<BR>";
}
} while (counter != logcount);
counter++;
if (counter > 9)
counter = 0;
if (Logging[counter].timeStamp > 0)
{
reply += Logging[counter].timeStamp;
reply += " : ";
reply += Logging[counter].Message;
reply += "<BR>";
}
} while (counter != logcount);
}
reply += F("</table>");
addFooter(reply);
WebServer.send(200, "text/html", reply);
@@ -737,29 +795,34 @@ void handle_log() {
// Web Interface debug page
//********************************************************************************
void handle_tools() {
Serial.print(F("HTTP : Webrequest : "));
if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG)
Serial.print(F("HTTP : Tools request : "));
String webrequest = WebServer.arg("cmd");
webrequest.replace("%3B", ";");
webrequest.replace("%2C", ",");
webrequest.replace("+", " ");
Serial.println(webrequest);
if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG)
Serial.println(webrequest);
char command[80];
command[0] = 0;
webrequest.toCharArray(command, 79);
String reply = "";
addMenu(reply);
// second menu
reply += F("<button onClick=\"window.location='/?cmd=reboot'\">Reboot</button>");
reply += F("<button onClick=\"window.location='/?cmd=wificonnect'\">Connect</button>");
reply += F("<button onClick=\"window.location='/?cmd=wifidisconnect'\">Disconnect</button>");
reply += F("<BR><BR><form>");
// second menu
reply += F("<a class=\"button-link\" href=\"/?cmd=reboot\">Reboot</a>");
reply += F("<a class=\"button-link\" href=\"/?cmd=wificonnect\">Connect</a>");
reply += F("<a class=\"button-link\" href=\"/?cmd=wifidisconnect\">Disconnect</a>");
reply += F("<a class=\"button-link\" href=\"/i2cscanner\">I2C Scanner</a>");
reply += F("<a class=\"button-link\" href=\"/wifiscanner\">Wifi Scanner</a><BR><BR>");
reply += F("<form>");
reply += F("<table bgcolor='#ddeeff'><tr bgcolor='#55bbff'><td>Command<TD>");
reply += F("<input type='text' name='cmd' value='");
reply += webrequest;
reply += F("'><TR><TD><TD><input type='submit' value='Submit'><TR><TD>");
reply += F("'><TR><TD><TD><input class=\"button-link\" type='submit' value='Submit'><TR><TD>");
printToWeb = true;
printWebString = "<BR>";
@@ -776,4 +839,81 @@ void handle_tools() {
printWebString = "";
printToWeb = false;
}
//********************************************************************************
// Web Interface I2C scanner
//********************************************************************************
void handle_i2cscanner() {
if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG)
Serial.println(F("HTTP : I2C Scanner"));
char *TempString = (char*)malloc(80);
String reply = "";
addMenu(reply);
reply += F("<table bgcolor='#ddeeff'><tr bgcolor='#55bbff'><td>I2C Addresses in use<TR><TD>");
byte error, address;
int nDevices;
nDevices = 0;
for(address = 1; address < 127; address++ )
{
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
reply += "0x";
reply += String(address,HEX);
reply += "<BR>";
nDevices++;
}
else if (error==4)
{
reply += F("Unknow error at address 0x");
reply += String(address,HEX);
}
}
if (nDevices == 0)
reply += F("No I2C devices found");
reply += F("</table>");
addFooter(reply);
WebServer.send(200, "text/html", reply);
free(TempString);
}
//********************************************************************************
// Web Interface I2C scanner
//********************************************************************************
void handle_wifiscanner() {
if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG)
Serial.println(F("HTTP : Wifi Scanner"));
char *TempString = (char*)malloc(80);
String reply = "";
addMenu(reply);
reply += F("<table bgcolor='#ddeeff'><tr bgcolor='#55bbff'><td>Access Points:<TD>RSSI");
int n = WiFi.scanNetworks();
if (n == 0)
reply += F("No Access Points found");
else
{
for (int i = 0; i < n; ++i)
{
reply += "<TR><TD>";
reply += WiFi.SSID(i);
reply += "<TD>";
reply += WiFi.RSSI(i);
}
}
reply += F("</table>");
addFooter(reply);
WebServer.send(200, "text/html", reply);
free(TempString);
}
+49 -48
View File
@@ -3,15 +3,15 @@
//********************************************************************************
void WifiAPconfig()
{
// create and store unique AP SSID/PW to prevent ESP from starting AP mode with default SSID and No password!
char ap_ssid[20];
ap_ssid[0] = 0;
strcpy(ap_ssid, "ESP_");
sprintf(ap_ssid, "%s%u", ap_ssid, Settings.Unit);
// setup ssid for AP Mode when needed
WiFi.softAP(ap_ssid, Settings.WifiAPKey);
// We start in STA mode
WiFi.mode(WIFI_STA);
// create and store unique AP SSID/PW to prevent ESP from starting AP mode with default SSID and No password!
char ap_ssid[20];
ap_ssid[0] = 0;
strcpy(ap_ssid, "ESP_");
sprintf(ap_ssid, "%s%u", ap_ssid, Settings.Unit);
// setup ssid for AP Mode when needed
WiFi.softAP(ap_ssid, Settings.WifiAPKey);
// We start in STA mode
WiFi.mode(WIFI_STA);
}
@@ -22,7 +22,7 @@ void WifiAPMode(boolean state)
{
if (state)
{
AP_Mode=true;
AP_Mode = true;
char ap_ssid[20];
ap_ssid[0] = 0;
strcpy(ap_ssid, "ESP_");
@@ -33,7 +33,7 @@ void WifiAPMode(boolean state)
}
else
{
AP_Mode=false;
AP_Mode = false;
Serial.println(F("WIFI : Ending AP Mode"));
WiFi.mode(WIFI_STA);
}
@@ -73,26 +73,26 @@ boolean WifiConnect()
Serial.println(ip);
WiFi.config(ip, gw, subnet);
}
if (Settings.IP[0] != 0 && Settings.IP[0] != 255)
{
Serial.print(F("IP : Static IP :"));
char str[20];
sprintf(str, "%u.%u.%u.%u", Settings.IP[0], Settings.IP[1], Settings.IP[2], Settings.IP[3]);
Serial.println(str);
IPAddress ip = Settings.IP;
IPAddress gw = Settings.Gateway;
IPAddress subnet = Settings.Subnet;
WiFi.config(ip,gw,subnet);
}
{
Serial.print(F("IP : Static IP :"));
char str[20];
sprintf(str, "%u.%u.%u.%u", Settings.IP[0], Settings.IP[1], Settings.IP[2], Settings.IP[3]);
Serial.println(str);
IPAddress ip = Settings.IP;
IPAddress gw = Settings.Gateway;
IPAddress subnet = Settings.Subnet;
WiFi.config(ip, gw, subnet);
}
}
else
{
Serial.println(F("WIFI : No SSID!"));
NC_Count=1;
WifiAPMode(true);
}
{
Serial.println(F("WIFI : No SSID!"));
NC_Count = 1;
WifiAPMode(true);
}
}
}
@@ -145,27 +145,28 @@ void WifiScan()
//********************************************************************************
void WifiCheck()
{
if (WiFi.status() != WL_CONNECTED)
if (WiFi.status() != WL_CONNECTED)
{
NC_Count++;
if (NC_Count > 10 && !AP_Mode)
{
C_Count = 0;
WifiAPMode(true);
}
}
else
{
C_Count++;
NC_Count = 0;
if (C_Count > 60)
{
byte wifimode = wifi_get_opmode();
if (wifimode == 2 || wifimode == 3) //apmode is active
{
NC_Count++;
if (NC_Count > 10 && !AP_Mode)
{
C_Count=0;
WifiAPMode(true);
}
Serial.println(F("WIFI : Return to STA mode"));
WifiAPMode(false);
}
else
{
if (NC_Count !=0) // there was a disconnect before...
{
C_Count++;
if (C_Count > 60)
{
Serial.println(F("WIFI : Return to STA mode"));
NC_Count=0;
WifiAPMode(false);
}
}
}
}
}
}
+13
View File
@@ -66,4 +66,17 @@
// Added syslog level for detailed logging
// Added menu buttons in webgui and stuctured data as HTML tables
// R011 12-07-2015
// Changed javascript link buttons into CSS button styled href links
// Fixed Main button, only worked on IE.
// Changed connectionFailures counting
// Reboot from main loop instead of web event callback
// Changed logging configuration
// BaudRate can be configured through webgui, defaults to 115200
// Added Serial.setDebugOutput(true) if serial loglevel = 4 (debug_more level)
// Added telnet protocol to send variable data to a Nodo Mega controller
// Added webgui I2C scanner
// Added webgui Wifi scanner
// Fixed bug in settingssave on devices
// Added delay(1) in Domoticz_sendData, while available loop