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.

373 lines
8.4 KiB

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