Skip to content

Instantly share code, notes, and snippets.

@anoochit
Created August 18, 2015 15:01
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save anoochit/526765176805cf19bee7 to your computer and use it in GitHub Desktop.
Save anoochit/526765176805cf19bee7 to your computer and use it in GitHub Desktop.
esp8266-smart-config
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
void setup() {
int cnt = 0;
// set for STA mode
WiFi.mode(WIFI_STA);
// put your setup code here, to run once:
Serial.begin(9600);
// led status at pin16
pinMode(16,OUTPUT);
//configure pin0
pinMode(0, INPUT_PULLUP);
// deplay for 2 sec for smartConfig
Serial.println("2 sec before clear SmartConfig");
delay(2000);
// read pullup
int isSmartConfig = digitalRead(0);
if (isSmartConfig==0) {
// bink for clear config
blinkClearConfig();
Serial.println("clear config");
// reset default config
WiFi.disconnect();
}
// if wifi cannot connect start smartconfig
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if(cnt++ >= 15){
WiFi.beginSmartConfig();
while(1){
delay(500);
if(WiFi.smartConfigDone()){
Serial.println("SmartConfig Success");
blinkSmartConfig();
break;
}
}
}
}
Serial.println("");
Serial.println("");
WiFi.printDiag(Serial);
// Print the IP address
Serial.println(WiFi.localIP());
}
void blinkSmartConfig() {
digitalWrite(16, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50); // wait for a second
digitalWrite(16, LOW); // turn the LED off by making the voltage LOW
delay(50);
}
void blinkClearConfig() {
int i=0;
while(i<=3) {
digitalWrite(16, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(16, LOW); // turn the LED off by making the voltage LOW
delay(100);
i++;
}
}
void blinkStartConfig() {
pinMode(16,OUTPUT);
int i=0;
digitalWrite(16, HIGH); // turn the LED on (HIGH is the voltage level)
}
void loop() {
}
@s90304a123
Copy link

very nice! Thanks

@cafe5hsang
Copy link

👍

@dancluj
Copy link

dancluj commented Aug 24, 2018

👍 Very useful repository

@marrobHD
Copy link

Can anyone help to add this to this code? Hastebin: https://hastebin.com/nanotamuri.cpp

@rodvan
Copy link

rodvan commented Jan 24, 2019

I dont really understand how you are doing to store the Wifi config and so we avoidhaving to reconnect manually everytime the board resets. My respects, you are a life saver, but... please explain how this works or how you save it :)

@bkrajendra
Copy link

Smart config works through UDP. ESP receives credentials through UDP broadcast packets sent by SmartConfig App.

@bkrajendra
Copy link

Can anyone help to add this to this code? Hastebin: https://hastebin.com/nanotamuri.cpp

https://hastebin.com/haziquruma.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment