Blynk example

Dependents:   Blynk_Example_WIZwiki-W7500

Fork of Blynk by Volodymyr Shymanskyy

Revision:
15:544afbc8228f
Child:
16:5d8386745e22
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BlynkSimpleEthernet2.h	Wed Aug 02 02:00:41 2017 +0000
@@ -0,0 +1,210 @@
+/**
+ * @file       BlynkSimpleEthernet2.h
+ * @author     Volodymyr Shymanskyy
+ * @license    This project is released under the MIT License (MIT)
+ * @copyright  Copyright (c) 2015 Volodymyr Shymanskyy
+ * @date       May 2016
+ * @brief
+ *
+ */
+
+#ifndef BlynkSimpleEthernet2_h
+#define BlynkSimpleEthernet2_h
+
+#ifndef BLYNK_INFO_CONNECTION
+#define BLYNK_INFO_CONNECTION "W5500"
+#endif
+
+#include <BlynkApiMbed.h>
+#include <Blynk/BlynkProtocol.h>
+#include <utility/BlynkFifo2.h>
+
+
+#include "EthernetInterface.h"
+#include "TCPSocketConnection.h"
+
+#define JCM_DEBUG
+
+//EthernetInterface eth;
+
+template <typename Client>
+class BlynkMbedClientGen
+{
+public:
+    BlynkMbedClientGen(Client& c)
+        : client(NULL),  port(0), isConn(false)
+    {
+        setClient(&c);
+    }
+    BlynkMbedClientGen()
+        : client(NULL),  port(0), isConn(false)
+    {}
+
+    void setClient(Client* c) {
+        client = c;
+        client->setTimeout(BLYNK_TIMEOUT_MS);
+    }
+
+
+    void begin(const char* a, uint16_t p) {
+        addr = a;
+        port = p;
+    }
+
+    bool connect() {
+        isConn = (1 == client->connect(addr, port));
+        return isConn;
+        
+    }
+    
+    void disconnect() {isConn=false; client->close(); }
+    
+    int read(void* buf, int len){
+        int res=client->receive((char*)buf, len);
+        return res;
+    }
+    
+    int write(const void* buf, int len){
+        int res=client->send((char*)buf, len);
+        return res;
+    }
+    
+    
+    bool connected() { return isConn && client->is_connected();}
+    int available() {return isConn;} //&& client->connected();} //eth get rx rsr 읽어오기
+    
+
+protected:
+    Client*     client;
+    const char* addr;
+    uint16_t    port;
+    bool        isConn;
+};
+
+typedef BlynkMbedClientGen<TCPSocketConnection> BlynkMbedClient;
+
+
+class BlynkEthernet
+    : public BlynkProtocol<BlynkMbedClient>
+{
+    typedef BlynkProtocol<BlynkMbedClient> Base;
+public:
+    BlynkEthernet(BlynkMbedClient& transp)
+        : Base(transp)
+    {}
+
+
+    void config(const char* auth,
+                const char* addr = BLYNK_DEFAULT_DOMAIN,
+                uint16_t    port   = BLYNK_DEFAULT_PORT)
+    {
+        Base::begin(auth);
+        this->conn.begin(addr, port);
+    }
+
+
+    // DHCP with domain
+    void begin( const char* auth,
+                const char* addr = BLYNK_DEFAULT_DOMAIN,
+                uint16_t port      = BLYNK_DEFAULT_PORT,
+                uint8_t mac[]   = NULL)
+    {
+       // int phy_link;
+
+        
+#ifdef JCM_DEBUG   
+//        printf("blnk begin 1\r\n");
+#endif
+        //BLYNK_LOG1(BLYNK_F("Getting IP..."));
+        //eth.init(mac_addr);
+#ifdef JCM_DEBUG   
+       // printf("eth init 1\r\n");
+#endif  
+//do{
+//            phy_link = eth.ethernet_link();
+//            printf("...");
+//            wait(2);
+//        }while(!phy_link);
+//        printf("\r\n");
+//              
+//        eth.connect();
+//        printf("eth connection!\r\n");
+//        // give the Ethernet shield a second to initialize:
+//        ::delay(1000);
+        
+        //char* myip = eth.getIPAddress();
+        //BLYNK_LOG_IP("IP:", myip);
+
+        config(auth, addr, port);
+        while(this->connect() != true) {}
+    }
+
+
+//    // Static IP with domain, gateway, etc
+//    void begin( const char* auth,
+//                const char* addr,
+//                uint16_t port,
+//                const char* gateway,
+//                const char* subnet,
+//                uint8_t mac[] = NULL)
+//    {
+//        BLYNK_LOG1(BLYNK_F("Using static IP"));
+//        eth.init(SelectMacAddress(auth, mac), addr, subnet, gateway);
+//        if (eth.connect()==-1) {
+//            //BLYNK_FATAL(BLYNK_F("DHCP Failed!"));
+//        }
+//        // give the Ethernet shield a second to initialize:
+//        ::delay(1000);
+//        char* myip = eth.getIPAddress();
+//        //BLYNK_LOG_IP("IP:", myip);
+//
+//        config(auth, addr, port);
+//        while(this->connect() != true) {}
+//    }
+    
+
+
+
+private:
+
+    uint8_t* SelectMacAddress(const char* token, const uint8_t mac[])
+    {
+        if (mac != NULL) {
+            return (uint8_t*)mac;
+        }
+
+        macAddress[0] = 0x00;
+        macAddress[1] = 0x08;
+        macAddress[2] = 0xdc;
+        macAddress[3] = 0x93;
+        macAddress[4] = 0x12;
+        macAddress[5] = 0x13;
+
+        int len = strlen(token);
+        int mac_index = 1;
+        for (int i=0; i<len; i++) {
+            macAddress[mac_index++] ^= token[i];
+
+            if (mac_index > 5) { mac_index = 1; }
+        }
+        /* BLYNK_LOG("MAC: %02X-%02X-%02X-%02X-%02X-%02X",
+                  macAddress[0], macAddress[1],
+                  macAddress[2], macAddress[3],
+                  macAddress[4], macAddress[5]);
+        */
+        return macAddress;
+    }
+
+private:
+    uint8_t macAddress[6];
+
+};
+    
+
+static BlynkMbedClient _blynkTransport;
+BlynkEthernet Blynk(_blynkTransport);
+
+
+#include <BlynkWidgets.h>
+
+#endif