Commit efb173a6 authored by Eric Duminil's avatar Eric Duminil
Browse files

Refactor

parent a79fc50f
#include <IotWebConf.h> #include <IotWebConf.h>
#include <IotWebConfTParameter.h> #include <IotWebConfTParameter.h>
/** Other scripts can use this namespace, in order to define commands, via callbacks. /***
* Those callbacks can then be used to send commands to the sensor (reset, calibrate, led on/off, ...) * Shell definition. Could be moved to shell.h
* The callbacks can either have no parameter, one int32_t parameter or one char pointer.
*/ */
namespace shell {
namespace sensor_console {
void defineCommand(const char *name, void (*function)(), const __FlashStringHelper *doc_fstring); void defineCommand(const char *name, void (*function)(), const __FlashStringHelper *doc_fstring);
void defineIntCommand(const char *name, void (*function)(int32_t), const __FlashStringHelper *doc_fstring); void defineIntCommand(const char *name, void (*function)(int32_t), const __FlashStringHelper *doc_fstring);
void defineStringCommand(const char *name, void (*function)(char*), const __FlashStringHelper *doc_fstring); void defineStringCommand(const char *name, void (*function)(char*), const __FlashStringHelper *doc_fstring);
...@@ -16,6 +14,10 @@ namespace sensor_console { ...@@ -16,6 +14,10 @@ namespace sensor_console {
void execute(const char *command_line); void execute(const char *command_line);
} }
/***
* IoTWebConf configuration
*/
// -- Initial name of the Thing. Used e.g. as SSID of the own Access Point. // -- Initial name of the Thing. Used e.g. as SSID of the own Access Point.
const char thingName[] = "testThing"; const char thingName[] = "testThing";
...@@ -93,27 +95,27 @@ iotwebconf::TimeTParameter timeParam = ...@@ -93,27 +95,27 @@ iotwebconf::TimeTParameter timeParam =
build(); build();
void defineConsoleCommands() { void defineConsoleCommands() {
sensor_console::defineCommand("reset", []() { shell::defineCommand("reset", []() {
ESP.restart(); ESP.restart();
}, F("(Restarts the ESP)")); }, F("(Restarts the ESP)"));
sensor_console::defineCommand("save_config", []() { shell::defineCommand("save_config", []() {
iotWebConf.saveConfig(); iotWebConf.saveConfig();
}, F("(Saves the config to EEPROM)")); }, F("(Saves the config to EEPROM)"));
sensor_console::defineCommand("reset_config", []() { 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)")); }, F("(Resets the complete IotWeb config)"));
sensor_console::defineStringCommand("ssid", [](char *ssid) { shell::defineStringCommand("ssid", [](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)")); }, F("name (Sets SSID to name)"));
sensor_console::defineStringCommand("wifi_pwd", shell::defineStringCommand("wifi_pwd",
[](char *pwd) { [](char *pwd) {
Serial.print(F("Setting WiFi password to ")); Serial.print(F("Setting WiFi password to "));
Serial.println(pwd); Serial.println(pwd);
...@@ -121,19 +123,19 @@ void defineConsoleCommands() { ...@@ -121,19 +123,19 @@ void defineConsoleCommands() {
iotWebConf.getWifiPasswordParameter()->getLength()); iotWebConf.getWifiPasswordParameter()->getLength());
}, F("abc (Sets WiFi password to abc)")); }, F("abc (Sets WiFi password to abc)"));
sensor_console::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 "));
Serial.println(pwd); Serial.println(pwd);
strlcpy(iotWebConf.getApPasswordParameter()->valueBuffer, pwd, iotWebConf.getApPasswordParameter()->getLength()); strlcpy(iotWebConf.getApPasswordParameter()->valueBuffer, pwd, iotWebConf.getApPasswordParameter()->getLength());
}, F("abc (Sets AP password to abc)")); }, F("abc (Sets AP password to abc)"));
sensor_console::defineStringCommand("name", [](char *name) { shell::defineStringCommand("name", [](char *name) {
Serial.print(F("Setting Thing name to ")); Serial.print(F("Setting Thing name to "));
Serial.println(name); Serial.println(name);
strlcpy(iotWebConf.getThingNameParameter()->valueBuffer, name, iotWebConf.getThingNameParameter()->getLength()); strlcpy(iotWebConf.getThingNameParameter()->valueBuffer, name, iotWebConf.getThingNameParameter()->getLength());
}, F("abc (Sets AP password to abc)")); }, F("abc (Sets AP password to abc)"));
sensor_console::defineIntCommand("ap", [](int onOff) { shell::defineIntCommand("ap", [](int onOff) {
if (onOff) { if (onOff) {
Serial.print(F("Enable ")); Serial.print(F("Enable "));
} else { } else {
...@@ -143,7 +145,7 @@ void defineConsoleCommands() { ...@@ -143,7 +145,7 @@ void defineConsoleCommands() {
iotWebConf.forceApMode(onOff); iotWebConf.forceApMode(onOff);
}, F("0/1 (Enables/disables access point)")); }, F("0/1 (Enables/disables access point)"));
sensor_console::defineIntCommand("wifi", [](int onOff) { shell::defineIntCommand("wifi", [](int onOff) {
if (onOff) { if (onOff) {
Serial.print(F("Enable Wifi!")); Serial.print(F("Enable Wifi!"));
iotWebConf.goOnLine(); iotWebConf.goOnLine();
...@@ -188,9 +190,12 @@ void loop() ...@@ -188,9 +190,12 @@ void loop()
{ {
// -- doLoop should be called as frequently as possible. // -- doLoop should be called as frequently as possible.
iotWebConf.doLoop(); iotWebConf.doLoop();
sensor_console::checkSerialInput(); shell::checkSerialInput();
} }
/**
* Handle web requests to "/" path.
*/
/** /**
* Handle web requests to "/" path. * Handle web requests to "/" path.
*/ */
...@@ -203,39 +208,18 @@ void handleRoot() ...@@ -203,39 +208,18 @@ void handleRoot()
return; return;
} }
String s = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>"; String s = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>";
s += "<title>IotWebConf 03 Custom Parameters</title></head><body>Hello world!"; s += "<title>IotWebConf 01 Minimal</title></head><body>";
s += "<ul>"; s += "Go to <a href='config'>configure page</a> to change settings.";
s += "<li>String param value: ";
s += stringParam.value();
s += "<li>Int param value: ";
s += intParam.value();
s += "<li>Float param value: ";
s += floatParam.value();
s += "<li>CheckBox selected: ";
s += checkboxParam.isChecked();
s += "<li>Option selected: ";
s += chooserParam.value();
s += "<li>Color selected: <div style='background-color:";
s += colorParam.value();
s += "';> sample </div> (";
s += colorParam.value();
s += ")";
s += "<li>Date value: ";
s += dateParam.value();
s += "<li>Time value: ";
s += timeParam.value();
s += "</ul>";
s += "Go to <a href='config'>configure page</a> to change values.";
s += "</body></html>\n"; s += "</body></html>\n";
server.send(200, "text/html", s); server.send(200, "text/html", s);
} }
/*** /***
* Sensor console * Shell logic. Could be moved to shell.cpp
*/ */
namespace sensor_console { namespace shell {
const uint8_t MAX_COMMANDS = 10; const uint8_t MAX_COMMANDS = 10;
const uint8_t MAX_COMMAND_SIZE = 40; const uint8_t MAX_COMMAND_SIZE = 40;
...@@ -386,7 +370,7 @@ namespace sensor_console { ...@@ -386,7 +370,7 @@ namespace sensor_console {
void checkSerialInput() { void checkSerialInput() {
while (Serial.available() > 0) { while (Serial.available() > 0) {
sensor_console::processSerialInput(Serial.read()); shell::processSerialInput(Serial.read());
} }
} }
......
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