diff --git a/esp8266/datainterface.cpp b/esp8266/datainterface.cpp deleted file mode 100644 index 39dd9b54..00000000 --- a/esp8266/datainterface.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* - datainterface.cpp - esp8266 configuration class - - Copyright (c) 2014 Luc Lebosse. All rights reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#include "config.h" -#include "datainterface.h" -#include -#include -#include - -//cannot put it in class then cast it as std::function so put outside -void handle_data_interface_root() -{ - data_interface.WebServer.send(200, "text/html", "hello from esp8266 from port 8888 "); -} - -DATAINTERFACE_CLASS::DATAINTERFACE_CLASS (int port):WebServer(port) -{ - //init what will handle "/" - WebServer.on("/",HTTP_GET, handle_data_interface_root); -} - -DATAINTERFACE_CLASS data_interface(8888); - diff --git a/esp8266/datainterface.h b/esp8266/datainterface.h deleted file mode 100644 index cf97a234..00000000 --- a/esp8266/datainterface.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - datainterface.h - esp8266 configuration class - - Copyright (c) 2014 Luc Lebosse. All rights reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#ifndef DATAINTERFACE_h -#define DATAINTERFACE_h -#include -#include -#include -#include - -class DATAINTERFACE_CLASS -{ - public: - DATAINTERFACE_CLASS (int port = 8888); - ESP8266WebServer WebServer; - private: - -}; - -extern DATAINTERFACE_CLASS data_interface; - -#endif diff --git a/esp8266/esp8266.ino b/esp8266/esp8266.ino index 3b67eafa..6ca80d45 100644 --- a/esp8266/esp8266.ino +++ b/esp8266/esp8266.ino @@ -32,7 +32,6 @@ #include "config.h" #include "wifi.h" #include "webinterface.h" -#include "datainterface.h" #include #include #include @@ -42,6 +41,9 @@ extern "C" { #include "user_interface.h" } +#define MAX_SRV_CLIENTS 1 +WiFiServer server(8888); +WiFiClient serverClients[MAX_SRV_CLIENTS]; void setup() { // init : @@ -76,7 +78,8 @@ void setup() { delay(1000); //start interfaces web_interface.WebServer.begin(); - data_interface.WebServer.begin(); + server.begin(); + server.setNoDelay(true); } @@ -89,5 +92,41 @@ void loop() { //web requests web_interface.WebServer.handleClient(); //TODO use a method to handle serial also in class and call it instead of this one -data_interface.WebServer.handleClient(); +uint8_t i; + //check if there are any new clients + if (server.hasClient()){ + for(i = 0; i < MAX_SRV_CLIENTS; i++){ + //find free/disconnected spot + if (!serverClients[i] || !serverClients[i].connected()){ + if(serverClients[i]) serverClients[i].stop(); + serverClients[i] = server.available(); + continue; + } + } + //no free/disconnected spot so reject + WiFiClient serverClient = server.available(); + serverClient.stop(); + } + //check clients for data + for(i = 0; i < MAX_SRV_CLIENTS; i++){ + if (serverClients[i] && serverClients[i].connected()){ + if(serverClients[i].available()){ + //get data from the telnet client and push it to the UART + while(serverClients[i].available()) Serial.write(serverClients[i].read()); + } + } + } + //check UART for data + if(Serial.available()){ + size_t len = Serial.available(); + uint8_t sbuf[len]; + Serial.readBytes(sbuf, len); + //push UART data to all connected telnet clients + for(i = 0; i < MAX_SRV_CLIENTS; i++){ + if (serverClients[i] && serverClients[i].connected()){ + serverClients[i].write(sbuf, len); + delay(1); + } + } + } }