Pages: [1]   Go Down
Author Topic: optical dust sensor data to xively  (Read 1823 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
Karma: 0
Posts: 81
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Recently I bought myself an optical dust sensor and I hooked it up with my Arduino and then I tested the code. It was satisfactory after consulting it's wiki page:
Here is the link:
Code is there in the wiki page to read the data in serial monitor.

Now I wanted  to upload the data to xively and so I used xively's Arduino Library. (

Looking at one of the "data-stream upload example", I modified my code in this way:

//for OLED********
#include <Wire.h>
#include <SeeedOLED.h>
#include <avr/pgmspace.h>
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Xively.h>

//Assigning parameters for the dust sensor//same as the previous code
int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;//sampe 30s ;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
int concentration = 0;

//Xively parameters**************

// MAC address for your Ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

char xivelyKey[] = "neZYOTXMfiqD9OJD3sYiasJdmX1X5DxzyzH2IuHmhk4SLFAH"; //this is my xively API Key

char sensorId[] = "Particle_Conc:";
XivelyDatastream datastreams[] = {
 XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),

// Finally, wrap the datastreams into a feed
XivelyFeed feed(1803940077, datastreams, 1); // my FEED ID is 1803940077 and single datastream

EthernetClient client;
XivelyClient xivelyclient(client);

void setup()
  Serial.println("Starting single datastream upload to Xively...");
  while (Ethernet.begin(mac) != 1)
    Serial.println("Error getting IP address via DHCP, trying again...");
// OLED stuff****************************
  //pinMode(CS_pin, OUTPUT);
  SeeedOled.init();  //initialze SEEED OLED display
  PORTB |= 0x21;
  SeeedOled.clearDisplay();          //clear the screen and set start position to top left corner
  SeeedOled.setNormalDisplay();      //Set display to normal mode (i.e non-inverse mode)
  SeeedOled.drawBitmap(url,1024);   // 1024 = 128 Pixels * 64 Pixels / 8  //Set addressing mode to Page Mode
  SeeedOled.setTextXY(4, 10);          //Set the cursor to Xth Page, Yth Column  
  SeeedOled.putString("Hello"); //Print the String
  SeeedOled.setTextXY(6, 10);          //Set the cursor to Xth Page, Yth Column  
  SeeedOled.putString("Folks"); //Print the String
  SeeedOled.setTextXY(2, 4);
  SeeedOled.setTextXY(4, 4);
  SeeedOled.setTextXY(3, 0);
 // SeeedOled.setHorizontalScrollProperties(Scroll_Right,3,0,Scroll_5Frames); //Set Scrolling properties to Scroll Right
 // SeeedOled.deactivateScroll();             // Activate Scrolling
//OLED stuff finished**********************

// Dust sensor parameters*****************
  starttime = millis();//get the current time;

void loop()
//Dust sensor calculation thing********************

  duration = pulseIn(pin, LOW);
  lowpulseoccupancy = lowpulseoccupancy+duration;
  if ((millis()-starttime) > sampletime_ms)//if the sampel time == 30s
    ratio = lowpulseoccupancy/(sampletime_ms*10.0);  // Integer percentage 0=>100
    concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
    Serial.print("Lowpulseoccupancy: ");
    Serial.println("Ratio: ");
    Serial.println("Conc.: ");
    lowpulseoccupancy = 0;
    starttime = millis();
//Xivly upload stuff happening here
    int ret = xivelyclient.put(feed, xivelyKey);
 //   problem is here some where

    SeeedOled.setTextXY(0, 0);
    SeeedOled.putString("Dust Conc.");
    SeeedOled.setTextXY(2, 0);
    SeeedOled.putString("in pcs/283ml:");
    SeeedOled.setTextXY(4, 1);

Now It's uploading data as expected after it's 30s sampling time, but the data's do not match. It's almost much lesser than the data I get using the previous code(Not implementing  xively alterations).

I'm confused if it has to do with delay or something?
It would be kind if any body out there can scan the code and tell me what's going wrong.

Offline Offline
Edison Member
Karma: 7
Posts: 1545
Another year! Alone! :(
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Obviously when you added a cushion of around 30seconds in sampling it will run in 30seconds intervals that too with a delay(500) in there

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

Pages: [1]   Go Up
Jump to: