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.

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