hellomqttt to thingspeak mqtt and ifttt

Dependencies:   Servo MQTTPacket FP

Committer:
jasonberry
Date:
Wed May 05 14:48:01 2021 +0000
Revision:
25:ca1b1098c77f
TEST

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jasonberry 25:ca1b1098c77f 1 /* ESP8266 implementation of NetworkInterfaceAPI
jasonberry 25:ca1b1098c77f 2 * Copyright (c) 2015 ARM Limited
jasonberry 25:ca1b1098c77f 3 *
jasonberry 25:ca1b1098c77f 4 * Licensed under the Apache License, Version 2.0 (the "License");
jasonberry 25:ca1b1098c77f 5 * you may not use this file except in compliance with the License.
jasonberry 25:ca1b1098c77f 6 * You may obtain a copy of the License at
jasonberry 25:ca1b1098c77f 7 *
jasonberry 25:ca1b1098c77f 8 * http://www.apache.org/licenses/LICENSE-2.0
jasonberry 25:ca1b1098c77f 9 *
jasonberry 25:ca1b1098c77f 10 * Unless required by applicable law or agreed to in writing, software
jasonberry 25:ca1b1098c77f 11 * distributed under the License is distributed on an "AS IS" BASIS,
jasonberry 25:ca1b1098c77f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jasonberry 25:ca1b1098c77f 13 * See the License for the specific language governing permissions and
jasonberry 25:ca1b1098c77f 14 * limitations under the License.
jasonberry 25:ca1b1098c77f 15 */
jasonberry 25:ca1b1098c77f 16
jasonberry 25:ca1b1098c77f 17 #ifndef ESP8266_INTERFACE_H
jasonberry 25:ca1b1098c77f 18 #define ESP8266_INTERFACE_H
jasonberry 25:ca1b1098c77f 19
jasonberry 25:ca1b1098c77f 20 #include "mbed.h"
jasonberry 25:ca1b1098c77f 21 #include "ESP8266.h"
jasonberry 25:ca1b1098c77f 22
jasonberry 25:ca1b1098c77f 23
jasonberry 25:ca1b1098c77f 24 #define ESP8266_SOCKET_COUNT 5
jasonberry 25:ca1b1098c77f 25
jasonberry 25:ca1b1098c77f 26 /** ESP8266Interface class
jasonberry 25:ca1b1098c77f 27 * Implementation of the NetworkStack for the ESP8266
jasonberry 25:ca1b1098c77f 28 */
jasonberry 25:ca1b1098c77f 29 class ESP8266Interface : public NetworkStack, public WiFiInterface
jasonberry 25:ca1b1098c77f 30 {
jasonberry 25:ca1b1098c77f 31 public:
jasonberry 25:ca1b1098c77f 32 /** ESP8266Interface lifetime
jasonberry 25:ca1b1098c77f 33 * @param tx TX pin
jasonberry 25:ca1b1098c77f 34 * @param rx RX pin
jasonberry 25:ca1b1098c77f 35 * @param debug Enable debugging
jasonberry 25:ca1b1098c77f 36 */
jasonberry 25:ca1b1098c77f 37 ESP8266Interface(PinName tx, PinName rx, bool debug = false);
jasonberry 25:ca1b1098c77f 38
jasonberry 25:ca1b1098c77f 39 /** Start the interface
jasonberry 25:ca1b1098c77f 40 *
jasonberry 25:ca1b1098c77f 41 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
jasonberry 25:ca1b1098c77f 42 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
jasonberry 25:ca1b1098c77f 43 *
jasonberry 25:ca1b1098c77f 44 * @return 0 on success, negative error code on failure
jasonberry 25:ca1b1098c77f 45 */
jasonberry 25:ca1b1098c77f 46 virtual int connect();
jasonberry 25:ca1b1098c77f 47
jasonberry 25:ca1b1098c77f 48 /** Start the interface
jasonberry 25:ca1b1098c77f 49 *
jasonberry 25:ca1b1098c77f 50 * Attempts to connect to a WiFi network.
jasonberry 25:ca1b1098c77f 51 *
jasonberry 25:ca1b1098c77f 52 * @param ssid Name of the network to connect to
jasonberry 25:ca1b1098c77f 53 * @param pass Security passphrase to connect to the network
jasonberry 25:ca1b1098c77f 54 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
jasonberry 25:ca1b1098c77f 55 * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
jasonberry 25:ca1b1098c77f 56 * @return 0 on success, or error code on failure
jasonberry 25:ca1b1098c77f 57 */
jasonberry 25:ca1b1098c77f 58 virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
jasonberry 25:ca1b1098c77f 59 uint8_t channel = 0);
jasonberry 25:ca1b1098c77f 60
jasonberry 25:ca1b1098c77f 61 /** Set the WiFi network credentials
jasonberry 25:ca1b1098c77f 62 *
jasonberry 25:ca1b1098c77f 63 * @param ssid Name of the network to connect to
jasonberry 25:ca1b1098c77f 64 * @param pass Security passphrase to connect to the network
jasonberry 25:ca1b1098c77f 65 * @param security Type of encryption for connection
jasonberry 25:ca1b1098c77f 66 * (defaults to NSAPI_SECURITY_NONE)
jasonberry 25:ca1b1098c77f 67 * @return 0 on success, or error code on failure
jasonberry 25:ca1b1098c77f 68 */
jasonberry 25:ca1b1098c77f 69 virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
jasonberry 25:ca1b1098c77f 70
jasonberry 25:ca1b1098c77f 71 /** Set the WiFi network channel - NOT SUPPORTED
jasonberry 25:ca1b1098c77f 72 *
jasonberry 25:ca1b1098c77f 73 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
jasonberry 25:ca1b1098c77f 74 *
jasonberry 25:ca1b1098c77f 75 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
jasonberry 25:ca1b1098c77f 76 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED
jasonberry 25:ca1b1098c77f 77 */
jasonberry 25:ca1b1098c77f 78 virtual int set_channel(uint8_t channel);
jasonberry 25:ca1b1098c77f 79
jasonberry 25:ca1b1098c77f 80 /** Stop the interface
jasonberry 25:ca1b1098c77f 81 * @return 0 on success, negative on failure
jasonberry 25:ca1b1098c77f 82 */
jasonberry 25:ca1b1098c77f 83 virtual int disconnect();
jasonberry 25:ca1b1098c77f 84
jasonberry 25:ca1b1098c77f 85 /** Get the internally stored IP address
jasonberry 25:ca1b1098c77f 86 * @return IP address of the interface or null if not yet connected
jasonberry 25:ca1b1098c77f 87 */
jasonberry 25:ca1b1098c77f 88 virtual const char *get_ip_address();
jasonberry 25:ca1b1098c77f 89
jasonberry 25:ca1b1098c77f 90 /** Get the internally stored MAC address
jasonberry 25:ca1b1098c77f 91 * @return MAC address of the interface
jasonberry 25:ca1b1098c77f 92 */
jasonberry 25:ca1b1098c77f 93 virtual const char *get_mac_address();
jasonberry 25:ca1b1098c77f 94
jasonberry 25:ca1b1098c77f 95 /** Get the local gateway
jasonberry 25:ca1b1098c77f 96 *
jasonberry 25:ca1b1098c77f 97 * @return Null-terminated representation of the local gateway
jasonberry 25:ca1b1098c77f 98 * or null if no network mask has been recieved
jasonberry 25:ca1b1098c77f 99 */
jasonberry 25:ca1b1098c77f 100 virtual const char *get_gateway();
jasonberry 25:ca1b1098c77f 101
jasonberry 25:ca1b1098c77f 102 /** Get the local network mask
jasonberry 25:ca1b1098c77f 103 *
jasonberry 25:ca1b1098c77f 104 * @return Null-terminated representation of the local network mask
jasonberry 25:ca1b1098c77f 105 * or null if no network mask has been recieved
jasonberry 25:ca1b1098c77f 106 */
jasonberry 25:ca1b1098c77f 107 virtual const char *get_netmask();
jasonberry 25:ca1b1098c77f 108
jasonberry 25:ca1b1098c77f 109 /** Gets the current radio signal strength for active connection
jasonberry 25:ca1b1098c77f 110 *
jasonberry 25:ca1b1098c77f 111 * @return Connection strength in dBm (negative value)
jasonberry 25:ca1b1098c77f 112 */
jasonberry 25:ca1b1098c77f 113 virtual int8_t get_rssi();
jasonberry 25:ca1b1098c77f 114
jasonberry 25:ca1b1098c77f 115 /** Scan for available networks
jasonberry 25:ca1b1098c77f 116 *
jasonberry 25:ca1b1098c77f 117 * This function will block.
jasonberry 25:ca1b1098c77f 118 *
jasonberry 25:ca1b1098c77f 119 * @param ap Pointer to allocated array to store discovered AP
jasonberry 25:ca1b1098c77f 120 * @param count Size of allocated @a res array, or 0 to only count available AP
jasonberry 25:ca1b1098c77f 121 * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
jasonberry 25:ca1b1098c77f 122 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
jasonberry 25:ca1b1098c77f 123 * see @a nsapi_error
jasonberry 25:ca1b1098c77f 124 */
jasonberry 25:ca1b1098c77f 125 virtual int scan(WiFiAccessPoint *res, unsigned count);
jasonberry 25:ca1b1098c77f 126
jasonberry 25:ca1b1098c77f 127 /** Translates a hostname to an IP address with specific version
jasonberry 25:ca1b1098c77f 128 *
jasonberry 25:ca1b1098c77f 129 * The hostname may be either a domain name or an IP address. If the
jasonberry 25:ca1b1098c77f 130 * hostname is an IP address, no network transactions will be performed.
jasonberry 25:ca1b1098c77f 131 *
jasonberry 25:ca1b1098c77f 132 * If no stack-specific DNS resolution is provided, the hostname
jasonberry 25:ca1b1098c77f 133 * will be resolve using a UDP socket on the stack.
jasonberry 25:ca1b1098c77f 134 *
jasonberry 25:ca1b1098c77f 135 * @param address Destination for the host SocketAddress
jasonberry 25:ca1b1098c77f 136 * @param host Hostname to resolve
jasonberry 25:ca1b1098c77f 137 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
jasonberry 25:ca1b1098c77f 138 * version is chosen by the stack (defaults to NSAPI_UNSPEC)
jasonberry 25:ca1b1098c77f 139 * @return 0 on success, negative error code on failure
jasonberry 25:ca1b1098c77f 140 */
jasonberry 25:ca1b1098c77f 141 using NetworkInterface::gethostbyname;
jasonberry 25:ca1b1098c77f 142
jasonberry 25:ca1b1098c77f 143 /** Add a domain name server to list of servers to query
jasonberry 25:ca1b1098c77f 144 *
jasonberry 25:ca1b1098c77f 145 * @param addr Destination for the host address
jasonberry 25:ca1b1098c77f 146 * @return 0 on success, negative error code on failure
jasonberry 25:ca1b1098c77f 147 */
jasonberry 25:ca1b1098c77f 148 using NetworkInterface::add_dns_server;
jasonberry 25:ca1b1098c77f 149
jasonberry 25:ca1b1098c77f 150 protected:
jasonberry 25:ca1b1098c77f 151 /** Open a socket
jasonberry 25:ca1b1098c77f 152 * @param handle Handle in which to store new socket
jasonberry 25:ca1b1098c77f 153 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
jasonberry 25:ca1b1098c77f 154 * @return 0 on success, negative on failure
jasonberry 25:ca1b1098c77f 155 */
jasonberry 25:ca1b1098c77f 156 virtual int socket_open(void **handle, nsapi_protocol_t proto);
jasonberry 25:ca1b1098c77f 157
jasonberry 25:ca1b1098c77f 158 /** Close the socket
jasonberry 25:ca1b1098c77f 159 * @param handle Socket handle
jasonberry 25:ca1b1098c77f 160 * @return 0 on success, negative on failure
jasonberry 25:ca1b1098c77f 161 * @note On failure, any memory associated with the socket must still
jasonberry 25:ca1b1098c77f 162 * be cleaned up
jasonberry 25:ca1b1098c77f 163 */
jasonberry 25:ca1b1098c77f 164 virtual int socket_close(void *handle);
jasonberry 25:ca1b1098c77f 165
jasonberry 25:ca1b1098c77f 166 /** Bind a server socket to a specific port
jasonberry 25:ca1b1098c77f 167 * @param handle Socket handle
jasonberry 25:ca1b1098c77f 168 * @param address Local address to listen for incoming connections on
jasonberry 25:ca1b1098c77f 169 * @return 0 on success, negative on failure.
jasonberry 25:ca1b1098c77f 170 */
jasonberry 25:ca1b1098c77f 171 virtual int socket_bind(void *handle, const SocketAddress &address);
jasonberry 25:ca1b1098c77f 172
jasonberry 25:ca1b1098c77f 173 /** Start listening for incoming connections
jasonberry 25:ca1b1098c77f 174 * @param handle Socket handle
jasonberry 25:ca1b1098c77f 175 * @param backlog Number of pending connections that can be queued up at any
jasonberry 25:ca1b1098c77f 176 * one time [Default: 1]
jasonberry 25:ca1b1098c77f 177 * @return 0 on success, negative on failure
jasonberry 25:ca1b1098c77f 178 */
jasonberry 25:ca1b1098c77f 179 virtual int socket_listen(void *handle, int backlog);
jasonberry 25:ca1b1098c77f 180
jasonberry 25:ca1b1098c77f 181 /** Connects this TCP socket to the server
jasonberry 25:ca1b1098c77f 182 * @param handle Socket handle
jasonberry 25:ca1b1098c77f 183 * @param address SocketAddress to connect to
jasonberry 25:ca1b1098c77f 184 * @return 0 on success, negative on failure
jasonberry 25:ca1b1098c77f 185 */
jasonberry 25:ca1b1098c77f 186 virtual int socket_connect(void *handle, const SocketAddress &address);
jasonberry 25:ca1b1098c77f 187
jasonberry 25:ca1b1098c77f 188 /** Accept a new connection.
jasonberry 25:ca1b1098c77f 189 * @param handle Handle in which to store new socket
jasonberry 25:ca1b1098c77f 190 * @param server Socket handle to server to accept from
jasonberry 25:ca1b1098c77f 191 * @return 0 on success, negative on failure
jasonberry 25:ca1b1098c77f 192 * @note This call is not-blocking, if this call would block, must
jasonberry 25:ca1b1098c77f 193 * immediately return NSAPI_ERROR_WOULD_WAIT
jasonberry 25:ca1b1098c77f 194 */
jasonberry 25:ca1b1098c77f 195 virtual int socket_accept(void *handle, void **socket, SocketAddress *address);
jasonberry 25:ca1b1098c77f 196
jasonberry 25:ca1b1098c77f 197 /** Send data to the remote host
jasonberry 25:ca1b1098c77f 198 * @param handle Socket handle
jasonberry 25:ca1b1098c77f 199 * @param data The buffer to send to the host
jasonberry 25:ca1b1098c77f 200 * @param size The length of the buffer to send
jasonberry 25:ca1b1098c77f 201 * @return Number of written bytes on success, negative on failure
jasonberry 25:ca1b1098c77f 202 * @note This call is not-blocking, if this call would block, must
jasonberry 25:ca1b1098c77f 203 * immediately return NSAPI_ERROR_WOULD_WAIT
jasonberry 25:ca1b1098c77f 204 */
jasonberry 25:ca1b1098c77f 205 virtual int socket_send(void *handle, const void *data, unsigned size);
jasonberry 25:ca1b1098c77f 206
jasonberry 25:ca1b1098c77f 207 /** Receive data from the remote host
jasonberry 25:ca1b1098c77f 208 * @param handle Socket handle
jasonberry 25:ca1b1098c77f 209 * @param data The buffer in which to store the data received from the host
jasonberry 25:ca1b1098c77f 210 * @param size The maximum length of the buffer
jasonberry 25:ca1b1098c77f 211 * @return Number of received bytes on success, negative on failure
jasonberry 25:ca1b1098c77f 212 * @note This call is not-blocking, if this call would block, must
jasonberry 25:ca1b1098c77f 213 * immediately return NSAPI_ERROR_WOULD_WAIT
jasonberry 25:ca1b1098c77f 214 */
jasonberry 25:ca1b1098c77f 215 virtual int socket_recv(void *handle, void *data, unsigned size);
jasonberry 25:ca1b1098c77f 216
jasonberry 25:ca1b1098c77f 217 /** Send a packet to a remote endpoint
jasonberry 25:ca1b1098c77f 218 * @param handle Socket handle
jasonberry 25:ca1b1098c77f 219 * @param address The remote SocketAddress
jasonberry 25:ca1b1098c77f 220 * @param data The packet to be sent
jasonberry 25:ca1b1098c77f 221 * @param size The length of the packet to be sent
jasonberry 25:ca1b1098c77f 222 * @return The number of written bytes on success, negative on failure
jasonberry 25:ca1b1098c77f 223 * @note This call is not-blocking, if this call would block, must
jasonberry 25:ca1b1098c77f 224 * immediately return NSAPI_ERROR_WOULD_WAIT
jasonberry 25:ca1b1098c77f 225 */
jasonberry 25:ca1b1098c77f 226 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
jasonberry 25:ca1b1098c77f 227
jasonberry 25:ca1b1098c77f 228 /** Receive a packet from a remote endpoint
jasonberry 25:ca1b1098c77f 229 * @param handle Socket handle
jasonberry 25:ca1b1098c77f 230 * @param address Destination for the remote SocketAddress or null
jasonberry 25:ca1b1098c77f 231 * @param buffer The buffer for storing the incoming packet data
jasonberry 25:ca1b1098c77f 232 * If a packet is too long to fit in the supplied buffer,
jasonberry 25:ca1b1098c77f 233 * excess bytes are discarded
jasonberry 25:ca1b1098c77f 234 * @param size The length of the buffer
jasonberry 25:ca1b1098c77f 235 * @return The number of received bytes on success, negative on failure
jasonberry 25:ca1b1098c77f 236 * @note This call is not-blocking, if this call would block, must
jasonberry 25:ca1b1098c77f 237 * immediately return NSAPI_ERROR_WOULD_WAIT
jasonberry 25:ca1b1098c77f 238 */
jasonberry 25:ca1b1098c77f 239 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
jasonberry 25:ca1b1098c77f 240
jasonberry 25:ca1b1098c77f 241 /** Register a callback on state change of the socket
jasonberry 25:ca1b1098c77f 242 * @param handle Socket handle
jasonberry 25:ca1b1098c77f 243 * @param callback Function to call on state change
jasonberry 25:ca1b1098c77f 244 * @param data Argument to pass to callback
jasonberry 25:ca1b1098c77f 245 * @note Callback may be called in an interrupt context.
jasonberry 25:ca1b1098c77f 246 */
jasonberry 25:ca1b1098c77f 247 virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
jasonberry 25:ca1b1098c77f 248
jasonberry 25:ca1b1098c77f 249 /** Provide access to the NetworkStack object
jasonberry 25:ca1b1098c77f 250 *
jasonberry 25:ca1b1098c77f 251 * @return The underlying NetworkStack object
jasonberry 25:ca1b1098c77f 252 */
jasonberry 25:ca1b1098c77f 253 virtual NetworkStack *get_stack()
jasonberry 25:ca1b1098c77f 254 {
jasonberry 25:ca1b1098c77f 255 return this;
jasonberry 25:ca1b1098c77f 256 }
jasonberry 25:ca1b1098c77f 257
jasonberry 25:ca1b1098c77f 258 private:
jasonberry 25:ca1b1098c77f 259 ESP8266 _esp;
jasonberry 25:ca1b1098c77f 260 bool _ids[ESP8266_SOCKET_COUNT];
jasonberry 25:ca1b1098c77f 261
jasonberry 25:ca1b1098c77f 262 char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
jasonberry 25:ca1b1098c77f 263 nsapi_security_t ap_sec;
jasonberry 25:ca1b1098c77f 264 uint8_t ap_ch;
jasonberry 25:ca1b1098c77f 265 char ap_pass[64]; /* The longest allowed passphrase */
jasonberry 25:ca1b1098c77f 266
jasonberry 25:ca1b1098c77f 267 void event();
jasonberry 25:ca1b1098c77f 268
jasonberry 25:ca1b1098c77f 269 struct {
jasonberry 25:ca1b1098c77f 270 void (*callback)(void *);
jasonberry 25:ca1b1098c77f 271 void *data;
jasonberry 25:ca1b1098c77f 272 } _cbs[ESP8266_SOCKET_COUNT];
jasonberry 25:ca1b1098c77f 273 };
jasonberry 25:ca1b1098c77f 274
jasonberry 25:ca1b1098c77f 275 #endif