version 1.6
Dependents: iot_water_monitor_v2
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 "ATCmdParser.h" 00021 00022 /** ESP8266Interface class. 00023 This is an interface to a ESP8266 radio. 00024 */ 00025 class ESP8266 00026 { 00027 public: 00028 ESP8266(PinName tx, PinName rx, bool debug=false); 00029 00030 /** 00031 * Check firmware version of ESP8266 00032 * 00033 * @return integer firmware version or -1 if firmware query command gives outdated response 00034 */ 00035 int get_firmware_version(void); 00036 00037 /** 00038 * Startup the ESP8266 00039 * 00040 * @param mode mode of WIFI 1-client, 2-host, 3-both 00041 * @return true only if ESP8266 was setup correctly 00042 */ 00043 bool startup(int mode); 00044 00045 /** 00046 * Reset ESP8266 00047 * 00048 * @return true only if ESP8266 resets successfully 00049 */ 00050 bool reset(void); 00051 00052 /** 00053 * Enable/Disable DHCP 00054 * 00055 * @param enabled DHCP enabled when true 00056 * @param mode mode of DHCP 0-softAP, 1-station, 2-both 00057 * @return true only if ESP8266 enables/disables DHCP successfully 00058 */ 00059 bool dhcp(bool enabled, int mode); 00060 00061 /** 00062 * Connect ESP8266 to AP 00063 * 00064 * @param ap the name of the AP 00065 * @param passPhrase the password of AP 00066 * @return true only if ESP8266 is connected successfully 00067 */ 00068 bool connect(const char *ap, const char *passPhrase); 00069 00070 /** 00071 * Disconnect ESP8266 from AP 00072 * 00073 * @return true only if ESP8266 is disconnected successfully 00074 */ 00075 bool disconnect(void); 00076 00077 /** 00078 * Get the IP address of ESP8266 00079 * 00080 * @return null-teriminated IP address or null if no IP address is assigned 00081 */ 00082 const char *getIPAddress(void); 00083 00084 /** 00085 * Get the MAC address of ESP8266 00086 * 00087 * @return null-terminated MAC address or null if no MAC address is assigned 00088 */ 00089 const char *getMACAddress(void); 00090 00091 /** Get the local gateway 00092 * 00093 * @return Null-terminated representation of the local gateway 00094 * or null if no network mask has been recieved 00095 */ 00096 const char *getGateway(); 00097 00098 /** Get the local network mask 00099 * 00100 * @return Null-terminated representation of the local network mask 00101 * or null if no network mask has been recieved 00102 */ 00103 const char *getNetmask(); 00104 00105 /* Return RSSI for active connection 00106 * 00107 * @return Measured RSSI 00108 */ 00109 int8_t getRSSI(); 00110 00111 /** 00112 * Check if ESP8266 is conenected 00113 * 00114 * @return true only if the chip has an IP address 00115 */ 00116 bool isConnected(void); 00117 00118 /** Scan for available networks 00119 * 00120 * @param ap Pointer to allocated array to store discovered AP 00121 * @param limit Size of allocated @a res array, or 0 to only count available AP 00122 * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error 00123 * see @a nsapi_error 00124 */ 00125 int scan(WiFiAccessPoint *res, unsigned limit); 00126 00127 /**Perform a dns query 00128 * 00129 * @param name Hostname to resolve 00130 * @param ip Buffer to store IP address 00131 * @return 0 true on success, false on failure 00132 */ 00133 bool dns_lookup(const char *name, char *ip); 00134 00135 /** 00136 * Open a socketed connection 00137 * 00138 * @param type the type of socket to open "UDP" or "TCP" 00139 * @param id id to give the new socket, valid 0-4 00140 * @param port port to open connection with 00141 * @param addr the IP address of the destination 00142 * @return true only if socket opened successfully 00143 */ 00144 bool open(const char *type, int id, const char* addr, int port); 00145 00146 /** 00147 * Sends data to an open socket 00148 * 00149 * @param id id of socket to send to 00150 * @param data data to be sent 00151 * @param amount amount of data to be sent - max 1024 00152 * @return true only if data sent successfully 00153 */ 00154 bool send(int id, const void *data, uint32_t amount); 00155 00156 /** 00157 * Receives data from an open socket 00158 * 00159 * @param id id to receive from 00160 * @param data placeholder for returned information 00161 * @param amount number of bytes to be received 00162 * @return the number of bytes received 00163 */ 00164 int32_t recv(int id, void *data, uint32_t amount); 00165 00166 /** 00167 * Closes a socket 00168 * 00169 * @param id id of socket to close, valid only 0-4 00170 * @return true only if socket is closed successfully 00171 */ 00172 bool close(int id); 00173 00174 /** 00175 * Allows timeout to be changed between commands 00176 * 00177 * @param timeout_ms timeout of the connection 00178 */ 00179 void setTimeout(uint32_t timeout_ms); 00180 00181 /** 00182 * Checks if data is available 00183 */ 00184 bool readable(); 00185 00186 /** 00187 * Checks if data can be written 00188 */ 00189 bool writeable(); 00190 00191 /** 00192 * Attach a function to call whenever network state has changed 00193 * 00194 * @param func A pointer to a void function, or 0 to set as none 00195 */ 00196 void attach(Callback<void()> func); 00197 00198 /** 00199 * Attach a function to call whenever network state has changed 00200 * 00201 * @param obj pointer to the object to call the member function on 00202 * @param method pointer to the member function to call 00203 */ 00204 template <typename T, typename M> 00205 void attach(T *obj, M method) { 00206 attach(Callback<void()>(obj, method)); 00207 } 00208 00209 private: 00210 UARTSerial _serial; 00211 ATCmdParser _parser; 00212 00213 struct packet { 00214 struct packet *next; 00215 int id; 00216 uint32_t len; 00217 // data follows 00218 } *_packets, **_packets_end; 00219 void _packet_handler(); 00220 bool recv_ap(nsapi_wifi_ap_t *ap); 00221 00222 char _ip_buffer[16]; 00223 char _gateway_buffer[16]; 00224 char _netmask_buffer[16]; 00225 char _mac_buffer[18]; 00226 }; 00227 00228 #endif
Generated on Thu Jul 14 2022 22:28:30 by 1.7.2