Commit 506cc7bf authored by Eric Duminil's avatar Eric Duminil
Browse files

Refactor

parent efb173a6
/**
* Example: Shell commands
* Description:
* This example is a modified IotWebConf01Minimal.ino, with added shell commands.
*
* Simply type "help" via Serial in order to see the available commands:
*
*
*/
#include <IotWebConf.h> #include <IotWebConf.h>
#include <IotWebConfTParameter.h>
// -- Initial name of the Thing. Used e.g. as SSID of the own Access Point.
const char thingName[] = "testThing";
// -- Initial password to connect to the Thing, when it creates an own Access Point.
const char wifiInitialApPassword[] = "smrtTHNG8266";
// -- Method declarations.
void handleRoot();
DNSServer dnsServer;
WebServer server(80);
IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword);
/*** /***
* Shell definition. Could be moved to shell.h * Shell interface. Could be moved to shell.h
*/ */
namespace shell { namespace shell {
void defineCommand(const char *name, void (*function)(), const __FlashStringHelper *doc_fstring); void defineCommand(const char *name, void (*function)(), const __FlashStringHelper *doc_fstring);
...@@ -14,114 +37,45 @@ namespace shell { ...@@ -14,114 +37,45 @@ namespace shell {
void execute(const char *command_line); void execute(const char *command_line);
} }
/*****************************************************************************/
/*** /***
* IoTWebConf configuration * Define functions which can be called in Shell.
*/ */
// -- Initial name of the Thing. Used e.g. as SSID of the own Access Point. void resetConfig(){
const char thingName[] = "testThing"; Serial.println(F("Resetting config..."));
iotWebConf.getRootParameterGroup()->applyDefaultValue();
// -- Initial password to connect to the Thing, when it creates an own Access Point. Serial.println(F("Done!"));
const char wifiInitialApPassword[] = "smrtTHNG8266"; }
#define STRING_LEN 128
#define NUMBER_LEN 32
// -- Configuration specific key. The value should be modified if config structure was changed. void setSSID(char *ssid){
#define CONFIG_VERSION "dem99" Serial.print(F("Setting WiFi ssid to "));
Serial.println(ssid);
strlcpy(iotWebConf.getWifiSsidParameter()->valueBuffer, ssid, iotWebConf.getWifiSsidParameter()->getLength());
}
// -- Method declarations. void setWifiPassword(char *pwd){
void handleRoot(); Serial.print(F("Setting WiFi password to "));
Serial.println(pwd);
strlcpy(iotWebConf.getWifiPasswordParameter()->valueBuffer, pwd, iotWebConf.getWifiPasswordParameter()->getLength());
}
/*****************************************************************************/
DNSServer dnsServer; /***
WebServer server(80); * Define shell commands with name, function and documentation.
*/
static const char chooserValues[][STRING_LEN] = { "red", "blue", "darkYellow" }; void defineShellCommands() {
static const char chooserNames[][STRING_LEN] = { "Red", "Blue", "Dark yellow" }; shell::defineCommand("reset", []() { ESP.restart(); }, F("(Restarts the ESP)"));
IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION);
iotwebconf::TextTParameter<STRING_LEN> stringParam =
iotwebconf::Builder<iotwebconf::TextTParameter<STRING_LEN>>("stringParam").
label("String param").
defaultValue("").
build();
iotwebconf::ParameterGroup group1 = iotwebconf::ParameterGroup("group1", "");
iotwebconf::IntTParameter<int16_t> intParam =
iotwebconf::Builder<iotwebconf::IntTParameter<int16_t>>("intParam").
label("Int param").
defaultValue(30).
min(1).
max(100).
step(1).
placeholder("1..100").
build();
// -- We can add a legend to the separator
iotwebconf::ParameterGroup group2 = iotwebconf::ParameterGroup("c_factor", "Calibration factor");
iotwebconf::FloatTParameter floatParam =
iotwebconf::Builder<iotwebconf::FloatTParameter>("floatParam").
label("Float param").
defaultValue(0.0).
step(0.1).
placeholder("e.g. 23.4").
build();
iotwebconf::CheckboxTParameter checkboxParam =
iotwebconf::Builder<iotwebconf::CheckboxTParameter>("checkParam").
label("Check param").
defaultValue(true).
build();
iotwebconf::SelectTParameter<STRING_LEN> chooserParam =
iotwebconf::Builder<iotwebconf::SelectTParameter<STRING_LEN>>("chooseParam").
label("Choose param").
optionValues((const char*)chooserValues).
optionNames((const char*)chooserNames).
optionCount(sizeof(chooserValues) / STRING_LEN).
nameLength(STRING_LEN).
defaultValue("blue").
build();
iotwebconf::ColorTParameter colorParam =
iotwebconf::Builder<iotwebconf::ColorTParameter>("colorParam").
label("Choose color").
defaultValue("#FFDD88").
build();
iotwebconf::DateTParameter dateParam =
iotwebconf::Builder<iotwebconf::DateTParameter>("dateParam").
label("Select date").
defaultValue("").
build();
iotwebconf::TimeTParameter timeParam =
iotwebconf::Builder<iotwebconf::TimeTParameter>("timeParam").
label("Select time").
defaultValue("").
build();
void defineConsoleCommands() {
shell::defineCommand("reset", []() {
ESP.restart();
}, F("(Restarts the ESP)"));
shell::defineCommand("save_config", []() { shell::defineCommand("save_config", []() { iotWebConf.saveConfig(); }, F("(Saves the config to EEPROM)"));
iotWebConf.saveConfig();
}, F("(Saves the config to EEPROM)"));
shell::defineCommand("reset_config", []() { shell::defineCommand("reset_config", resetConfig, F("(Resets the complete IotWeb config)"));
Serial.println(F("Resetting config..."));
iotWebConf.getRootParameterGroup()->applyDefaultValue();
Serial.println(F("Done!"));
}, F("(Resets the complete IotWeb config)"));
shell::defineStringCommand("ssid", [](char *ssid) { shell::defineStringCommand("ssid", setSSID, F("name (Sets SSID to name)"));
Serial.print(F("Setting WiFi ssid to "));
Serial.println(ssid);
strlcpy(iotWebConf.getWifiSsidParameter()->valueBuffer, ssid, iotWebConf.getWifiSsidParameter()->getLength());
}, F("name (Sets SSID to name)"));
shell::defineStringCommand("wifi_pwd", shell::defineStringCommand("wifi_pwd", setWifiPassword, F("abc (Sets WiFi password to abc)"));
[](char *pwd) {
Serial.print(F("Setting WiFi password to "));
Serial.println(pwd);
strlcpy(iotWebConf.getWifiPasswordParameter()->valueBuffer, pwd,
iotWebConf.getWifiPasswordParameter()->getLength());
}, F("abc (Sets WiFi password to abc)"));
shell::defineStringCommand("ap_pwd", [](char *pwd) { shell::defineStringCommand("ap_pwd", [](char *pwd) {
Serial.print(F("Setting AP password to ")); Serial.print(F("Setting AP password to "));
...@@ -162,18 +116,6 @@ void setup() ...@@ -162,18 +116,6 @@ void setup()
Serial.println(); Serial.println();
Serial.println("Starting up..."); Serial.println("Starting up...");
group1.addItem(&intParam);
group2.addItem(&floatParam);
group2.addItem(&checkboxParam);
group2.addItem(&chooserParam);
group2.addItem(&colorParam);
group2.addItem(&dateParam);
group2.addItem(&timeParam);
iotWebConf.addSystemParameter(&stringParam);
iotWebConf.addParameterGroup(&group1);
iotWebConf.addParameterGroup(&group2);
// -- Initializing the configuration. // -- Initializing the configuration.
iotWebConf.init(); iotWebConf.init();
...@@ -181,9 +123,10 @@ void setup() ...@@ -181,9 +123,10 @@ void setup()
server.on("/", handleRoot); server.on("/", handleRoot);
server.on("/config", []{ iotWebConf.handleConfig(); }); server.on("/config", []{ iotWebConf.handleConfig(); });
server.onNotFound([](){ iotWebConf.handleNotFound(); }); server.onNotFound([](){ iotWebConf.handleNotFound(); });
defineShellCommands();
defineConsoleCommands(); Serial.println("Ready.");
Serial.println(F("Ready."));
} }
void loop() void loop()
...@@ -193,9 +136,6 @@ void loop() ...@@ -193,9 +136,6 @@ void loop()
shell::checkSerialInput(); shell::checkSerialInput();
} }
/**
* Handle web requests to "/" path.
*/
/** /**
* Handle web requests to "/" path. * Handle web requests to "/" path.
*/ */
...@@ -207,10 +147,10 @@ void handleRoot() ...@@ -207,10 +147,10 @@ void handleRoot()
// -- Captive portal request were already served. // -- Captive portal request were already served.
return; return;
} }
String s = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>"; const char* s = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>"
s += "<title>IotWebConf 01 Minimal</title></head><body>"; "<title>IotWebConf 18 Shell</title></head><body>"
s += "Go to <a href='config'>configure page</a> to change settings."; "Go to <a href='config'>configure page</a> to change settings."
s += "</body></html>\n"; "</body></html>\n";
server.send(200, "text/html", s); server.send(200, "text/html", s);
} }
...@@ -409,4 +349,4 @@ namespace shell { ...@@ -409,4 +349,4 @@ namespace shell {
Serial.println(F("' not supported. Available commands :")); Serial.println(F("' not supported. Available commands :"));
listAvailableCommands(); listAvailableCommands();
} }
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment