#include <SoftwareSerial.h>
#define WIFI_RX_PIN 7
#define WIFI_TX_PIN 8
#define WIFI_BAUD_RATE 9600
#define SERIAL_TIMEOUT 250
SoftwareSerial wifiSerial(WIFI_RX_PIN, WIFI_TX_PIN);
void setup() {
Serial.begin(9600);
wifiSerial.begin(9600);
delay(5000);
wifi_configure();
}
void loop() {
wifi_makeHttpRequest("real-chess-with-friends.herokuapp.com","/","");
delay(5000);
}
void wifi_configure() {
Serial.println("configuring wifi");
wifi_enterCommandMode();
// wifi_sendCommand("set wlan join 0"); // disable auto associate
wifi_sendCommand("set comm size 1420"); // set flush size as large as possible
wifi_sendCommand("set ip proto 18"); // http + tcp mode
wifi_sendCommand("set ip remote 80"); // port 80
wifi_sendCommand("set ip address 0"); // turn on dns
wifi_sendCommand("set comm remote 0"); // no default string upon open TCPC connection...
// wifi_sendCommand("set wlan pass password"); // set wireless password if needed
wifi_sendCommand("join MIT"); // join wireless network
wifi_exitCommandMode();
}
void wifi_enterCommandMode() {
Serial.println("entering command mode...");
delay(250);
wifiSerial.print("$$$");
delay(250);
wifi_printResponse();
}
void wifi_exitCommandMode() {
wifi_sendCommand("exit");
}
void wifi_sendCommand(String cmd) {
Serial.print("sending wifi command: ");
Serial.println(cmd);
wifiSerial.println(cmd);
wifi_printResponse();
}
void wifi_printResponse() {
while(wifiSerial.available() <= 0);
long serialDataAvailableAt;
char b;
do {
b = wifiSerial.read();
Serial.write(b);
serialDataAvailableAt = millis();
while(millis() - serialDataAvailableAt < SERIAL_TIMEOUT) {
if( wifiSerial.available() > 0 ) {
serialDataAvailableAt = millis();
break;
}
}
} while(millis() - serialDataAvailableAt < SERIAL_TIMEOUT);
Serial.flush();
wifiSerial.flush();
Serial.println("");
}
void wifi_makeHttpRequest(String host, String path, String args) {
Serial.println("sending http request to " + host + path + "?" + args);
wifi_enterCommandMode();
wifi_sendCommand("set dns name " + host); // set host
wifi_sendCommand("open");
wifiSerial.print("GET " + path + "?" + args);
wifiSerial.print(" HTTP/1.1\nHost: ");
wifiSerial.print("real-chess-with-friends.herokuapp.com\n");
//wifiSerial.write("Accept:application/json\n");
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");
wifiSerial.print("Connection: keep-alive\n");
wifiSerial.print("Cache-Control: max-age=0\n\n");
//wifiSerial.flush();
wifi_printResponse();
}