Giter Site home page Giter Site logo

feature request about iotwebconf HOT 4 CLOSED

prampec avatar prampec commented on July 19, 2024
feature request

from iotwebconf.

Comments (4)

aandroide avatar aandroide commented on July 19, 2024

`#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 += "
      ";
      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 += "Clicca qui: Configurazione Modulo";
    s += "\n";

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.

aandroide avatar aandroide commented on July 19, 2024

the error is: invalid conversion from 'char*' to 'uint16_t {aka short unsigned int}' [-fpermissive]

from iotwebconf.

aandroide avatar aandroide commented on July 19, 2024

furthermore, as soon as I create a new parameter, the first time I find fifty characters printed

from iotwebconf.

prampec avatar prampec commented on July 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.