ConnectWPA.ino 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. WiFiEsp example: ConnectWPA
  3. This example connects to an encrypted WiFi network using an ESP8266 module.
  4. Then it prints the MAC address of the WiFi shield, the IP address obtained
  5. and other network details.
  6. For more details see: http://yaab-arduino.blogspot.com/p/wifiesp-example-connect.html
  7. */
  8. #include "WiFiEsp.h"
  9. // Emulate Serial1 on pins 6/7 if not present
  10. #ifndef HAVE_HWSERIAL1
  11. #include "SoftwareSerial.h"
  12. SoftwareSerial Serial1(6, 7); // RX, TX
  13. #endif
  14. char ssid[] = "Twim"; // your network SSID (name)
  15. char pass[] = "12345678"; // your network password
  16. int status = WL_IDLE_STATUS; // the Wifi radio's status
  17. void setup()
  18. {
  19. // initialize serial for debugging
  20. Serial.begin(115200);
  21. // initialize serial for ESP module
  22. Serial1.begin(9600);
  23. // initialize ESP module
  24. WiFi.init(&Serial1);
  25. // check for the presence of the shield
  26. if (WiFi.status() == WL_NO_SHIELD) {
  27. Serial.println("WiFi shield not present");
  28. // don't continue
  29. while (true);
  30. }
  31. // attempt to connect to WiFi network
  32. while ( status != WL_CONNECTED) {
  33. Serial.print("Attempting to connect to WPA SSID: ");
  34. Serial.println(ssid);
  35. // Connect to WPA/WPA2 network
  36. status = WiFi.begin(ssid, pass);
  37. }
  38. Serial.println("You're connected to the network");
  39. }
  40. void loop()
  41. {
  42. // print the network connection information every 10 seconds
  43. Serial.println();
  44. printCurrentNet();
  45. printWifiData();
  46. delay(10000);
  47. }
  48. void printWifiData()
  49. {
  50. // print your WiFi shield's IP address
  51. IPAddress ip = WiFi.localIP();
  52. Serial.print("IP Address: ");
  53. Serial.println(ip);
  54. // print your MAC address
  55. byte mac[6];
  56. WiFi.macAddress(mac);
  57. char buf[20];
  58. sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
  59. Serial.print("MAC address: ");
  60. Serial.println(buf);
  61. }
  62. void printCurrentNet()
  63. {
  64. // print the SSID of the network you're attached to
  65. Serial.print("SSID: ");
  66. Serial.println(WiFi.SSID());
  67. // print the MAC address of the router you're attached to
  68. byte bssid[6];
  69. WiFi.BSSID(bssid);
  70. char buf[20];
  71. sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]);
  72. Serial.print("BSSID: ");
  73. Serial.println(buf);
  74. // print the received signal strength
  75. long rssi = WiFi.RSSI();
  76. Serial.print("Signal strength (RSSI): ");
  77. Serial.println(rssi);
  78. }