Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
co2ampel
ampel-firmware
Commits
efb173a6
Commit
efb173a6
authored
Mar 05, 2022
by
Eric Duminil
Browse files
Refactor
parent
a79fc50f
Changes
1
Hide whitespace changes
Inline
Side-by-side
ampel-firmware/ampel-firmware.ino
View file @
efb173a6
#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
()
{
s
ensor_console
::
defineCommand
(
"reset"
,
[]()
{
s
hell
::
defineCommand
(
"reset"
,
[]()
{
ESP
.
restart
();
},
F
(
"(Restarts the ESP)"
));
s
ensor_console
::
defineCommand
(
"save_config"
,
[]()
{
s
hell
::
defineCommand
(
"save_config"
,
[]()
{
iotWebConf
.
saveConfig
();
},
F
(
"(Saves the config to EEPROM)"
));
s
ensor_console
::
defineCommand
(
"reset_config"
,
[]()
{
s
hell
::
defineCommand
(
"reset_config"
,
[]()
{
Serial
.
println
(
F
(
"Resetting config..."
));
iotWebConf
.
getRootParameterGroup
()
->
applyDefaultValue
();
Serial
.
println
(
F
(
"Done!"
));
},
F
(
"(Resets the complete IotWeb config)"
));
s
ensor_console
::
defineStringCommand
(
"ssid"
,
[](
char
*
ssid
)
{
s
hell
::
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)"
));
s
ensor_console
::
defineStringCommand
(
"wifi_pwd"
,
s
hell
::
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)"
));
s
ensor_console
::
defineStringCommand
(
"ap_pwd"
,
[](
char
*
pwd
)
{
s
hell
::
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)"
));
s
ensor_console
::
defineStringCommand
(
"name"
,
[](
char
*
name
)
{
s
hell
::
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)"
));
s
ensor_console
::
defineIntCommand
(
"ap"
,
[](
int
onOff
)
{
s
hell
::
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)"
));
s
ensor_console
::
defineIntCommand
(
"wifi"
,
[](
int
onOff
)
{
s
hell
::
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
();
s
ensor_console
::
checkSerialInput
();
s
hell
::
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
);
}
/***
* S
ensor console
* S
hell logic. Could be moved to shell.cpp
*/
namespace
s
ensor_console
{
namespace
s
hell
{
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
)
{
s
ensor_console
::
processSerialInput
(
Serial
.
read
());
s
hell
::
processSerialInput
(
Serial
.
read
());
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment