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.

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