Skip to content

Instantly share code, notes, and snippets.

@itarozzi
Last active January 11, 2018 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itarozzi/ad9859f7873ed098c8e933e7899cfe89 to your computer and use it in GitHub Desktop.
Save itarozzi/ad9859f7873ed098c8e933e7899cfe89 to your computer and use it in GitHub Desktop.
ESP8266/ESP32 WiFiSmartConfig
/* **************************************************************************************
* Test using WIFI SmartConfig for ESP8266/ESP32
*
* Download android app here:
* https://play.google.com/store/apps/details?id=com.cmmakerclub.iot.esptouch&hl=en
*
* TODO:
* [x] store ssid and password to flash
* [ ] try saved ssid and password and enter in SmartConfig if fails
*
*/
#include "WiFi.h"
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
#include <EEPROM.h>
#include <Preferences.h>
Preferences preferences;
//OLED pins to ESP32 GPIOs via this connecthin:
//OLED_SDA -- GPIO4
//OLED_SCL -- GPIO15
//OLED_RST -- GPIO16
SSD1306 display(0x3c, 4, 15);
void setup() {
Serial.begin(115200);
// Open Preferences with my-app namespace. Each application module, library, etc.
// has to use namespace name to prevent key name collisions. We will open storage in
// RW-mode (second parameter has to be false).
// Note: Namespace name is limited to 15 chars
preferences.begin("my-app", false);
String ssid = preferences.getString("ssid", "---");
String passwd = preferences.getString("passwd", "----");
//Init the OLED disaplay
pinMode(16,OUTPUT);
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
delay(50);
digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in high
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
// Display preferences from Eprom
display.clear();
display.drawString(0, 0, "Stored values:");
display.drawString(0, 10, ssid);
display.drawString(0, 20, passwd);
display.display();
delay(3000);
//Init WiFi as Station, start SmartConfig
WiFi.mode(WIFI_AP_STA);
WiFi.beginSmartConfig();
//Wait for SmartConfig packet from mobile
Serial.println("Waiting for SmartConfig.");
display.clear();
display.drawString(0, 0, "Waiting for SmartConfig....");
display.display();
while (!WiFi.smartConfigDone()) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("SmartConfig received.");
display.clear();
display.drawString(0, 0, "SmartConfig received");
display.display();
//Wait for WiFi to connect to AP
Serial.println("Waiting for WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
ssid = WiFi.SSID();
passwd = WiFi.psk();
// Print debug info about WIFI network
// Caution! psk printed here :)
Serial.println("WiFi Connected.");
Serial.println(WiFi.SSID());
Serial.println(WiFi.psk());
Serial.println(WiFi.RSSI());
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Save ssid and password to preferences (eeprom)
preferences.putString("ssid", ssid);
preferences.putString("passwd", passwd);
preferences.end();
}
void loop() {
String strIp = WiFi.localIP().toString();
// Print info to OLED
display.clear();
display.drawString(0, 0, "Wifi Connected");
display.drawString(0, 20,WiFi.SSID());
display.drawString(0, 30, "IP:");
display.drawString(40, 30, strIp );
display.drawString(0, 40, "RSSI:");
display.drawString(40, 40, String(WiFi.RSSI() ));
display.display();
delay(3000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment