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