Sensor Data Online

For this week, since I already used a sensor, I wanted to transfer the data online in order to track the data for the air quality headphones that we are building. The goal is to be able to map where the air quality is the best through your smartphone. Below you can see a UX chart of the product outcome as well as a preliminary technical diagram.

UXdiagram

Signal-Chain

I used the Arduino Yun to get the data online, which was easy as I just needed to transfer it to the other board. What took time was getting the Yun connected to the internet (it’s a little janky) and tweaking the code for it to get the values online via Sparkfun.  Now you can see the values in my stream.

#include <Bridge.h>
#include <HttpClient.h>
#include “passwords.h”

// set up net client info:
const unsigned long postingInterval = 10; //delay between updates to sparkfun and xively.com
unsigned long lastRequest = 0; // when you last made a request
String dataString = “”;

int dustPin = 0;
int dustVal = 0;
int redVal = 0;
int blueVal = 0;
int greenVal = 0;

#define RED 3
#define GREEN 5
#define BLUE 6

int ledPower = 2;

int delayTime = 280;

int delayTime2 = 40;

float offTime = 9680;
void setup() {
// start serial port:
Bridge.begin();
Console.begin();

while (!Console); // wait for Network Serial to open
Console.println(“data.sparkfun client”);

// Do a first update immediately
updateData();
sendData();
lastRequest = millis();
Serial.begin(9600);

pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(RED, OUTPUT);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
digitalWrite(RED, HIGH);
pinMode(ledPower, OUTPUT);
pinMode(4, OUTPUT);
}

void loop() {

// get a timestamp so you can calculate reading and sending intervals:
long now = millis();

// if the sending interval has passed since your
// last connection, then connect again and send data:
if (now – lastRequest >= postingInterval) {
updateData();
sendData();
lastRequest = now;
}

digitalWrite(BLUE, 255); // turn the LED off by making the voltage LOW
digitalWrite(GREEN, 255); // turn the LED off by making the voltage LOW
digitalWrite(RED, 255); // turn the LED off by making the voltage LOW

// ledPower is any digital pin on the arduino connected to Pin 3 on the sensor

if (dustVal < 224) {
digitalWrite(BLUE, 0);
delay(1000); // wait for a second
}

if (dustVal > 225 && dustVal < 500) {
digitalWrite(GREEN, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}

if (dustVal > 501) {
digitalWrite(RED, 0);
delay(1000); // wait for a second
}

digitalWrite(ledPower, LOW); // power on the LED
delayMicroseconds(delayTime);
dustVal = analogRead(dustPin); // read the dust value via pin 5 on the sensor
delayMicroseconds(delayTime2);
digitalWrite(ledPower, HIGH); // turn the LED off
delayMicroseconds(offTime);

delay(150);

Serial.println(dustVal);

}

void updateData() {
// convert the readings to a String to send it:
dataString = “dustval=”;
dataString += dustVal;
// dataString += random(0-600);
}
// this method makes a HTTP connection to the server:
void sendData() {
// form the string for the API header parameter:
// form the string for the URL parameter:
String url = “http://data.sparkfun.com/input/”;
url += FEEDID;
url += “?private_key=”;
url += APIKEY;
url += “&”;
url += dataString;

// Send the HTTP GET request

HttpClient client;
//Process xively;
Console.print(“\n\nSending data… “);
Console.println(url);
client.get(url);
Console.println(“done!”);

// If there’s incoming data from the net connection,
// send it out the Console:
while (client.available()) {
char c = client.read();
Console.print(c);
}
Console.flush();

}

Leave a Reply

Your email address will not be published.