BlynkSimpleShieldEsp8266_SoftSer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * @file BlynkSimpleShieldEsp8266.h
  3. * @author Volodymyr Shymanskyy
  4. * @license This project is released under the MIT License (MIT)
  5. * @copyright Copyright (c) 2015 Volodymyr Shymanskyy
  6. * @date Jun 2015
  7. * @brief
  8. *
  9. */
  10. #ifndef BlynkSimpleShieldEsp8266_h
  11. #define BlynkSimpleShieldEsp8266_h
  12. #ifdef ESP8266
  13. #error This code is not intended to run on the ESP8266 platform! Please check your Tools->Board setting.
  14. #endif
  15. #ifndef BLYNK_INFO_CONNECTION
  16. #define BLYNK_INFO_CONNECTION "ESP8266"
  17. #endif
  18. #define BLYNK_SEND_ATOMIC
  19. #include <BlynkApiArduino.h>
  20. #include <Blynk/BlynkProtocol.h>
  21. #include <utility/BlynkFifo.h>
  22. #include <ESP8266_SoftSer.h>
  23. class BlynkTransportShieldEsp8266
  24. {
  25. static void onData(uint8_t mux_id, uint32_t len, void* ptr) {
  26. ((BlynkTransportShieldEsp8266*)ptr)->onData(mux_id, len);
  27. }
  28. void onData(uint8_t mux_id, uint32_t len) {
  29. //sBLYNK_LOG("Got %d..", len);
  30. while (len) {
  31. if (client->getUart()->available()) {
  32. uint8_t b = client->getUart()->read();
  33. if(!buffer.push(b)) {
  34. BLYNK_LOG("Buffer overflow");
  35. }
  36. len--;
  37. }
  38. }
  39. }
  40. public:
  41. BlynkTransportShieldEsp8266()
  42. : client(NULL)
  43. , status(false)
  44. , domain(NULL)
  45. , port(0)
  46. {}
  47. void begin_domain(ESP8266* esp8266, const char* d, uint16_t p) {
  48. client = esp8266;
  49. client->setOnData(onData, this);
  50. domain = d;
  51. port = p;
  52. }
  53. bool connect() {
  54. if (!domain || !port)
  55. return false;
  56. status = client->createTCP(domain, port);
  57. return status;
  58. }
  59. void disconnect() {
  60. status = false;
  61. buffer.clear();
  62. client->releaseTCP();
  63. }
  64. size_t read(void* buf, size_t len) {
  65. uint32_t start = millis();
  66. //BLYNK_LOG("Waiting: %d, Occuied: %d", len, buffer.getOccupied());
  67. while ((buffer.getOccupied() < len) && (millis() - start < 1500)) {
  68. client->run();
  69. }
  70. return buffer.read((uint8_t*)buf, len);
  71. }
  72. size_t write(const void* buf, size_t len) {
  73. if (client->send((const uint8_t*)buf, len)) {
  74. return len;
  75. }
  76. return 0;
  77. }
  78. bool connected() { return status; }
  79. int available() {
  80. client->run();
  81. //BLYNK_LOG("Still: %d", buffer.getOccupied());
  82. return buffer.getOccupied();
  83. }
  84. private:
  85. ESP8266* client;
  86. bool status;
  87. BlynkFifo<uint8_t,256> buffer;
  88. const char* domain;
  89. uint16_t port;
  90. };
  91. class BlynkWifi
  92. : public BlynkProtocol<BlynkTransportShieldEsp8266>
  93. {
  94. typedef BlynkProtocol<BlynkTransportShieldEsp8266> Base;
  95. public:
  96. BlynkWifi(BlynkTransportShieldEsp8266& transp)
  97. : Base(transp)
  98. , wifi(NULL)
  99. {}
  100. bool connectWiFi(const char* ssid, const char* pass)
  101. {
  102. delay(500);
  103. BLYNK_LOG("Connecting to %s", ssid);
  104. /*if (!wifi->restart()) {
  105. BLYNK_LOG("Failed to restart");
  106. return false;
  107. }*/
  108. if (!wifi->setEcho(0)) {
  109. BLYNK_LOG("Failed to disable Echo");
  110. return false;
  111. }
  112. if (!wifi->setOprToStation()) {
  113. BLYNK_LOG("Failed to set STA mode");
  114. return false;
  115. }
  116. if (wifi->joinAP(ssid, pass)) {
  117. BLYNK_LOG("IP: %s", wifi->getLocalIP().c_str());
  118. } else {
  119. BLYNK_LOG("Failed to connect WiFi");
  120. return false;
  121. }
  122. if (!wifi->disableMUX()) {
  123. BLYNK_LOG("Failed to disable MUX");
  124. }
  125. BLYNK_LOG("Connected to WiFi");
  126. return true;
  127. }
  128. void config(ESP8266& esp8266,
  129. const char* auth,
  130. const char* domain = BLYNK_DEFAULT_DOMAIN,
  131. uint16_t port = BLYNK_DEFAULT_PORT)
  132. {
  133. Base::begin(auth);
  134. wifi = &esp8266;
  135. this->conn.begin_domain(wifi, domain, port);
  136. }
  137. void begin(const char* auth,
  138. ESP8266& esp8266,
  139. const char* ssid,
  140. const char* pass,
  141. const char* domain = BLYNK_DEFAULT_DOMAIN,
  142. uint16_t port = BLYNK_DEFAULT_PORT)
  143. {
  144. config(esp8266, auth, domain, port);
  145. connectWiFi(ssid, pass);
  146. }
  147. private:
  148. ESP8266* wifi;
  149. };
  150. static BlynkTransportShieldEsp8266 _blynkTransport;
  151. BlynkWifi Blynk(_blynkTransport);
  152. #include <BlynkWidgets.h>
  153. #endif