redFlyClient.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "redFlyClient.hpp"
  2. RedFlyClient client;
  3. byte ip[4];
  4. void debugout(const char* fmt, ...) {
  5. RedFly.disable();
  6. char buffer[100];
  7. va_list args;
  8. va_start(args,fmt);
  9. vsprintf(buffer, fmt, args);
  10. Serial.write(buffer);
  11. va_end(args);
  12. RedFly.enable();
  13. }
  14. int8_t extractIpAddress(char *sourceString, byte *ipAddress) {
  15. byte len=0;
  16. char cnt=0,cnt1=0,i,buf[5];
  17. len=strlen(sourceString);
  18. for(i=0;i<len;i++)
  19. {
  20. if(sourceString[i]!='.' && cnt < 4){
  21. buf[cnt++] =sourceString[i];
  22. }
  23. if((sourceString[i]=='.' || i==len-1) && cnt1 < 4){
  24. buf[cnt]='\0';
  25. cnt=0;
  26. ipAddress[cnt1++]=atoi(buf);
  27. }
  28. }
  29. if(cnt1 != 4) {
  30. return 1;
  31. }
  32. return 0;
  33. }
  34. int8_t connect() {
  35. //init the WiFi module on the shield
  36. // ret = RedFly.init(br, pwr) //br=9600|19200|38400|57600|115200|200000|230400, pwr=LOW_POWER|MED_POWER|HIGH_POWER
  37. wdt_reset();
  38. {
  39. size_t len = strnlen(config.ssid, sizeof(config.ssid));
  40. if(len == 0 && len == sizeof(config.ssid)) {
  41. debugout("CONF ERR\n");
  42. return 1;
  43. }
  44. }
  45. RedFly.scan();
  46. wdt_reset();
  47. matrix.bytes[20] ^= 0x0F;
  48. if(RedFly.join(config.ssid, config.pass, INFRASTRUCTURE)) {
  49. debugout("JOIN ERR\n");
  50. return 2;
  51. }
  52. wdt_reset();
  53. matrix.bytes[21] ^= 0x0F;
  54. if(RedFly.begin()) {
  55. debugout("BEGIN ERR\n");
  56. RedFly.disconnect();
  57. return 3;
  58. }
  59. wdt_reset();
  60. matrix.bytes[22] ^= 0x0F;
  61. if(extractIpAddress(config.host, ip) != 0) {
  62. if(RedFly.getip(config.host, ip) != 0) {
  63. debugout("DNS ERR\n");
  64. return 4;
  65. }
  66. }
  67. wdt_reset();
  68. matrix.bytes[23] ^= 0x0F;
  69. client.connectUDP(ip, config.port, 2021);
  70. client.write('C');
  71. return 0;
  72. }
  73. void ping() {
  74. if(client.connected()) {
  75. client.write('A');
  76. }
  77. }
  78. int8_t readUDP() {
  79. uint16_t len = client.available();
  80. uint8_t buf[4];
  81. wdt_reset();
  82. if(!client.connected()) {
  83. debugout("DC\n");
  84. client.stop(); //stop and reset server
  85. matrix.bytes[22] ^= 0x0F;
  86. if(extractIpAddress(config.host, ip) != 0) {
  87. if(RedFly.getip(config.host, ip) != 0) {
  88. debugout("DNS ERR\n");
  89. return 4;
  90. }
  91. }
  92. client.connectUDP(ip, config.port, 2021);
  93. client.write('C');
  94. return 2;
  95. }
  96. if(len < N_ROWS*N_COLS+1) {
  97. return -1;
  98. }
  99. client.read(buf, 1);
  100. if(buf[0] != 'A') {
  101. client.write(buf[0]);
  102. return 1;
  103. }
  104. client.read(matrix.bytes, N_ROWS*N_COLS);
  105. //sprintf((char*)buf, "%d\n", len);
  106. //client.write(buf, 3);
  107. return 0;
  108. }