sandbox / ESP8266Interface

Dependencies:   ESP8266

Fork of ESP8266Interface by Christopher Haster

Files at this revision

API Documentation at this revision

Comitter:
sarahmarshy
Date:
Fri Jul 17 23:15:21 2015 +0000
Parent:
17:c3f4095337b8
Child:
19:783c46b13285
Commit message:
ESP8266 driver implemented on top of NetworkSocketAPI

Changed in this revision

ESP8266Interface.cpp Show annotated file Show diff for this revision Revisions of this file
ESP8266Interface.h Show annotated file Show diff for this revision Revisions of this file
WiFiRadioInterface.cpp Show diff for this revision Revisions of this file
WiFiRadioInterface.h Show diff for this revision Revisions of this file
driver.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP8266Interface.cpp	Fri Jul 17 23:15:21 2015 +0000
@@ -0,0 +1,251 @@
+/* ESP8266Interface Example
+ * Copyright (c) 2015 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+ 
+#include "ESP8266Interface.h"
+#include <string>
+
+ESP8266Interface::ESP8266Interface(PinName tx, PinName rx)
+: serial(tx, rx), atParser(&serial)
+{
+    serial.baud(115200);
+    availableID = new int[5];
+    for(int i = 0; i<4; i++){
+        availableID[i] = -1;
+    }
+}
+
+ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, const char *ap, const char *pass_phrase, wifi_security_t security, uint32_t timeout_ms)
+: serial(tx, rx), atParser(&serial)
+{
+
+}
+
+int32_t ESP8266Interface::init(void) 
+{   
+    if(!atParser.command("AT","OK"))
+        return -1;
+    if(!atParser.command("AT+RST", "OK\r\nready"))
+        return -1;
+    if(!atParser.command("AT+CWMODE=3", "OK"))
+        return -1;
+    if(!atParser.command("AT+CIPMUX=1", "OK"))
+        return -1;
+    return 0;
+    
+}
+
+int32_t ESP8266Interface::init(const char *ip, const char *mask, const char *gateway) 
+{
+    return -1;
+}
+
+int32_t ESP8266Interface::connect(uint32_t timeout_ms) 
+{
+    return -1;
+}
+
+int32_t ESP8266Interface::connect(const char *ap, const char *pass_phrase, wifi_security_t security, uint32_t timeout_ms)
+{
+    if(!atParser.command("AT+CWDHCP=2,1","OK"))
+        return -1;
+    string connect_command = "AT+CWJAP=\""+(string)ap+"\",\""+(string)pass_phrase+"\"";
+    atParser.setTimeout(10000);
+    if(!atParser.command(connect_command.c_str(),"OK")){
+        return -1;
+    }
+    return 0;    
+}
+
+int32_t ESP8266Interface::disconnect(void) const
+{
+    return -1;
+}
+
+char *ESP8266Interface::getIPAddress(void)
+{
+    char* ip;
+    if(!atParser.command("AT+CIFSR", "+CIFSR:%s OK", ip))
+        return NULL;
+    return ip;
+}
+
+char *ESP8266Interface::getGateway(void) const
+{
+    return 0;
+}
+
+char *ESP8266Interface::getNetworkMask(void) const
+{
+    return 0;
+}
+
+char *ESP8266Interface::getMACAddress(void) const
+{
+    return 0;
+}
+
+int32_t ESP8266Interface::isConnected(void) 
+{
+    return (getIPAddress()==NULL) ? -1 : 0;
+}
+
+SocketInterface *ESP8266Interface::allocateSocket(socket_protocol_t socketProtocol) 
+{   
+    int id = -1;
+    uuidCounter += 1;
+    //Look through the array of available sockets for an unused ID
+    for(int i=0; i<sizeof(availableID); i++){
+        if (availableID[i] == -1){
+            id = i;   
+            availableID[i] = uuidCounter;
+            break;
+        }
+    }
+    if (id == -1){
+        return NULL;//tried  to allocate more than the maximum 5 sockets   
+    }
+    ESP8266Socket *socket = new ESP8266Socket(uuidCounter, &atParser, socketProtocol, (uint8_t)id);
+    return socket;
+}
+
+int ESP8266Interface::deallocateSocket(SocketInterface *socket) 
+{
+    int id = (int)static_cast<ESP8266Socket*>(socket)->getID();
+    availableID[id] = -1;
+    return id;
+}
+
+ESP8266Socket::ESP8266Socket(uint32_t handle, ATParser *atParser, socket_protocol_t type, uint8_t id)
+: _handle(handle), atParser(atParser), _id(id)
+{
+    SocketInterface::_type = type;
+}
+
+const char *ESP8266Socket::getHostByName(const char *name) const
+{
+    return 0;
+}
+
+void ESP8266Socket::setAddress(const char* addr)
+{
+    _addr = (char*)addr;
+}
+
+void ESP8266Socket::setPort(uint16_t port) 
+{
+     _port = port;
+}
+
+void ESP8266Socket::setAddressPort(const char* addr, uint16_t port)
+{
+    _addr = (char*)addr;
+    _port = port;
+}
+
+const char *ESP8266Socket::getAddress(void) const
+{
+    return (const char*)_addr;
+}
+
+uint16_t ESP8266Socket::getPort(void) const
+{
+    return _port;
+}
+
+
+int32_t ESP8266Socket::bind(uint16_t port) const
+{
+    return -1;
+}
+
+int32_t ESP8266Socket::listen(void) const
+{
+    return -1;
+}
+
+int32_t ESP8266Socket::accept() const
+{
+    return -1;
+}
+
+int32_t ESP8266Socket::open() 
+{
+    char portstr[5];
+    char idstr[2];
+    sprintf(idstr,"%d",_id);
+    sprintf(portstr, "%d", _port);
+
+    string sock_type;
+    if(_type == SOCK_UDP)
+        sock_type = "UDP";
+    else if(_type == SOCK_TCP)
+        sock_type = "TCP";
+    string start_command = "AT+CIPSTART="+(string)idstr+",\""+sock_type+"\",\""+(string)_addr+"\","+(string)portstr;
+    if(!atParser->command(start_command.c_str(),"OK")){
+        return -1;//opening socket not succesful
+    }
+    return 0;
+        
+}
+
+int32_t ESP8266Socket::send(const void *data, uint32_t amount, uint32_t timeout_ms) 
+{
+    char idstr[2];
+    sprintf(idstr,"%d",_id);
+    char lenstr[5];
+    sprintf(lenstr,"%d",(int)amount);
+    
+    atParser->setTimeout((int)timeout_ms);
+    
+    string send_command = "AT+CIPSEND="+(string)idstr+","+(string)lenstr;
+    if(!atParser->send(send_command.c_str())){ 
+        return -1;
+    }
+    atParser->write((char*)data, (int)amount);
+    return 0;
+}
+
+uint32_t ESP8266Socket::recv(void *data, uint32_t amount, uint32_t timeout_ms)
+{
+    atParser->setTimeout((int)timeout_ms);
+    char idstr[2];
+    sprintf(idstr,"%d",_id);
+    string matching = "+IPD,"+(string)idstr+",%d:";
+    int length;
+    if(!atParser->recv(matching.c_str(),&length))
+        return 0;
+    
+    if(!atParser->read((char *)data,length))
+        return 0;
+    
+    return length;   
+}
+
+int32_t ESP8266Socket::close() const
+{
+    char idstr[2];
+    sprintf(idstr,"%d",_id);
+    string close_command = "AT+CIPCLOSE="+(string)idstr;  
+    
+    if(!atParser->command(close_command.c_str(),"OK")){
+        return -1;//opening socket not succesful
+    }
+    return 0;
+}
+uint8_t ESP8266Socket::getID()
+{
+    return _id;   
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP8266Interface.h	Fri Jul 17 23:15:21 2015 +0000
@@ -0,0 +1,82 @@
+/* ESP8266Interface Example
+ * Copyright (c) 2015 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ESP8266INTERFACE_H
+#define ESP8266INTERFACE_H
+
+#include "WiFiInterface.h"
+#include "ATParser.h"
+
+/** ESP8266Socket class.
+    This is a ESP8266 implementation of a socket that implements the SocketInterface class.
+    This mock ESP8266 hardware uses AT commands, so an ATParser is used to communicate with the hardware over serial.
+    In a non-AT command set, ATParser could be replaced by RawSerial or another hardware communication interface.
+ */
+ 
+class ESP8266Socket : public SocketInterface
+{
+public:
+    ESP8266Socket(uint32_t handle, ATParser* atParser, socket_protocol_t type, uint8_t id);
+    virtual const char *getHostByName(const char *name) const;
+    virtual void setAddress(const char* addr) ;
+    virtual void setPort(uint16_t port) ;
+    virtual void setAddressPort(const char* addr, uint16_t port);
+    virtual const char *getAddress(void) const;
+    virtual uint16_t getPort(void) const;
+    virtual int32_t bind(uint16_t port) const;
+    virtual int32_t listen(void) const;
+    virtual int32_t accept() const;
+    virtual int32_t open() ;
+    virtual int32_t send(const void *data, uint32_t amount, uint32_t timeout_ms = 15000) ;
+    virtual uint32_t recv(void *data, uint32_t amount, uint32_t timeout_ms = 15000) ;
+    virtual int32_t close() const;
+    uint8_t getID();
+    void handleRecieve();
+protected:
+    uint32_t _handle;
+    uint8_t _id;    
+private:
+    ATParser *atParser;
+};
+
+/** ESP8266Interface class.
+    This is an interface to a ESP8266 radio.
+ */
+class ESP8266Interface : public WiFiInterface
+{
+public:
+    ESP8266Interface(PinName tx, PinName rx);
+    ESP8266Interface(PinName tx, PinName rx, const char *ap, const char *pass_phrase = 0, wifi_security_t security = WI_NONE, uint32_t timeout_ms = 15000);
+    virtual int32_t init(void);
+    virtual int32_t init(const char *ip, const char *mask, const char *gateway);
+    virtual int32_t connect(uint32_t timeout_ms) ;
+    virtual int32_t connect(const char *ap, const char *pass_phrase = 0, wifi_security_t security = WI_NONE, uint32_t timeout_ms = 15000);
+    virtual int32_t disconnect(void) const;
+    virtual char *getIPAddress(void) ;
+    virtual char *getGateway(void) const;
+    virtual char *getNetworkMask(void) const;
+    virtual char *getMACAddress(void) const;
+    virtual int32_t isConnected(void) ;
+    virtual SocketInterface *allocateSocket(socket_protocol_t socketProtocol) ;
+    virtual int deallocateSocket(SocketInterface *socket) ;
+    
+private:
+    BufferedSerial serial;
+    ATParser atParser;
+    int *availableID;
+};
+
+#endif
--- a/WiFiRadioInterface.cpp	Thu Jul 16 05:19:34 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,161 +0,0 @@
-/* WiFiRadioInterface Example
- * Copyright (c) 2015 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- 
-#include "WiFiRadioInterface.h"
-
-WiFiRadioInterface::WiFiRadioInterface(PinName tx, PinName rx)
-: serial(tx, rx), atParser(&serial)
-{
-
-}
-
-WiFiRadioInterface::WiFiRadioInterface(PinName tx, PinName rx, const char *ap, const char *pass_phrase, wifi_security_t security, uint32_t timeout_ms)
-: serial(tx, rx), atParser(&serial)
-{
-
-}
-
-int32_t WiFiRadioInterface::init(void) const
-{
-    return -1;
-}
-
-int32_t WiFiRadioInterface::init(const char *ip, const char *mask, const char *gateway) const
-{
-    return -1;
-}
-
-int32_t WiFiRadioInterface::connect(uint32_t timeout_ms) const
-{
-    return -1;
-}
-
-int32_t WiFiRadioInterface::connect(const char *ap, const char *pass_phrase, wifi_security_t security, uint32_t timeout_ms) const
-{
-    return -1;
-}
-
-int32_t WiFiRadioInterface::disconnect(void) const
-{
-    return -1;
-}
-
-char *WiFiRadioInterface::getIPAddress(void) const
-{
-    return 0;
-}
-
-char *WiFiRadioInterface::getGateway(void) const
-{
-    return 0;
-}
-
-char *WiFiRadioInterface::getNetworkMask(void) const
-{
-    return 0;
-}
-
-char *WiFiRadioInterface::getMACAddress(void) const
-{
-    return 0;
-}
-
-int32_t WiFiRadioInterface::isConnected(void) const
-{
-    return -1;
-}
-
-SocketInterface *WiFiRadioInterface::allocateSocket(socket_protocol_t socketProtocol) const
-{
-    return 0;
-}
-
-int WiFiRadioInterface::deallocateSocket(SocketInterface *socket) const
-{
-    return 0;
-}
-
-WiFiSocket::WiFiSocket(uint32_t handle, ATParser *atParser)
-: handle(handle), atParser(atParser)
-{
-    return;
-}
-
-const char *WiFiSocket::getHostByName(const char *name) const
-{
-    return 0;
-}
-
-int32_t WiFiSocket::setAddress(const char* addr) const
-{
-    return -1;
-}
-
-int32_t WiFiSocket::setPort(uint16_t port) const
-{
-    return -1;
-}
-
-int32_t WiFiSocket::setAddressPort(const char* addr, uint16_t port) const
-{
-    return -1;
-}
-
-const char *WiFiSocket::getAddress(void) const
-{
-    return 0;
-}
-
-uint16_t WiFiSocket::getPort(void) const
-{
-    return 0;
-}
-
-
-int32_t WiFiSocket::bind(uint16_t port) const
-{
-    return -1;
-}
-
-int32_t WiFiSocket::listen(void) const
-{
-    return -1;
-}
-
-int32_t WiFiSocket::accept() const
-{
-    return -1;
-}
-
-int32_t WiFiSocket::open() const
-{
-    return -1;
-}
-
-int32_t WiFiSocket::send(const void *data, uint32_t amount, uint32_t timeout_ms) const
-{
-    return -1;
-}
-
-uint32_t WiFiSocket::recv(const void *data, uint32_t amount, uint32_t timeout_ms) const
-{
-    return 0;
-}
-
-int32_t WiFiSocket::close() const
-{
-    return -1;
-}
\ No newline at end of file
--- a/WiFiRadioInterface.h	Thu Jul 16 05:19:34 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,81 +0,0 @@
-/* WiFiRadioInterface Example
- * Copyright (c) 2015 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef WIFIRADIOINTERFACE_H
-#define WIFIRADIOINTERFACE_H
-
-#include "WiFiInterface.h"
-#include "ATParser.h"
-
-/** WiFiSocket class.
-    This is a WiFi implementation of a socket that implements the SocketInterface class.
-    This mock WiFi hardware uses AT commands, so an ATParser is used to communicate with the hardware over serial.
-    In a non-AT command set, ATParser could be replaced by RawSerial or another hardware communication interface.
- */
- 
-class WiFiSocket : public SocketInterface
-{
-public:
-    WiFiSocket(uint32_t handle, ATParser* atParser);
-    virtual const char *getHostByName(const char *name) const;
-    virtual int32_t setAddress(const char* addr) const;
-    virtual int32_t setPort(uint16_t port) const;
-    virtual int32_t setAddressPort(const char* addr, uint16_t port) const;
-    virtual const char *getAddress(void) const;
-    virtual uint16_t getPort(void) const;
-    virtual int32_t bind(uint16_t port) const;
-    virtual int32_t listen(void) const;
-    virtual int32_t accept() const;
-    virtual int32_t open() const;
-    virtual int32_t send(const void *data, uint32_t amount, uint32_t timeout_ms = 15000) const;
-    virtual uint32_t recv(const void *data, uint32_t amount, uint32_t timeout_ms = 15000) const;
-    virtual int32_t close() const;
-
-protected:
-    uint32_t handle;
-
-private:
-    ATParser *atParser;
-   
-};
-
-/** WiFiRadioInterface class.
-    This is an interface to a WiFi radio.
- */
-class WiFiRadioInterface : public WiFiInterface
-{
-public:
-    WiFiRadioInterface(PinName tx, PinName rx);
-    WiFiRadioInterface(PinName tx, PinName rx, const char *ap, const char *pass_phrase = 0, wifi_security_t security = WI_NONE, uint32_t timeout_ms = 15000);
-    virtual int32_t init(void) const;
-    virtual int32_t init(const char *ip, const char *mask, const char *gateway) const;
-    virtual int32_t connect(uint32_t timeout_ms) const;
-    virtual int32_t connect(const char *ap, const char *pass_phrase = 0, wifi_security_t security = WI_NONE, uint32_t timeout_ms = 15000) const;
-    virtual int32_t disconnect(void) const;
-    virtual char *getIPAddress(void) const;
-    virtual char *getGateway(void) const;
-    virtual char *getNetworkMask(void) const;
-    virtual char *getMACAddress(void) const;
-    virtual int32_t isConnected(void) const;
-    virtual SocketInterface *allocateSocket(socket_protocol_t socketProtocol) const;
-    virtual int deallocateSocket(SocketInterface *socket) const;
-    
-private:
-    RawSerial serial;
-    ATParser atParser;
-};
-
-#endif
--- a/driver.lib	Thu Jul 16 05:19:34 2015 +0000
+++ b/driver.lib	Fri Jul 17 23:15:21 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/NetworkSocketAPI/code/driver/#a517950927fe
+http://developer.mbed.org/teams/NetworkSocketAPI/code/driver/#0c084df39bda