Skip to content

Instantly share code, notes, and snippets.

@tcr
Created March 12, 2013 03:52
Show Gist options
  • Save tcr/5140188 to your computer and use it in GitHub Desktop.
Save tcr/5140188 to your computer and use it in GitHub Desktop.
/*
* Modified from WiFlyHQ Example httpclient.ino
*
* This sketch implements a simple Web client that connects to a
* web server, sends a GET, and then sends the result to the
* Serial monitor.
*
* This sketch is released to the public domain.
*
*/
#include <WiFlyHQ.h>
WiFly wifly;
#include <SoftwareSerial.h>
SoftwareSerial wifiSerial(2,3);
/**
* Configuration
*/
const char mySSID[] = "OLIN_GUEST";
const char myPassword[] = "...";
int light = 12; // define our light pin
/**
* Setup
*/
void setup()
{
// Serial ports.
Serial.begin(9600);
wifiSerial.begin(9600);
pinMode(light, OUTPUT);
Serial.println("Connecting...");
// Setup network.
if (!connectWifi(mySSID, myPassword)) {
Serial.println("Failed to join network.");
} else {
Serial.println("Joined wifi network.");
}
}
void loop()
{
// Make request.
getRequestByUrl("http://notificationlight.herokuapp.com/action/1......");
// Get response.
char buf[256];
int len;
getResponse(buf, &len, sizeof(buf));
// Parse response.
if (len > 0) {
int ison = atoi(buf);
Serial.print("Notification state: ");
Serial.println(buf);
digitalWrite(light, ison ? HIGH : LOW);
}
}
/**
* Abstracted HTTP nonsense
*/
boolean connectWifi (const char *ssid, const char *pass) {
if (!wifly.begin(&wifiSerial, &Serial)) {
return false;
}
// Join wifi network if not already associated
if (!wifly.isAssociated()) {
wifly.setSSID(ssid);
wifly.setPassphrase(pass);
wifly.enableDHCP();
wifly.setDeviceID("Wifly-WebClient");
if (!wifly.join()) {
Serial.println("Failed to join wifi network");
return false;
}
}
return true;
}
void getRequestByUrl (char *url) {
// TODO not include 256 byte buffer here.
char host[256], *path;
parseUrl(url, host, &path);
getRequest(host, path);
}
void getRequest (char *host, char *path) {
// If an old connection is active, close.
if (wifly.isConnected()) {
wifly.close();
}
if (wifly.open(host, 80)) {
Serial.println("Connected.");
} else {
Serial.println("Failed to connect");
return;
}
getHeader(host, path);
}
void getResponse (char *buf, int *content_len, int max_len) {
boolean line_start = true;
char toss[1];
int len;
*content_len = 0;
while (true) {
// Read first character of line.
wifly.readBytes(buf, 1);
char ch = buf[0];
if (line_start) {
// Beginning content.
if (ch == '\r') {
wifly.readBytes(buf, 1); // read ...\n\r\n
break;
}
}
// Read header name.
len = wifly.readBytesUntil(':', &buf[1], max_len - 2);
buf[len + 1] = '\0';
wifly.readBytes(toss, 1); // space
if (strncmp(buf, "Content-Length", max_len) == 0) {
len = wifly.readBytesUntil('\n', buf, max_len);
buf[len] = '\0';
*content_len = atoi(buf);
} else {
wifly.readBytesUntil('\n', buf, max_len);
}
}
// Read content.
len = wifly.readBytes(buf, max_len > *content_len ? *content_len : max_len);
buf[len] = '\0'; // insurance
while (wifly.available() > 0) {
wifly.read();
}
wifly.close();
}
void parseUrl(char *url, char *host, char **path) {
*path = url;
int slash = 0, hoststart = 0, hostlen = 0;
while (**path != '\0') {
if (slash == 2) {
if (hostlen < 255) {
hostlen++;
}
} else {
hoststart++;
}
if (**path == '/') {
slash++;
if (slash == 3) {
break;
}
}
(*path)++;
}
strncpy(host, &url[hoststart], hostlen);
host[hostlen > 0 ? hostlen - 1 : hostlen] = '\0';
}
void getHeader(char *host, char *path) {
wifly.print("GET ");
wifly.print(path);
wifly.println(" HTTP/1.0"); // paste your number here
wifly.print("Host: ");
wifly.println(host);
wifly.println("User-Agent: lifegraph/0.0.1");
wifly.println();
}
/* Connect the WiFly serial to the serial monitor. */
void terminal()
{
while (1) {
if (wifly.available() > 0) {
Serial.write(wifly.read());
}
if (Serial.available() > 0) {
wifly.write(Serial.read());
}
}
}
void debugWifiState () {
char buf[32];
Serial.print("MAC: ");
Serial.println(wifly.getMAC(buf, sizeof(buf)));
Serial.print("IP: ");
Serial.println(wifly.getIP(buf, sizeof(buf)));
Serial.print("Netmask: ");
Serial.println(wifly.getNetmask(buf, sizeof(buf)));
Serial.print("Gateway: ");
Serial.println(wifly.getGateway(buf, sizeof(buf)));
Serial.print("DeviceID: ");
Serial.println(wifly.getDeviceID(buf, sizeof(buf)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment