Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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 = WiFi.scanNetworks();
- if (n == 0){
- Serial.println("no network");
- }
- 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));
- webSocket.broadcastTXT(buffer, sizeof(buffer));
- //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);
- }
- 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);
- 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.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- //WiFi.mode(WIFI_AP);
- //WiFi.softAP(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- 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");
- }
- void loop() {
- server.handleClient();
- webSocket.loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement