5x10_led_scrolltext.ino 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include <digitalWriteFast.h>
  2. #include <RedFly.h>
  3. #include <RedFlyServer.h>
  4. byte ip[] = { 192,168, 0, 10 }; //ip from shield (server)
  5. byte netmask[] = { 255,255,255, 0 }; //netmask
  6. byte server[] = { 192,168, 0, 2 }; //i.e. Raspberry Pi
  7. uint8_t http=INVALID_SOCKET; //socket handle
  8. uint16_t http_len=0; //receive len
  9. char http_buf[1024]; //receive buffer
  10. //debug output functions (9600 Baud, 8N2)
  11. //Leonardo boards use USB for communication, so we dont need to disable the RedFly
  12. void debugout(char *s)
  13. {
  14. #if defined(__AVR_ATmega32U4__)
  15. Serial.print(s);
  16. #else
  17. RedFly.disable();
  18. Serial.print(s);
  19. RedFly.enable();
  20. #endif
  21. }
  22. void debugoutln(char *s)
  23. {
  24. #if defined(__AVR_ATmega32U4__)
  25. Serial.println(s);
  26. #else
  27. RedFly.disable();
  28. Serial.println(s);
  29. RedFly.enable();
  30. #endif
  31. }
  32. void setup()
  33. {
  34. for(int i=4; i<20; i++) pinMode(i, OUTPUT);
  35. digitalWrite(15, 1);
  36. digitalWrite(13, 1);
  37. uint8_t ret;
  38. #if defined(__AVR_ATmega32U4__) //Leonardo boards use USB for communication
  39. Serial.begin(9600); //init serial port and set baudrate
  40. while(!Serial); //wait for serial port to connect (needed for Leonardo only)
  41. #endif
  42. digitalWrite(12, 1);
  43. digitalWrite(11, 1);
  44. //init the WiFi module on the shield
  45. // ret = RedFly.init(br, pwr) //br=9600|19200|38400|57600|115200|200000|230400, pwr=LOW_POWER|MED_POWER|HIGH_POWER
  46. // ret = RedFly.init(pwr) //9600 baud, pwr=LOW_POWER|MED_POWER|HIGH_POWER
  47. // ret = RedFly.init() //9600 baud, HIGH_POWER
  48. ret = RedFly.init();
  49. if(ret)
  50. {
  51. debugoutln("INIT ERR"); //there are problems with the communication between the Arduino and the RedFly
  52. }
  53. else
  54. {
  55. digitalWrite(10, 1);
  56. //scan for wireless networks (must be run before join command)
  57. RedFly.scan();
  58. //join network
  59. // ret = join("wlan-ssid", "wlan-passw", INFRASTRUCTURE or IBSS_JOINER or IBSS_CREATOR, chn, authmode) //join infrastructure or ad-hoc network, or create ad-hoc network
  60. // ret = join("wlan-ssid", "wlan-passw", IBSS_CREATOR, chn) //create ad-hoc network with password, channel 1-14
  61. // ret = join("wlan-ssid", IBSS_CREATOR, chn) //create ad-hoc network, channel 1-14
  62. // ret = join("wlan-ssid", "wlan-passw", INFRASTRUCTURE or IBSS_JOINER) //join infrastructure or ad-hoc network with password
  63. // ret = join("wlan-ssid", INFRASTRUCTURE or IBSS_JOINER) //join infrastructure or ad-hoc network
  64. // ret = join("wlan-ssid", "wlan-passw") //join infrastructure network with password
  65. // ret = join("wlan-ssid") //join infrastructure network
  66. digitalWrite(9, 1);
  67. digitalWrite(8, 1);
  68. ret = RedFly.join("wlan-ssid", "wlan-passw", INFRASTRUCTURE);
  69. if(ret)
  70. {
  71. debugoutln("JOIN ERR");
  72. for(;;); //do nothing forevermore
  73. }
  74. else
  75. {
  76. digitalWrite(7, 1);
  77. digitalWrite(6, 1);
  78. //set ip config
  79. // ret = RedFly.begin(); //DHCP
  80. // ret = RedFly.begin(1 or 2); //1=DHCP or 2=Auto-IP
  81. // ret = RedFly.begin(ip);
  82. // ret = RedFly.begin(ip, dnsserver);
  83. // ret = RedFly.begin(ip, dnsserver, gateway);
  84. // ret = RedFly.begin(ip, dnsserver, gateway, netmask);
  85. ret = RedFly.begin(ip, 0, 0, netmask);
  86. if(ret)
  87. {
  88. debugoutln("BEGIN ERR");
  89. RedFly.disconnect();
  90. for(;;); //do nothing forevermore
  91. }
  92. else
  93. {
  94. digitalWrite(5, 1);
  95. RedFly.getlocalip(ip); //receive shield IP in case of DHCP/Auto-IP
  96. http = RedFly.socketConnect(PROTO_TCP, server, 80); //start connection to server on port 80
  97. if(http == 0xFF)
  98. {
  99. debugoutln("SOCKET ERR");
  100. RedFly.disconnect();
  101. for(;;); //do nothing forevermore
  102. }
  103. else
  104. {
  105. //send HTTP request
  106. RedFly.socketSendPGM(http, PSTR("GET /scroll.php HTTP/1.1\r\nHost: 192.168.0.2\r\n\r\n"));
  107. }
  108. digitalWrite(4, 1);
  109. }
  110. }
  111. }
  112. }
  113. void loop()
  114. {
  115. uint8_t sock, buf[32];
  116. uint16_t rd, len;
  117. if(http == INVALID_SOCKET) //no socket open
  118. {
  119. return;
  120. }
  121. sock = 0xFF; //0xFF = return data from all open sockets
  122. rd = RedFly.socketRead(&sock, &len, buf, sizeof(buf));
  123. if(sock == http)
  124. {
  125. if((rd != 0) && (rd != 0xFFFF))
  126. {
  127. /*if((http_len+rd-250) > sizeof(http_buf)) //overflow
  128. {
  129. rd = sizeof(http_buf)-http_len;
  130. }*/
  131. if(http_len >= 250) //ignore header
  132. {
  133. memcpy(&http_buf[http_len-250], buf, rd);
  134. }
  135. http_len += rd;
  136. }
  137. if((rd == 0xFFFF) || (len == 0)) //connection closed or all data received
  138. {
  139. //close connection
  140. RedFly.socketClose(sock);
  141. //show http buffer
  142. //http_buf[sizeof(http_buf)-1] = 0;
  143. debugout(http_buf);
  144. int start, width=sizeof(http_buf)-1, n=4;
  145. for(int x=sizeof(http_buf)-1;x>0;x--)
  146. {
  147. if(http_buf[x] == 10)
  148. {
  149. width=start-x;
  150. start=x;
  151. if (n>0)
  152. n--;
  153. else
  154. break;
  155. }
  156. }
  157. if(0 && http_buf[start+1] < 32) {//http_buf[start[0]]!='0' && http_buf[start[0]]!='1'
  158. digitalWrite(4, !digitalRead(4));
  159. http = RedFly.socketConnect(PROTO_TCP, server, 80); //start connection to server on port 80
  160. RedFly.socketSendPGM(http, PSTR("GET /led/scroll.php HTTP/1.1\r\nHost: 192.168.0.2\r\n\r\n"));
  161. } else {
  162. unsigned long ms=0;
  163. unsigned long refresh=0;
  164. int s=-9;
  165. refresh=millis()+5*60*1000;
  166. for(;;) {
  167. if(millis()>ms) {
  168. ms=millis()+100;
  169. s++;
  170. if (s>=width*7-7) {
  171. s=-9;
  172. if ( millis() > refresh) {
  173. digitalWrite(15, 1);
  174. digitalWrite(4, !digitalRead(4));
  175. http_len=0;
  176. http = RedFly.socketConnect(PROTO_TCP, server, 80); //start connection to server on port 80
  177. RedFly.socketSendPGM(http, PSTR("GET /led/scroll.php HTTP/1.1\r\nHost: 192.168.0.2\r\n\r\n"));
  178. break;
  179. }
  180. }
  181. }
  182. for(int a=15; a<20; a++){
  183. digitalWrite(a, 1);
  184. for(int b=0; b<10; b++){
  185. if(s+b>=0 && s+b<width*7-7)
  186. digitalWrite(13-b, ((http_buf[start+1+(s+b)/7+(19-a)*width] - 32) >> ((s+b)%7)) & 1);
  187. else
  188. digitalWrite(13-b, 0);
  189. }
  190. delay(1);
  191. for(int b=4; b<14; b++){
  192. digitalWrite(b, 0);
  193. }
  194. digitalWrite(a, 0);
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }