I’ve been working on the wifly stuff, and I’ve had some big issues, namely with the IP address, not being set from the router which is my phone. Finally fixed it now, so hopefully if I don’t change anything tonight on my phone or wiFly shield settings, it should work tomorrow!
This script still does the thing with the lights, but it also sends the information over the wiFly’s server, so I can read it on my phone while I’m out and about wearing the bear scarf!
The only real bug with this is that for some reason I can’t change the delay without messing up the client, so it’s currently reading every 10 ms rather than 30s, but I’m still working on that!
/*
* Web Server
*
* (Based on Ethernet's WebServer Example)
*
* A simple web server that shows the value of the analog input pins.
*/
#include "WiFly.h"
char passphrase[] = "pass";
char ssid[] = "ssid";
int ledPin[] = {3,4,5};
int warningLevels[] = {200, 230, 270};
//int warningLevels[] = {20, 50, 100};
int sensorPin = A0;
int sensorValue = 0;
int readCount = 0;
int accumSound = 0;
int aveSound = 0;
int ledLit = 0;
Server server(80);
void setup() {
for (int i=0; i<(sizeof(ledPin)/2); i++){
pinMode(ledPin[i], OUTPUT);
}
WiFly.begin();
if (!WiFly.join(ssid, passphrase)) {
while (1) {
// Hang on failure.
}
}
Serial.begin(9600);
Serial.print("IP: ");
Serial.println(WiFly.ip());
server.begin();
}
void loop() {
//read sensor
sensorValue = analogRead(sensorPin);
//reread if values are not between our thresholds
while(sensorValue == 0 || sensorValue > 350){
sensorValue = analogRead(sensorPin);
}
//add to readCount
readCount ++;
//add to accumulative sound
accumSound += sensorValue;
//work out average sound per minute
aveSound = accumSound/readCount;
//check against set levels
for (int i=0; i<(sizeof(warningLevels)/2); i++){
if (aveSound > warningLevels[i]){
ledLit = i+1;
}
}
//light LEDs
for (int i=0; i<ledLit; i++){
digitalWrite(ledPin[i], HIGH);
}
Client client = server.available();
if (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();
// 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) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
//output the sensor values
client.print("sensor value: ");
client.print(sensorValue);
client.print("<br/><br/>");
//output the accumulative values
client.print("You have had ");
client.print(accumSound);
client.print(" over ");
client.print(readCount);
client.print(" minutes.");
client.println("<br />");
//output the average values
client.print("This is an average of ");
client.print(aveSound);
client.print(" per minute");
client.println("<br/><br/>");
//warn if over final level
if (ledLit == 3){
client.print("This is over your recommended allowance by ");
client.print(aveSound - warningLevels[2]);
client.print("<br/>");
} else {
client.print("This is under your recommended allowance by ");
client.print(warningLevels[2] - aveSound);
client.print("<br/>");
}
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);
//delay(30000);
client.stop();
}
}
Now I’m at another stage where we could present if necessary, although I really would like to get it sending to twitter, or have some sort of visualization, or preferably both! I will keep working on it for a while, but I’ll need to get some sleep soonish as I barely slept last night, and tomorrow is the big day!