Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

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