CayenneEthernetClient.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. The MIT License(MIT)
  3. Cayenne Arduino Client Library
  4. Copyright © 2016 myDevices
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  6. documentation files(the "Software"), to deal in the Software without restriction, including without limitation
  7. the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software,
  8. and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
  9. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  11. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
  12. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  13. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  14. This software uses open source blynk-library - see blynk-library/LICENSE
  15. */
  16. #ifndef _CAYENNEETHERNETCLIENT_h
  17. #define _CAYENNEETHERNETCLIENT_h
  18. #include "CayenneClient.h"
  19. class CayenneEthernetClient : public CayenneClient
  20. {
  21. public:
  22. /**
  23. * Begins Cayenne session
  24. * @param token Authentication token from Cayenne site
  25. * @param mac Mac address for device
  26. */
  27. void begin(const char* token, const byte mac[] = NULL)
  28. {
  29. Blynk.begin(token, CAYENNE_DOMAIN, CAYENNE_PORT, GetMACAddress(token, mac));
  30. }
  31. /**
  32. * Begins Cayenne session
  33. * @param token Authentication token from Cayenne site
  34. * @param local Static IP address of device
  35. * @param mac Mac address for device
  36. */
  37. void begin(const char* token, IPAddress local, const byte mac[] = NULL)
  38. {
  39. Blynk.begin(token, CAYENNE_DOMAIN, CAYENNE_PORT, local, GetMACAddress(token, mac));
  40. }
  41. /**
  42. * Begins Cayenne session
  43. * @param token Authentication token from Cayenne site
  44. * @param local Static IP address of device
  45. * @param dns IP address of DNS server
  46. * @param gateway IP address of gateway
  47. * @param subnet Subnet mask
  48. * @param mac Mac address for device
  49. */
  50. void begin(const char* token, IPAddress local, IPAddress dns, IPAddress gateway, IPAddress subnet, const byte mac[] = NULL)
  51. {
  52. Blynk.begin(token, CAYENNE_DOMAIN, CAYENNE_PORT, local, dns, gateway, subnet, GetMACAddress(token, mac));
  53. }
  54. private:
  55. /**
  56. * Get MAC address fror the device
  57. * @param token Authentication token from Cayenne site
  58. * @param mac User defined mac address for device
  59. * @returns Mac address for device
  60. */
  61. const byte* GetMACAddress(const char* token, const byte mac[])
  62. {
  63. if (mac != NULL)
  64. return mac;
  65. _mac[0] = 0xFE;
  66. if (strnlen(token, 10) != 10)
  67. {
  68. CAYENNE_LOG("Using default MAC");
  69. _mac[1] = 0xED;
  70. _mac[2] = 0xBA;
  71. _mac[3] = 0xFE;
  72. _mac[4] = 0xFE;
  73. _mac[5] = 0xED;
  74. }
  75. else
  76. {
  77. //Generate MAC from token to prevent collisions on the same network.
  78. _mac[1] = token[0] + token[1];
  79. _mac[2] = token[2] + token[3];
  80. _mac[3] = token[4] + token[5];
  81. _mac[4] = token[6] + token[7];
  82. _mac[5] = token[8] + token[9];
  83. }
  84. CAYENNE_LOG("MAC: %X-%X-%X-%X-%X-%X", _mac[0], _mac[1], _mac[2], _mac[3], _mac[4], _mac[5]);
  85. return _mac;
  86. }
  87. byte _mac[6];
  88. };
  89. CayenneEthernetClient Cayenne;
  90. #endif