Takehisa Oneta / ESP8266

Dependencies:   ATParser

Dependents:   ESP8266Interface

Fork of ESP8266 by NetworkSocketAPI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ESP8266.h Source File

ESP8266.h

00001 /* ESP8266Interface Example
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef ESP8266_H
00018 #define ESP8266_H
00019 
00020 #include "ATParser.h"
00021 #include <string>
00022 
00023 /** ESP8266Interface class.
00024     This is an interface to a ESP8266 radio.
00025  */
00026 class ESP8266 
00027 {
00028 public:
00029     ESP8266(PinName tx, PinName rx);
00030     
00031     /**
00032     * Test startup of ESP8266
00033     *
00034     * @return true only if ESP8266 AT system working correctly
00035     */
00036     bool startup(void);
00037     
00038     /**
00039     * Reset ESP8266
00040     *
00041     * @return true only if ESP8266 resets successfully 
00042     */
00043     bool reset(void);
00044     
00045     /**
00046     * Set WiFi mode
00047     *
00048     * @param mode mode of WiFi 1-client, 2-host, 3-both
00049     * @return true only if ESP8266 enables/disables DHCP successfully
00050     */
00051     bool wifiMode(int mode);
00052     
00053     /**
00054     * Enable/Disable multiple connections
00055     *
00056     * @param enabled multiple connections enabled when true
00057     * @return true only if ESP8266 enables/disables multiple connections successfully
00058     */
00059     bool multipleConnections(bool enabled);
00060     
00061     /**
00062     * Enable/Disable DHCP
00063     *
00064     * @param mode mode of DHCP 0-softAP, 1-station, 2-both
00065     * @param enabled DHCP enabled when true
00066     * @return true only if ESP8266 enables/disables DHCP successfully
00067     */
00068     bool dhcp(int mode, bool enabled);
00069     
00070     /**
00071     * Connect ESP8266 to AP
00072     *
00073     * @param ap the name of the AP
00074     * @param passPhrase the password of AP
00075     * @return true only if ESP8266 is connected successfully
00076     */
00077     bool connect(const char *ap, const char *passPhrase);
00078     
00079     /**
00080     * Disconnect ESP8266 from AP
00081     *
00082     * @return true only if ESP8266 is disconnected successfully
00083     */
00084     bool disconnect(void);
00085     
00086     /**
00087     * Get the IP address of ESP8266
00088     *
00089     * @param ip data placeholder for IP address
00090     * @return true only if ESP8266 is assigned an IP address
00091     */
00092     bool getIPAddress(char* ip);
00093     
00094     /**
00095     * Check if ESP8266 is conenected
00096     *
00097     * @return true only if the chip has an IP address
00098     */
00099     bool isConnected(void);
00100     
00101     /**
00102     * Open a socketed connection 
00103     *
00104     * @param sockType the type of socket to open "UDP" or "TCP"
00105     * @param id id to give the new socket, valid 0-4
00106     * @param port port to open connection with
00107     * @param addr the IP address of the destination 
00108     * @return true only if socket opened successfully
00109     */
00110     bool openSocket(string sockType, int id, int port, const char* addr);
00111     
00112     /**
00113     * Sends data to an open socket
00114     *
00115     * @param id id of socket to send to
00116     * @param data data to be sent
00117     * @param amount amount of data to be sent - max 1024
00118     * @return true only if data sent successfully
00119     */
00120     bool sendData(int id, const void *data, uint32_t amount);
00121     
00122     /**
00123     * Receives data from an open socket 
00124     *
00125     * @param data placeholder for returned information
00126     * @param amount number of bytes to be received
00127     * @return the number of bytes actually received
00128     */
00129     uint32_t recv(void *data, uint32_t amount);
00130     
00131     /**
00132     * Closes a socket
00133     *
00134     * @param id id of socket to close, valid only 0-4
00135     * @return true only if socket is closed successfully
00136     */
00137     bool close(int id);
00138     
00139     /**
00140     * Allows timeout to be changed between commands
00141     *
00142     * @param timeout_ms timeout of the connection
00143     */
00144     void setTimeout(uint32_t timeout_ms);
00145     
00146 private:
00147     BufferedSerial serial;
00148     ATParser atParser;
00149 };
00150 
00151 #endif