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.

386 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
  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. /*
  197. * GPRS functions
  198. */
  199. bool gprsConnect(const char* apn, const char* user, const char* pwd) {
  200. return false;
  201. }
  202. bool gprsDisconnect() {
  203. return false;
  204. }
  205. /* Public Utilities */
  206. template<typename... Args>
  207. void sendAT(Args... cmd) {
  208. streamWrite("AT", cmd..., GSM_NL);
  209. stream.flush();
  210. TINY_GSM_YIELD();
  211. DBG(GSM_NL, ">>> AT:", cmd...);
  212. }
  213. // TODO: Optimize this!
  214. uint8_t waitResponse(uint32_t timeout, String& data,
  215. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  216. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  217. {
  218. /*String r1s(r1); r1s.trim();
  219. String r2s(r2); r2s.trim();
  220. String r3s(r3); r3s.trim();
  221. String r4s(r4); r4s.trim();
  222. String r5s(r5); r5s.trim();
  223. DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/
  224. data.reserve(64);
  225. int index = 0;
  226. unsigned long startMillis = millis();
  227. do {
  228. TINY_GSM_YIELD();
  229. while (stream.available() > 0) {
  230. int a = streamRead();
  231. if (a <= 0) continue; // Skip 0x00 bytes, just in case
  232. data += (char)a;
  233. if (r1 && data.endsWith(r1)) {
  234. index = 1;
  235. goto finish;
  236. } else if (r2 && data.endsWith(r2)) {
  237. index = 2;
  238. goto finish;
  239. } else if (r3 && data.endsWith(r3)) {
  240. index = 3;
  241. goto finish;
  242. } else if (r4 && data.endsWith(r4)) {
  243. index = 4;
  244. goto finish;
  245. } else if (r5 && data.endsWith(r5)) {
  246. index = 5;
  247. goto finish;
  248. } else if (data.endsWith(GF(GSM_NL "+IPD,"))) {
  249. int mux = stream.readStringUntil(',').toInt();
  250. int len = stream.readStringUntil(':').toInt();
  251. if (len > sockets[mux]->rx.free()) {
  252. DBG("### Buffer overflow: ", len, "->", sockets[mux]->rx.free());
  253. } else {
  254. DBG("### Got: ", len, "->", sockets[mux]->rx.free());
  255. }
  256. while (len--) {
  257. while (!stream.available()) {}
  258. sockets[mux]->rx.put(stream.read());
  259. }
  260. data = "";
  261. return index;
  262. } else if (data.endsWith(GF(GSM_NL "1,CLOSED" GSM_NL))) { //TODO: use mux
  263. sockets[1]->sock_connected = false;
  264. data = "";
  265. }
  266. }
  267. } while (millis() - startMillis < timeout);
  268. finish:
  269. if (!index) {
  270. data.trim();
  271. if (data.length()) {
  272. DBG("### Unhandled:", data);
  273. }
  274. data = "";
  275. }
  276. return index;
  277. }
  278. uint8_t waitResponse(uint32_t timeout,
  279. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  280. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  281. {
  282. String data;
  283. return waitResponse(timeout, data, r1, r2, r3, r4, r5);
  284. }
  285. uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  286. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  287. {
  288. return waitResponse(1000, r1, r2, r3, r4, r5);
  289. }
  290. private:
  291. int modemConnect(const char* host, uint16_t port, uint8_t mux) {
  292. sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port, GF(",120"));
  293. int rsp = waitResponse(75000L,
  294. GFP(GSM_OK),
  295. GFP(GSM_ERROR),
  296. GF(GSM_NL "ALREADY CONNECT" GSM_NL));
  297. return (1 == rsp);
  298. }
  299. int modemSend(const void* buff, size_t len, uint8_t mux) {
  300. sendAT(GF("+CIPSEND="), mux, ',', len);
  301. if (waitResponse(GF(">")) != 1) {
  302. return -1;
  303. }
  304. stream.write((uint8_t*)buff, len);
  305. if (waitResponse(GF(GSM_NL "SEND OK" GSM_NL)) != 1) {
  306. return -1;
  307. }
  308. return len;
  309. }
  310. bool modemGetConnected(uint8_t mux) {
  311. sendAT(GF("+CIPSTATUS="), mux);
  312. int res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), GF(",\"CLOSING\""), GF(",\"INITIAL\""));
  313. waitResponse();
  314. return 1 == res;
  315. }
  316. /* Private Utilities */
  317. template<typename T>
  318. void streamWrite(T last) {
  319. stream.print(last);
  320. }
  321. template<typename T, typename... Args>
  322. void streamWrite(T head, Args... tail) {
  323. stream.print(head);
  324. streamWrite(tail...);
  325. }
  326. int streamRead() { return stream.read(); }
  327. private:
  328. Stream& stream;
  329. GsmClient* sockets[5];
  330. };
  331. typedef TinyGsm::GsmClient TinyGsmClient;
  332. #endif