/*
- This sketch demonstrates how to scan WiFi networks.
- The API is almost the same as with the WiFi Shield library,
- the most obvious difference being the different file you need to include:
/
#include <Arduino.h>
#include "ESP8266WiFi.h"
const char ssid = "xxxxx";
const char* password = "xxxxx";
WiFiServer server(80);
const char* host = "http://pubsub.pubnub.com";
void setup() {
Serial.begin(115200);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
/* Set WiFi to station mode and disconnect from an AP if it was previously connected*/
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup done");
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("scan start");
Serial.println(" ");
//int x = 11;
//int r = 0.5;
double y;
unsigned long before = millis();
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0)
Serial.println("no networks found");
else
{
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i)
{
unsigned long after = millis();
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
//Serial.print(A= WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"dB");
delay(10);
Serial.print("It took ");
Serial.print(after - before);
Serial.println(" ms ");
y= round(WiFi.RSSI(i)/11)*(-0.5);
Serial.print("You are ");
Serial.print(y);
Serial.print(" meters away from ");
Serial.println(WiFi.SSID(i));
String temp = (String)y; //converting value 1 to a string to be able to post it
// We now create a URI for the request
String url = "GET /publish/demo/demo/0/temperature/0/"" + temp + "" HTTP/1.0\r\n\r\n"; // instruction to post my analogread or 44 as a test value
Serial.print("Requesting URL: ");
Serial.println(url);
client.print(url +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
}
Serial.println("");
// Wait a bit before scanning again
delay(9000);
Serial.println("***************************************************************************************************");
}