Driver for the ESP8266 WiFi module using ATParser library. Espressif Firmware.
Dependencies: ATParser
Fork of ESP8266 by
Note
This library assumes your ESP8266 is running the Espressif Firmware. For instructions on how to update your ESP8266 to use the correct firmware see the Firmware Update Wiki Page.
ESP8266.h@17:8b541b19f391, 2016-02-18 (annotated)
- Committer:
- Christopher Haster
- Date:
- Thu Feb 18 16:08:29 2016 -0600
- Revision:
- 17:8b541b19f391
- Parent:
- 14:13cef05d740c
- Child:
- 18:11f2f6bd2e97
Removed carraige returns
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Christopher Haster |
17:8b541b19f391 | 1 | /* ESP8266Interface Example |
Christopher Haster |
17:8b541b19f391 | 2 | * Copyright (c) 2015 ARM Limited |
Christopher Haster |
17:8b541b19f391 | 3 | * |
Christopher Haster |
17:8b541b19f391 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Christopher Haster |
17:8b541b19f391 | 5 | * you may not use this file except in compliance with the License. |
Christopher Haster |
17:8b541b19f391 | 6 | * You may obtain a copy of the License at |
Christopher Haster |
17:8b541b19f391 | 7 | * |
Christopher Haster |
17:8b541b19f391 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Christopher Haster |
17:8b541b19f391 | 9 | * |
Christopher Haster |
17:8b541b19f391 | 10 | * Unless required by applicable law or agreed to in writing, software |
Christopher Haster |
17:8b541b19f391 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Christopher Haster |
17:8b541b19f391 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Christopher Haster |
17:8b541b19f391 | 13 | * See the License for the specific language governing permissions and |
Christopher Haster |
17:8b541b19f391 | 14 | * limitations under the License. |
Christopher Haster |
17:8b541b19f391 | 15 | */ |
Christopher Haster |
17:8b541b19f391 | 16 | |
Christopher Haster |
17:8b541b19f391 | 17 | #ifndef ESP8266_H |
Christopher Haster |
17:8b541b19f391 | 18 | #define ESP8266_H |
Christopher Haster |
17:8b541b19f391 | 19 | |
Christopher Haster |
17:8b541b19f391 | 20 | #include "ATParser.h" |
Christopher Haster |
17:8b541b19f391 | 21 | #include <string> |
Christopher Haster |
17:8b541b19f391 | 22 | |
Christopher Haster |
17:8b541b19f391 | 23 | /** ESP8266Interface class. |
Christopher Haster |
17:8b541b19f391 | 24 | This is an interface to a ESP8266 radio. |
Christopher Haster |
17:8b541b19f391 | 25 | */ |
Christopher Haster |
17:8b541b19f391 | 26 | class ESP8266 |
Christopher Haster |
17:8b541b19f391 | 27 | { |
Christopher Haster |
17:8b541b19f391 | 28 | public: |
Christopher Haster |
17:8b541b19f391 | 29 | ESP8266(PinName tx, PinName rx, uint8_t debug = 0); |
Christopher Haster |
17:8b541b19f391 | 30 | |
Christopher Haster |
17:8b541b19f391 | 31 | /** |
Christopher Haster |
17:8b541b19f391 | 32 | * Test startup of ESP8266 |
Christopher Haster |
17:8b541b19f391 | 33 | * |
Christopher Haster |
17:8b541b19f391 | 34 | * @return true only if ESP8266 AT system working correctly |
Christopher Haster |
17:8b541b19f391 | 35 | */ |
Christopher Haster |
17:8b541b19f391 | 36 | bool startup(void); |
Christopher Haster |
17:8b541b19f391 | 37 | |
Christopher Haster |
17:8b541b19f391 | 38 | /** |
Christopher Haster |
17:8b541b19f391 | 39 | * Reset ESP8266 |
Christopher Haster |
17:8b541b19f391 | 40 | * |
Christopher Haster |
17:8b541b19f391 | 41 | * @return true only if ESP8266 resets successfully |
Christopher Haster |
17:8b541b19f391 | 42 | */ |
Christopher Haster |
17:8b541b19f391 | 43 | bool reset(void); |
Christopher Haster |
17:8b541b19f391 | 44 | |
Christopher Haster |
17:8b541b19f391 | 45 | /** |
Christopher Haster |
17:8b541b19f391 | 46 | * Set WiFi mode |
Christopher Haster |
17:8b541b19f391 | 47 | * |
Christopher Haster |
17:8b541b19f391 | 48 | * @param mode mode of WiFi 1-client, 2-host, 3-both |
Christopher Haster |
17:8b541b19f391 | 49 | * @return true only if ESP8266 enables/disables DHCP successfully |
Christopher Haster |
17:8b541b19f391 | 50 | */ |
Christopher Haster |
17:8b541b19f391 | 51 | bool wifiMode(int mode); |
Christopher Haster |
17:8b541b19f391 | 52 | |
Christopher Haster |
17:8b541b19f391 | 53 | /** |
Christopher Haster |
17:8b541b19f391 | 54 | * Enable/Disable multiple connections |
Christopher Haster |
17:8b541b19f391 | 55 | * |
Christopher Haster |
17:8b541b19f391 | 56 | * @param enabled multiple connections enabled when true |
Christopher Haster |
17:8b541b19f391 | 57 | * @return true only if ESP8266 enables/disables multiple connections successfully |
Christopher Haster |
17:8b541b19f391 | 58 | */ |
Christopher Haster |
17:8b541b19f391 | 59 | bool multipleConnections(bool enabled); |
Christopher Haster |
17:8b541b19f391 | 60 | |
Christopher Haster |
17:8b541b19f391 | 61 | /** |
Christopher Haster |
17:8b541b19f391 | 62 | * Enable/Disable DHCP |
Christopher Haster |
17:8b541b19f391 | 63 | * |
Christopher Haster |
17:8b541b19f391 | 64 | * @param mode mode of DHCP 0-softAP, 1-station, 2-both |
Christopher Haster |
17:8b541b19f391 | 65 | * @param enabled DHCP enabled when true |
Christopher Haster |
17:8b541b19f391 | 66 | * @return true only if ESP8266 enables/disables DHCP successfully |
Christopher Haster |
17:8b541b19f391 | 67 | */ |
Christopher Haster |
17:8b541b19f391 | 68 | bool dhcp(int mode, bool enabled); |
Christopher Haster |
17:8b541b19f391 | 69 | |
Christopher Haster |
17:8b541b19f391 | 70 | /** |
Christopher Haster |
17:8b541b19f391 | 71 | * Connect ESP8266 to AP |
Christopher Haster |
17:8b541b19f391 | 72 | * |
Christopher Haster |
17:8b541b19f391 | 73 | * @param ap the name of the AP |
Christopher Haster |
17:8b541b19f391 | 74 | * @param passPhrase the password of AP |
Christopher Haster |
17:8b541b19f391 | 75 | * @return true only if ESP8266 is connected successfully |
Christopher Haster |
17:8b541b19f391 | 76 | */ |
Christopher Haster |
17:8b541b19f391 | 77 | bool connect(const char *ap, const char *passPhrase); |
Christopher Haster |
17:8b541b19f391 | 78 | |
Christopher Haster |
17:8b541b19f391 | 79 | /** |
Christopher Haster |
17:8b541b19f391 | 80 | * Disconnect ESP8266 from AP |
Christopher Haster |
17:8b541b19f391 | 81 | * |
Christopher Haster |
17:8b541b19f391 | 82 | * @return true only if ESP8266 is disconnected successfully |
Christopher Haster |
17:8b541b19f391 | 83 | */ |
Christopher Haster |
17:8b541b19f391 | 84 | bool disconnect(void); |
Christopher Haster |
17:8b541b19f391 | 85 | |
Christopher Haster |
17:8b541b19f391 | 86 | /** |
Christopher Haster |
17:8b541b19f391 | 87 | * Get the IP address of ESP8266 |
Christopher Haster |
17:8b541b19f391 | 88 | * |
Christopher Haster |
17:8b541b19f391 | 89 | * @param ip data placeholder for IP address |
Christopher Haster |
17:8b541b19f391 | 90 | * @return true only if ESP8266 is assigned an IP address |
Christopher Haster |
17:8b541b19f391 | 91 | */ |
Christopher Haster |
17:8b541b19f391 | 92 | bool getIPAddress(char* ip); |
Christopher Haster |
17:8b541b19f391 | 93 | |
Christopher Haster |
17:8b541b19f391 | 94 | /** |
Christopher Haster |
17:8b541b19f391 | 95 | * Check if ESP8266 is conenected |
Christopher Haster |
17:8b541b19f391 | 96 | * |
Christopher Haster |
17:8b541b19f391 | 97 | * @return true only if the chip has an IP address |
Christopher Haster |
17:8b541b19f391 | 98 | */ |
Christopher Haster |
17:8b541b19f391 | 99 | bool isConnected(void); |
Christopher Haster |
17:8b541b19f391 | 100 | |
Christopher Haster |
17:8b541b19f391 | 101 | /** |
Christopher Haster |
17:8b541b19f391 | 102 | * Open a socketed connection |
Christopher Haster |
17:8b541b19f391 | 103 | * |
Christopher Haster |
17:8b541b19f391 | 104 | * @param sockType the type of socket to open "UDP" or "TCP" |
Christopher Haster |
17:8b541b19f391 | 105 | * @param id id to give the new socket, valid 0-4 |
Christopher Haster |
17:8b541b19f391 | 106 | * @param port port to open connection with |
Christopher Haster |
17:8b541b19f391 | 107 | * @param addr the IP address of the destination |
Christopher Haster |
17:8b541b19f391 | 108 | * @return true only if socket opened successfully |
Christopher Haster |
17:8b541b19f391 | 109 | */ |
Christopher Haster |
17:8b541b19f391 | 110 | bool openSocket(string sockType, int id, int port, const char* addr); |
Christopher Haster |
17:8b541b19f391 | 111 | |
Christopher Haster |
17:8b541b19f391 | 112 | /** |
Christopher Haster |
17:8b541b19f391 | 113 | * Sends data to an open socket |
Christopher Haster |
17:8b541b19f391 | 114 | * |
Christopher Haster |
17:8b541b19f391 | 115 | * @param id id of socket to send to |
Christopher Haster |
17:8b541b19f391 | 116 | * @param data data to be sent |
Christopher Haster |
17:8b541b19f391 | 117 | * @param amount amount of data to be sent - max 1024 |
Christopher Haster |
17:8b541b19f391 | 118 | * @return true only if data sent successfully |
Christopher Haster |
17:8b541b19f391 | 119 | */ |
Christopher Haster |
17:8b541b19f391 | 120 | bool sendData(int id, const void *data, uint32_t amount); |
Christopher Haster |
17:8b541b19f391 | 121 | |
Christopher Haster |
17:8b541b19f391 | 122 | /** |
Christopher Haster |
17:8b541b19f391 | 123 | * Receives data from an open socket |
Christopher Haster |
17:8b541b19f391 | 124 | * |
Christopher Haster |
17:8b541b19f391 | 125 | * @param data placeholder for returned information |
Christopher Haster |
17:8b541b19f391 | 126 | * @param amount number of bytes to be received |
Christopher Haster |
17:8b541b19f391 | 127 | * @return the number of bytes actually received |
Christopher Haster |
17:8b541b19f391 | 128 | */ |
Christopher Haster |
17:8b541b19f391 | 129 | uint32_t recv(void *data, uint32_t amount); |
Christopher Haster |
17:8b541b19f391 | 130 | |
Christopher Haster |
17:8b541b19f391 | 131 | /** |
Christopher Haster |
17:8b541b19f391 | 132 | * Closes a socket |
Christopher Haster |
17:8b541b19f391 | 133 | * |
Christopher Haster |
17:8b541b19f391 | 134 | * @param id id of socket to close, valid only 0-4 |
Christopher Haster |
17:8b541b19f391 | 135 | * @return true only if socket is closed successfully |
Christopher Haster |
17:8b541b19f391 | 136 | */ |
Christopher Haster |
17:8b541b19f391 | 137 | bool close(int id); |
Christopher Haster |
17:8b541b19f391 | 138 | |
Christopher Haster |
17:8b541b19f391 | 139 | /** |
Christopher Haster |
17:8b541b19f391 | 140 | * Allows timeout to be changed between commands |
Christopher Haster |
17:8b541b19f391 | 141 | * |
Christopher Haster |
17:8b541b19f391 | 142 | * @param timeout_ms timeout of the connection |
Christopher Haster |
17:8b541b19f391 | 143 | */ |
Christopher Haster |
17:8b541b19f391 | 144 | void setTimeout(uint32_t timeout_ms); |
Christopher Haster |
17:8b541b19f391 | 145 | |
Christopher Haster |
17:8b541b19f391 | 146 | private: |
Christopher Haster |
17:8b541b19f391 | 147 | BufferedSerial serial; |
Christopher Haster |
17:8b541b19f391 | 148 | ATParser atParser; |
Christopher Haster |
17:8b541b19f391 | 149 | }; |
Christopher Haster |
17:8b541b19f391 | 150 | |
Christopher Haster |
17:8b541b19f391 | 151 | #endif |