hellomqttt to thingspeak mqtt and ifttt
Dependencies: Servo MQTTPacket FP
easy-connect/esp8266-driver/ESP8266Interface.cpp@25:ca1b1098c77f, 2021-05-05 (annotated)
- Committer:
- jasonberry
- Date:
- Wed May 05 14:48:01 2021 +0000
- Revision:
- 25:ca1b1098c77f
TEST
Who changed what in which revision?
User | Revision | Line number | New 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 | #include <string.h> |
jasonberry | 25:ca1b1098c77f | 18 | #include "ESP8266Interface.h" |
jasonberry | 25:ca1b1098c77f | 19 | |
jasonberry | 25:ca1b1098c77f | 20 | // Various timeouts for different ESP8266 operations |
jasonberry | 25:ca1b1098c77f | 21 | #define ESP8266_CONNECT_TIMEOUT 15000//15000 |
jasonberry | 25:ca1b1098c77f | 22 | #define ESP8266_SEND_TIMEOUT 500 |
jasonberry | 25:ca1b1098c77f | 23 | #define ESP8266_RECV_TIMEOUT 0 |
jasonberry | 25:ca1b1098c77f | 24 | #define ESP8266_MISC_TIMEOUT 500 |
jasonberry | 25:ca1b1098c77f | 25 | |
jasonberry | 25:ca1b1098c77f | 26 | // ESP8266Interface implementation |
jasonberry | 25:ca1b1098c77f | 27 | ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug) |
jasonberry | 25:ca1b1098c77f | 28 | : _esp(tx, rx, debug) |
jasonberry | 25:ca1b1098c77f | 29 | { |
jasonberry | 25:ca1b1098c77f | 30 | memset(_ids, 0, sizeof(_ids)); |
jasonberry | 25:ca1b1098c77f | 31 | memset(_cbs, 0, sizeof(_cbs)); |
jasonberry | 25:ca1b1098c77f | 32 | |
jasonberry | 25:ca1b1098c77f | 33 | _esp.attach(this, &ESP8266Interface::event); |
jasonberry | 25:ca1b1098c77f | 34 | } |
jasonberry | 25:ca1b1098c77f | 35 | |
jasonberry | 25:ca1b1098c77f | 36 | int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security, |
jasonberry | 25:ca1b1098c77f | 37 | uint8_t channel) |
jasonberry | 25:ca1b1098c77f | 38 | { |
jasonberry | 25:ca1b1098c77f | 39 | if (channel != 0) { |
jasonberry | 25:ca1b1098c77f | 40 | return NSAPI_ERROR_UNSUPPORTED; |
jasonberry | 25:ca1b1098c77f | 41 | } |
jasonberry | 25:ca1b1098c77f | 42 | |
jasonberry | 25:ca1b1098c77f | 43 | set_credentials(ssid, pass, security); |
jasonberry | 25:ca1b1098c77f | 44 | return connect(); |
jasonberry | 25:ca1b1098c77f | 45 | } |
jasonberry | 25:ca1b1098c77f | 46 | |
jasonberry | 25:ca1b1098c77f | 47 | int ESP8266Interface::connect() |
jasonberry | 25:ca1b1098c77f | 48 | { |
jasonberry | 25:ca1b1098c77f | 49 | _esp.setTimeout(ESP8266_CONNECT_TIMEOUT); |
jasonberry | 25:ca1b1098c77f | 50 | |
jasonberry | 25:ca1b1098c77f | 51 | if (!_esp.startup(3)) { |
jasonberry | 25:ca1b1098c77f | 52 | return NSAPI_ERROR_DEVICE_ERROR; |
jasonberry | 25:ca1b1098c77f | 53 | } |
jasonberry | 25:ca1b1098c77f | 54 | |
jasonberry | 25:ca1b1098c77f | 55 | if (!_esp.dhcp(true, 1)) { |
jasonberry | 25:ca1b1098c77f | 56 | return NSAPI_ERROR_DHCP_FAILURE; |
jasonberry | 25:ca1b1098c77f | 57 | } |
jasonberry | 25:ca1b1098c77f | 58 | |
jasonberry | 25:ca1b1098c77f | 59 | if (!_esp.connect(ap_ssid, ap_pass)) { |
jasonberry | 25:ca1b1098c77f | 60 | return NSAPI_ERROR_NO_CONNECTION; |
jasonberry | 25:ca1b1098c77f | 61 | } |
jasonberry | 25:ca1b1098c77f | 62 | |
jasonberry | 25:ca1b1098c77f | 63 | if (!_esp.getIPAddress()) { |
jasonberry | 25:ca1b1098c77f | 64 | return NSAPI_ERROR_DHCP_FAILURE; |
jasonberry | 25:ca1b1098c77f | 65 | } |
jasonberry | 25:ca1b1098c77f | 66 | |
jasonberry | 25:ca1b1098c77f | 67 | return NSAPI_ERROR_OK; |
jasonberry | 25:ca1b1098c77f | 68 | } |
jasonberry | 25:ca1b1098c77f | 69 | |
jasonberry | 25:ca1b1098c77f | 70 | int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security) |
jasonberry | 25:ca1b1098c77f | 71 | { |
jasonberry | 25:ca1b1098c77f | 72 | memset(ap_ssid, 0, sizeof(ap_ssid)); |
jasonberry | 25:ca1b1098c77f | 73 | strncpy(ap_ssid, ssid, sizeof(ap_ssid)); |
jasonberry | 25:ca1b1098c77f | 74 | |
jasonberry | 25:ca1b1098c77f | 75 | memset(ap_pass, 0, sizeof(ap_pass)); |
jasonberry | 25:ca1b1098c77f | 76 | strncpy(ap_pass, pass, sizeof(ap_pass)); |
jasonberry | 25:ca1b1098c77f | 77 | |
jasonberry | 25:ca1b1098c77f | 78 | ap_sec = security; |
jasonberry | 25:ca1b1098c77f | 79 | |
jasonberry | 25:ca1b1098c77f | 80 | return 0; |
jasonberry | 25:ca1b1098c77f | 81 | } |
jasonberry | 25:ca1b1098c77f | 82 | |
jasonberry | 25:ca1b1098c77f | 83 | int ESP8266Interface::set_channel(uint8_t channel) |
jasonberry | 25:ca1b1098c77f | 84 | { |
jasonberry | 25:ca1b1098c77f | 85 | return NSAPI_ERROR_UNSUPPORTED; |
jasonberry | 25:ca1b1098c77f | 86 | } |
jasonberry | 25:ca1b1098c77f | 87 | |
jasonberry | 25:ca1b1098c77f | 88 | |
jasonberry | 25:ca1b1098c77f | 89 | int ESP8266Interface::disconnect() |
jasonberry | 25:ca1b1098c77f | 90 | { |
jasonberry | 25:ca1b1098c77f | 91 | _esp.setTimeout(ESP8266_MISC_TIMEOUT); |
jasonberry | 25:ca1b1098c77f | 92 | |
jasonberry | 25:ca1b1098c77f | 93 | if (!_esp.disconnect()) { |
jasonberry | 25:ca1b1098c77f | 94 | return NSAPI_ERROR_DEVICE_ERROR; |
jasonberry | 25:ca1b1098c77f | 95 | } |
jasonberry | 25:ca1b1098c77f | 96 | |
jasonberry | 25:ca1b1098c77f | 97 | return NSAPI_ERROR_OK; |
jasonberry | 25:ca1b1098c77f | 98 | } |
jasonberry | 25:ca1b1098c77f | 99 | |
jasonberry | 25:ca1b1098c77f | 100 | const char *ESP8266Interface::get_ip_address() |
jasonberry | 25:ca1b1098c77f | 101 | { |
jasonberry | 25:ca1b1098c77f | 102 | return _esp.getIPAddress(); |
jasonberry | 25:ca1b1098c77f | 103 | } |
jasonberry | 25:ca1b1098c77f | 104 | |
jasonberry | 25:ca1b1098c77f | 105 | const char *ESP8266Interface::get_mac_address() |
jasonberry | 25:ca1b1098c77f | 106 | { |
jasonberry | 25:ca1b1098c77f | 107 | return _esp.getMACAddress(); |
jasonberry | 25:ca1b1098c77f | 108 | } |
jasonberry | 25:ca1b1098c77f | 109 | |
jasonberry | 25:ca1b1098c77f | 110 | const char *ESP8266Interface::get_gateway() |
jasonberry | 25:ca1b1098c77f | 111 | { |
jasonberry | 25:ca1b1098c77f | 112 | return _esp.getGateway(); |
jasonberry | 25:ca1b1098c77f | 113 | } |
jasonberry | 25:ca1b1098c77f | 114 | |
jasonberry | 25:ca1b1098c77f | 115 | const char *ESP8266Interface::get_netmask() |
jasonberry | 25:ca1b1098c77f | 116 | { |
jasonberry | 25:ca1b1098c77f | 117 | return _esp.getNetmask(); |
jasonberry | 25:ca1b1098c77f | 118 | } |
jasonberry | 25:ca1b1098c77f | 119 | |
jasonberry | 25:ca1b1098c77f | 120 | int8_t ESP8266Interface::get_rssi() |
jasonberry | 25:ca1b1098c77f | 121 | { |
jasonberry | 25:ca1b1098c77f | 122 | return _esp.getRSSI(); |
jasonberry | 25:ca1b1098c77f | 123 | } |
jasonberry | 25:ca1b1098c77f | 124 | |
jasonberry | 25:ca1b1098c77f | 125 | int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count) |
jasonberry | 25:ca1b1098c77f | 126 | { |
jasonberry | 25:ca1b1098c77f | 127 | return _esp.scan(res, count); |
jasonberry | 25:ca1b1098c77f | 128 | } |
jasonberry | 25:ca1b1098c77f | 129 | |
jasonberry | 25:ca1b1098c77f | 130 | struct esp8266_socket { |
jasonberry | 25:ca1b1098c77f | 131 | int id; |
jasonberry | 25:ca1b1098c77f | 132 | nsapi_protocol_t proto; |
jasonberry | 25:ca1b1098c77f | 133 | bool connected; |
jasonberry | 25:ca1b1098c77f | 134 | SocketAddress addr; |
jasonberry | 25:ca1b1098c77f | 135 | }; |
jasonberry | 25:ca1b1098c77f | 136 | |
jasonberry | 25:ca1b1098c77f | 137 | int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto) |
jasonberry | 25:ca1b1098c77f | 138 | { |
jasonberry | 25:ca1b1098c77f | 139 | // Look for an unused socket |
jasonberry | 25:ca1b1098c77f | 140 | int id = -1; |
jasonberry | 25:ca1b1098c77f | 141 | |
jasonberry | 25:ca1b1098c77f | 142 | for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { |
jasonberry | 25:ca1b1098c77f | 143 | if (!_ids[i]) { |
jasonberry | 25:ca1b1098c77f | 144 | id = i; |
jasonberry | 25:ca1b1098c77f | 145 | _ids[i] = true; |
jasonberry | 25:ca1b1098c77f | 146 | break; |
jasonberry | 25:ca1b1098c77f | 147 | } |
jasonberry | 25:ca1b1098c77f | 148 | } |
jasonberry | 25:ca1b1098c77f | 149 | |
jasonberry | 25:ca1b1098c77f | 150 | if (id == -1) { |
jasonberry | 25:ca1b1098c77f | 151 | return NSAPI_ERROR_NO_SOCKET; |
jasonberry | 25:ca1b1098c77f | 152 | } |
jasonberry | 25:ca1b1098c77f | 153 | |
jasonberry | 25:ca1b1098c77f | 154 | struct esp8266_socket *socket = new struct esp8266_socket; |
jasonberry | 25:ca1b1098c77f | 155 | if (!socket) { |
jasonberry | 25:ca1b1098c77f | 156 | return NSAPI_ERROR_NO_SOCKET; |
jasonberry | 25:ca1b1098c77f | 157 | } |
jasonberry | 25:ca1b1098c77f | 158 | |
jasonberry | 25:ca1b1098c77f | 159 | socket->id = id; |
jasonberry | 25:ca1b1098c77f | 160 | socket->proto = proto; |
jasonberry | 25:ca1b1098c77f | 161 | socket->connected = false; |
jasonberry | 25:ca1b1098c77f | 162 | *handle = socket; |
jasonberry | 25:ca1b1098c77f | 163 | return 0; |
jasonberry | 25:ca1b1098c77f | 164 | } |
jasonberry | 25:ca1b1098c77f | 165 | |
jasonberry | 25:ca1b1098c77f | 166 | int ESP8266Interface::socket_close(void *handle) |
jasonberry | 25:ca1b1098c77f | 167 | { |
jasonberry | 25:ca1b1098c77f | 168 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
jasonberry | 25:ca1b1098c77f | 169 | int err = 0; |
jasonberry | 25:ca1b1098c77f | 170 | _esp.setTimeout(ESP8266_MISC_TIMEOUT); |
jasonberry | 25:ca1b1098c77f | 171 | |
jasonberry | 25:ca1b1098c77f | 172 | if (!_esp.close(socket->id)) { |
jasonberry | 25:ca1b1098c77f | 173 | err = NSAPI_ERROR_DEVICE_ERROR; |
jasonberry | 25:ca1b1098c77f | 174 | } |
jasonberry | 25:ca1b1098c77f | 175 | |
jasonberry | 25:ca1b1098c77f | 176 | _ids[socket->id] = false; |
jasonberry | 25:ca1b1098c77f | 177 | delete socket; |
jasonberry | 25:ca1b1098c77f | 178 | return err; |
jasonberry | 25:ca1b1098c77f | 179 | } |
jasonberry | 25:ca1b1098c77f | 180 | |
jasonberry | 25:ca1b1098c77f | 181 | int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address) |
jasonberry | 25:ca1b1098c77f | 182 | { |
jasonberry | 25:ca1b1098c77f | 183 | return NSAPI_ERROR_UNSUPPORTED; |
jasonberry | 25:ca1b1098c77f | 184 | } |
jasonberry | 25:ca1b1098c77f | 185 | |
jasonberry | 25:ca1b1098c77f | 186 | int ESP8266Interface::socket_listen(void *handle, int backlog) |
jasonberry | 25:ca1b1098c77f | 187 | { |
jasonberry | 25:ca1b1098c77f | 188 | return NSAPI_ERROR_UNSUPPORTED; |
jasonberry | 25:ca1b1098c77f | 189 | } |
jasonberry | 25:ca1b1098c77f | 190 | |
jasonberry | 25:ca1b1098c77f | 191 | int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr) |
jasonberry | 25:ca1b1098c77f | 192 | { |
jasonberry | 25:ca1b1098c77f | 193 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
jasonberry | 25:ca1b1098c77f | 194 | _esp.setTimeout(ESP8266_MISC_TIMEOUT); |
jasonberry | 25:ca1b1098c77f | 195 | |
jasonberry | 25:ca1b1098c77f | 196 | const char *proto = (socket->proto == NSAPI_UDP) ? "UDP" : "TCP"; |
jasonberry | 25:ca1b1098c77f | 197 | if (!_esp.open(proto, socket->id, addr.get_ip_address(), addr.get_port())) { |
jasonberry | 25:ca1b1098c77f | 198 | return NSAPI_ERROR_DEVICE_ERROR; |
jasonberry | 25:ca1b1098c77f | 199 | } |
jasonberry | 25:ca1b1098c77f | 200 | |
jasonberry | 25:ca1b1098c77f | 201 | socket->connected = true; |
jasonberry | 25:ca1b1098c77f | 202 | return 0; |
jasonberry | 25:ca1b1098c77f | 203 | } |
jasonberry | 25:ca1b1098c77f | 204 | |
jasonberry | 25:ca1b1098c77f | 205 | int ESP8266Interface::socket_accept(void *server, void **socket, SocketAddress *addr) |
jasonberry | 25:ca1b1098c77f | 206 | { |
jasonberry | 25:ca1b1098c77f | 207 | return NSAPI_ERROR_UNSUPPORTED; |
jasonberry | 25:ca1b1098c77f | 208 | } |
jasonberry | 25:ca1b1098c77f | 209 | |
jasonberry | 25:ca1b1098c77f | 210 | int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size) |
jasonberry | 25:ca1b1098c77f | 211 | { |
jasonberry | 25:ca1b1098c77f | 212 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
jasonberry | 25:ca1b1098c77f | 213 | _esp.setTimeout(ESP8266_SEND_TIMEOUT); |
jasonberry | 25:ca1b1098c77f | 214 | |
jasonberry | 25:ca1b1098c77f | 215 | if (!_esp.send(socket->id, data, size)) { |
jasonberry | 25:ca1b1098c77f | 216 | return NSAPI_ERROR_DEVICE_ERROR; |
jasonberry | 25:ca1b1098c77f | 217 | } |
jasonberry | 25:ca1b1098c77f | 218 | |
jasonberry | 25:ca1b1098c77f | 219 | return size; |
jasonberry | 25:ca1b1098c77f | 220 | } |
jasonberry | 25:ca1b1098c77f | 221 | |
jasonberry | 25:ca1b1098c77f | 222 | int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size) |
jasonberry | 25:ca1b1098c77f | 223 | { |
jasonberry | 25:ca1b1098c77f | 224 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
jasonberry | 25:ca1b1098c77f | 225 | _esp.setTimeout(ESP8266_RECV_TIMEOUT); |
jasonberry | 25:ca1b1098c77f | 226 | |
jasonberry | 25:ca1b1098c77f | 227 | int32_t recv = _esp.recv(socket->id, data, size); |
jasonberry | 25:ca1b1098c77f | 228 | if (recv < 0) { |
jasonberry | 25:ca1b1098c77f | 229 | return NSAPI_ERROR_WOULD_BLOCK; |
jasonberry | 25:ca1b1098c77f | 230 | } |
jasonberry | 25:ca1b1098c77f | 231 | |
jasonberry | 25:ca1b1098c77f | 232 | return recv; |
jasonberry | 25:ca1b1098c77f | 233 | } |
jasonberry | 25:ca1b1098c77f | 234 | |
jasonberry | 25:ca1b1098c77f | 235 | int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) |
jasonberry | 25:ca1b1098c77f | 236 | { |
jasonberry | 25:ca1b1098c77f | 237 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
jasonberry | 25:ca1b1098c77f | 238 | |
jasonberry | 25:ca1b1098c77f | 239 | if (socket->connected && socket->addr != addr) { |
jasonberry | 25:ca1b1098c77f | 240 | _esp.setTimeout(ESP8266_MISC_TIMEOUT); |
jasonberry | 25:ca1b1098c77f | 241 | if (!_esp.close(socket->id)) { |
jasonberry | 25:ca1b1098c77f | 242 | return NSAPI_ERROR_DEVICE_ERROR; |
jasonberry | 25:ca1b1098c77f | 243 | } |
jasonberry | 25:ca1b1098c77f | 244 | socket->connected = false; |
jasonberry | 25:ca1b1098c77f | 245 | } |
jasonberry | 25:ca1b1098c77f | 246 | |
jasonberry | 25:ca1b1098c77f | 247 | if (!socket->connected) { |
jasonberry | 25:ca1b1098c77f | 248 | int err = socket_connect(socket, addr); |
jasonberry | 25:ca1b1098c77f | 249 | if (err < 0) { |
jasonberry | 25:ca1b1098c77f | 250 | return err; |
jasonberry | 25:ca1b1098c77f | 251 | } |
jasonberry | 25:ca1b1098c77f | 252 | socket->addr = addr; |
jasonberry | 25:ca1b1098c77f | 253 | } |
jasonberry | 25:ca1b1098c77f | 254 | |
jasonberry | 25:ca1b1098c77f | 255 | return socket_send(socket, data, size); |
jasonberry | 25:ca1b1098c77f | 256 | } |
jasonberry | 25:ca1b1098c77f | 257 | |
jasonberry | 25:ca1b1098c77f | 258 | int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) |
jasonberry | 25:ca1b1098c77f | 259 | { |
jasonberry | 25:ca1b1098c77f | 260 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
jasonberry | 25:ca1b1098c77f | 261 | int ret = socket_recv(socket, data, size); |
jasonberry | 25:ca1b1098c77f | 262 | if (ret >= 0 && addr) { |
jasonberry | 25:ca1b1098c77f | 263 | *addr = socket->addr; |
jasonberry | 25:ca1b1098c77f | 264 | } |
jasonberry | 25:ca1b1098c77f | 265 | |
jasonberry | 25:ca1b1098c77f | 266 | return ret; |
jasonberry | 25:ca1b1098c77f | 267 | } |
jasonberry | 25:ca1b1098c77f | 268 | |
jasonberry | 25:ca1b1098c77f | 269 | void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data) |
jasonberry | 25:ca1b1098c77f | 270 | { |
jasonberry | 25:ca1b1098c77f | 271 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
jasonberry | 25:ca1b1098c77f | 272 | _cbs[socket->id].callback = callback; |
jasonberry | 25:ca1b1098c77f | 273 | _cbs[socket->id].data = data; |
jasonberry | 25:ca1b1098c77f | 274 | } |
jasonberry | 25:ca1b1098c77f | 275 | |
jasonberry | 25:ca1b1098c77f | 276 | void ESP8266Interface::event() { |
jasonberry | 25:ca1b1098c77f | 277 | for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { |
jasonberry | 25:ca1b1098c77f | 278 | if (_cbs[i].callback) { |
jasonberry | 25:ca1b1098c77f | 279 | _cbs[i].callback(_cbs[i].data); |
jasonberry | 25:ca1b1098c77f | 280 | } |
jasonberry | 25:ca1b1098c77f | 281 | } |
jasonberry | 25:ca1b1098c77f | 282 | } |