redFlyClient.cpp 2.4 KB

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