Fixed UDP Client. Allow interfacing with WebSocketClient. General improvements.

Fork of ESP8266Interface by ESP8266

Files at this revision

API Documentation at this revision

Comitter:
sarahmarshy
Date:
Wed Jun 03 18:21:19 2015 +0000
Parent:
35:22d30e936e4c
Commit message:
-UDP Client working ; -Interfaces with WebSocketClient; -General improvements

Changed in this revision

ESP8266/ESP8266.cpp Show annotated file Show diff for this revision Revisions of this file
ESP8266/ESP8266.h Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketConnection.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketServer.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketServer.h Show annotated file Show diff for this revision Revisions of this file
Socket/UDPSocket.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/ESP8266/ESP8266.cpp	Fri May 01 18:29:38 2015 +0000
+++ b/ESP8266/ESP8266.cpp	Wed Jun 03 18:21:19 2015 +0000
@@ -23,7 +23,7 @@
 #include <algorithm>
 
 //Debug is disabled by default
-#ifdef DEBUG
+#if 1
 #define DBG(x, ...)  printf("[ESP8266 : DBG]"x"\r\n", ##__VA_ARGS__);
 #define WARN(x, ...) printf("[ESP8266 : WARN]"x"\r\n", ##__VA_ARGS__);
 #define ERR(x, ...)  printf("[ESP8266 : ERR]"x"\r\n", ##__VA_ARGS__);
@@ -162,20 +162,49 @@
     }
 }
 
