Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <SoftwareSerial.h>
  2.  
  3. #define WIFI_RX_PIN 7
  4. #define WIFI_TX_PIN 8
  5. #define WIFI_BAUD_RATE 9600
  6.  
  7. #define SERIAL_TIMEOUT 250
  8.  
  9. SoftwareSerial wifiSerial(WIFI_RX_PIN, WIFI_TX_PIN);
  10.  
  11. void setup() {
  12.   Serial.begin(9600);
  13.  
  14.  
  15.   wifiSerial.begin(9600);
  16.    
  17.   delay(5000);
  18.  
  19.   wifi_configure();
  20. }
  21.  
  22. void loop() {
  23.     wifi_makeHttpRequest("real-chess-with-friends.herokuapp.com","/","");
  24.     delay(5000);
  25. }
  26.  
  27. void wifi_configure() {
  28.   Serial.println("configuring wifi");
  29.  
  30.   wifi_enterCommandMode();
  31.  
  32.   // wifi_sendCommand("set wlan join 0");    // disable auto associate
  33.  
  34.   wifi_sendCommand("set comm size 1420"); // set flush size as large as possible
  35.   wifi_sendCommand("set ip proto 18"); // http + tcp mode
  36.   wifi_sendCommand("set ip remote 80"); // port 80
  37.   wifi_sendCommand("set ip address 0");  // turn on dns
  38.   wifi_sendCommand("set comm remote 0"); // no default string upon open TCPC connection...
  39.  
  40.   // wifi_sendCommand("set wlan pass password");     // set wireless password if needed
  41.   wifi_sendCommand("join MIT");          // join wireless network
  42.  
  43.   wifi_exitCommandMode();
  44. }
  45.  
  46. void wifi_enterCommandMode() {
  47.    Serial.println("entering command mode...");
  48.  
  49.    delay(250);
  50.    wifiSerial.print("$$$");  
  51.    delay(250);
  52.    
  53.    wifi_printResponse();
  54. }
  55.  
  56. void wifi_exitCommandMode() {
  57.   wifi_sendCommand("exit");
  58. }
  59.  
  60. void wifi_sendCommand(String cmd) {
  61.   Serial.print("sending wifi command: ");
  62.   Serial.println(cmd);
  63.  
  64.   wifiSerial.println(cmd);  
  65.   wifi_printResponse();
  66. }
  67.  
  68. void wifi_printResponse() {
  69.   while(wifiSerial.available() <= 0);
  70.  
  71.   long serialDataAvailableAt;
  72.  
  73.   char b;
  74.   do {
  75.     b = wifiSerial.read();
  76.     Serial.write(b);
  77.    
  78.     serialDataAvailableAt = millis();
  79.        
  80.     while(millis() - serialDataAvailableAt < SERIAL_TIMEOUT) {
  81.       if( wifiSerial.available() > 0 ) {
  82.          serialDataAvailableAt = millis();
  83.          break;
  84.       }
  85.     }
  86.  
  87.   } while(millis() - serialDataAvailableAt < SERIAL_TIMEOUT);
  88.  
  89.   Serial.flush();
  90.   wifiSerial.flush();
  91.  
  92.   Serial.println("");
  93. }
  94.  
  95. void wifi_makeHttpRequest(String host, String path, String args) {  
  96.   Serial.println("sending http request to " + host + path + "?" + args);
  97.  
  98.   wifi_enterCommandMode();
  99.   wifi_sendCommand("set dns name " + host);  // set host
  100.   wifi_sendCommand("open");
  101.  
  102.   wifiSerial.print("GET " + path + "?" + args);
  103.   wifiSerial.print(" HTTP/1.1\nHost: ");
  104.   wifiSerial.print("real-chess-with-friends.herokuapp.com\n");
  105.   //wifiSerial.write("Accept:application/json\n");
  106.   wifiSerial.print("User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.91 Safari/537.11\n");
  107.   wifiSerial.print("Connection: keep-alive\n");
  108.   wifiSerial.print("Cache-Control: max-age=0\n\n");
  109.   //wifiSerial.flush();
  110.  
  111.   wifi_printResponse();
  112. }