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