test

Dependencies:   ATParser

Fork of ESP8266 by NetworkSocketAPI

Committer:
Christopher Haster
Date:
Thu Feb 18 16:11:03 2016 -0600
Revision:
18:11f2f6bd2e97
Parent:
17:8b541b19f391
Backported changes to espressif driver

Who changed what in which revision?

UserRevisionLine numberNew 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
Christopher Haster 17:8b541b19f391 22 /** ESP8266Interface class.
Christopher Haster 17:8b541b19f391 23 This is an interface to a ESP8266 radio.
Christopher Haster 17:8b541b19f391 24 */
Christopher Haster 18:11f2f6bd2e97 25 class ESP8266
Christopher Haster 17:8b541b19f391 26 {
Christopher Haster 17:8b541b19f391 27 public:
Christopher Haster 18:11f2f6bd2e97 28 ESP8266(PinName tx, PinName rx, bool debug=false);
Christopher Haster 18:11f2f6bd2e97 29
Christopher Haster 17:8b541b19f391 30 /**
Christopher Haster 18:11f2f6bd2e97 31 * Startup the ESP8266
Christopher Haster 17:8b541b19f391 32 *
Christopher Haster 18:11f2f6bd2e97 33 * @param mode mode of WIFI 1-client, 2-host, 3-both
Christopher Haster 18:11f2f6bd2e97 34 * @return true only if ESP8266 was setup correctly
Christopher Haster 17:8b541b19f391 35 */
Christopher Haster 18:11f2f6bd2e97 36 bool startup(int mode);
Christopher Haster 18:11f2f6bd2e97 37
Christopher Haster 17:8b541b19f391 38 /**
Christopher Haster 17:8b541b19f391 39 * Reset ESP8266
Christopher Haster 17:8b541b19f391 40 *
Christopher Haster 18:11f2f6bd2e97 41 * @return true only if ESP8266 resets successfully
Christopher Haster 17:8b541b19f391 42 */
Christopher Haster 17:8b541b19f391 43 bool reset(void);
Christopher Haster 18:11f2f6bd2e97 44
Christopher Haster 17:8b541b19f391 45 /**
Christopher Haster 17:8b541b19f391 46 * Enable/Disable DHCP
Christopher Haster 17:8b541b19f391 47 *
Christopher Haster 18:11f2f6bd2e97 48 * @param enabled DHCP enabled when true
Christopher Haster 17:8b541b19f391 49 * @param mode mode of DHCP 0-softAP, 1-station, 2-both
Christopher Haster 17:8b541b19f391 50 * @return true only if ESP8266 enables/disables DHCP successfully
Christopher Haster 17:8b541b19f391 51 */
Christopher Haster 18:11f2f6bd2e97 52 bool dhcp(bool enabled, int mode);
Christopher Haster 18:11f2f6bd2e97 53
Christopher Haster 17:8b541b19f391 54 /**
Christopher Haster 17:8b541b19f391 55 * Connect ESP8266 to AP
Christopher Haster 17:8b541b19f391 56 *
Christopher Haster 17:8b541b19f391 57 * @param ap the name of the AP
Christopher Haster 17:8b541b19f391 58 * @param passPhrase the password of AP
Christopher Haster 17:8b541b19f391 59 * @return true only if ESP8266 is connected successfully
Christopher Haster 17:8b541b19f391 60 */
Christopher Haster 17:8b541b19f391 61 bool connect(const char *ap, const char *passPhrase);
Christopher Haster 18:11f2f6bd2e97 62
Christopher Haster 17:8b541b19f391 63 /**
Christopher Haster 17:8b541b19f391 64 * Disconnect ESP8266 from AP
Christopher Haster 17:8b541b19f391 65 *
Christopher Haster 17:8b541b19f391 66 * @return true only if ESP8266 is disconnected successfully
Christopher Haster 17:8b541b19f391 67 */
Christopher Haster 17:8b541b19f391 68 bool disconnect(void);
Christopher Haster 18:11f2f6bd2e97 69
Christopher Haster 17:8b541b19f391 70 /**
Christopher Haster 17:8b541b19f391 71 * Get the IP address of ESP8266
Christopher Haster 17:8b541b19f391 72 *
Christopher Haster 18:11f2f6bd2e97 73 * @return null-teriminated IP address or null if no IP address is assigned
Christopher Haster 17:8b541b19f391 74 */
Christopher Haster 18:11f2f6bd2e97 75 const char *getIPAddress(void);
Christopher Haster 18:11f2f6bd2e97 76
Christopher Haster 18:11f2f6bd2e97 77 /**
Christopher Haster 18:11f2f6bd2e97 78 * Get the MAC address of ESP8266
Christopher Haster 18:11f2f6bd2e97 79 *
Christopher Haster 18:11f2f6bd2e97 80 * @return null-terminated MAC address or null if no MAC address is assigned
Christopher Haster 18:11f2f6bd2e97 81 */
Christopher Haster 18:11f2f6bd2e97 82 const char *getMACAddress(void);
Christopher Haster 18:11f2f6bd2e97 83
Christopher Haster 17:8b541b19f391 84 /**
Christopher Haster 17:8b541b19f391 85 * Check if ESP8266 is conenected
Christopher Haster 17:8b541b19f391 86 *
Christopher Haster 17:8b541b19f391 87 * @return true only if the chip has an IP address
Christopher Haster 17:8b541b19f391 88 */
Christopher Haster 17:8b541b19f391 89 bool isConnected(void);
Christopher Haster 18:11f2f6bd2e97 90
Christopher Haster 17:8b541b19f391 91 /**
Christopher Haster 18:11f2f6bd2e97 92 * Open a socketed connection
Christopher Haster 17:8b541b19f391 93 *
Christopher Haster 18:11f2f6bd2e97 94 * @param type the type of socket to open "UDP" or "TCP"
Christopher Haster 17:8b541b19f391 95 * @param id id to give the new socket, valid 0-4
Christopher Haster 17:8b541b19f391 96 * @param port port to open connection with
Christopher Haster 18:11f2f6bd2e97 97 * @param addr the IP address of the destination
Christopher Haster 17:8b541b19f391 98 * @return true only if socket opened successfully
Christopher Haster 17:8b541b19f391 99 */
Christopher Haster 18:11f2f6bd2e97 100 bool open(const char *type, int id, const char* addr, int port);
Christopher Haster 18:11f2f6bd2e97 101
Christopher Haster 17:8b541b19f391 102 /**
Christopher Haster 17:8b541b19f391 103 * Sends data to an open socket
Christopher Haster 17:8b541b19f391 104 *
Christopher Haster 17:8b541b19f391 105 * @param id id of socket to send to
Christopher Haster 17:8b541b19f391 106 * @param data data to be sent
Christopher Haster 17:8b541b19f391 107 * @param amount amount of data to be sent - max 1024
Christopher Haster 17:8b541b19f391 108 * @return true only if data sent successfully
Christopher Haster 17:8b541b19f391 109 */
Christopher Haster 18:11f2f6bd2e97 110 bool send(int id, const void *data, uint32_t amount);
Christopher Haster 18:11f2f6bd2e97 111
Christopher Haster 17:8b541b19f391 112 /**
Christopher Haster 18:11f2f6bd2e97 113 * Receives data from an open socket
Christopher Haster 17:8b541b19f391 114 *
Christopher Haster 18:11f2f6bd2e97 115 * @param id id to receive from
Christopher Haster 17:8b541b19f391 116 * @param data placeholder for returned information
Christopher Haster 17:8b541b19f391 117 * @param amount number of bytes to be received
Christopher Haster 18:11f2f6bd2e97 118 * @return the number of bytes received
Christopher Haster 17:8b541b19f391 119 */
Christopher Haster 18:11f2f6bd2e97 120 int32_t recv(int id, void *data, uint32_t amount);
Christopher Haster 18:11f2f6bd2e97 121
Christopher Haster 17:8b541b19f391 122 /**
Christopher Haster 17:8b541b19f391 123 * Closes a socket
Christopher Haster 17:8b541b19f391 124 *
Christopher Haster 17:8b541b19f391 125 * @param id id of socket to close, valid only 0-4
Christopher Haster 17:8b541b19f391 126 * @return true only if socket is closed successfully
Christopher Haster 17:8b541b19f391 127 */
Christopher Haster 17:8b541b19f391 128 bool close(int id);
Christopher Haster 18:11f2f6bd2e97 129
Christopher Haster 17:8b541b19f391 130 /**
Christopher Haster 17:8b541b19f391 131 * Allows timeout to be changed between commands
Christopher Haster 17:8b541b19f391 132 *
Christopher Haster 17:8b541b19f391 133 * @param timeout_ms timeout of the connection
Christopher Haster 17:8b541b19f391 134 */
Christopher Haster 17:8b541b19f391 135 void setTimeout(uint32_t timeout_ms);
Christopher Haster 18:11f2f6bd2e97 136
Christopher Haster 18:11f2f6bd2e97 137 /**
Christopher Haster 18:11f2f6bd2e97 138 * Checks if data is available
Christopher Haster 18:11f2f6bd2e97 139 */
Christopher Haster 18:11f2f6bd2e97 140 bool readable();
Christopher Haster 18:11f2f6bd2e97 141
Christopher Haster 18:11f2f6bd2e97 142 /**
Christopher Haster 18:11f2f6bd2e97 143 * Checks if data can be written
Christopher Haster 18:11f2f6bd2e97 144 */
Christopher Haster 18:11f2f6bd2e97 145 bool writeable();
Christopher Haster 18:11f2f6bd2e97 146
Christopher Haster 17:8b541b19f391 147 private:
Christopher Haster 18:11f2f6bd2e97 148 BufferedSerial _serial;
Christopher Haster 18:11f2f6bd2e97 149 ATParser _parser;
Christopher Haster 18:11f2f6bd2e97 150
Christopher Haster 18:11f2f6bd2e97 151 char _ip_buffer[16];
Christopher Haster 18:11f2f6bd2e97 152 char _mac_buffer[18];
Christopher Haster 17:8b541b19f391 153 };
Christopher Haster 17:8b541b19f391 154
Christopher Haster 17:8b541b19f391 155 #endif