Skip to content

Commit

Permalink
WebServer wrapper
Browse files Browse the repository at this point in the history
Fixing examples and introducing scripts producing PlatformIO examples.
Introducing name spaces.
  • Loading branch information
prampec committed Dec 18, 2020
1 parent caa5fe2 commit 65e614f
Show file tree
Hide file tree
Showing 27 changed files with 720 additions and 413 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,2 +1,2 @@
.vscode
pio-src
examples-pio
5 changes: 4 additions & 1 deletion examples/IotWebConf01Minimal/IotWebConf01Minimal.ino
Expand Up @@ -3,7 +3,7 @@
* non blocking WiFi/AP web configuration library for Arduino.
* https://github.com/prampec/IotWebConf
*
* Copyright (C) 2018 Balazs Kelemen <prampec+arduino@gmail.com>
* Copyright (C) 2020 Balazs Kelemen <prampec+arduino@gmail.com>
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
Expand Down Expand Up @@ -37,6 +37,9 @@ 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);

Expand Down
Expand Up @@ -45,6 +45,9 @@ const char wifiInitialApPassword[] = "smrtTHNG8266";
// when connected to the Wifi it will turn off (kept HIGH).
#define STATUS_PIN LED_BUILTIN

// -- Method declarations.
void handleRoot();

DNSServer dnsServer;
WebServer server(80);

Expand Down
Expand Up @@ -3,7 +3,7 @@
* non blocking WiFi/AP web configuration library for Arduino.
* https://github.com/prampec/IotWebConf
*
* Copyright (C) 2018 Balazs Kelemen <prampec+arduino@gmail.com>
* Copyright (C) 2020 Balazs Kelemen <prampec+arduino@gmail.com>
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
Expand All @@ -26,6 +26,7 @@
*/

#include <IotWebConf.h>
#include <IotWebConfUsing.h> // This loads aliases for easier class names.

// -- Initial name of the Thing. Used e.g. as SSID of the own Access Point.
const char thingName[] = "testThing";
Expand All @@ -48,10 +49,11 @@ const char wifiInitialApPassword[] = "smrtTHNG8266";
// when connected to the Wifi it will turn off (kept HIGH).
#define STATUS_PIN LED_BUILTIN

// -- Callback method declarations.
// -- Method declarations.
void handleRoot();
// -- Callback methods.
void configSaved();
boolean formValidator();
void handleRoot();

DNSServer dnsServer;
WebServer server(80);
Expand All @@ -66,6 +68,7 @@ static char chooserValues[][STRING_LEN] = { "red", "blue", "darkYellow" };
static char chooserNames[][STRING_LEN] = { "Red", "Blue", "Dark yellow" };

IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION);
// -- You can also use namespace formats e.g.: iotwebconf::TextParameter
IotWebConfTextParameter stringParam = IotWebConfTextParameter("String param", "stringParam", stringParamValue, STRING_LEN);
IotWebConfParameterGroup group1 = IotWebConfParameterGroup("group1", "");
IotWebConfNumberParameter intParam = IotWebConfNumberParameter("Int param", "intParam", intParamValue, NUMBER_LEN, "20", "1..100", "min='1' max='100' step='1'");
Expand Down
22 changes: 19 additions & 3 deletions examples/IotWebConf04UpdateServer/IotWebConf04UpdateServer.ino
Expand Up @@ -3,7 +3,7 @@
* non blocking WiFi/AP web configuration library for Arduino.
* https://github.com/prampec/IotWebConf
*
* Copyright (C) 2018 Balazs Kelemen <prampec+arduino@gmail.com>
* Copyright (C) 2020 Balazs Kelemen <prampec+arduino@gmail.com>
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
Expand All @@ -26,7 +26,13 @@
*/

#include <IotWebConf.h>

// UpdateServer includes
#ifdef ESP8266
# include <ESP8266HTTPUpdateServer.h>
#elif defined(ESP32)
// For ESP32 IotWebConf provides a drop-in replacement for UpdateServer.
# include <IotWebConfESP32HTTPUpdateServer.h>
#endif
// -- Initial name of the Thing. Used e.g. as SSID of the own Access Point.
const char thingName[] = "testThing";

Expand All @@ -45,9 +51,16 @@ const char wifiInitialApPassword[] = "smrtTHNG8266";
// when connected to the Wifi it will turn off (kept HIGH).
#define STATUS_PIN LED_BUILTIN

// -- Callback method declarations.
void handleRoot();

DNSServer dnsServer;
WebServer server(80);
#ifdef ESP8266
ESP8266HTTPUpdateServer httpUpdater;
#elif defined(ESP32)
HTTPUpdateServer httpUpdater;
#endif

IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION);

Expand All @@ -59,7 +72,10 @@ void setup()

iotWebConf.setStatusPin(STATUS_PIN);
iotWebConf.setConfigPin(CONFIG_PIN);
iotWebConf.setupUpdateServer(&httpUpdater);
// -- Define how to handle updateServer calls.
iotWebConf.setupUpdateServer(
[](const char* updatePath) { httpUpdater.setup(&server, updatePath); },
[](const char* userName, char* password) { httpUpdater.updateCredentials(userName, password); });

// -- Initializing the configuration.
iotWebConf.init();
Expand Down
13 changes: 8 additions & 5 deletions examples/IotWebConf05Callbacks/IotWebConf05Callbacks.ino
Expand Up @@ -3,7 +3,7 @@
* non blocking WiFi/AP web configuration library for Arduino.
* https://github.com/prampec/IotWebConf
*
* Copyright (C) 2018 Balazs Kelemen <prampec+arduino@gmail.com>
* Copyright (C) 2020 Balazs Kelemen <prampec+arduino@gmail.com>
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
Expand All @@ -22,6 +22,7 @@
*/

#include <IotWebConf.h>
#include <IotWebConfUsing.h> // This loads aliases for easier class names.

// -- Initial name of the Thing. Used e.g. as SSID of the own Access Point.
const char thingName[] = "testThing";
Expand All @@ -43,19 +44,21 @@ const char wifiInitialApPassword[] = "smrtTHNG8266";
// when connected to the Wifi it will turn off (kept HIGH).
#define STATUS_PIN LED_BUILTIN

// -- Callback method declarations.
// -- Method declarations.
void handleRoot();
// -- Callback methods.
void wifiConnected();
void configSaved();
boolean formValidator();

DNSServer dnsServer;
WebServer server(80);
HTTPUpdateServer httpUpdater;

char stringParamValue[STRING_LEN];

IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION);
IotWebConfParameter stringParam = IotWebConfParameter("String param", "stringParam", stringParamValue, STRING_LEN);
// -- You can also use namespace formats e.g.: iotwebconf::TextParameter
IotWebConfTextParameter stringParam = IotWebConfTextParameter("String param", "stringParam", stringParamValue, STRING_LEN);

void setup()
{
Expand All @@ -65,7 +68,7 @@ void setup()

iotWebConf.setStatusPin(STATUS_PIN);
iotWebConf.setConfigPin(CONFIG_PIN);
iotWebConf.addParameter(&stringParam);
iotWebConf.addSystemParameter(&stringParam);
iotWebConf.setConfigSavedCallback(&configSaved);
iotWebConf.setFormValidator(&formValidator);
iotWebConf.setWifiConnectionCallback(&wifiConnected);
Expand Down
17 changes: 10 additions & 7 deletions examples/IotWebConf06MqttApp/IotWebConf06MqttApp.ino
Expand Up @@ -3,7 +3,7 @@
* non blocking WiFi/AP web configuration library for Arduino.
* https://github.com/prampec/IotWebConf
*
* Copyright (C) 2018 Balazs Kelemen <prampec+arduino@gmail.com>
* Copyright (C) 2020 Balazs Kelemen <prampec+arduino@gmail.com>
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
Expand All @@ -15,7 +15,7 @@
* All IotWebConf specific aspects of this example are described in
* previous examples, so please get familiar with IotWebConf before
* starting this example. So nothing new will be explained here,
* but a complete demo application will be build.
* but a complete demo application will be built.
* It is also expected from the reader to have a basic knowledge over
* MQTT to understand this code.
*
Expand All @@ -38,6 +38,7 @@

#include <MQTT.h>
#include <IotWebConf.h>
#include <IotWebConfUsing.h> // This loads aliases for easier class names.

// -- Initial name of the Thing. Used e.g. as SSID of the own Access Point.
const char thingName[] = "testThing";
Expand All @@ -59,15 +60,18 @@ const char wifiInitialApPassword[] = "smrtTHNG8266";
// when connected to the Wifi it will turn off (kept HIGH).
#define STATUS_PIN LED_BUILTIN

// -- Callback method declarations.
// -- Method declarations.
void handleRoot();
void mqttMessageReceived(String &topic, String &payload);
boolean connectMqtt();
boolean connectMqttOptions();
// -- Callback methods.
void wifiConnected();
void configSaved();
boolean formValidator();
void mqttMessageReceived(String &topic, String &payload);

DNSServer dnsServer;
WebServer server(80);
HTTPUpdateServer httpUpdater;
WiFiClient net;
MQTTClient mqttClient;

Expand All @@ -77,7 +81,7 @@ char mqttUserPasswordValue[STRING_LEN];

IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION);
// -- You can also use namespace formats e.g.: iotwebconf::ParameterGroup
IotWebConfParameterGroup mqttGroup = IotWebConfParameterGroup("mqtt", "MQTT configuration");
IotWebConfParameterGroup mqttGroup = IotWebConfParameterGroup("MQTT configuration");
IotWebConfTextParameter mqttServerParam = IotWebConfTextParameter("MQTT server", "mqttServer", mqttServerValue, STRING_LEN);
IotWebConfTextParameter mqttUserNameParam = IotWebConfTextParameter("MQTT user", "mqttUser", mqttUserNameValue, STRING_LEN);
IotWebConfPasswordParameter mqttUserPasswordParam = IotWebConfPasswordParameter("MQTT password", "mqttPass", mqttUserPasswordValue, STRING_LEN);
Expand All @@ -104,7 +108,6 @@ void setup()
iotWebConf.setConfigSavedCallback(&configSaved);
iotWebConf.setFormValidator(&formValidator);
iotWebConf.setWifiConnectionCallback(&wifiConnected);
iotWebConf.setupUpdateServer(&httpUpdater);

// -- Initializing the configuration.
boolean validConfig = iotWebConf.init();
Expand Down
20 changes: 20 additions & 0 deletions examples/IotWebConf06MqttApp/pio/platformio.ini
@@ -0,0 +1,20 @@
[env:d1]
platform = espressif8266
board = d1
framework = arduino
monitor_speed = 115200
;upload_port = /dev/cu.SLAB_USBtoUART
lib_deps =
MQTT
IotWebConf

[env:d1_32]
platform = espressif32
board = wemos_d1_mini32
framework = arduino
monitor_speed = 115200
monitor_port = /dev/cu.SLAB_USBtoUART
upload_port = /dev/cu.SLAB_USBtoUART
lib_deps =
MQTT
IotWebConf
28 changes: 22 additions & 6 deletions examples/IotWebConf07MqttRelay/IotWebConf07MqttRelay.ino
Expand Up @@ -3,7 +3,7 @@
* non blocking WiFi/AP web configuration library for Arduino.
* https://github.com/prampec/IotWebConf
*
* Copyright (C) 2018 Balazs Kelemen <prampec+arduino@gmail.com>
* Copyright (C) 2020 Balazs Kelemen <prampec+arduino@gmail.com>
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
Expand Down Expand Up @@ -54,6 +54,12 @@

#include <MQTT.h>
#include <IotWebConf.h>
#include <IotWebConfUsing.h> // This loads aliases for easier class names.
#ifdef ESP8266
# include <ESP8266HTTPUpdateServer.h>
#elif defined(ESP32)
# include <IotWebConfESP32HTTPUpdateServer.h>
#endif

// -- Initial name of the Thing. Used e.g. as SSID of the own Access Point.
const char thingName[] = "testThing";
Expand Down Expand Up @@ -84,22 +90,30 @@ const char wifiInitialApPassword[] = "smrtTHNG8266";
#define ACTION_FEQ_LIMIT 7000
#define NO_ACTION -1

// -- Callback method declarations.
// -- Method declarations.
void handleRoot();
void mqttMessageReceived(String &topic, String &payload);
boolean connectMqtt();
// -- Callback methods.
void wifiConnected();
void configSaved();
boolean formValidator();
void mqttMessageReceived(String &topic, String &payload);

DNSServer dnsServer;
WebServer server(80);
#ifdef ESP8266
ESP8266HTTPUpdateServer httpUpdater;
#elif defined(ESP32)
HTTPUpdateServer httpUpdater;
#endif
WiFiClient net;
MQTTClient mqttClient;

char mqttServerValue[STRING_LEN];

IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION);
IotWebConfParameter mqttServerParam = IotWebConfParameter("MQTT server", "mqttServer", mqttServerValue, STRING_LEN);
// -- You can also use namespace formats e.g.: iotwebconf::TextParameter
IotWebConfTextParameter mqttServerParam = IotWebConfTextParameter("MQTT server", "mqttServer", mqttServerValue, STRING_LEN);

boolean needMqttConnect = false;
boolean needReset = false;
Expand All @@ -120,11 +134,13 @@ void setup()

iotWebConf.setStatusPin(STATUS_PIN);
iotWebConf.setConfigPin(BUTTON_PIN);
iotWebConf.addParameter(&mqttServerParam);
iotWebConf.addSystemParameter(&mqttServerParam);
iotWebConf.setConfigSavedCallback(&configSaved);
iotWebConf.setFormValidator(&formValidator);
iotWebConf.setWifiConnectionCallback(&wifiConnected);
iotWebConf.setupUpdateServer(&httpUpdater);
iotWebConf.setupUpdateServer(
[](const char* updatePath) { httpUpdater.setup(&server, updatePath); },
[](const char* userName, char* password) { httpUpdater.updateCredentials(userName, password); });

// -- Initializing the configuration.
boolean validConfig = iotWebConf.init();
Expand Down
20 changes: 20 additions & 0 deletions examples/IotWebConf07MqttRelay/pio/platformio.ini
@@ -0,0 +1,20 @@
[env:d1]
platform = espressif8266
board = d1
framework = arduino
monitor_speed = 115200
;upload_port = /dev/cu.SLAB_USBtoUART
lib_deps =
MQTT
IotWebConf

[env:d1_32]
platform = espressif32
board = wemos_d1_mini32
framework = arduino
monitor_speed = 115200
monitor_port = /dev/cu.SLAB_USBtoUART
upload_port = /dev/cu.SLAB_USBtoUART
lib_deps =
MQTT
IotWebConf

0 comments on commit 65e614f

Please sign in to comment.