Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1.  
  2.  
  3. #include <Arduino.h>
  4. #include <SoftwareSerial.h>
  5. #include <ESP8266_Simple.h>
  6.  
  7. // Setup SoftwareSerial to pins (RX, TX)
  8. ESP8266_Simple wifi(8,9);
  9.  
  10. void setup()
  11. {
  12. Serial.begin(115200);
  13. Serial.println("ESP8266 Demo Sketch");
  14. wifi.begin(9600);
  15. // A blank line just for debug formatting
  16. Serial.println();
  17. }
  18.  
  19. void loop()
  20. {
  21. String scan_results;
  22. byte responseCode;
  23. char buffer[350];
  24. // Debug formatting
  25. Serial.println();
  26. Serial.print("Getting Access Points: ");
  27. // Send to scan networks command to the ESP8266
  28. if((responseCode = wifi.sendCommand(F("AT+CWLAP" ), buffer, sizeof(buffer))) == ESP8266_OK)
  29. {
  30. // The command worked:
  31. Serial.println("OK");
  32. Serial.println(buffer);
  33. scan_results = buffer;
  34. int strongest_RSSI;
  35. String strongest_SSID;
  36. // Loop through each line, the start of a line is defined by a + .
  37. for (int line = scan_results.indexOf('+'); line < scan_results.length();)
  38. {
  39. // To retreive the signal strength and SSID the index of multiple ','
  40. // are found and used to only get the usefull parts of the output.
  41. int commaIndex = scan_results.indexOf(',', line);
  42. int secondCommaIndex = scan_results.indexOf(',', commaIndex + 1);
  43. int thirdCommaIndex = scan_results.indexOf(',', secondCommaIndex + 1);
  44. int fourthCommaIndex = scan_results.indexOf(',', thirdCommaIndex + 1);
  45. int fifthCommaIndex = scan_results.indexOf(',', fourthCommaIndex + 1);
  46. String SSID_line = scan_results.substring(commaIndex + 1, secondCommaIndex);
  47. String RSSI_line_noInt = scan_results.substring(secondCommaIndex +1, thirdCommaIndex);
  48. int RSSI_line = RSSI_line_noInt.toInt();
  49. // At the start of the first line, the strongest SSID cannot be encoutered yet so:
  50. if(line == 0){
  51. strongest_RSSI = 0;
  52. strongest_SSID = "No Zones in range";
  53. }
  54. String Network1 = "Network1"
  55. String Network2 = "Network2"
  56. String Network3 = "Network3"
  57. //filter for only our networks
  58. if(SSID_line==Network1 || SSID_line==Network2 || SSID_line==Network3){
  59. // Find the stongest network in the scan.
  60. if ((RSSI_line < strongest_RSSI)){
  61. strongest_RSSI = RSSI_line;
  62. strongest_SSID = SSID_line;
  63. }
  64. }
  65.  
  66. }
  67. // Iterate to the next line.
  68. line = scan_results.indexOf('+', line + 1);
  69. }
  70. // Print results. Later this can be saved to output for vib. motors.
  71. Serial.print(strongest_SSID);
  72. Serial.print(", strength: ");
  73. Serial.print(strongest_RSSI);
  74. }
  75. // Debug formatting
  76. Serial.println();
  77. // Wait before scanning again.
  78. delay(2500);
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement