├── .gitattributes ├── AdafruitIO_WiFiProv └── AdafruitIO_WiFiProv.ino └── WiFiProv_Basic_Example └── WiFiProv_Basic_Example.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /AdafruitIO_WiFiProv/AdafruitIO_WiFiProv.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library ESP8266 Example 3 | 4 | Must use ESP8266 Arduino from: 5 | https://github.com/esp8266/Arduino 6 | 7 | Works great with Adafruit's Huzzah ESP board & Feather 8 | ----> https://www.adafruit.com/product/2471 9 | ----> https://www.adafruit.com/products/2821 10 | 11 | Adafruit invests time and resources providing this open source code, 12 | please support Adafruit and open-source hardware by purchasing 13 | products from Adafruit! 14 | 15 | Written by Tony DiCola for Adafruit Industries. 16 | MIT license, all text above must be included in any redistribution 17 | ****************************************************/ 18 | #include 19 | #include "Adafruit_MQTT.h" 20 | #include "Adafruit_MQTT_Client.h" 21 | 22 | #include "WiFiProv.h" 23 | #include "WiFi.h" 24 | 25 | // #define USE_SOFT_AP // Uncomment if you want to enforce using Soft AP method instead of BLE 26 | 27 | const char * pop = "abcd1234"; // Proof of possession - otherwise called a PIN - string provided by the device, entered by user in the phone app 28 | const char * service_name = "PROV_123"; // Name of your device (the Espressif apps expects by default device name starting with "Prov_") 29 | const char * service_key = NULL; // Password used for SofAP method (NULL = no password needed) 30 | bool reset_provisioned = false; // When true the library will automatically delete previously provisioned data. 31 | bool wifi_connected = 0; 32 | 33 | /************************* WiFi Access Point *********************************/ 34 | 35 | 36 | 37 | /************************* Adafruit.io Setup *********************************/ 38 | 39 | #define AIO_SERVER "io.adafruit.com" 40 | #define AIO_SERVERPORT 1883 // use 8883 for SSL 41 | #define AIO_USERNAME "AIO_USERNAME" 42 | #define AIO_USERNAME "AIO_USERNAME" 43 | 44 | /************ Global State (you don't need to change this!) ******************/ 45 | 46 | // Create an ESP8266 WiFiClient class to connect to the MQTT server. 47 | WiFiClient client; 48 | // or... use WiFiClientSecure for SSL 49 | //WiFiClientSecure client; 50 | 51 | // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 52 | Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); 53 | 54 | /****************************** Feeds ***************************************/ 55 | 56 | // Setup a feed called 'temperature' for publishing. 57 | // Notice MQTT paths for AIO follow the form: /feeds/ 58 | Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temperature"); 59 | 60 | 61 | /*************************** Sketch Code ************************************/ 62 | 63 | // Bug workaround for Arduino 1.6.6, it seems to need a function declaration 64 | // for some reason (only affects ESP8266, likely an arduino-builder bug). 65 | void MQTT_connect(); 66 | 67 | void SysProvEvent(arduino_event_t *sys_event) 68 | { 69 | switch (sys_event->event_id) { 70 | case ARDUINO_EVENT_WIFI_STA_GOT_IP: 71 | Serial.print("\nConnected IP address : "); 72 | Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr)); 73 | wifi_connected = 1; 74 | break; 75 | case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: 76 | Serial.println("\nDisconnected. Connecting to the AP again... "); 77 | wifi_connected = 0; 78 | break; 79 | case ARDUINO_EVENT_PROV_START: 80 | Serial.println("\nProvisioning started\nGive Credentials of your access point using smartphone app"); 81 | break; 82 | case ARDUINO_EVENT_PROV_CRED_RECV: { 83 | Serial.println("\nReceived Wi-Fi credentials"); 84 | Serial.print("\tSSID : "); 85 | Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid); 86 | Serial.print("\tPassword : "); 87 | Serial.println((char const *) sys_event->event_info.prov_cred_recv.password); 88 | break; 89 | } 90 | case ARDUINO_EVENT_PROV_CRED_FAIL: { 91 | Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n"); 92 | if (sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR) 93 | Serial.println("\nWi-Fi AP password incorrect"); 94 | else 95 | Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()"); 96 | break; 97 | } 98 | case ARDUINO_EVENT_PROV_CRED_SUCCESS: 99 | Serial.println("\nProvisioning Successful"); 100 | break; 101 | case ARDUINO_EVENT_PROV_END: 102 | Serial.println("\nProvisioning Ends"); 103 | break; 104 | default: 105 | break; 106 | } 107 | } 108 | 109 | void setup() 110 | { 111 | Serial.begin(115200); 112 | delay(10); 113 | 114 | Serial.println(F("Adafruit MQTT demo")); 115 | 116 | WiFi.onEvent(SysProvEvent); 117 | pinMode(0, INPUT); 118 | #if CONFIG_IDF_TARGET_ESP32 && CONFIG_BLUEDROID_ENABLED && not USE_SOFT_AP 119 | Serial.println("Begin Provisioning using BLE"); 120 | // Sample uuid that user can pass during provisioning using BLE 121 | uint8_t uuid[16] = {0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf, 122 | 0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02 123 | }; 124 | WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name, service_key, uuid, reset_provisioned); 125 | #else 126 | Serial.println("Begin Provisioning using Soft AP"); 127 | WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name, service_key); 128 | #endif 129 | 130 | #if (CONFIG_BLUEDROID_ENABLED && not (USE_SOFT_AP)) 131 | log_d("ble qr"); 132 | WiFiProv.printQR(service_name, pop, "ble"); 133 | #else 134 | log_d("wifi qr"); 135 | WiFiProv.printQR(service_name, pop, "softap"); 136 | #endif 137 | } 138 | 139 | uint32_t x = 0; 140 | 141 | void loop() { 142 | // Ensure the connection to the MQTT server is alive (this will make the first 143 | // connection and automatically reconnect when disconnected). See the MQTT_connect 144 | // function definition further below. 145 | 146 | if (wifi_connected) 147 | { 148 | MQTT_connect(); 149 | 150 | // this is our 'wait for incoming subscription packets' busy subloop 151 | // try to spend your time here 152 | 153 | Adafruit_MQTT_Subscribe *subscription; 154 | while ((subscription = mqtt.readSubscription(10000))) { 155 | 156 | } 157 | 158 | // Now we can publish stuff! 159 | Serial.print(F("\nSending temperature val ")); 160 | Serial.print(x); 161 | Serial.print("..."); 162 | if (! temperature.publish(x++)) { 163 | Serial.println(F("Failed")); 164 | } else { 165 | Serial.println(F("OK!")); 166 | } 167 | 168 | // ping the server to keep the mqtt connection alive 169 | // NOT required if you are publishing once every KEEPALIVE seconds 170 | /* 171 | if(! mqtt.ping()) { 172 | mqtt.disconnect(); 173 | } 174 | */ 175 | } 176 | 177 | else 178 | { 179 | Serial.println("Waiting For WiFi"); 180 | delay(1000); 181 | } 182 | 183 | if (digitalRead(0) == LOW) 184 | { 185 | Serial.println("Reset Button Pressed"); 186 | delay(5000); 187 | 188 | if (digitalRead(0) == LOW) { 189 | Serial.println("Resetting"); 190 | wifi_prov_mgr_reset_provisioning(); 191 | ESP.restart(); 192 | } 193 | } 194 | } 195 | 196 | // Function to connect and reconnect as necessary to the MQTT server. 197 | // Should be called in the loop function and it will take care if connecting. 198 | void MQTT_connect() 199 | { 200 | int8_t ret; 201 | 202 | // Stop if already connected. 203 | if (mqtt.connected()) { 204 | return; 205 | } 206 | 207 | Serial.print("Connecting to MQTT... "); 208 | 209 | uint8_t retries = 3; 210 | while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected 211 | Serial.println(mqtt.connectErrorString(ret)); 212 | Serial.println("Retrying MQTT connection in 5 seconds..."); 213 | mqtt.disconnect(); 214 | delay(5000); // wait 5 seconds 215 | retries--; 216 | if (retries == 0) { 217 | // basically die and wait for WDT to reset me 218 | while (1); 219 | } 220 | } 221 | Serial.println("MQTT Connected!"); 222 | } 223 | -------------------------------------------------------------------------------- /WiFiProv_Basic_Example/WiFiProv_Basic_Example.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Please read README.md file in this folder, or on the web: 3 | https://github.com/espressif/arduino-esp32/tree/master/libraries/WiFiProv/examples/WiFiProv 4 | 5 | Note: This sketch takes up a lot of space for the app and may not be able to flash with default setting on some chips. 6 | If you see Error like this: "Sketch too big" 7 | In Arduino IDE go to: Tools > Partition scheme > chose anything that has more than 1.4MB APP 8 | - for example "No OTA (2MB APP/2MB SPIFFS)" 9 | */ 10 | 11 | #include "WiFiProv.h" 12 | #include "WiFi.h" 13 | 14 | // #define USE_SOFT_AP // Uncomment if you want to enforce using Soft AP method instead of BLE 15 | 16 | const char * pop = "abcd1234"; // Proof of possession - otherwise called a PIN - string provided by the device, entered by user in the phone app 17 | const char * service_name = "PROV_123"; // Name of your device (the Espressif apps expects by default device name starting with "Prov_") 18 | const char * service_key = NULL; // Password used for SofAP method (NULL = no password needed) 19 | bool reset_provisioned = false; // When true the library will automatically delete previously provisioned data. 20 | 21 | void SysProvEvent(arduino_event_t *sys_event) 22 | { 23 | switch (sys_event->event_id) { 24 | case ARDUINO_EVENT_WIFI_STA_GOT_IP: 25 | Serial.print("\nConnected IP address : "); 26 | Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr)); 27 | break; 28 | case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: 29 | Serial.println("\nDisconnected. Connecting to the AP again... "); 30 | break; 31 | case ARDUINO_EVENT_PROV_START: 32 | Serial.println("\nProvisioning started\nGive Credentials of your access point using smartphone app"); 33 | break; 34 | case ARDUINO_EVENT_PROV_CRED_RECV: { 35 | Serial.println("\nReceived Wi-Fi credentials"); 36 | Serial.print("\tSSID : "); 37 | Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid); 38 | Serial.print("\tPassword : "); 39 | Serial.println((char const *) sys_event->event_info.prov_cred_recv.password); 40 | break; 41 | } 42 | case ARDUINO_EVENT_PROV_CRED_FAIL: { 43 | Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n"); 44 | if (sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR) 45 | Serial.println("\nWi-Fi AP password incorrect"); 46 | else 47 | Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()"); 48 | break; 49 | } 50 | case ARDUINO_EVENT_PROV_CRED_SUCCESS: 51 | Serial.println("\nProvisioning Successful"); 52 | break; 53 | case ARDUINO_EVENT_PROV_END: 54 | Serial.println("\nProvisioning Ends"); 55 | break; 56 | default: 57 | break; 58 | } 59 | } 60 | 61 | void setup() { 62 | Serial.begin(115200); 63 | WiFi.onEvent(SysProvEvent); 64 | pinMode(0, INPUT); 65 | 66 | #if CONFIG_IDF_TARGET_ESP32 && CONFIG_BLUEDROID_ENABLED && not USE_SOFT_AP 67 | Serial.println("Begin Provisioning using BLE"); 68 | // Sample uuid that user can pass during provisioning using BLE 69 | uint8_t uuid[16] = {0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf, 70 | 0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02 71 | }; 72 | WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name, service_key, uuid, reset_provisioned); 73 | #else 74 | Serial.println("Begin Provisioning using Soft AP"); 75 | WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name, service_key); 76 | #endif 77 | 78 | #if CONFIG_BLUEDROID_ENABLED && not USE_SOFT_AP 79 | log_d("ble qr"); 80 | WiFiProv.printQR(service_name, pop, "ble"); 81 | #else 82 | log_d("wifi qr"); 83 | WiFiProv.printQR(service_name, pop, "softap"); 84 | #endif 85 | } 86 | 87 | void loop() { 88 | 89 | if (digitalRead(0) == LOW) 90 | { 91 | Serial.println("Reset Button Pressed"); 92 | delay(5000); 93 | 94 | if (digitalRead(0) == LOW) { 95 | Serial.println("Resetting"); 96 | wifi_prov_mgr_reset_provisioning(); 97 | ESP.restart(); 98 | } 99 | } 100 | 101 | } 102 | --------------------------------------------------------------------------------