Comments (4)
`#include <IotWebConf.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
// -- Initial name of the Thing. Used e.g. as SSID of the own Access Point.
const char thingName[] = "IOT-Esp-8266";
// -- Initial password to connect to the Thing, when it creates an own Access Point.
const char wifiInitialApPassword[] = "iotesp8266";
#define STRING_LEN 128
#define LED D6
// -- Configuration specific key. The value should be modified if config structure was changed.
#define CONFIG_VERSION "Adafruit"
// -- When CONFIG_PIN is pulled to ground on startup, the Thing will use the initial
// password to buld an AP. (E.g. in case of lost password)
#define CONFIG_PIN D2
// -- Status indicator pin.
// First it will light up (kept LOW), on Wifi connection it will blink,
// when connected to the Wifi it will turn off (kept HIGH).
#define STATUS_PIN LED_BUILTIN
// -- Callback method declarations.
void wifiConnected();
void configSaved();
boolean formValidator();
void messageReceived(String &topic, String &payload);
// WiFiFlientSecure for SSL/TLS support
WiFiClientSecure client;
DNSServer dnsServer;
WebServer server(80);
HTTPUpdateServer httpUpdater;
char nomeserverValue[STRING_LEN];
char portaValue[STRING_LEN];
char usernameValue[STRING_LEN];
char aiokeyValue[STRING_LEN];
IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION);
IotWebConfParameter nomeserver = IotWebConfParameter("Server MQTT", "Server", nomeserverValue, STRING_LEN);
IotWebConfParameter porta = IotWebConfParameter("Porta MQTT", "Porta", portaValue, STRING_LEN);
IotWebConfParameter username = IotWebConfParameter("Username MQTT", "username", usernameValue, STRING_LEN);
IotWebConfParameter aiokey = IotWebConfParameter("Aio KEY MQTT", "aiokey", aiokeyValue, STRING_LEN);
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, nomeserverValue, portaValue, usernameValue, aiokeyValue);
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, usernameValue "/feeds/onoff");
// io.adafruit.com SHA1 fingerprint
char*fingerprint = "77 00 54 2D DA E7 D8 03 27 31 23 99 EB 27 DB CB A5 4C 57 18";
void MQTT_connect();
void verifyFingerprint();
void setup()
{
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
Serial.begin(115200);
Serial.println();
Serial.println("Starting up...");
iotWebConf.setStatusPin(STATUS_PIN);
iotWebConf.setConfigPin(CONFIG_PIN);
iotWebConf.addParameter(&nomeserver);
iotWebConf.addParameter(&porta);
iotWebConf.addParameter(&username);
iotWebConf.addParameter(&aiokey);
iotWebConf.setConfigSavedCallback(&configSaved);
iotWebConf.setFormValidator(&formValidator);
iotWebConf.setWifiConnectionCallback(&wifiConnected);
// -- Initializing the configuration.
boolean validConfig = iotWebConf.init();
if (!validConfig)
{
nomeserverValue[0] = '\0';
portaValue[0] = '\0';
usernameValue[0] = '\0';
aiokeyValue[0] = '\0';
}
// -- Set up required URL handlers on the web server.
server.on("/", handleRoot);
server.on("/config", []{ iotWebConf.handleConfig(); });
server.onNotFound({ iotWebConf.handleNotFound(); });
Serial.println("Ready.");
mqtt.subscribe(&onoffbutton);
// check the fingerprint of io.adafruit.com's SSL cert
verifyFingerprint();
}
void loop()
{
// -- doLoop should be called as frequently as possible.
iotWebConf.doLoop();
MQTT_connect();
// this is our 'wait for incoming subscription packets' busy subloop
// try to spend your time here
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
// Check if its the onoff button feed
if (subscription == &onoffbutton) {
Serial.print(F("Button: "));
Serial.println((char *)onoffbutton.lastread);
if (strcmp((char *)onoffbutton.lastread, "ON") == 0) {
digitalWrite(LED, HIGH);
}
if (strcmp((char *)onoffbutton.lastread, "OFF") == 0) {
digitalWrite(LED, LOW);
}
}
}
// ping the server to keep the mqtt connection alive
if(! mqtt.ping()) {
mqtt.disconnect();
}
}
void verifyFingerprint() {
const char* host = AIO_SERVER;
Serial.print("Connecting to ");
Serial.println(host);
Serial.printf("Using fingerprint '%s'\n", fingerprint);
client.setFingerprint(fingerprint);
if (! client.connect(host, AIO_SERVERPORT)) {
Serial.println("Connection failed. Halting execution.");
while(1);
}
if (client.verify(fingerprint, host)) {
Serial.println("Connection secure.");
} else {
Serial.println("Connection insecure! Halting execution.");
while(1);
}
}
/**
- Handle web requests to "/" path.
*/
void handleRoot()
{
// -- Let IotWebConf test and handle captive portal requests.
if (iotWebConf.handleCaptivePortal())
{
// -- Captive portal request were already served.
return;
}
String s = "<html lang="en"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/>";
s += "<title>IotWebConf 05 Callbacks</title>Benvenuto";
s += "- ";
- Server: ";
s += nomeserverValue;
s += "
s += "
s += "- ";
- Porta: ";
s += portaValue;
s += "
s += "
s += "- ";
- Username: ";
s += usernameValue;
s += "
s += "
s += "- ";
- Aio Key: ";
s += aiokeyValue;
s += "
s += "
s += "Clicca qui: Configurazione Modulo";
s += "\n"; - Server: ";
server.send(200, "text/html", s);
}
void wifiConnected()
{
Serial.println("WiFi was connected.");
}
void configSaved()
{
Serial.println("Configuration was updated.");
}
boolean formValidator()
{
Serial.println("Validating form.");
boolean valid = true;
int l = server.arg(nomeserver.getId()).length();
if (l < 3)
{
nomeserver.errorMessage = "Please provide at least 3 characters for this test!";
valid = false;
}
else int l = server.arg(porta.getId()).length();
if (l < 3)
{
porta.errorMessage = "Please provide at least 3 characters for this test!";
valid = false;
}
else int l = server.arg(username.getId()).length();
if (l < 3)
{
username.errorMessage = "Please provide at least 3 characters for this test!";
valid = false;
}
else int l = server.arg(aiokey.getId()).length();
if (l < 3)
{
aiokey.errorMessage = "Please provide at least 3 characters for this test!";
valid = false;
}
return valid;
}
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}`
from iotwebconf.
the error is: invalid conversion from 'char*' to 'uint16_t {aka short unsigned int}' [-fpermissive]
from iotwebconf.
furthermore, as soon as I create a new parameter, the first time I find fifty characters printed
from iotwebconf.
You shouldn't use uninitialized values for variable construction at global scope (e.g. in Adafruit_MQTT_Client mqtt(...) ). At that point in the code, the values are not ready to be used. It might happen that Adafruit_MQTT_Client is not a capable for dynamic configuration.
Besides that, please consult example 03 for using numeric parameters.
from iotwebconf.
Related Issues (20)
- suggested code: add upload progress bar for firmware update
- found bug concerning iotWebConf.delay() during bootup and /firmware
- Problem
- Support for RGB led
- Use as temporary AP without connecting to an existing Wifi
- Won't Connect to Network Sparkfun ESP32 S2 Thing Plus HOT 3
- ver 3.2.1 Error compiling for board LOLIN(WEMOS) D1 mini
- Typo on https://github.com/prampec/IotWebConf/blob/master/doc/UsersManual.md
- Advanced setConfigSavedCallback - provide parameters classes changed
- Improving offline mode
- WiFi password length is limited to 32 characters
- Bug the analogRead will return 0 with IotWebConf.
- MQTT Port setting is missing in MQTT examples
- Unable to commit messages on EEPROM with this library HOT 4
- Error Config size: 811 Wrong config version. Applying defaults.
- Default Values are not applied HOT 1
- Is a description of parameters available ? HOT 2
- After flashing new software i cant connect to SMLReader Wifi
- WiFi is constantly lost HOT 1
- maxlength is not always set.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from iotwebconf.