123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- #include <digitalWriteFast.h>
- #include <RedFly.h>
- #include <RedFlyServer.h>
- byte ip[] = { 192,168, 0, 10 }; //ip from shield (server)
- byte netmask[] = { 255,255,255, 0 }; //netmask
- byte server[] = { 192,168, 0, 2 }; //i.e. Raspberry Pi
- uint8_t http=INVALID_SOCKET; //socket handle
- uint16_t http_len=0; //receive len
- char http_buf[1024]; //receive buffer
- //debug output functions (9600 Baud, 8N2)
- //Leonardo boards use USB for communication, so we dont need to disable the RedFly
- void debugout(char *s)
- {
- #if defined(__AVR_ATmega32U4__)
- Serial.print(s);
- #else
- RedFly.disable();
- Serial.print(s);
- RedFly.enable();
- #endif
- }
- void debugoutln(char *s)
- {
- #if defined(__AVR_ATmega32U4__)
- Serial.println(s);
- #else
- RedFly.disable();
- Serial.println(s);
- RedFly.enable();
- #endif
- }
- void setup()
- {
- for(int i=4; i<20; i++) pinMode(i, OUTPUT);
- digitalWrite(15, 1);
- digitalWrite(13, 1);
- uint8_t ret;
- #if defined(__AVR_ATmega32U4__) //Leonardo boards use USB for communication
- Serial.begin(9600); //init serial port and set baudrate
- while(!Serial); //wait for serial port to connect (needed for Leonardo only)
- #endif
- digitalWrite(12, 1);
- digitalWrite(11, 1);
- //init the WiFi module on the shield
- // ret = RedFly.init(br, pwr) //br=9600|19200|38400|57600|115200|200000|230400, pwr=LOW_POWER|MED_POWER|HIGH_POWER
- // ret = RedFly.init(pwr) //9600 baud, pwr=LOW_POWER|MED_POWER|HIGH_POWER
- // ret = RedFly.init() //9600 baud, HIGH_POWER
- ret = RedFly.init();
- if(ret)
- {
- debugoutln("INIT ERR"); //there are problems with the communication between the Arduino and the RedFly
- }
- else
- {
- digitalWrite(10, 1);
- //scan for wireless networks (must be run before join command)
- RedFly.scan();
- //join network
- // 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
- // ret = join("wlan-ssid", "wlan-passw", IBSS_CREATOR, chn) //create ad-hoc network with password, channel 1-14
- // ret = join("wlan-ssid", IBSS_CREATOR, chn) //create ad-hoc network, channel 1-14
- // ret = join("wlan-ssid", "wlan-passw", INFRASTRUCTURE or IBSS_JOINER) //join infrastructure or ad-hoc network with password
- // ret = join("wlan-ssid", INFRASTRUCTURE or IBSS_JOINER) //join infrastructure or ad-hoc network
- // ret = join("wlan-ssid", "wlan-passw") //join infrastructure network with password
- // ret = join("wlan-ssid") //join infrastructure network
- digitalWrite(9, 1);
- digitalWrite(8, 1);
- ret = RedFly.join("wlan-ssid", "wlan-passw", INFRASTRUCTURE);
- if(ret)
- {
- debugoutln("JOIN ERR");
- for(;;); //do nothing forevermore
- }
- else
- {
- digitalWrite(7, 1);
- digitalWrite(6, 1);
- //set ip config
- // ret = RedFly.begin(); //DHCP
- // ret = RedFly.begin(1 or 2); //1=DHCP or 2=Auto-IP
- // ret = RedFly.begin(ip);
- // ret = RedFly.begin(ip, dnsserver);
- // ret = RedFly.begin(ip, dnsserver, gateway);
- // ret = RedFly.begin(ip, dnsserver, gateway, netmask);
- ret = RedFly.begin(ip, 0, 0, netmask);
- if(ret)
- {
- debugoutln("BEGIN ERR");
- RedFly.disconnect();
- for(;;); //do nothing forevermore
- }
- else
- {
- digitalWrite(5, 1);
- RedFly.getlocalip(ip); //receive shield IP in case of DHCP/Auto-IP
- http = RedFly.socketConnect(PROTO_TCP, server, 80); //start connection to server on port 80
- if(http == 0xFF)
- {
- debugoutln("SOCKET ERR");
- RedFly.disconnect();
- for(;;); //do nothing forevermore
- }
- else
- {
- //send HTTP request
- RedFly.socketSendPGM(http, PSTR("GET /scroll.php HTTP/1.1\r\nHost: 192.168.0.2\r\n\r\n"));
- }
- digitalWrite(4, 1);
- }
- }
- }
- }
- void loop()
- {
- uint8_t sock, buf[32];
- uint16_t rd, len;
- if(http == INVALID_SOCKET) //no socket open
- {
- return;
- }
- sock = 0xFF; //0xFF = return data from all open sockets
- rd = RedFly.socketRead(&sock, &len, buf, sizeof(buf));
- if(sock == http)
- {
- if((rd != 0) && (rd != 0xFFFF))
- {
- /*if((http_len+rd-250) > sizeof(http_buf)) //overflow
- {
- rd = sizeof(http_buf)-http_len;
- }*/
- if(http_len >= 250) //ignore header
- {
- memcpy(&http_buf[http_len-250], buf, rd);
- }
- http_len += rd;
- }
- if((rd == 0xFFFF) || (len == 0)) //connection closed or all data received
- {
- //close connection
- RedFly.socketClose(sock);
- //show http buffer
- //http_buf[sizeof(http_buf)-1] = 0;
- debugout(http_buf);
- int start, width=sizeof(http_buf)-1, n=4;
- for(int x=sizeof(http_buf)-1;x>0;x--)
- {
- if(http_buf[x] == 10)
- {
- width=start-x;
- start=x;
-
- if (n>0)
- n--;
- else
- break;
- }
- }
- if(0 && http_buf[start+1] < 32) {//http_buf[start[0]]!='0' && http_buf[start[0]]!='1'
- digitalWrite(4, !digitalRead(4));
- http = RedFly.socketConnect(PROTO_TCP, server, 80); //start connection to server on port 80
- RedFly.socketSendPGM(http, PSTR("GET /led/scroll.php HTTP/1.1\r\nHost: 192.168.0.2\r\n\r\n"));
- } else {
- unsigned long ms=0;
- unsigned long refresh=0;
- int s=-9;
- refresh=millis()+5*60*1000;
- for(;;) {
- if(millis()>ms) {
- ms=millis()+100;
- s++;
- if (s>=width*7-7) {
- s=-9;
- if ( millis() > refresh) {
- digitalWrite(15, 1);
- digitalWrite(4, !digitalRead(4));
- http_len=0;
- http = RedFly.socketConnect(PROTO_TCP, server, 80); //start connection to server on port 80
- RedFly.socketSendPGM(http, PSTR("GET /led/scroll.php HTTP/1.1\r\nHost: 192.168.0.2\r\n\r\n"));
- break;
- }
- }
- }
- for(int a=15; a<20; a++){
- digitalWrite(a, 1);
- for(int b=0; b<10; b++){
- if(s+b>=0 && s+b<width*7-7)
- digitalWrite(13-b, ((http_buf[start+1+(s+b)/7+(19-a)*width] - 32) >> ((s+b)%7)) & 1);
- else
- digitalWrite(13-b, 0);
- }
- delay(1);
- for(int b=4; b<14; b++){
- digitalWrite(b, 0);
- }
- digitalWrite(a, 0);
- }
- }
- }
- }
- }
- }
|