Tweet tweet

With the help of Will Skates writing a php script for me to send my values from the Arduino to, and send them on to twitter, it’s finally sending the sensor readings to twitter and flashing the lights.

I had to set this one up as a client to Will’s page instead of a server as before, but it’s all good.

// (Based on Ethernet's WebClient Example)

#include "WiFly.h"


#include "Credentials.h"


byte server[] = { 66, 249, 89, 104 }; // Google
int canConnect = 0;

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;

//Client client(server, 80);

Client client(server, 80);

void setup() {
  
  Serial.begin(9600);

  WiFly.begin();
  
  Serial.println("associating");
  if (!WiFly.join(ssid, passphrase)) {
    Serial.println("Association failed.");
    while (1) {
      // Hang on failure.
    }
  }else{
    Serial.println("Association succeeded.");
  }  

  Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    canConnect = 1;
    client.println("GET /new?msg=openConn&pwd=(password) HTTP/1.1");
    client.println();
  } else {
    Serial.println("connection failed");
  }
  
}

void loop() {
  
  Serial.println("attempting request");
  
  
  delay(5000);
  
  //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.stop();
  if (client.connect()) {
    String req = "GET /new?msg=";
    req += sensorValue;
    req += "&pwd=(password)";
    client.println(req);
    client.println();
  } else {
    Serial.println("connection failed");
  }

}

You can see the tweets here: https://twitter.com/decibear
Although now, being half 2am, I think it’s time for bed, which means no visualization, but we have a working blogject!

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s