I'm running this on ESP-12F
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <WebSocketsServer.h>
#include <Hash.h>
#include <Ticker.h>
#include <FS.h>
#include <ArduinoJson.h>
#include <SPI.h>
const char* ssid = "********";
const char* password = "********";
//text/plain
ESP8266WebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);
Ticker timer;
int value = 200;
void scanNet(){
StaticJsonBuffer<200> jsonBuffer;
int n = 0;
n = WiFi.scanNetworks();
if (n == 0){
Serial.println("no networks found");
}
else
{
JsonObject& json = jsonBuffer.createObject();
json["action"] = "getNet"; // action name
for (int i = 0; i < n; ++i)
{
char buffer[81] = "";
// Print SSID and RSSI for each network found
json["ssid"] = WiFi.SSID(i);
json["rssi"] = WiFi.RSSI(i);
json["enc"] = WiFi.encryptionType(i);
json.printTo(buffer, sizeof(buffer));
// cant pass buffer directly to broadcast, server doesnt get any data
//bcos of quatation marks maybe?
String strn = String(buffer);
webSocket.broadcastTXT(strn);
//Serial.print(buffer);
}
}
}
void broadcastINT(int *value){
//
String string = String(*value);
//char string[17];
//itoa(value, string, 10);
webSocket.broadcastTXT(string);
(*value)+=1;
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
switch(type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\n", num);
break;
timer.detach();
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
scanNet();
timer.attach_ms(5000, scanNet);
}
break;
case WStype_TEXT:
Serial.printf("[%u] get Text: %s\n", num, payload);
// // echo data back to browser
//broadcastINT(value);
//webSocket.sendTXT(num, payload, lenght);
//
// // send data to all connected clients
//webSocket.broadcastTXT(payload, lenght);
break;
case WStype_BIN:
Serial.printf("[%u] get binary lenght: %u\n", num, lenght);
// hexdump(payload, lenght);
//
// // echo data back to browser
//webSocket.sendBIN(num, payload, lenght);
break;
}
}
void sendFile(String name, String type){
bool result = SPIFFS.begin();
Serial.println("SPIFFS opened: " + result);
// this opens the file in read-mode
File f = SPIFFS.open("/" + name, "r");
if (!f) {
Serial.println("File open failed" + name );
return;
}
server.streamFile(f, type);
f.close();
}
void handleRoot() {
sendFile("index.html", "text/html");
}
void handleAdmin() {
sendFile("admin.html", "text/html");
}
void handleAdminJS (){
sendFile("admin.js", "application/javascript");
}
void handleJquery (){
sendFile("jquery.js.gz", "application/javascript");
}
void handleChart (){
sendFile("highcharts.js.gz", "application/javascript");
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/admin", handleAdmin);
server.on("/admin.js",handleAdminJS);
server.on("/jquery.js",handleJquery);
server.on("/highcharts.js", handleChart);
//server.on("/", handleJS);
server.begin();
Serial.println("HTTP server started");
webSocket.begin();
webSocket.onEvent(webSocketEvent);
Serial.println("WebSocket init");
// always use this to "mount" the filesyste
}
void loop() {
server.handleClient();
webSocket.loop();
}