#include "oled.h"
ADC_MODE(ADC_VCC);
OLED Display=OLED(D1,D2);
int RawPort = A0;
char server[] = "192.168.1.101";
String uploadfile = "wifi.php";
String postVariable = "WiFi=";
const char* ssid = "MyWiFi";
const char* password = "P@55w0rd";
String total = "";
void setup() {
// Serial.begin(115200);
// Serial.println("");
Display.begin();
Display.setTTYMode(true);
pinMode(LED_BUILTIN, OUTPUT);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Display.print("Battery: ");
Display.print(ESP.getVcc());
// Display.print("\n\rSetup done");
}
void loop() {
// int Batt = ESP.getVcc();
// Serial.print("Battery voltage is: "+Batt);
// Display.print(ESP.getVcc());
// Serial.print("Wifi scan...");
Display.set_contrast(128);
Display.print("\n\rScan...");
int n = WiFi.scanNetworks();
// Serial.println(" done");
Display.println(" done");
if (n == 0) {
// Serial.println("No Networks Found");
Display.printf("No Networks Found");
digitalWrite(LED_BUILTIN, LOW);
}
else {
// Serial.print(n);
// Serial.println(" Networks found");
digitalWrite(LED_BUILTIN, HIGH);
Display.set_contrast(8);
Display.printf("%d networks found",n);
String nr = String(n) + " APs detected. ";
for (int i = 0; i < n; ++i) {
// Serial.print(i + 1);
// Serial.print(": ");
// Serial.print(WiFi.SSID(i));
// Serial.print(" RSSI: ");
// Serial.print(WiFi.RSSI(i));
// Serial.print(" MAC: ");
// Serial.print(WiFi.BSSIDstr(i));
// Serial.print(" Enc: ");
// Serial.println(encType(i));
Display.println();
Display.print(i + 1);
Display.print(": ");
Display.print(WiFi.SSID(i));
Display.print(" (");
Display.print(WiFi.RSSI(i));
Display.println(")");
Display.print(" ");
Display.print(WiFi.BSSIDstr(i));
total = total+String(i+1)+": "+String(WiFi.SSID(i))+" "+String(WiFi.BSSIDstr(i))+" "+String(WiFi.RSSI(i))+"db "+String(encType(i))+". ";
delay(500);
}
WiFi.scanDelete();
delay (500);
Display.println("\n\rSleep for 1min...");
WiFiClient client;
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
String postData = postVariable + nr + total;
// Serial.print("Transmitted data: ");
// Serial.println(postData);
if (client.connect(server, 80)) {
client.print("POST /");
client.print(uploadfile);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println (server);
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(postData.length());
client.println();
client.print(postData);
}
if (client.connected()) {
client.stop();
}
// Serial.println("Sleeping ");
Display.set_power(false);
// delay(10000);
ESP.deepSleep(6e+7); // 60 sec
}
}
// Readable Encryption Type
String encType(int id){
String type;
if(WiFi.encryptionType(id) == ENC_TYPE_WEP){ type=" WEP";
}else if(WiFi.encryptionType(id) == ENC_TYPE_TKIP){ type="WPA-TKIP";
}else if(WiFi.encryptionType(id) == ENC_TYPE_CCMP){ type="WPA2-CCMP";
}else if(WiFi.encryptionType(id) == ENC_TYPE_AUTO){ type="WPA-WPA2";
}else if(WiFi.encryptionType(id) == ENC_TYPE_NONE){ type="OPEN";
}
return type;
}