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 Example
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 "ESP8266.h"
jasonberry 25:ca1b1098c77f 18
jasonberry 25:ca1b1098c77f 19 ESP8266::ESP8266(PinName tx, PinName rx, bool debug)
jasonberry 25:ca1b1098c77f 20 : _serial(tx, rx, 1024), _parser(_serial)
jasonberry 25:ca1b1098c77f 21 , _packets(0), _packets_end(&_packets)
jasonberry 25:ca1b1098c77f 22 {
jasonberry 25:ca1b1098c77f 23 _serial.baud(115200);
jasonberry 25:ca1b1098c77f 24 _parser.debugOn(debug);
jasonberry 25:ca1b1098c77f 25 }
jasonberry 25:ca1b1098c77f 26
jasonberry 25:ca1b1098c77f 27 bool ESP8266::startup(int mode)
jasonberry 25:ca1b1098c77f 28 {
jasonberry 25:ca1b1098c77f 29 //only 3 valid modes
jasonberry 25:ca1b1098c77f 30 if(mode < 1 || mode > 3) {
jasonberry 25:ca1b1098c77f 31 return false;
jasonberry 25:ca1b1098c77f 32 }
jasonberry 25:ca1b1098c77f 33
jasonberry 25:ca1b1098c77f 34 bool success = reset()
jasonberry 25:ca1b1098c77f 35 && _parser.send("AT+CWMODE=%d", mode)
jasonberry 25:ca1b1098c77f 36 && _parser.recv("OK")
jasonberry 25:ca1b1098c77f 37 && _parser.send("AT+CIPMUX=1")
jasonberry 25:ca1b1098c77f 38 && _parser.recv("OK");
jasonberry 25:ca1b1098c77f 39
jasonberry 25:ca1b1098c77f 40 _parser.oob("+IPD", this, &ESP8266::_packet_handler);
jasonberry 25:ca1b1098c77f 41
jasonberry 25:ca1b1098c77f 42 return success;
jasonberry 25:ca1b1098c77f 43 }
jasonberry 25:ca1b1098c77f 44
jasonberry 25:ca1b1098c77f 45 bool ESP8266::reset(void)
jasonberry 25:ca1b1098c77f 46 {
jasonberry 25:ca1b1098c77f 47 for (int i = 0; i < 2; i++) {
jasonberry 25:ca1b1098c77f 48 if (_parser.send("AT+RST")
jasonberry 25:ca1b1098c77f 49 && _parser.recv("OK\r\nready")) {
jasonberry 25:ca1b1098c77f 50 return true;
jasonberry 25:ca1b1098c77f 51 }
jasonberry 25:ca1b1098c77f 52 }
jasonberry 25:ca1b1098c77f 53
jasonberry 25:ca1b1098c77f 54 return false;
jasonberry 25:ca1b1098c77f 55 }
jasonberry 25:ca1b1098c77f 56
jasonberry 25:ca1b1098c77f 57 bool ESP8266::dhcp(bool enabled, int mode)
jasonberry 25:ca1b1098c77f 58 {
jasonberry 25:ca1b1098c77f 59 //only 3 valid modes
jasonberry 25:ca1b1098c77f 60 if(mode < 0 || mode > 2) {
jasonberry 25:ca1b1098c77f 61 return false;
jasonberry 25:ca1b1098c77f 62 }
jasonberry 25:ca1b1098c77f 63
jasonberry 25:ca1b1098c77f 64 return _parser.send("AT+CWDHCP=%d,%d", enabled?1:0, mode)
jasonberry 25:ca1b1098c77f 65 && _parser.recv("OK");
jasonberry 25:ca1b1098c77f 66 }
jasonberry 25:ca1b1098c77f 67
jasonberry 25:ca1b1098c77f 68 bool ESP8266::connect(const char *ap, const char *passPhrase)
jasonberry 25:ca1b1098c77f 69 {
jasonberry 25:ca1b1098c77f 70 return _parser.send("AT+CWJAP=\"%s\",\"%s\"", ap, passPhrase)
jasonberry 25:ca1b1098c77f 71 && _parser.recv("OK");
jasonberry 25:ca1b1098c77f 72 }
jasonberry 25:ca1b1098c77f 73
jasonberry 25:ca1b1098c77f 74 bool ESP8266::disconnect(void)
jasonberry 25:ca1b1098c77f 75 {
jasonberry 25:ca1b1098c77f 76 return _parser.send("AT+CWQAP") && _parser.recv("OK");
jasonberry 25:ca1b1098c77f 77 }
jasonberry 25:ca1b1098c77f 78
jasonberry 25:ca1b1098c77f 79 const char *ESP8266::getIPAddress(void)
jasonberry 25:ca1b1098c77f 80 {
jasonberry 25:ca1b1098c77f 81 if (!(_parser.send("AT+CIFSR")
jasonberry 25:ca1b1098c77f 82 && _parser.recv("+CIFSR:STAIP,\"%15[^\"]\"", _ip_buffer)
jasonberry 25:ca1b1098c77f 83 && _parser.recv("OK"))) {
jasonberry 25:ca1b1098c77f 84 return 0;
jasonberry 25:ca1b1098c77f 85 }
jasonberry 25:ca1b1098c77f 86
jasonberry 25:ca1b1098c77f 87 return _ip_buffer;
jasonberry 25:ca1b1098c77f 88 }
jasonberry 25:ca1b1098c77f 89
jasonberry 25:ca1b1098c77f 90 const char *ESP8266::getMACAddress(void)
jasonberry 25:ca1b1098c77f 91 {
jasonberry 25:ca1b1098c77f 92 if (!(_parser.send("AT+CIFSR")
jasonberry 25:ca1b1098c77f 93 && _parser.recv("+CIFSR:STAMAC,\"%17[^\"]\"", _mac_buffer)
jasonberry 25:ca1b1098c77f 94 && _parser.recv("OK"))) {
jasonberry 25:ca1b1098c77f 95 return 0;
jasonberry 25:ca1b1098c77f 96 }
jasonberry 25:ca1b1098c77f 97
jasonberry 25:ca1b1098c77f 98 return _mac_buffer;
jasonberry 25:ca1b1098c77f 99 }
jasonberry 25:ca1b1098c77f 100
jasonberry 25:ca1b1098c77f 101 const char *ESP8266::getGateway()
jasonberry 25:ca1b1098c77f 102 {
jasonberry 25:ca1b1098c77f 103 if (!(_parser.send("AT+CIPSTA?")
jasonberry 25:ca1b1098c77f 104 && _parser.recv("+CIPSTA:gateway:\"%15[^\"]\"", _gateway_buffer)
jasonberry 25:ca1b1098c77f 105 && _parser.recv("OK"))) {
jasonberry 25:ca1b1098c77f 106 return 0;
jasonberry 25:ca1b1098c77f 107 }
jasonberry 25:ca1b1098c77f 108
jasonberry 25:ca1b1098c77f 109 return _gateway_buffer;
jasonberry 25:ca1b1098c77f 110 }
jasonberry 25:ca1b1098c77f 111
jasonberry 25:ca1b1098c77f 112 const char *ESP8266::getNetmask()
jasonberry 25:ca1b1098c77f 113 {
jasonberry 25:ca1b1098c77f 114 if (!(_parser.send("AT+CIPSTA?")
jasonberry 25:ca1b1098c77f 115 && _parser.recv("+CIPSTA:netmask:\"%15[^\"]\"", _netmask_buffer)
jasonberry 25:ca1b1098c77f 116 && _parser.recv("OK"))) {
jasonberry 25:ca1b1098c77f 117 return 0;
jasonberry 25:ca1b1098c77f 118 }
jasonberry 25:ca1b1098c77f 119
jasonberry 25:ca1b1098c77f 120 return _netmask_buffer;
jasonberry 25:ca1b1098c77f 121 }
jasonberry 25:ca1b1098c77f 122
jasonberry 25:ca1b1098c77f 123 int8_t ESP8266::getRSSI()
jasonberry 25:ca1b1098c77f 124 {
jasonberry 25:ca1b1098c77f 125 int8_t rssi;
jasonberry 25:ca1b1098c77f 126 char bssid[18];
jasonberry 25:ca1b1098c77f 127
jasonberry 25:ca1b1098c77f 128 if (!(_parser.send("AT+CWJAP?")
jasonberry 25:ca1b1098c77f 129 && _parser.recv("+CWJAP:\"%*[^\"]\",\"%17[^\"]\"", bssid)
jasonberry 25:ca1b1098c77f 130 && _parser.recv("OK"))) {
jasonberry 25:ca1b1098c77f 131 return 0;
jasonberry 25:ca1b1098c77f 132 }
jasonberry 25:ca1b1098c77f 133
jasonberry 25:ca1b1098c77f 134 if (!(_parser.send("AT+CWLAP=\"\",\"%s\",", bssid)
jasonberry 25:ca1b1098c77f 135 && _parser.recv("+CWLAP:(%*d,\"%*[^\"]\",%hhd,", &rssi)
jasonberry 25:ca1b1098c77f 136 && _parser.recv("OK"))) {
jasonberry 25:ca1b1098c77f 137 return 0;
jasonberry 25:ca1b1098c77f 138 }
jasonberry 25:ca1b1098c77f 139
jasonberry 25:ca1b1098c77f 140 return rssi;
jasonberry 25:ca1b1098c77f 141 }
jasonberry 25:ca1b1098c77f 142
jasonberry 25:ca1b1098c77f 143 bool ESP8266::isConnected(void)
jasonberry 25:ca1b1098c77f 144 {
jasonberry 25:ca1b1098c77f 145 return getIPAddress() != 0;
jasonberry 25:ca1b1098c77f 146 }
jasonberry 25:ca1b1098c77f 147
jasonberry 25:ca1b1098c77f 148 int ESP8266::scan(WiFiAccessPoint *res, unsigned limit)
jasonberry 25:ca1b1098c77f 149 {
jasonberry 25:ca1b1098c77f 150 unsigned cnt = 0;
jasonberry 25:ca1b1098c77f 151 nsapi_wifi_ap_t ap;
jasonberry 25:ca1b1098c77f 152
jasonberry 25:ca1b1098c77f 153 if (!_parser.send("AT+CWLAP")) {
jasonberry 25:ca1b1098c77f 154 return NSAPI_ERROR_DEVICE_ERROR;
jasonberry 25:ca1b1098c77f 155 }
jasonberry 25:ca1b1098c77f 156
jasonberry 25:ca1b1098c77f 157 while (recv_ap(&ap)) {
jasonberry 25:ca1b1098c77f 158 if (cnt < limit) {
jasonberry 25:ca1b1098c77f 159 res[cnt] = WiFiAccessPoint(ap);
jasonberry 25:ca1b1098c77f 160 }
jasonberry 25:ca1b1098c77f 161
jasonberry 25:ca1b1098c77f 162 cnt++;
jasonberry 25:ca1b1098c77f 163 if (limit != 0 && cnt >= limit) {
jasonberry 25:ca1b1098c77f 164 break;
jasonberry 25:ca1b1098c77f 165 }
jasonberry 25:ca1b1098c77f 166 }
jasonberry 25:ca1b1098c77f 167
jasonberry 25:ca1b1098c77f 168 return cnt;
jasonberry 25:ca1b1098c77f 169 }
jasonberry 25:ca1b1098c77f 170
jasonberry 25:ca1b1098c77f 171 bool ESP8266::open(const char *type, int id, const char* addr, int port)
jasonberry 25:ca1b1098c77f 172 {
jasonberry 25:ca1b1098c77f 173 //IDs only 0-4
jasonberry 25:ca1b1098c77f 174 if(id > 4) {
jasonberry 25:ca1b1098c77f 175 return false;
jasonberry 25:ca1b1098c77f 176 }
jasonberry 25:ca1b1098c77f 177
jasonberry 25:ca1b1098c77f 178 return _parser.send("AT+CIPSTART=%d,\"%s\",\"%s\",%d", id, type, addr, port)
jasonberry 25:ca1b1098c77f 179 && _parser.recv("OK");
jasonberry 25:ca1b1098c77f 180 }
jasonberry 25:ca1b1098c77f 181
jasonberry 25:ca1b1098c77f 182 bool ESP8266::send(int id, const void *data, uint32_t amount)
jasonberry 25:ca1b1098c77f 183 {
jasonberry 25:ca1b1098c77f 184 //May take a second try if device is busy
jasonberry 25:ca1b1098c77f 185 for (unsigned i = 0; i < 2; i++) {
jasonberry 25:ca1b1098c77f 186 if (_parser.send("AT+CIPSEND=%d,%d", id, amount)
jasonberry 25:ca1b1098c77f 187 && _parser.recv(">")
jasonberry 25:ca1b1098c77f 188 && _parser.write((char*)data, (int)amount) >= 0) {
jasonberry 25:ca1b1098c77f 189 return true;
jasonberry 25:ca1b1098c77f 190 }
jasonberry 25:ca1b1098c77f 191 }
jasonberry 25:ca1b1098c77f 192
jasonberry 25:ca1b1098c77f 193 return false;
jasonberry 25:ca1b1098c77f 194 }
jasonberry 25:ca1b1098c77f 195
jasonberry 25:ca1b1098c77f 196 void ESP8266::_packet_handler()
jasonberry 25:ca1b1098c77f 197 {
jasonberry 25:ca1b1098c77f 198 int id;
jasonberry 25:ca1b1098c77f 199 uint32_t amount;
jasonberry 25:ca1b1098c77f 200
jasonberry 25:ca1b1098c77f 201 // parse out the packet
jasonberry 25:ca1b1098c77f 202 if (!_parser.recv(",%d,%d:", &id, &amount)) {
jasonberry 25:ca1b1098c77f 203 return;
jasonberry 25:ca1b1098c77f 204 }
jasonberry 25:ca1b1098c77f 205
jasonberry 25:ca1b1098c77f 206 struct packet *packet = (struct packet*)malloc(
jasonberry 25:ca1b1098c77f 207 sizeof(struct packet) + amount);
jasonberry 25:ca1b1098c77f 208 if (!packet) {
jasonberry 25:ca1b1098c77f 209 return;
jasonberry 25:ca1b1098c77f 210 }
jasonberry 25:ca1b1098c77f 211
jasonberry 25:ca1b1098c77f 212 packet->id = id;
jasonberry 25:ca1b1098c77f 213 packet->len = amount;
jasonberry 25:ca1b1098c77f 214 packet->next = 0;
jasonberry 25:ca1b1098c77f 215
jasonberry 25:ca1b1098c77f 216 if (!(_parser.read((char*)(packet + 1), amount))) {
jasonberry 25:ca1b1098c77f 217 free(packet);
jasonberry 25:ca1b1098c77f 218 return;
jasonberry 25:ca1b1098c77f 219 }
jasonberry 25:ca1b1098c77f 220
jasonberry 25:ca1b1098c77f 221 // append to packet list
jasonberry 25:ca1b1098c77f 222 *_packets_end = packet;
jasonberry 25:ca1b1098c77f 223 _packets_end = &packet->next;
jasonberry 25:ca1b1098c77f 224 }
jasonberry 25:ca1b1098c77f 225
jasonberry 25:ca1b1098c77f 226 int32_t ESP8266::recv(int id, void *data, uint32_t amount)
jasonberry 25:ca1b1098c77f 227 {
jasonberry 25:ca1b1098c77f 228 while (true) {
jasonberry 25:ca1b1098c77f 229 // check if any packets are ready for us
jasonberry 25:ca1b1098c77f 230 for (struct packet **p = &_packets; *p; p = &(*p)->next) {
jasonberry 25:ca1b1098c77f 231 if ((*p)->id == id) {
jasonberry 25:ca1b1098c77f 232 struct packet *q = *p;
jasonberry 25:ca1b1098c77f 233
jasonberry 25:ca1b1098c77f 234 if (q->len <= amount) { // Return and remove full packet
jasonberry 25:ca1b1098c77f 235 memcpy(data, q+1, q->len);
jasonberry 25:ca1b1098c77f 236
jasonberry 25:ca1b1098c77f 237 if (_packets_end == &(*p)->next) {
jasonberry 25:ca1b1098c77f 238 _packets_end = p;
jasonberry 25:ca1b1098c77f 239 }
jasonberry 25:ca1b1098c77f 240 *p = (*p)->next;
jasonberry 25:ca1b1098c77f 241
jasonberry 25:ca1b1098c77f 242 uint32_t len = q->len;
jasonberry 25:ca1b1098c77f 243 free(q);
jasonberry 25:ca1b1098c77f 244 return len;
jasonberry 25:ca1b1098c77f 245 } else { // return only partial packet
jasonberry 25:ca1b1098c77f 246 memcpy(data, q+1, amount);
jasonberry 25:ca1b1098c77f 247
jasonberry 25:ca1b1098c77f 248 q->len -= amount;
jasonberry 25:ca1b1098c77f 249 memmove(q+1, (uint8_t*)(q+1) + amount, q->len);
jasonberry 25:ca1b1098c77f 250
jasonberry 25:ca1b1098c77f 251 return amount;
jasonberry 25:ca1b1098c77f 252 }
jasonberry 25:ca1b1098c77f 253 }
jasonberry 25:ca1b1098c77f 254 }
jasonberry 25:ca1b1098c77f 255
jasonberry 25:ca1b1098c77f 256 // Wait for inbound packet
jasonberry 25:ca1b1098c77f 257 if (!_parser.recv("OK")) {
jasonberry 25:ca1b1098c77f 258 return -1;
jasonberry 25:ca1b1098c77f 259 }
jasonberry 25:ca1b1098c77f 260 }
jasonberry 25:ca1b1098c77f 261 }
jasonberry 25:ca1b1098c77f 262
jasonberry 25:ca1b1098c77f 263 bool ESP8266::close(int id)
jasonberry 25:ca1b1098c77f 264 {
jasonberry 25:ca1b1098c77f 265 //May take a second try if device is busy
jasonberry 25:ca1b1098c77f 266 for (unsigned i = 0; i < 2; i++) {
jasonberry 25:ca1b1098c77f 267 if (_parser.send("AT+CIPCLOSE=%d", id)
jasonberry 25:ca1b1098c77f 268 && _parser.recv("OK")) {
jasonberry 25:ca1b1098c77f 269 return true;
jasonberry 25:ca1b1098c77f 270 }
jasonberry 25:ca1b1098c77f 271 }
jasonberry 25:ca1b1098c77f 272
jasonberry 25:ca1b1098c77f 273 return false;
jasonberry 25:ca1b1098c77f 274 }
jasonberry 25:ca1b1098c77f 275
jasonberry 25:ca1b1098c77f 276 void ESP8266::setTimeout(uint32_t timeout_ms)
jasonberry 25:ca1b1098c77f 277 {
jasonberry 25:ca1b1098c77f 278 _parser.setTimeout(timeout_ms);
jasonberry 25:ca1b1098c77f 279 }
jasonberry 25:ca1b1098c77f 280
jasonberry 25:ca1b1098c77f 281 bool ESP8266::readable()
jasonberry 25:ca1b1098c77f 282 {
jasonberry 25:ca1b1098c77f 283 return _serial.readable();
jasonberry 25:ca1b1098c77f 284 }
jasonberry 25:ca1b1098c77f 285
jasonberry 25:ca1b1098c77f 286 bool ESP8266::writeable()
jasonberry 25:ca1b1098c77f 287 {
jasonberry 25:ca1b1098c77f 288 return _serial.writeable();
jasonberry 25:ca1b1098c77f 289 }
jasonberry 25:ca1b1098c77f 290
jasonberry 25:ca1b1098c77f 291 void ESP8266::attach(Callback<void()> func)
jasonberry 25:ca1b1098c77f 292 {
jasonberry 25:ca1b1098c77f 293 _serial.attach(func);
jasonberry 25:ca1b1098c77f 294 }
jasonberry 25:ca1b1098c77f 295
jasonberry 25:ca1b1098c77f 296 bool ESP8266::recv_ap(nsapi_wifi_ap_t *ap)
jasonberry 25:ca1b1098c77f 297 {
jasonberry 25:ca1b1098c77f 298 int sec;
jasonberry 25:ca1b1098c77f 299 bool ret = _parser.recv("+CWLAP:(%d,\"%32[^\"]\",%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\",%d", &sec, ap->ssid,
jasonberry 25:ca1b1098c77f 300 &ap->rssi, &ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4],
jasonberry 25:ca1b1098c77f 301 &ap->bssid[5], &ap->channel);
jasonberry 25:ca1b1098c77f 302
jasonberry 25:ca1b1098c77f 303 ap->security = sec < 5 ? (nsapi_security_t)sec : NSAPI_SECURITY_UNKNOWN;
jasonberry 25:ca1b1098c77f 304
jasonberry 25:ca1b1098c77f 305 return ret;
jasonberry 25:ca1b1098c77f 306 }