Code update so far

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); 

  if (!WiFly.join(ssid, passphrase)) {
    while (1) {
      // Hang on failure.

  Serial.print("IP: ");

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 =;
        // 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");
          //output the sensor values
          client.print("sensor value: ");
          //output the accumulative values
          client.print("You have had ");
          client.print(" over ");
          client.print(" minutes.");
          client.println("<br />");
          //output the average values
          client.print("This is an average of ");
          client.print(" per minute");
          //warn if over final level
          if (ledLit == 3){
           client.print("This is over your recommended allowance by ");
           client.print(aveSound - warningLevels[2]);
          } else {
           client.print("This is under your recommended allowance by ");
           client.print(warningLevels[2] - aveSound);
        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

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!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s