How to connect ESP8266 to WIFI with smart config then connect to MQTT broker?

I want to configure ESP in smart mode If I press the button for 10 seconds, and then connect to the MQTT broker to send / rec messages and the wifi user and password are saved in esp for next turn on, but my code does not work, Is there a library? Or an example to do this?

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <PubSubClient.h>
#include <Thread.h>
const char* mqttServer = "gw.zmd-par.com"; //
const int mqttPort = 1883;
Thread myThread = Thread();
WiFiUDP Udp;
WiFiClient espClient;
PubSubClient client(espClient);
#define pin 12
int cnt = 0;
int count = 0;
int sensorVal;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(pin,INPUT);
  Serial.println("here");
  myThread.onRun(try1);
  myThread.setInterval(200);
  


}

void loop() {

     client.loop();
   sensorVal = digitalRead(pin);
 //  Serial.println(sensorVal);
   if (sensorVal == HIGH){
   cnt ++ ;
 //  Serial.println(cnt);
   if(cnt == 10){
    Serial.println("wifi reset");
    wifiReset();
    cnt=0;
   }
   }
   if (sensorVal == LOW)
   cnt =0;
  // }
  //print out the value of the pushbutton
 delay(1000);
 // Serial.println(sensorVal);
}


void wifiReset(){
   
  WiFi.disconnect(true); // remove wifi details stored already
  cnt=0;
  WiFi.mode(WIFI_STA);
  
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if(count++ >= 10){
       WiFi.beginSmartConfig();
       while(1){
           delay(1000);
           if(WiFi.smartConfigDone()){
             Serial.println("SmartConfig Success");
             break;
           }
       }
    }
  }

     Serial.println("");
  Serial.println("");
  
  WiFi.printDiag(Serial);
  
  // Start the server
  Udp.begin(49999);
  Serial.println("USP Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());


}
  }


  void callback(char* topic, byte* payload, unsigned int length) {
 
  Serial.print("Message arrived in topic: ");
  Serial.println(topic);
 
  Serial.print("Message:");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
 
  Serial.println();
  Serial.println("-----------------------");
 
}

void try1(){

    // Check if a client has connected
  Udp.parsePacket();
  while(Udp.available()){
   
    Serial.println(Udp.remoteIP());
    Udp.flush();
    cnt=0;
    delay(5);

    while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
 
    if (client.connect("ESP8266Client" )) {
 
      Serial.println("connected");  
      client.setServer(mqttServer, mqttPort);
      client.setCallback(callback);
 
    } else {
 
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);
 
    }
  }
}

never heard of a "smartmode" yet.

Can you provide a link to the documentation?

What results do you get?
Your code has several serial-output
what serial-output do you get?

best regards Stefan

StefanL38:
never heard of a "smartmode" yet.

Can you provide a link to the documentation?

What results do you get?
Your code has several serial-output
what serial-output do you get?

best regards Stefan

My mean smart config
Esp connects to wifi but does not receive a message

So you are using a library that is known for not working anymore.

would be smart if smartconfig would work. But it doesn't.
What is your main application that you want to realise? Depending on your application other solutions can be sugested.

  • OTA over the air update
  • configuring ssid and password using bluetooth
  • configuring ssid and password using ESP-NOW
  • configuring ssid and password using a serial connection
  • configuring ssid and password reading from a SD-card
  • configuring ssid and password by ,checking an IO-pin to be high so if IO-pin is high starting Wifi in AP-mode
  • connect your mobile phone to this separate created wlan and the config-code shows a tiny website where you can enter ssid and password

in all this versions saving ssid and password into flash using SPIFFS

make IO-pin low again and reboot

with IO-pin low read out ssid and password from flash and connect

best regards Stefan

You code, OP, is all over the place. I'd pair the code down to just basics before adding in bells and whistles.

Also, could you do your research? Consider your use of #include <Thread.h>. Why not read about the thread class? You can read it at this link Thread - Arduino Reference
Important to note, about the thread class:

Compatibility
This library is compatible with the avr architecture so you should be able to use it on the following Arduino boards:

ESP8266 is not on that list. Once agian, I highly recommend you pair the code down and find one of the many ESP8266 MQTT connection things and use it as a starting platform and add bells and whistles when you got a basic working model.

Also, there is a version of the ESP8266 with OS, that comes with a multi threading environment called freeRTOS.

There are many examples on the internet and this site that will easily get you started doing the MQTT thing.