You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

368 lines
8.3 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. /**
  2. * @file TinyWiFiClientESP8266.h
  3. * @author Volodymyr Shymanskyy
  4. * @license LGPL-3.0
  5. * @copyright Copyright (c) 2016 Volodymyr Shymanskyy
  6. * @date Nov 2016
  7. */
  8. #ifndef TinyWiFiClientESP8266_h
  9. #define TinyWiFiClientESP8266_h
  10. //#define GSM_DEBUG Serial
  11. #if !defined(TINY_GSM_RX_BUFFER)
  12. #define TINY_GSM_RX_BUFFER 256
  13. #endif
  14. #include <TinyGsmCommon.h>
  15. #define GSM_NL "\r\n"
  16. static const char GSM_OK[] TINY_GSM_PROGMEM = "OK" GSM_NL;
  17. static const char GSM_ERROR[] TINY_GSM_PROGMEM = "ERROR" GSM_NL;
  18. class TinyGsm
  19. {
  20. public:
  21. TinyGsm(Stream& stream)
  22. : stream(stream)
  23. {}
  24. public:
  25. class GsmClient : public Client
  26. {
  27. friend class TinyGsm;
  28. typedef TinyGsmFifo<uint8_t, TINY_GSM_RX_BUFFER> RxFifo;
  29. public:
  30. GsmClient() {
  31. init(NULL, -1);
  32. }
  33. GsmClient(TinyGsm& at, uint8_t mux = 1) {
  34. init(&at, mux);
  35. }
  36. bool init(TinyGsm* at, uint8_t mux = 1) {
  37. this->at = at;
  38. this->mux = mux;
  39. at->sockets[mux] = this;
  40. sock_connected = false;
  41. return true;
  42. }
  43. public:
  44. virtual int connect(const char *host, uint16_t port) {
  45. rx.clear();
  46. sock_connected = at->modemConnect(host, port, mux);
  47. return sock_connected;
  48. }
  49. virtual int connect(IPAddress ip, uint16_t port) {
  50. String host; host.reserve(16);
  51. host += ip[0];
  52. host += ".";
  53. host += ip[1];
  54. host += ".";
  55. host += ip[2];
  56. host += ".";
  57. host += ip[3];
  58. return connect(host.c_str(), port);
  59. }
  60. virtual void stop() {
  61. at->sendAT(GF("+CIPCLOSE="), mux);
  62. sock_connected = false;
  63. at->waitResponse();
  64. }
  65. virtual size_t write(const uint8_t *buf, size_t size) {
  66. //at->maintain();
  67. return at->modemSend(buf, size, mux);
  68. }
  69. virtual size_t write(uint8_t c) {
  70. return write(&c, 1);
  71. }
  72. virtual int available() {
  73. if (!rx.size()) {
  74. at->maintain();
  75. }
  76. return rx.size();
  77. }
  78. virtual int read(uint8_t *buf, size_t size) {
  79. size_t cnt = 0;
  80. while (cnt < size) {
  81. size_t chunk = min(size-cnt, rx.size());
  82. if (chunk > 0) {
  83. rx.get(buf, chunk);
  84. buf += chunk;
  85. cnt += chunk;
  86. continue;
  87. }
  88. // TODO: Read directly into user buffer?
  89. if (!rx.size()) {
  90. at->maintain();
  91. //break;
  92. }
  93. }
  94. return cnt;
  95. }
  96. virtual int read() {
  97. uint8_t c;
  98. if (read(&c, 1) == 1) {
  99. return c;
  100. }
  101. return -1;
  102. }
  103. virtual int peek() { return -1; } //TODO
  104. virtual void flush() { at->stream.flush(); }
  105. virtual uint8_t connected() {
  106. if (available()) {
  107. return true;
  108. }
  109. return sock_connected;
  110. }
  111. virtual operator bool() { return connected(); }
  112. private:
  113. TinyGsm* at;
  114. uint8_t mux;
  115. bool sock_connected;
  116. RxFifo rx;
  117. };
  118. public:
  119. /*
  120. * Basic functions
  121. */
  122. bool begin() {
  123. return init();
  124. }
  125. bool init() {
  126. if (!autoBaud()) {
  127. return false;
  128. }
  129. return true;
  130. }
  131. bool autoBaud(unsigned long timeout = 10000L) {
  132. for (unsigned long start = millis(); millis() - start < timeout; ) {
  133. sendAT(GF("E0"));
  134. if (waitResponse(200) == 1) {
  135. delay(100);
  136. return true;
  137. }
  138. delay(100);
  139. }
  140. return false;
  141. }
  142. void maintain() {
  143. //while (stream.available()) {
  144. waitResponse(10, NULL, NULL);
  145. //}
  146. }
  147. bool factoryDefault() {
  148. sendAT(GF("+RESTORE"));
  149. return waitResponse() == 1;
  150. }
  151. /*
  152. * Power functions
  153. */
  154. bool restart() {
  155. if (!autoBaud()) {
  156. return false;
  157. }
  158. sendAT(GF("+RST"));
  159. if (waitResponse(10000L) != 1) {
  160. return false;
  161. }
  162. if (waitResponse(10000L, GF(GSM_NL "ready" GSM_NL)) != 1) {
  163. return false;
  164. }
  165. delay(500);
  166. return autoBaud();
  167. }
  168. bool waitForNetwork(unsigned long timeout = 60000L) {
  169. return true;
  170. }
  171. /*
  172. * WiFi functions
  173. */
  174. bool networkConnect(const char* ssid, const char* pwd) {
  175. sendAT(GF("+CIPMUX=1"));
  176. if (waitResponse() != 1) {
  177. return false;
  178. }
  179. sendAT(GF("+CWMODE_CUR=1"));
  180. if (waitResponse() != 1) {
  181. return false;
  182. }
  183. sendAT(GF("+CWJAP_CUR=\""), ssid, GF("\",\""), pwd, GF("\""));
  184. if (waitResponse(30000L, GFP(GSM_OK), GF(GSM_NL "FAIL" GSM_NL)) != 1) {
  185. return false;
  186. }
  187. return true;
  188. }
  189. bool networkDisconnect() {
  190. sendAT(GF("+CWQAP"));
  191. return waitResponse(10000L) == 1;
  192. }
  193. private:
  194. int modemConnect(const char* host, uint16_t port, uint8_t mux) {
  195. sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port, GF(",120"));
  196. int rsp = waitResponse(75000L,
  197. GFP(GSM_OK),
  198. GFP(GSM_ERROR),
  199. GF(GSM_NL "ALREADY CONNECT" GSM_NL));
  200. return (1 == rsp);
  201. }
  202. int modemSend(const void* buff, size_t len, uint8_t mux) {
  203. sendAT(GF("+CIPSEND="), mux, ',', len);
  204. if (waitResponse(GF(">")) != 1) {
  205. return -1;
  206. }
  207. stream.write((uint8_t*)buff, len);
  208. if (waitResponse(GF(GSM_NL "SEND OK" GSM_NL)) != 1) {
  209. return -1;
  210. }
  211. return len;
  212. }
  213. bool modemGetConnected(uint8_t mux) {
  214. sendAT(GF("+CIPSTATUS="), mux);
  215. int res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), GF(",\"CLOSING\""), GF(",\"INITIAL\""));
  216. waitResponse();
  217. return 1 == res;
  218. }
  219. /* Utilities */
  220. template<typename T>
  221. void streamWrite(T last) {
  222. stream.print(last);
  223. }
  224. template<typename T, typename... Args>
  225. void streamWrite(T head, Args... tail) {
  226. stream.print(head);
  227. streamWrite(tail...);
  228. }
  229. int streamRead() { return stream.read(); }
  230. template<typename... Args>
  231. void sendAT(Args... cmd) {
  232. streamWrite("AT", cmd..., GSM_NL);
  233. stream.flush();
  234. TINY_GSM_YIELD();
  235. //DBG("### AT:", cmd...);
  236. }
  237. // TODO: Optimize this!
  238. uint8_t waitResponse(uint32_t timeout, String& data,
  239. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  240. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  241. {
  242. /*String r1s(r1); r1s.trim();
  243. String r2s(r2); r2s.trim();
  244. String r3s(r3); r3s.trim();
  245. String r4s(r4); r4s.trim();
  246. String r5s(r5); r5s.trim();
  247. DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/
  248. data.reserve(64);
  249. int index = 0;
  250. unsigned long startMillis = millis();
  251. do {
  252. TINY_GSM_YIELD();
  253. while (stream.available() > 0) {
  254. int a = streamRead();
  255. if (a <= 0) continue; // Skip 0x00 bytes, just in case
  256. data += (char)a;
  257. if (r1 && data.endsWith(r1)) {
  258. index = 1;
  259. goto finish;
  260. } else if (r2 && data.endsWith(r2)) {
  261. index = 2;
  262. goto finish;
  263. } else if (r3 && data.endsWith(r3)) {
  264. index = 3;
  265. goto finish;
  266. } else if (r4 && data.endsWith(r4)) {
  267. index = 4;
  268. goto finish;
  269. } else if (r5 && data.endsWith(r5)) {
  270. index = 5;
  271. goto finish;
  272. } else if (data.endsWith(GF(GSM_NL "+IPD,"))) {
  273. int mux = stream.readStringUntil(',').toInt();
  274. int len = stream.readStringUntil(':').toInt();
  275. if (len > sockets[mux]->rx.free()) {
  276. DBG("### Buffer overflow: ", len, "->", sockets[mux]->rx.free());
  277. } else {
  278. DBG("### Got: ", len, "->", sockets[mux]->rx.free());
  279. }
  280. while (len--) {
  281. while (!stream.available()) {}
  282. sockets[mux]->rx.put(stream.read());
  283. }
  284. data = "";
  285. return index;
  286. } else if (data.endsWith(GF(GSM_NL "1,CLOSED" GSM_NL))) { //TODO: use mux
  287. sockets[1]->sock_connected = false;
  288. data = "";
  289. }
  290. }
  291. } while (millis() - startMillis < timeout);
  292. finish:
  293. if (!index) {
  294. if (data.length()) {
  295. DBG("### Unhandled:", data);
  296. }
  297. data = "";
  298. }
  299. return index;
  300. }
  301. uint8_t waitResponse(uint32_t timeout,
  302. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  303. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  304. {
  305. String data;
  306. return waitResponse(timeout, data, r1, r2, r3, r4, r5);
  307. }
  308. uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  309. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  310. {
  311. return waitResponse(1000, r1, r2, r3, r4, r5);
  312. }
  313. private:
  314. Stream& stream;
  315. GsmClient* sockets[5];
  316. };
  317. typedef TinyGsm::GsmClient TinyGsmClient;
  318. #endif