SHARE
TWEET

websockets

a guest Jul 28th, 2016 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <ESP8266WiFi.h>
  3. #include <WiFiClient.h>
  4. #include <ESP8266WebServer.h>
  5. #include <ESP8266mDNS.h>
  6. #include <WebSocketsServer.h>
  7. #include <Hash.h>
  8. #include <Ticker.h>
  9. #include <FS.h>
  10. #include <ArduinoJson.h>
  11. #include <SPI.h>
  12.  
  13. const char* ssid = ".....";
  14. const char* password = ".....";
  15.  
  16. //text/plain
  17.  
  18. ESP8266WebServer server(80);
  19. WebSocketsServer webSocket = WebSocketsServer(81);
  20. Ticker timer;
  21. int value = 200;
  22.  
  23.  
  24. void scanNet(){
  25.   StaticJsonBuffer<200> jsonBuffer;
  26.   int n = WiFi.scanNetworks();
  27.   if (n == 0){
  28.     Serial.println("no network");
  29.   }
  30.    else
  31.    {
  32.      JsonObject& json = jsonBuffer.createObject();
  33.      json["action"] = "getNet"; // action name
  34.      for (int i = 0; i < n; ++i)
  35.      {
  36.        char buffer[81] = "";
  37.        // Print SSID and RSSI for each network found
  38.        json["ssid"] = WiFi.SSID(i);
  39.        json["rssi"] = WiFi.RSSI(i);
  40.        json["enc"] = WiFi.encryptionType(i);
  41.        json.printTo(buffer, sizeof(buffer));
  42.        webSocket.broadcastTXT(buffer, sizeof(buffer));
  43.        //String strn = String(buffer);
  44.        //webSocket.broadcastTXT(strn);
  45.        //Serial.print(buffer);
  46.     }
  47.   }
  48. }
  49.  
  50.  
  51. void broadcastINT(int *value){
  52.   //
  53.   String string = String(*value);
  54.   //char string[17];
  55.   //itoa(value, string, 10);
  56.   webSocket.broadcastTXT(string);
  57. }
  58.  
  59.  
  60. void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
  61.  
  62.     switch(type) {
  63.         case WStype_DISCONNECTED:
  64.             Serial.printf("[%u] Disconnected!\n", num);
  65.             break;
  66.             timer.detach();
  67.         case WStype_CONNECTED:
  68.             {
  69.                 IPAddress ip = webSocket.remoteIP(num);
  70.                 Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
  71.                 timer.attach_ms(5000, scanNet);
  72.             }
  73.             break;
  74.         case WStype_TEXT:
  75.             Serial.printf("[%u] get Text: %s\n", num, payload);
  76. //            // echo data back to browser
  77.           //broadcastINT(value);
  78.       //webSocket.sendTXT(num, payload, lenght);
  79. //
  80. //            // send data to all connected clients
  81.       //webSocket.broadcastTXT(payload, lenght);
  82.             break;
  83.         case WStype_BIN:
  84.             Serial.printf("[%u] get binary lenght: %u\n", num, lenght);
  85. //            hexdump(payload, lenght);
  86. //
  87. //            // echo data back to browser
  88.       //webSocket.sendBIN(num, payload, lenght);
  89.             break;
  90.     }
  91.  
  92. }
  93.  
  94.  
  95. void sendFile(String name, String type){
  96.      bool result = SPIFFS.begin();
  97.   Serial.println("SPIFFS opened: " + result);
  98.  
  99.   // this opens the file in read-mode
  100.   File f = SPIFFS.open("/" + name, "r");
  101.  
  102.   if (!f) {
  103.     Serial.println("File open failed" + name );
  104.     return;
  105.   }
  106.   server.streamFile(f, type);
  107.   f.close();
  108. }
  109.  
  110. void handleRoot() {
  111.   sendFile("index.html", "text/html");
  112. }
  113.  
  114. void handleAdmin() {
  115.   sendFile("admin.html", "text/html");
  116. }
  117.  
  118. void handleAdminJS (){
  119.     sendFile("admin.js", "application/javascript");
  120. }
  121.  
  122. void handleJquery (){
  123.     sendFile("jquery.js.gz", "application/javascript");
  124. }
  125.  
  126. void handleChart (){
  127.     sendFile("highcharts.js.gz", "application/javascript");
  128. }
  129.  
  130.  
  131. void setup() {
  132.   Serial.begin(115200);
  133.   WiFi.mode(WIFI_STA);
  134.   WiFi.begin(ssid, password);
  135.   //WiFi.mode(WIFI_AP);
  136.   //WiFi.softAP(ssid, password);
  137.   while (WiFi.status() != WL_CONNECTED) {
  138.     delay(500);
  139.     Serial.print(".");
  140.   }
  141.  
  142.   server.on("/", handleRoot);
  143.   server.on("/admin", handleAdmin);
  144.   server.on("/admin.js",handleAdminJS);
  145.   server.on("/jquery.js",handleJquery);
  146.   server.on("/highcharts.js", handleChart);
  147.   //server.on("/", handleJS);
  148.  
  149.   server.begin();
  150.   Serial.println("HTTP server started");
  151.   webSocket.begin();
  152.   webSocket.onEvent(webSocketEvent);
  153.   Serial.println("WebSocket init");
  154. }
  155.  
  156. void loop() {
  157.   server.handleClient();
  158.   webSocket.loop();
  159.  
  160. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top