Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <SoftwareSerial.h>
- #include <ESP8266_Simple.h>
- // Setup SoftwareSerial to pins (RX, TX)
- ESP8266_Simple wifi(8,9);
- void setup()
- {
- Serial.begin(115200);
- Serial.println("ESP8266 Demo Sketch");
- wifi.begin(9600);
- // A blank line just for debug formatting
- Serial.println();
- }
- void loop()
- {
- String scan_results;
- byte responseCode;
- char buffer[350];
- // Debug formatting
- Serial.println();
- Serial.print("Getting Access Points: ");
- // Send to scan networks command to the ESP8266
- if((responseCode = wifi.sendCommand(F("AT+CWLAP" ), buffer, sizeof(buffer))) == ESP8266_OK)
- {
- // The command worked:
- Serial.println("OK");
- Serial.println(buffer);
- scan_results = buffer;
- int strongest_RSSI;
- String strongest_SSID;
- // Loop through each line, the start of a line is defined by a + .
- for (int line = scan_results.indexOf('+'); line < scan_results.length();)
- {
- // To retreive the signal strength and SSID the index of multiple ','
- // are found and used to only get the usefull parts of the output.
- int commaIndex = scan_results.indexOf(',', line);
- int secondCommaIndex = scan_results.indexOf(',', commaIndex + 1);
- int thirdCommaIndex = scan_results.indexOf(',', secondCommaIndex + 1);
- int fourthCommaIndex = scan_results.indexOf(',', thirdCommaIndex + 1);
- int fifthCommaIndex = scan_results.indexOf(',', fourthCommaIndex + 1);
- String SSID_line = scan_results.substring(commaIndex + 1, secondCommaIndex);
- String RSSI_line_noInt = scan_results.substring(secondCommaIndex +1, thirdCommaIndex);
- int RSSI_line = RSSI_line_noInt.toInt();
- // At the start of the first line, the strongest SSID cannot be encoutered yet so:
- if(line == 0){
- strongest_RSSI = 0;
- strongest_SSID = "No Zones in range";
- }
- String Network1 = "Network1"
- String Network2 = "Network2"
- String Network3 = "Network3"
- //filter for only our networks
- if(SSID_line==Network1 || SSID_line==Network2 || SSID_line==Network3){
- // Find the stongest network in the scan.
- if ((RSSI_line < strongest_RSSI)){
- strongest_RSSI = RSSI_line;
- strongest_SSID = SSID_line;
- }
- }
- }
- // Iterate to the next line.
- line = scan_results.indexOf('+', line + 1);
- }
- // Print results. Later this can be saved to output for vib. motors.
- Serial.print(strongest_SSID);
- Serial.print(", strength: ");
- Serial.print(strongest_RSSI);
- }
- // Debug formatting
- Serial.println();
- // Wait before scanning again.
- delay(2500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement