Guest User

ESP32_BT_WIFI

a guest
Nov 4th, 2020
192
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. #include <WiFi.h>
  4.  
  5. #include <Preferences.h>
  6. #include "BluetoothSerial.h"
  7.  
  8.  
  9. String ssids_array[50];
  10. String network_string;
  11. String connected_string;
  12.  
  13. const char* pref_ssid = "";
  14. const char* pref_pass = "";
  15. String client_wifi_ssid;
  16. String client_wifi_password;
  17.  
  18. const char* bluetooth_name = "robot01";
  19.  
  20. long start_wifi_millis;
  21. long wifi_timeout = 10000;
  22. bool bluetooth_disconnect = false;
  23.  
  24. enum wifi_setup_stages { NONE, SCAN_START, SCAN_COMPLETE, SSID_ENTERED, WAIT_PASS, PASS_ENTERED, WAIT_CONNECT, LOGIN_FAILED };
  25. enum wifi_setup_stages wifi_stage = NONE;
  26.  
  27.  
  28. BluetoothSerial SerialBT;
  29. Preferences preferences;
  30.  
  31.  
  32. void setup()
  33. {
  34. Serial.begin(115200);
  35. Serial.println("Booting...");
  36.  
  37. preferences.begin("wifi_access", false);
  38.  
  39.  
  40. if (!init_wifi()) { // Connect to Wi-Fi fails
  41. SerialBT.register_callback(callback);
  42. } else {
  43. SerialBT.register_callback(callback_show_ip);
  44. }
  45.  
  46. SerialBT.begin(bluetooth_name);
  47.  
  48.  
  49. }
  50.  
  51. bool init_wifi()
  52. {
  53. String temp_pref_ssid = preferences.getString("pref_ssid");
  54. String temp_pref_pass = preferences.getString("pref_pass");
  55. pref_ssid = temp_pref_ssid.c_str();
  56. pref_pass = temp_pref_pass.c_str();
  57.  
  58. Serial.println(pref_ssid);
  59. Serial.println(pref_pass);
  60.  
  61. WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
  62.  
  63. start_wifi_millis = millis();
  64. WiFi.begin(pref_ssid, pref_pass);
  65. while (WiFi.status() != WL_CONNECTED) {
  66. delay(500);
  67. Serial.print(".");
  68. if (millis() - start_wifi_millis > wifi_timeout) {
  69. WiFi.disconnect(true, true);
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75.  
  76. void scan_wifi_networks()
  77. {
  78. WiFi.mode(WIFI_STA);
  79. // WiFi.scanNetworks will return the number of networks found
  80. int n = WiFi.scanNetworks();
  81. if (n == 0) {
  82. SerialBT.println("no networks found");
  83. } else {
  84. SerialBT.println();
  85. SerialBT.print(n);
  86. SerialBT.println(" networks found");
  87. delay(1000);
  88. for (int i = 0; i < n; i++) {
  89. ssids_array[i + 1] = WiFi.SSID(i);
  90. Serial.print(i + 1);
  91. Serial.print(": ");
  92. Serial.println(ssids_array[i + 1]);
  93. network_string = i + 1;
  94. network_string = network_string + ": " + WiFi.SSID(i) + " (Strength:" + WiFi.RSSI(i) + ")";
  95. SerialBT.println(network_string);
  96. }
  97. wifi_stage = SCAN_COMPLETE;
  98. }
  99. }
  100.  
  101. void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
  102. {
  103.  
  104. if (event == ESP_SPP_SRV_OPEN_EVT) {
  105. wifi_stage = SCAN_START;
  106. }
  107.  
  108. if (event == ESP_SPP_DATA_IND_EVT && wifi_stage == SCAN_COMPLETE) { // data from phone is SSID
  109. int client_wifi_ssid_id = SerialBT.readString().toInt();
  110. client_wifi_ssid = ssids_array[client_wifi_ssid_id];
  111. wifi_stage = SSID_ENTERED;
  112. }
  113.  
  114. if (event == ESP_SPP_DATA_IND_EVT && wifi_stage == WAIT_PASS) { // data from phone is password
  115. client_wifi_password = SerialBT.readString();
  116. client_wifi_password.trim();
  117. wifi_stage = PASS_ENTERED;
  118. }
  119.  
  120. }
  121.  
  122. void callback_show_ip(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
  123. {
  124. if (event == ESP_SPP_SRV_OPEN_EVT) {
  125. SerialBT.print("ESP32 IP: ");
  126. SerialBT.println(WiFi.localIP());
  127. bluetooth_disconnect = true;
  128. }
  129. }
  130.  
  131.  
  132.  
  133. void disconnect_bluetooth()
  134. {
  135. delay(1000);
  136. Serial.println("BT stopping");
  137. SerialBT.println("Bluetooth disconnecting...");
  138. delay(1000);
  139. SerialBT.flush();
  140. SerialBT.disconnect();
  141. SerialBT.end();
  142. Serial.println("BT stopped");
  143. delay(1000);
  144. bluetooth_disconnect = false;
  145. }
  146.  
  147. void loop()
  148. {
  149.  
  150. if (bluetooth_disconnect)
  151. {
  152. disconnect_bluetooth();
  153. }
  154.  
  155. switch (wifi_stage)
  156. {
  157. case SCAN_START:
  158. SerialBT.println("Scanning Wi-Fi networks");
  159. Serial.println("Scanning Wi-Fi networks");
  160. scan_wifi_networks();
  161. SerialBT.println("Please enter the number for your Wi-Fi");
  162. wifi_stage = SCAN_COMPLETE;
  163. break;
  164.  
  165. case SSID_ENTERED:
  166. SerialBT.println("Please enter your Wi-Fi password");
  167. Serial.println("Please enter your Wi-Fi password");
  168. wifi_stage = WAIT_PASS;
  169. break;
  170.  
  171. case PASS_ENTERED:
  172. SerialBT.println("Please wait for Wi-Fi connection...");
  173. Serial.println("Please wait for Wi_Fi connection...");
  174. wifi_stage = WAIT_CONNECT;
  175. preferences.putString("pref_ssid", client_wifi_ssid);
  176. preferences.putString("pref_pass", client_wifi_password);
  177. if (init_wifi()) { // Connected to WiFi
  178. connected_string = "ESP32 IP: ";
  179. connected_string = connected_string + WiFi.localIP().toString();
  180. SerialBT.println(connected_string);
  181. Serial.println(connected_string);
  182. bluetooth_disconnect = true;
  183. } else { // try again
  184. wifi_stage = LOGIN_FAILED;
  185. }
  186. break;
  187.  
  188. case LOGIN_FAILED:
  189. SerialBT.println("Wi-Fi connection failed");
  190. Serial.println("Wi-Fi connection failed");
  191. delay(2000);
  192. wifi_stage = SCAN_START;
  193. break;
  194. }
  195.  
  196.  
  197.  
  198. }
RAW Paste Data