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";
// -- Initial password to connect to the Thing, when it creates an own Access Point.
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.
#define CONFIG_VERSION "dem99"
// -- Method declarations.
void handleRoot();
DNSServer dnsServer;
WebServer server(80);
static const char chooserValues[][STRING_LEN] = { "red", "blue", "darkYellow" };
static const char chooserNames[][STRING_LEN] = { "Red", "Blue", "Dark yellow" };
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", []() {
iotWebConf.saveConfig();
}, F("(Saves the config to EEPROM)"));
shell::defineCommand("reset_config", []() {
Serial.println(F("Resetting config...")); Serial.println(F("Resetting config..."));
iotWebConf.getRootParameterGroup()->applyDefaultValue(); iotWebConf.getRootParameterGroup()->applyDefaultValue();
Serial.println(F("Done!")); Serial.println(F("Done!"));
}, F("(Resets the complete IotWeb config)")); }
shell::defineStringCommand("ssid", [](char *ssid) { void setSSID(char *ssid){
Serial.print(F("Setting WiFi ssid to ")); Serial.print(F("Setting WiFi ssid to "));
Serial.println(ssid); Serial.println(ssid);
strlcpy(iotWebConf.getWifiSsidParameter()->valueBuffer, ssid, iotWebConf.getWifiSsidParameter()->getLength()); strlcpy(iotWebConf.getWifiSsidParameter()->valueBuffer, ssid, iotWebConf.getWifiSsidParameter()->getLength());
}, F("name (Sets SSID to name)")); }
shell::defineStringCommand("wifi_pwd", void setWifiPassword(char *pwd){
[](char *pwd) {
Serial.print(F("Setting WiFi password to ")); Serial.print(F("Setting WiFi password to "));
Serial.println(pwd); Serial.println(pwd);
strlcpy(iotWebConf.getWifiPasswordParameter()->valueBuffer, pwd, strlcpy(iotWebConf.getWifiPasswordParameter()->valueBuffer, pwd, iotWebConf.getWifiPasswordParameter()->getLength());
iotWebConf.getWifiPasswordParameter()->getLength()); }
}, F("abc (Sets WiFi password to abc)")); /*****************************************************************************/
/***
* Define shell commands with name, function and documentation.
*/
void defineShellCommands() {
shell::defineCommand("reset", []() { ESP.restart(); }, F("(Restarts the ESP)"));
shell::defineCommand("save_config", []() { iotWebConf.saveConfig(); }, F("(Saves the config to EEPROM)"));
shell::defineCommand("reset_config", resetConfig, F("(Resets the complete IotWeb config)"));
shell::defineStringCommand("ssid", setSSID, F("name (Sets SSID to name)"));
shell::defineStringCommand("wifi_pwd", setWifiPassword, 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();
...@@ -182,8 +124,9 @@ void setup() ...@@ -182,8 +124,9 @@ void setup()
server.on("/config", []{ iotWebConf.handleConfig(); }); server.on("/config", []{ iotWebConf.handleConfig(); });
server.onNotFound([](){ iotWebConf.handleNotFound(); }); server.onNotFound([](){ iotWebConf.handleNotFound(); });
defineConsoleCommands(); defineShellCommands();
Serial.println(F("Ready."));
Serial.println("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);
} }
......
Supports Markdown
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