-bool ESP8266::startUDP(char* ip, int port)
+bool ESP8266::startUDP(char* ip, int port, int id, int length)
 {
     char portstr[5];
+    char idstr[1];
+    char lenstr[2];
+    
     sprintf(portstr, "%d", port);
-    sendCommand(( "AT+CIPSTART=\"UDP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000);
-
-    sendCommand("AT+CIPMODE=1", "OK", NULL, 1000);// go into transparent mode
-    sendCommand("AT+CIPSEND", ">", NULL, 1000);// go into transparent mode
+    sprintf(idstr, "%d", id);
+    sprintf(lenstr, "%d", length);
+    
+    sendCommand("AT+CIPMUX=1", "OK", NULL, 1000);
+    sendCommand(( "AT+CIPSTART=" + string(idstr) + ",\"UDP\",\"" + (string) ip + "\"," + (string) portstr + ",1112,0").c_str(), "OK", NULL, 10000);
+    sendCommand(("AT+CIPSEND=" + (string)idstr + "," +  (string)lenstr).c_str(), ">", NULL, 1000);// go into transparent mode
     DBG("Data Mode\r\n");
     state.cmdMode = false;
 
     return true;
 }
 
+bool ESP8266::startTCPServer(int port)
+{
+    bool command_results[3];
+    command_results[0]=sendCommand("AT+CWMODE=3", "OK", NULL, 1000);
+    command_results[1]=sendCommand("AT+CIPMUX=1", "OK", NULL, 1000);
+    if(port == 333){
+        command_results[2]=sendCommand("AT+CIPSERVER=1", "OK", NULL, 1000);
+    }
+    else{
+        char portstr[5];
+        sprintf(portstr, "%d", port);
+        command_results[2]=sendCommand(("AT+CIPSERVER=1," + (string)portstr).c_str(), "OK", NULL, 1000);
+    }
+    //sendCommand("AT+CIFSR", "OK", NULL, 1000);
+    DBG("Data Mode\r\n");
+    state.cmdMode = false;
+    if (command_results[0] and command_results[1] and command_results[2]){
+        return true;
+    }
+    else{
+        return false;
+    }
+}
+
 bool ESP8266::close()
 {
     send("+++",3);
--- a/ESP8266/ESP8266.h	Fri May 01 18:29:38 2015 +0000
+++ b/ESP8266/ESP8266.h	Wed Jun 03 18:21:19 2015 +0000
@@ -92,9 +92,17 @@
     /*
     * Legacy Start for UDP only connection in transparent mode
     * @param ip A string that contains the IP, no quotes
+    * @param id number between 0-4
     * @param port Numerical port number to connect to
+    * @param length number of characters in the message being sent
     */
-    bool startUDP(char* ip, int port);
+    bool startUDP(char* ip, int port, int id, int length);
+
+    /*
+    *Starts the ESP chip as a TCP Server
+    *@param port Numerical port of the server, default is 333
+    */
+    bool startTCPServer(int port = 333);
 
     /**
     * Close a connection
@@ -162,7 +170,7 @@
     * Send a command to the wifi module. Check if the module is in command mode. If not enter in command mode
     *
     * @param str string to be sent
-    * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
+    * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknowledged. (default: "NO")
     * @param res this field will contain the response from the wifi module, result of a command sent. This field is available only if ACK = "NO" AND res != NULL (default: NULL)
     *
     * @return true if successful
--- a/Socket/TCPSocketConnection.cpp	Fri May 01 18:29:38 2015 +0000
+++ b/Socket/TCPSocketConnection.cpp	Wed Jun 03 18:21:19 2015 +0000
@@ -23,7 +23,7 @@
 using std::memcpy;
 
 //Debug is disabled by default
-#ifdef DEBUG
+#if 1
 #define DBG(x, ...)  printf("[TCPConnection : DBG]"x"\r\n", ##__VA_ARGS__);
 #define WARN(x, ...) printf("[TCPConnection: WARN]"x"\r\n", ##__VA_ARGS__);
 #define ERR(x, ...)  printf("[TCPConnection : ERR]"x"\r\n", ##__VA_ARGS__);
@@ -67,7 +67,7 @@
 int TCPSocketConnection::send(char* data, int length)
 {
     if (!_is_connected) {
-        ERR("TCPSocketConnection::receive() - _is_connected is false : you cant receive data untill you connect to a socket!");
+        ERR("TCPSocketConnection::receive() - _is_connected is false : you cant receive data until you connect to a socket!");
         return -1;
     }
     Timer tmr;
@@ -130,7 +130,7 @@
 int TCPSocketConnection::receive(char* buffer, int length)
 {
     if (!_is_connected) {
-        ERR("TCPSocketConnection::receive() - _is_connected is false : you cant receive data untill you connect to a socket!");
+        ERR("TCPSocketConnection::receive() - _is_connected is false : you cant receive data until you connect to a socket!");
         return -1;
     }
     Timer tmr;
@@ -240,5 +240,5 @@
 //        }
 //    }
 //    return readLen;
-    return 0;
+    receive(data,length);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/TCPSocketServer.cpp	Wed Jun 03 18:21:19 2015 +0000
@@ -0,0 +1,44 @@
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include "TCPSocketServer.h"
+ 
+#include <cstring>
+ 
+using std::memset;
+using std::memcpy;
+ 
+TCPSocketServer::TCPSocketServer() {
+    
+}
+ 
+int TCPSocketServer::bind(int port) {
+  if(!wifi->startTCPServer(port)) {
+            return(-1);
+   }
+    _port = port;
+    return 0;
+}
+ 
+int TCPSocketServer::listen(int max) {
+    
+    return 0;
+}
+ 
+int TCPSocketServer::accept(TCPSocketConnection& connection) {
+    return 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/TCPSocketServer.h	Wed Jun 03 18:21:19 2015 +0000
@@ -0,0 +1,55 @@
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#ifndef TCPSOCKETSERVER_H
+#define TCPSOCKETSERVER_H
+ 
+#include "Socket/Socket.h"
+#include "TCPSocketConnection.h"
+ 
+/** TCP Server.
+  */
+class TCPSocketServer : public Socket {
+  friend class TCPSocketConnection;
+  public:
+    /** Instantiate a TCP Server.
+    */
+    TCPSocketServer();
+    
+    /** Bind a socket to a specific port.
+    \param port The port to listen for incoming connections on.
+    \return 0 on success, -1 on failure.
+    */
+    int bind(int port);
+    
+    /** Start listening for incoming connections.
+    \param backlog number of pending connections that can be queued up at any
+                   one time [Default: 1].
+    \return 0 on success, -1 on failure.
+    */
+    int listen(int backlog=1);
+    
+    /** Accept a new connection.
+    \param connection A TCPSocketConnection instance that will handle the incoming connection.
+    \return 0 on success, -1 on failure.
+    */
+    int accept(TCPSocketConnection& connection);
+private:
+    int _port;
+};
+ 
+#endif
\ No newline at end of file
--- a/Socket/UDPSocket.cpp	Fri May 01 18:29:38 2015 +0000
+++ b/Socket/UDPSocket.cpp	Wed Jun 03 18:21:19 2015 +0000
@@ -53,7 +53,8 @@
     // initialize transparent mode if not already done
     if(!endpoint_configured) {
         // initialize UDP (default id of -1 means transparent mode)
-        if(!wifi->start(ESP_UDP_TYPE, remote._ipAddress, remote._port, remote._id)) {
+        //!wifi->start(ESP_UDP_TYPE, remote._ipAddress, remote._port, remote._id
+        if(!wifi->startUDP(remote._ipAddress, remote._port, 0,length)) {
             return(-1);
         }
         endpoint_configured = true;