A fork of the original interface for OS/2. Features a correctly-implemented recv (but retains the old behavior via recv2).
Dependents: weather_clock weather_clock
ESP8266/ESP8266.cpp@49:cac09a34102c, 2019-03-31 (annotated)
- Committer:
- alexhrao
- Date:
- Sun Mar 31 00:53:46 2019 +0000
- Revision:
- 49:cac09a34102c
- Parent:
- 47:04632d22a723
- Child:
- 50:f484783b8a34
Add correct implementation for recv
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
geky | 44:16da10e7b3f7 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
geky | 44:16da10e7b3f7 | 2 | * |
geky | 44:16da10e7b3f7 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
geky | 44:16da10e7b3f7 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
geky | 44:16da10e7b3f7 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
geky | 44:16da10e7b3f7 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
geky | 44:16da10e7b3f7 | 7 | * furnished to do so, subject to the following conditions: |
geky | 44:16da10e7b3f7 | 8 | * |
geky | 44:16da10e7b3f7 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
geky | 44:16da10e7b3f7 | 10 | * substantial portions of the Software. |
geky | 44:16da10e7b3f7 | 11 | * |
geky | 44:16da10e7b3f7 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
geky | 44:16da10e7b3f7 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
geky | 44:16da10e7b3f7 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
geky | 44:16da10e7b3f7 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
geky | 44:16da10e7b3f7 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
geky | 44:16da10e7b3f7 | 17 | */ |
geky | 44:16da10e7b3f7 | 18 | |
geky | 44:16da10e7b3f7 | 19 | #include "mbed.h" |
geky | 44:16da10e7b3f7 | 20 | #include "ESP8266.h" |
geky | 44:16da10e7b3f7 | 21 | #include "Endpoint.h" |
geky | 44:16da10e7b3f7 | 22 | #include <string> |
geky | 44:16da10e7b3f7 | 23 | #include <algorithm> |
geky | 44:16da10e7b3f7 | 24 | |
geky | 44:16da10e7b3f7 | 25 | //Debug is disabled by default |
alexhrao | 49:cac09a34102c | 26 | #if 1 |
geky | 44:16da10e7b3f7 | 27 | #define DBG(x, ...) printf("[ESP8266 : DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); |
geky | 44:16da10e7b3f7 | 28 | #define WARN(x, ...) printf("[ESP8266 : WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); |
geky | 44:16da10e7b3f7 | 29 | #define ERR(x, ...) printf("[ESP8266 : ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); |
geky | 44:16da10e7b3f7 | 30 | #define INFO(x, ...) printf("[ESP8266 : INFO]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); |
geky | 44:16da10e7b3f7 | 31 | #ifndef ESP8266_ECHO |
geky | 44:16da10e7b3f7 | 32 | #define ESP8266_ECHO 1 |
geky | 44:16da10e7b3f7 | 33 | #endif |
geky | 44:16da10e7b3f7 | 34 | #else |
geky | 44:16da10e7b3f7 | 35 | #define DBG(x, ...) |
geky | 44:16da10e7b3f7 | 36 | #define WARN(x, ...) |
geky | 44:16da10e7b3f7 | 37 | #define ERR(x, ...) |
geky | 44:16da10e7b3f7 | 38 | #define INFO(x, ...) |
geky | 44:16da10e7b3f7 | 39 | #endif |
geky | 44:16da10e7b3f7 | 40 | |
alexhrao | 49:cac09a34102c | 41 | int tmp_buf_ind = 0; |
alexhrao | 49:cac09a34102c | 42 | char tmp_buf[8192]; |
alexhrao | 49:cac09a34102c | 43 | RawSerial dev1(p28,p27); |
geky | 44:16da10e7b3f7 | 44 | ESP8266 *ESP8266::_inst; |
geky | 44:16da10e7b3f7 | 45 | |
geky | 44:16da10e7b3f7 | 46 | ESP8266::ESP8266(PinName tx, PinName rx, PinName reset, int baud, int timeout) : |
geky | 44:16da10e7b3f7 | 47 | _serial(tx, rx), _reset_pin(reset) { |
geky | 44:16da10e7b3f7 | 48 | INFO("Initializing ESP8266 object"); |
geky | 44:16da10e7b3f7 | 49 | |
geky | 44:16da10e7b3f7 | 50 | _baud = baud; |
geky | 44:16da10e7b3f7 | 51 | _timeout = timeout; |
geky | 44:16da10e7b3f7 | 52 | |
geky | 44:16da10e7b3f7 | 53 | _serial.baud(_baud); |
geky | 44:16da10e7b3f7 | 54 | |
geky | 44:16da10e7b3f7 | 55 | _inst = this; |
geky | 44:16da10e7b3f7 | 56 | } |
geky | 44:16da10e7b3f7 | 57 | |
geky | 44:16da10e7b3f7 | 58 | bool ESP8266::reset() { |
geky | 44:16da10e7b3f7 | 59 | _reset_pin = 0; |
geky | 44:16da10e7b3f7 | 60 | wait_us(20); |
geky | 44:16da10e7b3f7 | 61 | _reset_pin = 1; |
geky | 44:16da10e7b3f7 | 62 | |
geky | 44:16da10e7b3f7 | 63 | // Send reboot command in case reset is not connected |
geky | 44:16da10e7b3f7 | 64 | return command("\x03\r\n" "node.restart()") && execute(); |
geky | 44:16da10e7b3f7 | 65 | } |
geky | 44:16da10e7b3f7 | 66 | |
geky | 44:16da10e7b3f7 | 67 | bool ESP8266::init() { |
geky | 44:16da10e7b3f7 | 68 | // Reset device to clear state |
geky | 44:16da10e7b3f7 | 69 | return reset(); |
geky | 44:16da10e7b3f7 | 70 | } |
geky | 44:16da10e7b3f7 | 71 | |
geky | 44:16da10e7b3f7 | 72 | bool ESP8266::connect(const char *ssid, const char *phrase) { |
geky | 44:16da10e7b3f7 | 73 | // Configure as station with passed ssid and passphrase |
geky | 44:16da10e7b3f7 | 74 | if (!(command("wifi.setmode(wifi.STATION);") && |
alexhrao | 49:cac09a34102c | 75 | command("wifi.sta.config(\"") && |
geky | 44:16da10e7b3f7 | 76 | command(ssid) && |
alexhrao | 49:cac09a34102c | 77 | command("\",\"") && |
geky | 44:16da10e7b3f7 | 78 | command(phrase) && |
alexhrao | 49:cac09a34102c | 79 | command("\")") && |
geky | 44:16da10e7b3f7 | 80 | execute())) |
geky | 44:16da10e7b3f7 | 81 | return false; |
geky | 44:16da10e7b3f7 | 82 | |
geky | 44:16da10e7b3f7 | 83 | // Wait for an IP address |
geky | 44:16da10e7b3f7 | 84 | // TODO WISHLIST make this a seperate check so it can run asynch? |
geky | 44:16da10e7b3f7 | 85 | Timer timer; |
geky | 44:16da10e7b3f7 | 86 | timer.start(); |
alexhrao | 49:cac09a34102c | 87 | |
geky | 44:16da10e7b3f7 | 88 | while (true) { |
geky | 44:16da10e7b3f7 | 89 | int ip_len = 15; |
geky | 44:16da10e7b3f7 | 90 | |
geky | 44:16da10e7b3f7 | 91 | if (!(command("ip=wifi.sta.getip();") && |
geky | 44:16da10e7b3f7 | 92 | command("print(ip)") && |
geky | 44:16da10e7b3f7 | 93 | execute(_ip, &ip_len))) |
geky | 44:16da10e7b3f7 | 94 | return false; |
geky | 44:16da10e7b3f7 | 95 | |
geky | 44:16da10e7b3f7 | 96 | _ip[ip_len] = 0; |
geky | 44:16da10e7b3f7 | 97 | |
geky | 44:16da10e7b3f7 | 98 | if (strcmp(_ip, "nil") != 0) |
geky | 44:16da10e7b3f7 | 99 | return true; |
geky | 44:16da10e7b3f7 | 100 | |
geky | 44:16da10e7b3f7 | 101 | if (timer.read_ms() > _timeout) |
geky | 44:16da10e7b3f7 | 102 | return false; |
geky | 44:16da10e7b3f7 | 103 | } |
geky | 44:16da10e7b3f7 | 104 | } |
geky | 44:16da10e7b3f7 | 105 | |
geky | 44:16da10e7b3f7 | 106 | bool ESP8266::disconnect() { |
geky | 44:16da10e7b3f7 | 107 | int ip_len = 15; |
geky | 44:16da10e7b3f7 | 108 | |
geky | 44:16da10e7b3f7 | 109 | if (!(command("wifi.sta.disconnect();") && |
geky | 44:16da10e7b3f7 | 110 | command("ip=wifi.sta.getip();") && |
geky | 44:16da10e7b3f7 | 111 | command("print(ip)") && |
geky | 44:16da10e7b3f7 | 112 | execute(_ip, &ip_len))) |
geky | 44:16da10e7b3f7 | 113 | return false; |
geky | 44:16da10e7b3f7 | 114 | |
geky | 44:16da10e7b3f7 | 115 | _ip[ip_len] = 0; |
geky | 44:16da10e7b3f7 | 116 | |
geky | 44:16da10e7b3f7 | 117 | return (strcmp(_ip, "nil") == 0); |
geky | 44:16da10e7b3f7 | 118 | } |
geky | 44:16da10e7b3f7 | 119 | |
geky | 44:16da10e7b3f7 | 120 | bool ESP8266::is_connected() { |
geky | 44:16da10e7b3f7 | 121 | return (strcmp(_ip, "nil") != 0); |
geky | 44:16da10e7b3f7 | 122 | } |
geky | 44:16da10e7b3f7 | 123 | |
geky | 44:16da10e7b3f7 | 124 | bool ESP8266::open(bool type, char* ip, int port, int id) { |
geky | 44:16da10e7b3f7 | 125 | // Create the actual connection |
geky | 44:16da10e7b3f7 | 126 | if (!(command("c=net.createConnection(") && |
geky | 44:16da10e7b3f7 | 127 | command(type ? "net.TCP" : "net.UDP") && |
geky | 44:16da10e7b3f7 | 128 | command(")") && |
geky | 44:16da10e7b3f7 | 129 | execute())) |
geky | 44:16da10e7b3f7 | 130 | return false; |
geky | 44:16da10e7b3f7 | 131 | |
geky | 44:16da10e7b3f7 | 132 | // Setup handlers for connecting and disconnecting |
geky | 44:16da10e7b3f7 | 133 | if (!(command("cc=nil;") && |
geky | 44:16da10e7b3f7 | 134 | command("c:on('connection',function() cc=true end);") && |
geky | 44:16da10e7b3f7 | 135 | command("c:on('disconnection',function() cc=false end)") && |
geky | 44:16da10e7b3f7 | 136 | execute())) |
geky | 44:16da10e7b3f7 | 137 | return false; |
geky | 44:16da10e7b3f7 | 138 | |
geky | 44:16da10e7b3f7 | 139 | // Setup functions for sending and recieving characters on the esp side |
geky | 44:16da10e7b3f7 | 140 | if (!(command("cm='';") && |
alexhrao | 49:cac09a34102c | 141 | command("function cs(n) c:send(n); end;") && |
geky | 44:16da10e7b3f7 | 142 | command("function ca() print(#cm) end;") && |
geky | 45:3cfb7406d993 | 143 | command("function cr(n) " |
geky | 45:3cfb7406d993 | 144 | "d=cm:sub(1,n):gsub('.', function(s) " |
geky | 45:3cfb7406d993 | 145 | "return s.format('\\\\%03d', s:byte(1)) " |
geky | 45:3cfb7406d993 | 146 | "end);" |
alexhrao | 49:cac09a34102c | 147 | "cm=cm:sub(n+1,-1); " |
geky | 45:3cfb7406d993 | 148 | "print(d) " |
geky | 45:3cfb7406d993 | 149 | "end;") && |
alexhrao | 49:cac09a34102c | 150 | command("c:on('receive',function(c,n) " |
alexhrao | 49:cac09a34102c | 151 | "cm=cm..n " |
alexhrao | 49:cac09a34102c | 152 | "end)") && |
geky | 44:16da10e7b3f7 | 153 | execute())) |
geky | 44:16da10e7b3f7 | 154 | return false; |
geky | 44:16da10e7b3f7 | 155 | |
geky | 44:16da10e7b3f7 | 156 | // Convert port to a string |
geky | 44:16da10e7b3f7 | 157 | char port_buf[16]; |
geky | 44:16da10e7b3f7 | 158 | sprintf(port_buf, "%d", port); |
geky | 44:16da10e7b3f7 | 159 | |
geky | 44:16da10e7b3f7 | 160 | // Connect to the ip address |
geky | 44:16da10e7b3f7 | 161 | if (!(command("c:connect(") && |
geky | 44:16da10e7b3f7 | 162 | command(port_buf) && |
geky | 44:16da10e7b3f7 | 163 | command(",'") && |
geky | 44:16da10e7b3f7 | 164 | command(ip) && |
geky | 44:16da10e7b3f7 | 165 | command("')") && |
geky | 44:16da10e7b3f7 | 166 | execute())) |
geky | 44:16da10e7b3f7 | 167 | return false; |
geky | 44:16da10e7b3f7 | 168 | |
geky | 44:16da10e7b3f7 | 169 | // Wait for it to connect |
geky | 44:16da10e7b3f7 | 170 | // TODO WISHLIST make this a seperate check so it can run asynch? |
geky | 44:16da10e7b3f7 | 171 | Timer timer; |
geky | 44:16da10e7b3f7 | 172 | timer.start(); |
geky | 44:16da10e7b3f7 | 173 | |
geky | 44:16da10e7b3f7 | 174 | while (true) { |
geky | 44:16da10e7b3f7 | 175 | char con_buf[5]; |
geky | 44:16da10e7b3f7 | 176 | int con_len = 5; |
geky | 44:16da10e7b3f7 | 177 | |
geky | 44:16da10e7b3f7 | 178 | if (!(command("print(cc)") && execute(con_buf, &con_len))) |
geky | 44:16da10e7b3f7 | 179 | return false; |
geky | 44:16da10e7b3f7 | 180 | |
alexhrao | 49:cac09a34102c | 181 | if (memcmp(con_buf, "true", con_len) == 0) { |
geky | 44:16da10e7b3f7 | 182 | return true; |
alexhrao | 49:cac09a34102c | 183 | } |
geky | 44:16da10e7b3f7 | 184 | else if (memcmp(con_buf, "false", con_len) == 0) |
geky | 44:16da10e7b3f7 | 185 | return false; |
geky | 44:16da10e7b3f7 | 186 | |
geky | 44:16da10e7b3f7 | 187 | if (timer.read_ms() > _timeout) |
geky | 44:16da10e7b3f7 | 188 | return false; |
geky | 44:16da10e7b3f7 | 189 | } |
geky | 44:16da10e7b3f7 | 190 | } |
geky | 44:16da10e7b3f7 | 191 | |
geky | 44:16da10e7b3f7 | 192 | bool ESP8266::close() { |
geky | 44:16da10e7b3f7 | 193 | return command("c:close();" "c=nil") && execute(); |
geky | 44:16da10e7b3f7 | 194 | } |
geky | 44:16da10e7b3f7 | 195 | |
geky | 46:4abb80f0a920 | 196 | bool ESP8266::send(const char *buffer, int len) { |
geky | 46:4abb80f0a920 | 197 | for (int line = 0; line < len; line += ESP_MAX_LINE) { |
geky | 46:4abb80f0a920 | 198 | if (!command("cs('")) |
geky | 46:4abb80f0a920 | 199 | return false; |
geky | 47:04632d22a723 | 200 | |
geky | 46:4abb80f0a920 | 201 | for (int i = 0; i < ESP_MAX_LINE && line+i < len; i++) { |
geky | 46:4abb80f0a920 | 202 | int a = buffer[line+i] / 100; |
geky | 46:4abb80f0a920 | 203 | int b = (buffer[line+i] - a*100) / 10; |
geky | 46:4abb80f0a920 | 204 | int c = (buffer[line+i] - a*100 - b*10); |
geky | 46:4abb80f0a920 | 205 | |
geky | 46:4abb80f0a920 | 206 | if (serialputc('\\') < 0 || |
geky | 46:4abb80f0a920 | 207 | serialputc(a + '0') < 0 || |
geky | 46:4abb80f0a920 | 208 | serialputc(b + '0') < 0 || |
geky | 46:4abb80f0a920 | 209 | serialputc(c + '0') < 0) |
geky | 46:4abb80f0a920 | 210 | return false; |
geky | 46:4abb80f0a920 | 211 | } |
geky | 46:4abb80f0a920 | 212 | if (!(command("')") && execute())) |
geky | 45:3cfb7406d993 | 213 | return false; |
alexhrao | 49:cac09a34102c | 214 | |
geky | 45:3cfb7406d993 | 215 | } |
geky | 44:16da10e7b3f7 | 216 | return true; |
geky | 44:16da10e7b3f7 | 217 | } |
geky | 44:16da10e7b3f7 | 218 | |
alexhrao | 49:cac09a34102c | 219 | int ESP8266::recv2(char* buffer, int len, int offset=0) { |
alexhrao | 49:cac09a34102c | 220 | // start at offset, read at most LEN bytes |
alexhrao | 49:cac09a34102c | 221 | // first make sure we have that many bytes to read! |
alexhrao | 49:cac09a34102c | 222 | |
alexhrao | 49:cac09a34102c | 223 | // Get total number of bytes |
alexhrao | 49:cac09a34102c | 224 | char num_buf[16]; |
alexhrao | 49:cac09a34102c | 225 | int num_i = 0; |
alexhrao | 49:cac09a34102c | 226 | char cmd[128]; |
alexhrao | 49:cac09a34102c | 227 | |
alexhrao | 49:cac09a34102c | 228 | sprintf(cmd, "print(#cm)\r\n"); |
alexhrao | 49:cac09a34102c | 229 | command(cmd); |
alexhrao | 49:cac09a34102c | 230 | |
alexhrao | 49:cac09a34102c | 231 | // cleanup |
alexhrao | 49:cac09a34102c | 232 | while ((serialgetc() != '\n')); |
alexhrao | 49:cac09a34102c | 233 | |
alexhrao | 49:cac09a34102c | 234 | // Get number |
alexhrao | 49:cac09a34102c | 235 | while (char tmp = serialgetc()) { |
alexhrao | 49:cac09a34102c | 236 | if (tmp == '\r') { |
alexhrao | 49:cac09a34102c | 237 | serialgetc(); |
alexhrao | 49:cac09a34102c | 238 | break; |
alexhrao | 49:cac09a34102c | 239 | } |
alexhrao | 49:cac09a34102c | 240 | num_buf[num_i++] = tmp; |
alexhrao | 49:cac09a34102c | 241 | } |
alexhrao | 49:cac09a34102c | 242 | num_buf[num_i] = '\0'; |
alexhrao | 49:cac09a34102c | 243 | |
alexhrao | 49:cac09a34102c | 244 | // now we can get number |
alexhrao | 49:cac09a34102c | 245 | int resp_len = atoi(num_buf); |
alexhrao | 49:cac09a34102c | 246 | if (offset >= resp_len) { |
alexhrao | 49:cac09a34102c | 247 | // Can't read anything! |
alexhrao | 49:cac09a34102c | 248 | return -1; |
alexhrao | 49:cac09a34102c | 249 | } |
alexhrao | 49:cac09a34102c | 250 | if ((offset + len) > resp_len) { |
alexhrao | 49:cac09a34102c | 251 | len = resp_len - offset; |
alexhrao | 49:cac09a34102c | 252 | } |
alexhrao | 49:cac09a34102c | 253 | // we're good |
alexhrao | 49:cac09a34102c | 254 | sprintf(cmd, "print(cm:sub(%d, %d))\r\n", offset + 1, offset + len); |
alexhrao | 49:cac09a34102c | 255 | command(cmd); |
alexhrao | 49:cac09a34102c | 256 | |
alexhrao | 49:cac09a34102c | 257 | // cleanup |
alexhrao | 49:cac09a34102c | 258 | while ((serialgetc() != '\n')); |
alexhrao | 49:cac09a34102c | 259 | |
alexhrao | 49:cac09a34102c | 260 | // Read Data |
alexhrao | 49:cac09a34102c | 261 | for (int i = 0; i < len; i++) { |
alexhrao | 49:cac09a34102c | 262 | buffer[i] = serialgetc(); |
alexhrao | 49:cac09a34102c | 263 | } |
alexhrao | 49:cac09a34102c | 264 | // Will be two more characters to clean up, but NOT PART OF DATA |
alexhrao | 49:cac09a34102c | 265 | serialgetc(); |
alexhrao | 49:cac09a34102c | 266 | serialgetc(); |
alexhrao | 49:cac09a34102c | 267 | |
alexhrao | 49:cac09a34102c | 268 | return len; |
alexhrao | 49:cac09a34102c | 269 | } |
alexhrao | 49:cac09a34102c | 270 | bool ESP8266::recv2(char *buffer, int *len) { |
geky | 44:16da10e7b3f7 | 271 | char len_buf[16]; |
geky | 44:16da10e7b3f7 | 272 | sprintf(len_buf, "%d", *len); |
geky | 44:16da10e7b3f7 | 273 | |
alexhrao | 49:cac09a34102c | 274 | //if (!(command("cr(") && |
alexhrao | 49:cac09a34102c | 275 | // command(len_buf) && |
alexhrao | 49:cac09a34102c | 276 | // command(")"))) |
alexhrao | 49:cac09a34102c | 277 | // return false; |
alexhrao | 49:cac09a34102c | 278 | //if (!(command("\r\n") && discardEcho())) |
alexhrao | 49:cac09a34102c | 279 | // return false; |
alexhrao | 49:cac09a34102c | 280 | //if (!(command("print(cm)"))) |
alexhrao | 49:cac09a34102c | 281 | // return false; |
alexhrao | 49:cac09a34102c | 282 | char buf[4096]; |
alexhrao | 49:cac09a34102c | 283 | int buf_len = 4095; |
alexhrao | 49:cac09a34102c | 284 | int ind = 0; |
alexhrao | 49:cac09a34102c | 285 | while (true) { |
alexhrao | 49:cac09a34102c | 286 | dev1.printf("\r\nIterating\r\n"); |
alexhrao | 49:cac09a34102c | 287 | int res = recv2(buf + ind, buf_len, ind); |
alexhrao | 49:cac09a34102c | 288 | dev1.printf("Result: %d\n", res); |
alexhrao | 49:cac09a34102c | 289 | if (res < 0) { |
alexhrao | 49:cac09a34102c | 290 | break; |
alexhrao | 49:cac09a34102c | 291 | } else { |
alexhrao | 49:cac09a34102c | 292 | ind += res; |
alexhrao | 49:cac09a34102c | 293 | } |
geky | 45:3cfb7406d993 | 294 | |
alexhrao | 49:cac09a34102c | 295 | } |
alexhrao | 49:cac09a34102c | 296 | dev1.printf("\r\n** WHAT WE HAVE **\r\n"); |
alexhrao | 49:cac09a34102c | 297 | dev1.printf(buf); |
alexhrao | 49:cac09a34102c | 298 | dev1.printf("\r\n** DONE ** \r\n"); |
alexhrao | 49:cac09a34102c | 299 | dev1.getc(); |
alexhrao | 49:cac09a34102c | 300 | |
alexhrao | 49:cac09a34102c | 301 | int len2 = 127; |
alexhrao | 49:cac09a34102c | 302 | int n = 0; |
alexhrao | 49:cac09a34102c | 303 | char* CMD = "print(cm:sub(%d, %d))\r\n"; |
alexhrao | 49:cac09a34102c | 304 | char cmd[1024]; |
alexhrao | 49:cac09a34102c | 305 | char cmd_buf_f[8192]; |
alexhrao | 49:cac09a34102c | 306 | int cmd_buf_i = 0; |
alexhrao | 49:cac09a34102c | 307 | while (true) { |
alexhrao | 49:cac09a34102c | 308 | tmp_buf_ind = 0; |
alexhrao | 49:cac09a34102c | 309 | //command("print(#cm)\r\n"); |
alexhrao | 49:cac09a34102c | 310 | //sprintf(cmd, "print(#cm)\r\n"); |
alexhrao | 49:cac09a34102c | 311 | //command(cmd); |
alexhrao | 49:cac09a34102c | 312 | //command(cmd); |
alexhrao | 49:cac09a34102c | 313 | sprintf(cmd, CMD, n + 1, n + 1024); |
alexhrao | 49:cac09a34102c | 314 | command(cmd); |
alexhrao | 49:cac09a34102c | 315 | // cleanup the command |
alexhrao | 49:cac09a34102c | 316 | for (int l = 0; l <= strlen(cmd); l++) { |
alexhrao | 49:cac09a34102c | 317 | serialgetc(); |
alexhrao | 49:cac09a34102c | 318 | } |
alexhrao | 49:cac09a34102c | 319 | /* |
alexhrao | 49:cac09a34102c | 320 | // While we have numbers, get |
alexhrao | 49:cac09a34102c | 321 | char num_buf[16]; |
alexhrao | 49:cac09a34102c | 322 | int ind2 = 0; |
alexhrao | 49:cac09a34102c | 323 | while (char tmp = serialgetc()) { |
alexhrao | 49:cac09a34102c | 324 | if (tmp < '0' || tmp > '9') { |
alexhrao | 49:cac09a34102c | 325 | break; |
alexhrao | 49:cac09a34102c | 326 | } |
alexhrao | 49:cac09a34102c | 327 | num_buf[ind2++] = tmp; |
alexhrao | 49:cac09a34102c | 328 | } |
alexhrao | 49:cac09a34102c | 329 | num_buf[ind2] = '\0'; |
alexhrao | 49:cac09a34102c | 330 | dev1.printf("NUM: %s = %d\r\n", num_buf, atoi(num_buf)); |
alexhrao | 49:cac09a34102c | 331 | */ |
alexhrao | 49:cac09a34102c | 332 | for (int l = 0; l < 3; l++) { |
alexhrao | 49:cac09a34102c | 333 | cmd_buf_f[cmd_buf_i++] = serialgetc(); |
alexhrao | 49:cac09a34102c | 334 | } |
alexhrao | 49:cac09a34102c | 335 | cmd_buf_f[cmd_buf_i] = '\0'; |
alexhrao | 49:cac09a34102c | 336 | n += 1024; |
alexhrao | 49:cac09a34102c | 337 | // tmp_buf[tmp_buf_ind % 8192] = '\0'; |
alexhrao | 49:cac09a34102c | 338 | // dev1.printf("Raw output\r\n%s\r\n", tmp_buf); |
alexhrao | 49:cac09a34102c | 339 | //dev1.printf("Length: %d\n", len2); |
alexhrao | 49:cac09a34102c | 340 | // for (int k = 0; k < 4000; k++) { |
alexhrao | 49:cac09a34102c | 341 | // dev1.putc(buf[k]); |
alexhrao | 49:cac09a34102c | 342 | // } |
alexhrao | 49:cac09a34102c | 343 | dev1.printf("** WHAT WE HAVE SO FAR **\r\n"); |
alexhrao | 49:cac09a34102c | 344 | dev1.printf(cmd_buf_f); |
alexhrao | 49:cac09a34102c | 345 | dev1.printf("\r\n** END WHAT WE HAVE **\r\n"); |
alexhrao | 49:cac09a34102c | 346 | dev1.printf("Waiting...\r\n"); |
alexhrao | 49:cac09a34102c | 347 | dev1.getc(); |
alexhrao | 49:cac09a34102c | 348 | dev1.printf("Continuing...\r\n"); |
alexhrao | 49:cac09a34102c | 349 | } |
alexhrao | 49:cac09a34102c | 350 | printf("\r\n**EXECUTION**\r\n%s", buf); |
alexhrao | 49:cac09a34102c | 351 | while(true); |
geky | 45:3cfb7406d993 | 352 | if (!(command("\r\n") && discardEcho())) |
geky | 44:16da10e7b3f7 | 353 | return false; |
geky | 45:3cfb7406d993 | 354 | |
geky | 45:3cfb7406d993 | 355 | // Read in response |
geky | 45:3cfb7406d993 | 356 | for (int i = 0; i < *len; i++) { |
geky | 45:3cfb7406d993 | 357 | int e = serialgetc(); |
geky | 45:3cfb7406d993 | 358 | |
geky | 45:3cfb7406d993 | 359 | if (e == '\r') { |
geky | 45:3cfb7406d993 | 360 | *len = i; |
geky | 45:3cfb7406d993 | 361 | break; |
geky | 45:3cfb7406d993 | 362 | } else if (e != '\\') { |
geky | 45:3cfb7406d993 | 363 | return false; |
geky | 45:3cfb7406d993 | 364 | } |
geky | 45:3cfb7406d993 | 365 | |
geky | 45:3cfb7406d993 | 366 | int a = serialgetc(); |
geky | 45:3cfb7406d993 | 367 | int b = serialgetc(); |
geky | 45:3cfb7406d993 | 368 | int c = serialgetc(); |
geky | 45:3cfb7406d993 | 369 | |
geky | 45:3cfb7406d993 | 370 | if (a < 0 || b < 0 || c < 0) |
geky | 45:3cfb7406d993 | 371 | return false; |
geky | 45:3cfb7406d993 | 372 | |
geky | 45:3cfb7406d993 | 373 | buffer[i] = (a-'0')*100 + (b-'0')*10 + (c-'0'); |
geky | 45:3cfb7406d993 | 374 | } |
geky | 45:3cfb7406d993 | 375 | // Flush to next prompt |
geky | 45:3cfb7406d993 | 376 | return flush(); |
geky | 44:16da10e7b3f7 | 377 | } |
geky | 44:16da10e7b3f7 | 378 | |
geky | 44:16da10e7b3f7 | 379 | int ESP8266::putc(char c) { |
geky | 44:16da10e7b3f7 | 380 | char buffer[1] = { c }; |
geky | 44:16da10e7b3f7 | 381 | |
geky | 44:16da10e7b3f7 | 382 | return send(buffer, 1) ? 0 : -1; |
geky | 44:16da10e7b3f7 | 383 | } |
geky | 44:16da10e7b3f7 | 384 | |
geky | 44:16da10e7b3f7 | 385 | int ESP8266::getc() { |
geky | 44:16da10e7b3f7 | 386 | char buffer[1]; |
geky | 44:16da10e7b3f7 | 387 | |
geky | 44:16da10e7b3f7 | 388 | while (true) { |
geky | 44:16da10e7b3f7 | 389 | int len = 1; |
geky | 44:16da10e7b3f7 | 390 | |
geky | 44:16da10e7b3f7 | 391 | if (!recv(buffer, &len)) |
geky | 44:16da10e7b3f7 | 392 | return -1; |
geky | 44:16da10e7b3f7 | 393 | |
geky | 44:16da10e7b3f7 | 394 | if (len > 0) |
geky | 44:16da10e7b3f7 | 395 | break; |
geky | 44:16da10e7b3f7 | 396 | } |
geky | 44:16da10e7b3f7 | 397 | |
geky | 44:16da10e7b3f7 | 398 | return buffer[0]; |
geky | 44:16da10e7b3f7 | 399 | } |
geky | 44:16da10e7b3f7 | 400 | |
geky | 44:16da10e7b3f7 | 401 | int ESP8266::writeable() { |
geky | 44:16da10e7b3f7 | 402 | // Always succeeds since message can be temporarily stored on the esp |
geky | 44:16da10e7b3f7 | 403 | return 1; |
geky | 44:16da10e7b3f7 | 404 | } |
geky | 44:16da10e7b3f7 | 405 | |
geky | 44:16da10e7b3f7 | 406 | int ESP8266::readable() { |
geky | 44:16da10e7b3f7 | 407 | char count_buf[16]; |
geky | 44:16da10e7b3f7 | 408 | int count_len = 15; |
geky | 44:16da10e7b3f7 | 409 | |
geky | 44:16da10e7b3f7 | 410 | if (!(command("ca()") && execute(count_buf, &count_len))) |
geky | 44:16da10e7b3f7 | 411 | return false; |
geky | 44:16da10e7b3f7 | 412 | |
geky | 44:16da10e7b3f7 | 413 | count_buf[count_len] = 0; |
geky | 44:16da10e7b3f7 | 414 | return atoi(count_buf); |
geky | 44:16da10e7b3f7 | 415 | } |
geky | 44:16da10e7b3f7 | 416 | |
geky | 44:16da10e7b3f7 | 417 | const char *ESP8266::getIPAddress() { |
geky | 44:16da10e7b3f7 | 418 | if (strcmp(_ip, "nil") != 0) |
geky | 44:16da10e7b3f7 | 419 | return _ip; |
geky | 44:16da10e7b3f7 | 420 | else |
geky | 44:16da10e7b3f7 | 421 | return 0; |
geky | 44:16da10e7b3f7 | 422 | } |
geky | 44:16da10e7b3f7 | 423 | |
geky | 44:16da10e7b3f7 | 424 | bool ESP8266::getHostByName(const char *host, char *ip) { |
geky | 44:16da10e7b3f7 | 425 | if (!(command("dh='") && |
geky | 44:16da10e7b3f7 | 426 | command(host) && |
geky | 44:16da10e7b3f7 | 427 | command("';") && |
geky | 44:16da10e7b3f7 | 428 | command("dn=nil;") && |
geky | 44:16da10e7b3f7 | 429 | command("if dh:match('%d+.%d+.%d+.%d+') then ") && |
geky | 44:16da10e7b3f7 | 430 | command("dn=dh ") && |
geky | 44:16da10e7b3f7 | 431 | command("else ") && |
geky | 44:16da10e7b3f7 | 432 | command("dc=net.createConnection(net.TCP);") && |
geky | 44:16da10e7b3f7 | 433 | command("dc:dns(dh,function(dc,ip) dn=ip end);") && |
geky | 44:16da10e7b3f7 | 434 | command("dc=nil ") && |
geky | 44:16da10e7b3f7 | 435 | command("end") && |
geky | 44:16da10e7b3f7 | 436 | execute())) |
geky | 44:16da10e7b3f7 | 437 | return false; |
geky | 44:16da10e7b3f7 | 438 | |
geky | 44:16da10e7b3f7 | 439 | // Wait for a response |
geky | 44:16da10e7b3f7 | 440 | Timer timer; |
geky | 44:16da10e7b3f7 | 441 | timer.start(); |
geky | 44:16da10e7b3f7 | 442 | |
geky | 44:16da10e7b3f7 | 443 | while (true) { |
geky | 44:16da10e7b3f7 | 444 | int ip_len = 15; |
geky | 44:16da10e7b3f7 | 445 | |
geky | 44:16da10e7b3f7 | 446 | if (!(command("print(dn)") && execute(ip, &ip_len))) |
geky | 44:16da10e7b3f7 | 447 | return false; |
geky | 44:16da10e7b3f7 | 448 | |
geky | 44:16da10e7b3f7 | 449 | ip[ip_len] = 0; |
geky | 44:16da10e7b3f7 | 450 | |
geky | 44:16da10e7b3f7 | 451 | if (strcmp(ip, "nil") != 0) |
geky | 44:16da10e7b3f7 | 452 | return true; |
geky | 44:16da10e7b3f7 | 453 | |
geky | 44:16da10e7b3f7 | 454 | if (timer.read_ms() > _timeout) |
geky | 44:16da10e7b3f7 | 455 | return false; |
geky | 44:16da10e7b3f7 | 456 | } |
geky | 44:16da10e7b3f7 | 457 | } |
geky | 44:16da10e7b3f7 | 458 | |
geky | 44:16da10e7b3f7 | 459 | int ESP8266::serialgetc() { |
geky | 44:16da10e7b3f7 | 460 | Timer timer; |
alexhrao | 49:cac09a34102c | 461 | |
geky | 44:16da10e7b3f7 | 462 | timer.start(); |
geky | 44:16da10e7b3f7 | 463 | |
geky | 44:16da10e7b3f7 | 464 | while (true) { |
geky | 44:16da10e7b3f7 | 465 | if (_serial.readable()) { |
geky | 44:16da10e7b3f7 | 466 | char c = _serial.getc(); |
geky | 44:16da10e7b3f7 | 467 | #ifdef ESP8266_ECHO |
alexhrao | 49:cac09a34102c | 468 | // tmp_buf[tmp_buf_ind++ % 8192] = c; |
geky | 44:16da10e7b3f7 | 469 | printf("%c", c); |
geky | 44:16da10e7b3f7 | 470 | #endif |
geky | 44:16da10e7b3f7 | 471 | return c; |
geky | 44:16da10e7b3f7 | 472 | } |
geky | 44:16da10e7b3f7 | 473 | |
geky | 44:16da10e7b3f7 | 474 | if (timer.read_ms() > _timeout) |
geky | 44:16da10e7b3f7 | 475 | return -1; |
geky | 44:16da10e7b3f7 | 476 | } |
geky | 44:16da10e7b3f7 | 477 | } |
geky | 44:16da10e7b3f7 | 478 | |
geky | 44:16da10e7b3f7 | 479 | int ESP8266::serialputc(char c) { |
geky | 44:16da10e7b3f7 | 480 | Timer timer; |
geky | 44:16da10e7b3f7 | 481 | timer.start(); |
geky | 44:16da10e7b3f7 | 482 | |
geky | 44:16da10e7b3f7 | 483 | while (true) { |
geky | 44:16da10e7b3f7 | 484 | if (_serial.writeable()) |
geky | 44:16da10e7b3f7 | 485 | return _serial.putc(c); |
geky | 44:16da10e7b3f7 | 486 | |
geky | 44:16da10e7b3f7 | 487 | if (timer.read_ms() > _timeout) |
geky | 44:16da10e7b3f7 | 488 | return -1; |
geky | 44:16da10e7b3f7 | 489 | } |
geky | 44:16da10e7b3f7 | 490 | } |
geky | 44:16da10e7b3f7 | 491 | |
geky | 44:16da10e7b3f7 | 492 | bool ESP8266::discardEcho() { |
geky | 44:16da10e7b3f7 | 493 | while (true) { |
geky | 44:16da10e7b3f7 | 494 | int c = serialgetc(); |
geky | 44:16da10e7b3f7 | 495 | |
geky | 44:16da10e7b3f7 | 496 | if (c < 0) |
geky | 44:16da10e7b3f7 | 497 | return false; |
geky | 45:3cfb7406d993 | 498 | else if (c == '\n') |
geky | 45:3cfb7406d993 | 499 | return true; |
geky | 45:3cfb7406d993 | 500 | } |
geky | 45:3cfb7406d993 | 501 | } |
geky | 45:3cfb7406d993 | 502 | |
geky | 45:3cfb7406d993 | 503 | bool ESP8266::flush() { |
geky | 45:3cfb7406d993 | 504 | while (true) { |
geky | 45:3cfb7406d993 | 505 | int c = serialgetc(); |
geky | 45:3cfb7406d993 | 506 | |
geky | 45:3cfb7406d993 | 507 | if (c < 0) |
geky | 45:3cfb7406d993 | 508 | return false; |
geky | 44:16da10e7b3f7 | 509 | else if (c == '>') |
geky | 44:16da10e7b3f7 | 510 | return true; |
geky | 44:16da10e7b3f7 | 511 | } |
geky | 44:16da10e7b3f7 | 512 | } |
geky | 44:16da10e7b3f7 | 513 | |
geky | 44:16da10e7b3f7 | 514 | bool ESP8266::command(const char *cmd) { |
alexhrao | 49:cac09a34102c | 515 | //DBG("command sent:\t %s", cmd); |
geky | 44:16da10e7b3f7 | 516 | |
geky | 44:16da10e7b3f7 | 517 | for (int i = 0; cmd[i]; i++) { |
geky | 44:16da10e7b3f7 | 518 | if (serialputc(cmd[i]) < 0) |
geky | 44:16da10e7b3f7 | 519 | return false; |
geky | 44:16da10e7b3f7 | 520 | } |
geky | 44:16da10e7b3f7 | 521 | |
geky | 44:16da10e7b3f7 | 522 | return true; |
geky | 45:3cfb7406d993 | 523 | } |
geky | 44:16da10e7b3f7 | 524 | |
geky | 44:16da10e7b3f7 | 525 | bool ESP8266::execute(char *resp_buf, int *resp_len) { |
geky | 44:16da10e7b3f7 | 526 | // Finish command with a newline |
geky | 45:3cfb7406d993 | 527 | if (!(command("\r\n") && discardEcho())) |
geky | 44:16da10e7b3f7 | 528 | return false; |
geky | 44:16da10e7b3f7 | 529 | |
geky | 44:16da10e7b3f7 | 530 | // Read in response if any |
geky | 44:16da10e7b3f7 | 531 | if (resp_buf && resp_len) { |
geky | 44:16da10e7b3f7 | 532 | int i; |
geky | 44:16da10e7b3f7 | 533 | |
geky | 44:16da10e7b3f7 | 534 | for (i = 0; i < *resp_len; i++) { |
geky | 44:16da10e7b3f7 | 535 | int c = serialgetc(); |
geky | 44:16da10e7b3f7 | 536 | |
geky | 44:16da10e7b3f7 | 537 | if (c < 0) |
geky | 44:16da10e7b3f7 | 538 | return false; |
geky | 44:16da10e7b3f7 | 539 | |
geky | 44:16da10e7b3f7 | 540 | if (c == '\r') { |
geky | 44:16da10e7b3f7 | 541 | *resp_len = i; |
geky | 44:16da10e7b3f7 | 542 | break; |
geky | 44:16da10e7b3f7 | 543 | } |
geky | 44:16da10e7b3f7 | 544 | |
geky | 44:16da10e7b3f7 | 545 | resp_buf[i] = c; |
geky | 44:16da10e7b3f7 | 546 | } |
geky | 44:16da10e7b3f7 | 547 | |
alexhrao | 49:cac09a34102c | 548 | //DBG("command response:\t %.*s", *resp_len, resp_buf); |
geky | 44:16da10e7b3f7 | 549 | } |
geky | 44:16da10e7b3f7 | 550 | |
geky | 45:3cfb7406d993 | 551 | return flush(); |
geky | 44:16da10e7b3f7 | 552 | } |