The driver for the ESP8266 WiFi module - isConnected function added, working and tested with mbed os 5.7.6

Fork of esp8266-driver by ESP8266

Committer:
mareszym
Date:
Sun Nov 04 12:50:49 2018 +0000
Revision:
2:0bc0b654995c
Parent:
0:6946b0b9e323
isConnected function added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-ESP8266 0:6946b0b9e323 1 /* ESP8266Interface Example
group-ESP8266 0:6946b0b9e323 2 * Copyright (c) 2015 ARM Limited
group-ESP8266 0:6946b0b9e323 3 *
group-ESP8266 0:6946b0b9e323 4 * Licensed under the Apache License, Version 2.0 (the "License");
group-ESP8266 0:6946b0b9e323 5 * you may not use this file except in compliance with the License.
group-ESP8266 0:6946b0b9e323 6 * You may obtain a copy of the License at
group-ESP8266 0:6946b0b9e323 7 *
group-ESP8266 0:6946b0b9e323 8 * http://www.apache.org/licenses/LICENSE-2.0
group-ESP8266 0:6946b0b9e323 9 *
group-ESP8266 0:6946b0b9e323 10 * Unless required by applicable law or agreed to in writing, software
group-ESP8266 0:6946b0b9e323 11 * distributed under the License is distributed on an "AS IS" BASIS,
group-ESP8266 0:6946b0b9e323 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
group-ESP8266 0:6946b0b9e323 13 * See the License for the specific language governing permissions and
group-ESP8266 0:6946b0b9e323 14 * limitations under the License.
group-ESP8266 0:6946b0b9e323 15 */
group-ESP8266 0:6946b0b9e323 16
group-ESP8266 0:6946b0b9e323 17 #ifndef ESP8266_H
group-ESP8266 0:6946b0b9e323 18 #define ESP8266_H
group-ESP8266 0:6946b0b9e323 19
group-ESP8266 0:6946b0b9e323 20 #include "ATParser.h"
group-ESP8266 0:6946b0b9e323 21
group-ESP8266 0:6946b0b9e323 22 /** ESP8266Interface class.
group-ESP8266 0:6946b0b9e323 23 This is an interface to a ESP8266 radio.
group-ESP8266 0:6946b0b9e323 24 */
group-ESP8266 0:6946b0b9e323 25 class ESP8266
group-ESP8266 0:6946b0b9e323 26 {
group-ESP8266 0:6946b0b9e323 27 public:
group-ESP8266 0:6946b0b9e323 28 ESP8266(PinName tx, PinName rx, bool debug=false);
group-ESP8266 0:6946b0b9e323 29
group-ESP8266 0:6946b0b9e323 30 /**
group-ESP8266 0:6946b0b9e323 31 * Startup the ESP8266
group-ESP8266 0:6946b0b9e323 32 *
group-ESP8266 0:6946b0b9e323 33 * @param mode mode of WIFI 1-client, 2-host, 3-both
group-ESP8266 0:6946b0b9e323 34 * @return true only if ESP8266 was setup correctly
group-ESP8266 0:6946b0b9e323 35 */
group-ESP8266 0:6946b0b9e323 36 bool startup(int mode);
group-ESP8266 0:6946b0b9e323 37
group-ESP8266 0:6946b0b9e323 38 /**
group-ESP8266 0:6946b0b9e323 39 * Reset ESP8266
group-ESP8266 0:6946b0b9e323 40 *
group-ESP8266 0:6946b0b9e323 41 * @return true only if ESP8266 resets successfully
group-ESP8266 0:6946b0b9e323 42 */
group-ESP8266 0:6946b0b9e323 43 bool reset(void);
group-ESP8266 0:6946b0b9e323 44
group-ESP8266 0:6946b0b9e323 45 /**
group-ESP8266 0:6946b0b9e323 46 * Enable/Disable DHCP
group-ESP8266 0:6946b0b9e323 47 *
group-ESP8266 0:6946b0b9e323 48 * @param enabled DHCP enabled when true
group-ESP8266 0:6946b0b9e323 49 * @param mode mode of DHCP 0-softAP, 1-station, 2-both
group-ESP8266 0:6946b0b9e323 50 * @return true only if ESP8266 enables/disables DHCP successfully
group-ESP8266 0:6946b0b9e323 51 */
group-ESP8266 0:6946b0b9e323 52 bool dhcp(bool enabled, int mode);
group-ESP8266 0:6946b0b9e323 53
group-ESP8266 0:6946b0b9e323 54 /**
group-ESP8266 0:6946b0b9e323 55 * Connect ESP8266 to AP
group-ESP8266 0:6946b0b9e323 56 *
group-ESP8266 0:6946b0b9e323 57 * @param ap the name of the AP
group-ESP8266 0:6946b0b9e323 58 * @param passPhrase the password of AP
group-ESP8266 0:6946b0b9e323 59 * @return true only if ESP8266 is connected successfully
group-ESP8266 0:6946b0b9e323 60 */
group-ESP8266 0:6946b0b9e323 61 bool connect(const char *ap, const char *passPhrase);
group-ESP8266 0:6946b0b9e323 62
group-ESP8266 0:6946b0b9e323 63 /**
group-ESP8266 0:6946b0b9e323 64 * Disconnect ESP8266 from AP
group-ESP8266 0:6946b0b9e323 65 *
group-ESP8266 0:6946b0b9e323 66 * @return true only if ESP8266 is disconnected successfully
group-ESP8266 0:6946b0b9e323 67 */
group-ESP8266 0:6946b0b9e323 68 bool disconnect(void);
group-ESP8266 0:6946b0b9e323 69
group-ESP8266 0:6946b0b9e323 70 /**
group-ESP8266 0:6946b0b9e323 71 * Get the IP address of ESP8266
group-ESP8266 0:6946b0b9e323 72 *
group-ESP8266 0:6946b0b9e323 73 * @return null-teriminated IP address or null if no IP address is assigned
group-ESP8266 0:6946b0b9e323 74 */
group-ESP8266 0:6946b0b9e323 75 const char *getIPAddress(void);
group-ESP8266 0:6946b0b9e323 76
group-ESP8266 0:6946b0b9e323 77 /**
group-ESP8266 0:6946b0b9e323 78 * Get the MAC address of ESP8266
group-ESP8266 0:6946b0b9e323 79 *
group-ESP8266 0:6946b0b9e323 80 * @return null-terminated MAC address or null if no MAC address is assigned
group-ESP8266 0:6946b0b9e323 81 */
group-ESP8266 0:6946b0b9e323 82 const char *getMACAddress(void);
group-ESP8266 0:6946b0b9e323 83
group-ESP8266 0:6946b0b9e323 84 /** Get the local gateway
group-ESP8266 0:6946b0b9e323 85 *
group-ESP8266 0:6946b0b9e323 86 * @return Null-terminated representation of the local gateway
group-ESP8266 0:6946b0b9e323 87 * or null if no network mask has been recieved
group-ESP8266 0:6946b0b9e323 88 */
group-ESP8266 0:6946b0b9e323 89 const char *getGateway();
group-ESP8266 0:6946b0b9e323 90
group-ESP8266 0:6946b0b9e323 91 /** Get the local network mask
group-ESP8266 0:6946b0b9e323 92 *
group-ESP8266 0:6946b0b9e323 93 * @return Null-terminated representation of the local network mask
group-ESP8266 0:6946b0b9e323 94 * or null if no network mask has been recieved
group-ESP8266 0:6946b0b9e323 95 */
group-ESP8266 0:6946b0b9e323 96 const char *getNetmask();
group-ESP8266 0:6946b0b9e323 97
group-ESP8266 0:6946b0b9e323 98 /* Return RSSI for active connection
group-ESP8266 0:6946b0b9e323 99 *
group-ESP8266 0:6946b0b9e323 100 * @return Measured RSSI
group-ESP8266 0:6946b0b9e323 101 */
group-ESP8266 0:6946b0b9e323 102 int8_t getRSSI();
group-ESP8266 0:6946b0b9e323 103
group-ESP8266 0:6946b0b9e323 104 /**
group-ESP8266 0:6946b0b9e323 105 * Check if ESP8266 is conenected
group-ESP8266 0:6946b0b9e323 106 *
group-ESP8266 0:6946b0b9e323 107 * @return true only if the chip has an IP address
group-ESP8266 0:6946b0b9e323 108 */
group-ESP8266 0:6946b0b9e323 109 bool isConnected(void);
group-ESP8266 0:6946b0b9e323 110
group-ESP8266 0:6946b0b9e323 111 /** Scan for available networks
group-ESP8266 0:6946b0b9e323 112 *
group-ESP8266 0:6946b0b9e323 113 * @param ap Pointer to allocated array to store discovered AP
group-ESP8266 0:6946b0b9e323 114 * @param limit Size of allocated @a res array, or 0 to only count available AP
group-ESP8266 0:6946b0b9e323 115 * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
group-ESP8266 0:6946b0b9e323 116 * see @a nsapi_error
group-ESP8266 0:6946b0b9e323 117 */
group-ESP8266 0:6946b0b9e323 118 int scan(WiFiAccessPoint *res, unsigned limit);
group-ESP8266 0:6946b0b9e323 119
group-ESP8266 0:6946b0b9e323 120 /**
group-ESP8266 0:6946b0b9e323 121 * Open a socketed connection
group-ESP8266 0:6946b0b9e323 122 *
group-ESP8266 0:6946b0b9e323 123 * @param type the type of socket to open "UDP" or "TCP"
group-ESP8266 0:6946b0b9e323 124 * @param id id to give the new socket, valid 0-4
group-ESP8266 0:6946b0b9e323 125 * @param port port to open connection with
group-ESP8266 0:6946b0b9e323 126 * @param addr the IP address of the destination
group-ESP8266 0:6946b0b9e323 127 * @return true only if socket opened successfully
group-ESP8266 0:6946b0b9e323 128 */
group-ESP8266 0:6946b0b9e323 129 bool open(const char *type, int id, const char* addr, int port);
group-ESP8266 0:6946b0b9e323 130
group-ESP8266 0:6946b0b9e323 131 /**
group-ESP8266 0:6946b0b9e323 132 * Sends data to an open socket
group-ESP8266 0:6946b0b9e323 133 *
group-ESP8266 0:6946b0b9e323 134 * @param id id of socket to send to
group-ESP8266 0:6946b0b9e323 135 * @param data data to be sent
group-ESP8266 0:6946b0b9e323 136 * @param amount amount of data to be sent - max 1024
group-ESP8266 0:6946b0b9e323 137 * @return true only if data sent successfully
group-ESP8266 0:6946b0b9e323 138 */
group-ESP8266 0:6946b0b9e323 139 bool send(int id, const void *data, uint32_t amount);
group-ESP8266 0:6946b0b9e323 140
group-ESP8266 0:6946b0b9e323 141 /**
group-ESP8266 0:6946b0b9e323 142 * Receives data from an open socket
group-ESP8266 0:6946b0b9e323 143 *
group-ESP8266 0:6946b0b9e323 144 * @param id id to receive from
group-ESP8266 0:6946b0b9e323 145 * @param data placeholder for returned information
group-ESP8266 0:6946b0b9e323 146 * @param amount number of bytes to be received
group-ESP8266 0:6946b0b9e323 147 * @return the number of bytes received
group-ESP8266 0:6946b0b9e323 148 */
group-ESP8266 0:6946b0b9e323 149 int32_t recv(int id, void *data, uint32_t amount);
group-ESP8266 0:6946b0b9e323 150
group-ESP8266 0:6946b0b9e323 151 /**
group-ESP8266 0:6946b0b9e323 152 * Closes a socket
group-ESP8266 0:6946b0b9e323 153 *
group-ESP8266 0:6946b0b9e323 154 * @param id id of socket to close, valid only 0-4
group-ESP8266 0:6946b0b9e323 155 * @return true only if socket is closed successfully
group-ESP8266 0:6946b0b9e323 156 */
group-ESP8266 0:6946b0b9e323 157 bool close(int id);
group-ESP8266 0:6946b0b9e323 158
group-ESP8266 0:6946b0b9e323 159 /**
group-ESP8266 0:6946b0b9e323 160 * Allows timeout to be changed between commands
group-ESP8266 0:6946b0b9e323 161 *
group-ESP8266 0:6946b0b9e323 162 * @param timeout_ms timeout of the connection
group-ESP8266 0:6946b0b9e323 163 */
group-ESP8266 0:6946b0b9e323 164 void setTimeout(uint32_t timeout_ms);
group-ESP8266 0:6946b0b9e323 165
group-ESP8266 0:6946b0b9e323 166 /**
group-ESP8266 0:6946b0b9e323 167 * Checks if data is available
group-ESP8266 0:6946b0b9e323 168 */
group-ESP8266 0:6946b0b9e323 169 bool readable();
group-ESP8266 0:6946b0b9e323 170
group-ESP8266 0:6946b0b9e323 171 /**
group-ESP8266 0:6946b0b9e323 172 * Checks if data can be written
group-ESP8266 0:6946b0b9e323 173 */
group-ESP8266 0:6946b0b9e323 174 bool writeable();
group-ESP8266 0:6946b0b9e323 175
group-ESP8266 0:6946b0b9e323 176 /**
group-ESP8266 0:6946b0b9e323 177 * Attach a function to call whenever network state has changed
group-ESP8266 0:6946b0b9e323 178 *
group-ESP8266 0:6946b0b9e323 179 * @param func A pointer to a void function, or 0 to set as none
group-ESP8266 0:6946b0b9e323 180 */
group-ESP8266 0:6946b0b9e323 181 void attach(Callback<void()> func);
group-ESP8266 0:6946b0b9e323 182
group-ESP8266 0:6946b0b9e323 183 /**
group-ESP8266 0:6946b0b9e323 184 * Attach a function to call whenever network state has changed
group-ESP8266 0:6946b0b9e323 185 *
group-ESP8266 0:6946b0b9e323 186 * @param obj pointer to the object to call the member function on
group-ESP8266 0:6946b0b9e323 187 * @param method pointer to the member function to call
group-ESP8266 0:6946b0b9e323 188 */
group-ESP8266 0:6946b0b9e323 189 template <typename T, typename M>
group-ESP8266 0:6946b0b9e323 190 void attach(T *obj, M method) {
group-ESP8266 0:6946b0b9e323 191 attach(Callback<void()>(obj, method));
group-ESP8266 0:6946b0b9e323 192 }
group-ESP8266 0:6946b0b9e323 193
group-ESP8266 0:6946b0b9e323 194 private:
group-ESP8266 0:6946b0b9e323 195 BufferedSerial _serial;
group-ESP8266 0:6946b0b9e323 196 ATParser _parser;
group-ESP8266 0:6946b0b9e323 197
group-ESP8266 0:6946b0b9e323 198 struct packet {
group-ESP8266 0:6946b0b9e323 199 struct packet *next;
group-ESP8266 0:6946b0b9e323 200 int id;
group-ESP8266 0:6946b0b9e323 201 uint32_t len;
group-ESP8266 0:6946b0b9e323 202 // data follows
group-ESP8266 0:6946b0b9e323 203 } *_packets, **_packets_end;
group-ESP8266 0:6946b0b9e323 204 void _packet_handler();
group-ESP8266 0:6946b0b9e323 205 bool recv_ap(nsapi_wifi_ap_t *ap);
group-ESP8266 0:6946b0b9e323 206
group-ESP8266 0:6946b0b9e323 207 char _ip_buffer[16];
group-ESP8266 0:6946b0b9e323 208 char _gateway_buffer[16];
group-ESP8266 0:6946b0b9e323 209 char _netmask_buffer[16];
group-ESP8266 0:6946b0b9e323 210 char _mac_buffer[18];
group-ESP8266 0:6946b0b9e323 211 };
group-ESP8266 0:6946b0b9e323 212
group-ESP8266 0:6946b0b9e323 213 #endif