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 <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 {
void defineCommand(const char *name, void (*function)(), const __FlashStringHelper *doc_fstring);
......@@ -14,114 +37,45 @@ namespace shell {
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.
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
void resetConfig(){
Serial.println(F("Resetting config..."));
iotWebConf.getRootParameterGroup()->applyDefaultValue();
Serial.println(F("Done!"));
}
// -- Configuration specific key. The value should be modified if config structure was changed.
#define CONFIG_VERSION "dem99"
void setSSID(char *ssid){
Serial.print(F("Setting WiFi ssid to "));
Serial.println(ssid);
strlcpy(iotWebConf.getWifiSsidParameter()->valueBuffer, ssid, iotWebConf.getWifiSsidParameter()->getLength());
}
// -- Method declarations.
void handleRoot();
void setWifiPassword(char *pwd){
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" };
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)"));
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("save_config", []() { iotWebConf.saveConfig(); }, F("(Saves the config to EEPROM)"));
shell::defineCommand("reset_config", []() {
Serial.println(F("Resetting config..."));
iotWebConf.getRootParameterGroup()->applyDefaultValue();
Serial.println(F("Done!"));
}, F("(Resets the complete IotWeb config)"));
shell::defineCommand("reset_config", resetConfig, F("(Resets the complete IotWeb config)"));
shell::defineStringCommand("ssid", [](char *ssid) {
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("ssid", setSSID, F("name (Sets SSID to name)"));
shell::defineStringCommand("wifi_pwd",
[](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("wifi_pwd", setWifiPassword, F("abc (Sets WiFi password to abc)"));
shell::defineStringCommand("ap_pwd", [](char *pwd) {
Serial.print(F("Setting AP password to "));
......@@ -162,18 +116,6 @@ void setup()
Serial.println();
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.
iotWebConf.init();
......@@ -181,9 +123,10 @@ void setup()
server.on("/", handleRoot);
server.on("/config", []{ iotWebConf.handleConfig(); });
server.onNotFound([](){ iotWebConf.handleNotFound(); });
defineShellCommands();
defineConsoleCommands();
Serial.println(F("Ready."));
Serial.println("Ready.");
}
void loop()
......@@ -193,9 +136,6 @@ void loop()
shell::checkSerialInput();
}
/**
* Handle web requests to "/" path.
*/
/**
* Handle web requests to "/" path.
*/
......@@ -207,10 +147,10 @@ void handleRoot()
// -- Captive portal request were already served.
return;
}
String 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>";
s += "Go to <a href='config'>configure page</a> to change settings.";
s += "</body></html>\n";
const char* s = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>"
"<title>IotWebConf 18 Shell</title></head><body>"
"Go to <a href='config'>configure page</a> to change settings."
"</body></html>\n";
server.send(200, "text/html", s);
}
......@@ -409,4 +349,4 @@ namespace shell {
Serial.println(F("' not supported. Available commands :"));
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