blynk & neopixelring & w7500

Fork of WIZwiki-7500_Blynk by IOP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BlynkEthernet.h Source File

BlynkEthernet.h

Go to the documentation of this file.
00001 /**
00002  * @file       BlynkEthernet.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       Jan 2015
00007  * @brief
00008  *
00009  */
00010 
00011 #ifndef BlynkEthernet_h
00012 #define BlynkEthernet_h
00013 
00014 #ifndef BLYNK_INFO_CONNECTION
00015 #define BLYNK_INFO_CONNECTION "W5000"
00016 #endif
00017 
00018 #include <EthernetInterface.h>
00019 #include <BlynkApiMbed.h >
00020 #include <Blynk/BlynkProtocol.h>
00021 #include <Adapters/BlynkMbedClient.h>
00022 
00023 EthernetInterface eth;
00024 
00025 class BlynkEthernet
00026     : public BlynkProtocol<BlynkMbedClient>
00027 {
00028     typedef BlynkProtocol<BlynkMbedClient> Base;
00029 public:
00030     BlynkEthernet(BlynkMbedClient& transp)
00031         : Base(transp)
00032     {}
00033 
00034     void config(const char* auth,
00035                 const char* domain = BLYNK_DEFAULT_DOMAIN,
00036                 uint16_t    port   = BLYNK_DEFAULT_PORT)
00037     {
00038         Base::begin(auth);
00039         this->conn.begin(domain, port);
00040     }
00041 
00042     void config(const char* auth,
00043                 char*   ip,
00044                 uint16_t    port = BLYNK_DEFAULT_PORT)
00045     {
00046         Base::begin(auth);
00047         this->conn.begin(ip, port);
00048     }
00049 
00050     // DHCP with domain
00051     void begin( const char* auth,
00052                 const char* domain = BLYNK_DEFAULT_DOMAIN,
00053                 uint16_t port      = BLYNK_DEFAULT_PORT,
00054                 uint8_t mac[]   = NULL)
00055     {
00056         int phy_link;
00057         eth.init(mac);
00058 
00059         do{
00060             phy_link = eth.ethernet_link();
00061             printf("...");
00062             wait(2);
00063         }while(!phy_link);
00064         
00065         BLYNK_LOG1(BLYNK_F("Getting IP..."));
00066         if (eth.connect()==-1) {
00067             BLYNK_FATAL(BLYNK_F("DHCP Failed!"));
00068         }
00069         
00070     
00071         // give the Ethernet shield a second to initialize:
00072         ::delay(1000);
00073         char* myip = eth.getIPAddress();
00074         BLYNK_LOG_IP("IP:", myip);
00075         printf("my IP : %s\r\n", myip);
00076 
00077         config(auth, domain, port);
00078         //printf("configure!\r\n");
00079         while(this->connect() != true) {}
00080     }
00081 //
00082 //    // Static IP with domain
00083 //    void begin( const char* auth,
00084 //                const char* domain,
00085 //                uint16_t port,
00086 //                char* local,
00087 //                char* dns,
00088 //                const uint8_t mac[] = NULL)
00089 //    {
00090 //        BLYNK_LOG1(BLYNK_F("Using static IP"));
00091 //        Ethernet.begin(SelectMacAddress(auth, mac), local, dns);
00092 //        // give the Ethernet shield a second to initialize:
00093 //        ::delay(1000);
00094 //        char* myip = Ethernet.localIP();
00095 //        BLYNK_LOG_IP("IP:", myip);
00096 //
00097 //        config(auth, domain, port);
00098 //        while(this->connect() != true) {}
00099 //    }
00100 //
00101 //    // Static IP with domain, gateway, etc
00102 //    void begin( const char* auth,
00103 //                const char* domain,
00104 //                uint16_t port,
00105 //                char* local,
00106 //                char* dns,
00107 //                char* gateway,
00108 //                char* subnet,
00109 //                const uint8_t mac[] = NULL)
00110 //    {
00111 //        BLYNK_LOG1(BLYNK_F("Using static IP"));
00112 //        Ethernet.begin(SelectMacAddress(auth, mac), local, dns, gateway, subnet);
00113 //        // give the Ethernet shield a second to initialize:
00114 //        ::delay(1000);
00115 //        char* myip = Ethernet.localIP();
00116 //        BLYNK_LOG_IP("IP:", myip);
00117 //
00118 //        config(auth, domain, port);
00119 //        while(this->connect() != true) {}
00120 //    }
00121 //
00122 //    // DHCP with server IP
00123 //    void begin( const char* auth,
00124 //                char* addr,
00125 //                uint16_t port    = BLYNK_DEFAULT_PORT,
00126 //                const uint8_t mac[] = NULL)
00127 //    {
00128 //        BLYNK_LOG1(BLYNK_F("Getting IP..."));
00129 //        if (!Ethernet.begin(SelectMacAddress(auth, mac))) {
00130 //            BLYNK_FATAL(BLYNK_F("DHCP Failed!"));
00131 //        }
00132 //        // give the Ethernet shield a second to initialize:
00133 //        ::delay(1000);
00134 //        char* myip = Ethernet.localIP();
00135 //        BLYNK_LOG_IP("IP:", myip);
00136 //
00137 //        config(auth, addr, port);
00138 //        while(this->connect() != true) {}
00139 //    }
00140 //
00141 //    // Static IP with server IP
00142 //    void begin( const char* auth,
00143 //                char* addr,
00144 //                uint16_t port,
00145 //                char* local,
00146 //                const uint8_t mac[] = NULL)
00147 //    {
00148 //        BLYNK_LOG1(BLYNK_F("Using static IP"));
00149 //        Ethernet.begin(SelectMacAddress(auth, mac), local);
00150 //        // give the Ethernet shield a second to initialize:
00151 //        ::delay(1000);
00152 //        char* myip = Ethernet.localIP();
00153 //        BLYNK_LOG_IP("IP:", myip);
00154 //
00155 //        config(auth, addr, port);
00156 //        while(this->connect() != true) {}
00157 //    }
00158 //
00159 //    // Static IP with server IP, DNS, gateway, etc
00160 //    void begin( const char* auth,
00161 //                char* addr,
00162 //                uint16_t port,
00163 //                char* local,
00164 //                char* dns,
00165 //                char* gateway,
00166 //                char* subnet,
00167 //                const uint8_t mac[] = NULL)
00168 //    {
00169 //        BLYNK_LOG1(BLYNK_F("Using static IP"));
00170 //        Ethernet.begin(SelectMacAddress(auth, mac), local, dns, gateway, subnet);
00171 //        // give the Ethernet shield a second to initialize:
00172 //        ::delay(1000);
00173 //        char* myip = Ethernet.localIP();
00174 //        BLYNK_LOG_IP("IP:", myip);
00175 //
00176 //        config(auth, addr, port);
00177 //        while(this->connect() != true) {}
00178 //    }
00179 //
00180 //private:
00181 //
00182 //    byte* SelectMacAddress(const char* token, const uint8_t mac[])
00183 //    {
00184 //        if (mac != NULL) {
00185 //            return (byte*)mac;
00186 //        }
00187 //
00188 //        macAddress[0] = 0xFE;
00189 //        macAddress[1] = 0xED;
00190 //        macAddress[2] = 0xBA;
00191 //        macAddress[3] = 0xFE;
00192 //        macAddress[4] = 0xFE;
00193 //        macAddress[5] = 0xED;
00194 //
00195 //        int len = strlen(token);
00196 //        int mac_index = 1;
00197 //        for (int i=0; i<len; i++) {
00198 //            macAddress[mac_index++] ^= token[i];
00199 //
00200 //            if (mac_index > 5) { mac_index = 1; }
00201 //        }
00202 //        /* BLYNK_LOG("MAC: %02X-%02X-%02X-%02X-%02X-%02X",
00203 //                  macAddress[0], macAddress[1],
00204 //                  macAddress[2], macAddress[3],
00205 //                  macAddress[4], macAddress[5]);
00206 //        */
00207 //        return macAddress;
00208 //    }
00209 
00210 private:
00211     uint8_t macAddress[6];
00212 
00213 };
00214 
00215 #endif