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

Refactor

parent a79fc50f
#include <IotWebConf.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, ...)
* The callbacks can either have no parameter, one int32_t parameter or one char pointer.
/***
* Shell definition. Could be moved to shell.h
*/
namespace sensor_console {
namespace shell {
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 defineStringCommand(const char *name, void (*function)(char*), const __FlashStringHelper *doc_fstring);
......@@ -16,6 +14,10 @@ namespace sensor_console {
void execute(const char *command_line);
}
/***
* IoTWebConf configuration
*/
// -- Initial name of the Thing. Used e.g. as SSID of the own Access Point.
const char thingName[] = "testThing";
......@@ -93,27 +95,27 @@ iotwebconf::TimeTParameter timeParam =
build();
void defineConsoleCommands() {
sensor_console::defineCommand("reset", []() {
shell::defineCommand("reset", []() {
ESP.restart();
}, F("(Restarts the ESP)"));
sensor_console::defineCommand("save_config", []() {
shell::defineCommand("save_config", []() {
iotWebConf.saveConfig();
}, F("(Saves the config to EEPROM)"));
sensor_console::defineCommand("reset_config", []() {
shell::defineCommand("reset_config", []() {
Serial.println(F("Resetting config..."));
iotWebConf.getRootParameterGroup()->applyDefaultValue();
Serial.println(F("Done!"));
}, 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.println(ssid);
strlcpy(iotWebConf.getWifiSsidParameter()->valueBuffer, ssid, iotWebConf.getWifiSsidParameter()->getLength());
}, F("name (Sets SSID to name)"));
sensor_console::defineStringCommand("wifi_pwd",
shell::defineStringCommand("wifi_pwd",
[](char *pwd) {
Serial.print(F("Setting WiFi password to "));
Serial.println(pwd);
......@@ -121,19 +123,19 @@ void defineConsoleCommands() {
iotWebConf.getWifiPasswordParameter()->getLength());
}, 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.println(pwd);
strlcpy(iotWebConf.getApPasswordParameter()->valueBuffer, pwd, iotWebConf.getApPasswordParameter()->getLength());
}, 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.println(name);
strlcpy(iotWebConf.getThingNameParameter()->valueBuffer, name, iotWebConf.getThingNameParameter()->getLength());
}, F("abc (Sets AP password to abc)"));
sensor_console::defineIntCommand("ap", [](int onOff) {
shell::defineIntCommand("ap", [](int onOff) {
if (onOff) {
Serial.print(F("Enable "));
} else {
......@@ -143,7 +145,7 @@ void defineConsoleCommands() {
iotWebConf.forceApMode(onOff);
}, F("0/1 (Enables/disables access point)"));
sensor_console::defineIntCommand("wifi", [](int onOff) {
shell::defineIntCommand("wifi", [](int onOff) {
if (onOff) {
Serial.print(F("Enable Wifi!"));
iotWebConf.goOnLine();
......@@ -188,9 +190,12 @@ void loop()
{
// -- doLoop should be called as frequently as possible.
iotWebConf.doLoop();
sensor_console::checkSerialInput();
shell::checkSerialInput();
}
/**
* Handle web requests to "/" path.
*/
/**
* Handle web requests to "/" path.
*/
......@@ -203,39 +208,18 @@ void handleRoot()
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 03 Custom Parameters</title></head><body>Hello world!";
s += "<ul>";
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 += "<title>IotWebConf 01 Minimal</title></head><body>";
s += "Go to <a href='config'>configure page</a> to change settings.";
s += "</body></html>\n";
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_COMMAND_SIZE = 40;
......@@ -386,7 +370,7 @@ namespace sensor_console {
void checkSerialInput() {
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