hadif azli / Mbed 2 deprecated TEST123

Dependencies:   mbed Blynk

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BlynkSimpleShieldEsp8266_HardSer.h Source File

BlynkSimpleShieldEsp8266_HardSer.h

00001 /**
00002  * @file       BlynkSimpleShieldEsp8266.h
00003  * @author     Volodymyr Shymanskyy
00004  * @license    This project is released under the MIT License (MIT)
00005  * @copyright  Copyright (c) 2015 Volodymyr Shymanskyy
00006  * @date       Jun 2015
00007  * @brief
00008  *
00009  */
00010 
00011 #ifndef BlynkSimpleShieldEsp8266_h
00012 #define BlynkSimpleShieldEsp8266_h
00013 
00014 #ifdef ESP8266
00015 #error This code is not intended to run on the ESP8266 platform! Please check your Tools->Board setting.
00016 #endif
00017 
00018 #ifndef BLYNK_INFO_CONNECTION
00019 #define BLYNK_INFO_CONNECTION  "ESP8266"
00020 #endif
00021 
00022 extern Serial pc;
00023 //#define LOG_ESP8266
00024 #ifdef LOG_ESP8266
00025 #define ESP8266_LOG pc.printf
00026 #define LOG_ENTER ESP8266_LOG("Enter %s\r\n", __func__);
00027 #else
00028 #define ESP8266_LOG(...)
00029 #define LOG_ENTER
00030 #endif
00031 
00032 #define BLYNK_SEND_ATOMIC
00033 
00034 // TODO: Remove this hotfix
00035 #define BLYNK_NO_INFO
00036 
00037 //#include <BlynkApiArduino.h>
00038 #include <BlynkApiMbed.h>
00039 #include <Blynk/BlynkProtocol.h>
00040 #include <utility/BlynkFifo.h>
00041 #include <ESP8266_HardSer.h>
00042 
00043 class BlynkTransportShieldEsp8266
00044 {
00045     static void onData(uint8_t mux_id, uint32_t len, void* ptr) {
00046         ((BlynkTransportShieldEsp8266*)ptr)->onData(mux_id, len);
00047     }
00048 
00049     void onData(uint8_t mux_id, uint32_t len) {
00050         //BLYNK_LOG2("Got ", len);
00051         while (len) {
00052             if (client->getUart()->readable()) {
00053                 uint8_t b = client->getUart()->getc();
00054                 if(!buffer.push(b)) {
00055                     BLYNK_LOG1(BLYNK_F("Buffer overflow"));
00056                     ESP8266_LOG(BLYNK_F("Buffer overflow"));
00057                 }
00058                 len--;
00059             }
00060         }
00061     }
00062 
00063 public:
00064     BlynkTransportShieldEsp8266()
00065         : client(NULL)
00066         , status(false)
00067         , domain(NULL)
00068         , port(0)
00069     {}
00070 
00071     void begin_domain(ESP8266* esp8266, const char* d,  uint16_t p) {
00072         LOG_ENTER;
00073         client = esp8266;
00074         client->setOnData(onData, this);
00075         domain = d;
00076         port = p;
00077     }
00078 
00079     bool connect() {
00080         ESP8266_LOG("connect(), domain = %s, port = %d\r\n", domain, port);
00081         if (!domain || !port)
00082             return false;
00083         status = client->createTCP(domain, port);
00084         return status;
00085     }
00086 
00087     void disconnect() {
00088         #if 0
00089         LOG_ENTER;
00090         
00091         status = false;
00092         buffer.clear();
00093         client->releaseTCP();
00094         #endif
00095     }
00096 
00097     size_t read(void* buf, size_t len) {
00098         uint32_t start = millis();
00099         ESP8266_LOG("Waiting: %d, Occuied: %d\r\n", len, buffer.getOccupied());
00100         //BLYNK_LOG4("Waiting: ", len, " Occuied: ", buffer.getOccupied());
00101         while ((buffer.getOccupied() < len) && (millis() - start < 4500)) {
00102             client->run();
00103         }
00104         return buffer.read((uint8_t*)buf, len);
00105     }
00106     size_t write(const void* buf, size_t len) {
00107         ESP8266_LOG("Enter write, len = %d\r\n", len);
00108         if (client->send((const uint8_t*)buf, len)) {
00109             return len;
00110         }
00111         return 0;
00112     }
00113 
00114     bool connected() {
00115         return status;
00116     }
00117 
00118     int available() {
00119         LOG_ENTER;
00120         client->run();
00121         //BLYNK_LOG2("Still: ", buffer.getOccupied());
00122         return buffer.getOccupied();
00123     }
00124 
00125 private:
00126     ESP8266* client;
00127     bool status;
00128     BlynkFifo<uint8_t,256> buffer;
00129     const char* domain;
00130     uint16_t    port;
00131 };
00132 
00133 class BlynkWifi
00134     : public BlynkProtocol<BlynkTransportShieldEsp8266>
00135 {
00136     typedef BlynkProtocol<BlynkTransportShieldEsp8266> Base;
00137 public:
00138     BlynkWifi(BlynkTransportShieldEsp8266& transp)
00139         : Base(transp)
00140         , wifi(NULL)
00141     {}
00142 
00143     bool connectWiFi(const char* ssid, const char* pass) {
00144         LOG_ENTER;
00145         delay(500);
00146         BLYNK_LOG2(BLYNK_F("Connecting to "), ssid);
00147         ESP8266_LOG(BLYNK_F("Connecting to %s\r\n"), ssid);
00148         /*if (!wifi->restart()) {
00149             BLYNK_LOG1(BLYNK_F("Failed to restart"));
00150             return false;
00151         }*/
00152         if (!wifi->setEcho(0)) {
00153             BLYNK_LOG1(BLYNK_F("Failed to disable Echo"));
00154             ESP8266_LOG(BLYNK_F("Failed to disable Echo"));
00155             return false;
00156         }
00157         if (!wifi->setOprToStation()) {
00158             BLYNK_LOG1(BLYNK_F("Failed to set STA mode"));
00159             ESP8266_LOG(BLYNK_F("Failed to set STA mode"));
00160             return false;
00161         }
00162         if (wifi->joinAP(ssid, pass)) {
00163             BLYNK_LOG2(BLYNK_F("IP: "), wifi->getLocalIP().c_str());
00164             ESP8266_LOG(BLYNK_F("IP: "), wifi->getLocalIP().c_str());
00165         } else {
00166             BLYNK_LOG1(BLYNK_F("Failed to connect WiFi"));
00167             ESP8266_LOG(BLYNK_F("Failed to connect WiFi"));
00168             return false;
00169         }
00170         if (!wifi->disableMUX()) {
00171             BLYNK_LOG1(BLYNK_F("Failed to disable MUX"));
00172             ESP8266_LOG(BLYNK_F("Failed to disable MUX"));
00173         }
00174         BLYNK_LOG1(BLYNK_F("Connected to WiFi"));
00175         ESP8266_LOG(BLYNK_F("Connected to WiFi"));
00176         return true;
00177     }
00178 
00179     void config(ESP8266&    esp8266,
00180                 const char* auth,
00181                 const char* domain = BLYNK_DEFAULT_DOMAIN,
00182                 uint16_t    port   = BLYNK_DEFAULT_PORT) {
00183         LOG_ENTER;
00184         Base::begin(auth);
00185         wifi = &esp8266;
00186         this->conn.begin_domain(wifi, domain, port);
00187     }
00188 
00189     void begin(const char* auth,
00190                ESP8266&    esp8266,
00191                const char* ssid,
00192                const char* pass,
00193                const char* domain = BLYNK_DEFAULT_DOMAIN,
00194                uint16_t    port   = BLYNK_DEFAULT_PORT) {
00195         LOG_ENTER;
00196         config(esp8266, auth, domain, port);
00197         connectWiFi(ssid, pass);
00198     }
00199 
00200 private:
00201     ESP8266* wifi;
00202 };
00203 
00204 static BlynkTransportShieldEsp8266 _blynkTransport;
00205 BlynkWifi Blynk(_blynkTransport);
00206 
00207 #include <BlynkWidgets.h>
00208 
00209 #endif
00210