Skip to content

Instantly share code, notes, and snippets.

Created June 30, 2012 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3025646 to your computer and use it in GitHub Desktop.
Save anonymous/3025646 to your computer and use it in GitHub Desktop.
/*
* Web Server
*
* (Based on Ethernet's WebServer Example)
*
* A simple web server that shows the value of the analog input pins.
*/
#include <SPI.h>
#include <WiFly.h>
#include <Wire.h>
#include <SHT2x.h>
#include "Credentials.h"
WiFlyServer server(80);
void setup() {
Wire.begin();
WiFly.begin();
if (!WiFly.join(ssid, passphrase)) {
while (1) {
// Hang on failure.
}
}
Serial.begin(9600);
Serial.print("IP: ");
Serial.println(WiFly.ip());
server.begin();
}
String readString = String(100);
String teststring = String(100);
String finalstring = String(100);
String flag = String(2);
int ind1 = 0;
int ind2 = 0;
int pos = 0;
void loop() {
float temp = SHT2x.GetTemperature();
float rh = SHT2x.GetHumidity();
delay(100);
WiFlyClient client = server.available();
delay(100);
if (client) {
Serial.println("We have a client!");
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
if (readString.length() < 100) {
//store characters to string
readString.concat(c);
}
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if (c == '\n' && current_line_is_blank) {
if(readString.indexOf("callback=") >=0) { //test for servo control sring
//readString.replace('y', '#');
pos = readString.length(); //capture string length
//find start of servo command string (#)
ind1 = readString.indexOf("callback=")+9;
//capture front part of command string
teststring = readString.substring(ind1, pos);
//locate the end of the command string
ind2 = teststring.indexOf(' ');
//capturing the servo command string from readString
finalstring = readString.substring(ind1, ind2+ind1);
//print "finalstring" to com port;
Serial.println(finalstring); //print string with CR
}
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println();
client.print(finalstring);
client.print("('{");
client.print("\"temp\":");
client.print(temp);
client.print(",");
client.print("\"hum\":");
client.print(rh);
client.println("}')");
delay(100);
client.stop();
break;
}
if (c == '\n') {
// we're starting a new line
current_line_is_blank = true;
}
else if (c != '\r') {
// we've gotten a character on the current line
current_line_is_blank = false;
}
}
}
// give the web browser time to receive the data
delay(100);
client.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment