XILEVY sketch


I need help.

I try to feed my data to vively and i can’t.

Here my sketch. Can someone have a look to it and tell me what is wrong.

Interface to Shinyei Model PPD42NS Particle Sensor
Program by Christopher Nafis
Written April 2012


JST Pin 1 (Black Wire) => Arduino GND
JST Pin 3 (Red wire) => Arduino 5VDC
JST Pin 4 (Yellow wire) => Arduino Digital Pin 8

#include <SPI.h>
#include <Ethernet.h>
#include <stdlib.h>

byte mac = {
0x90, 0xA2, 0xDA, 0x0D, 0xA2, 0x94};
IPAddress ip(192,168,1,89);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255, 255, 255, 0);

// initialize the library instance:
EthernetClient client;

long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const int postingInterval = 10000; //delay between updates to Pachube.com

int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
char s[32];

void setup(){

// give the ethernet module time to boot up:
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
// Configure manually:
Ethernet.begin(mac, ip);
starttime = millis();

void loop(){
duration = pulseIn(pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;

if ((millis()-starttime) > sampletime_ms)
ratio = lowpulseoccupancy/(sampletime_ms10.0); // Integer percentage 0=>100
concentration = 1.1
lowpulseoccupancy = 0;
starttime = millis();

// if there’s incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();

// if there’s no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {

// if you’re not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
String dataString = “”;
dataString += dtostrf(concentration, 9, 4, s);
dataString += “,”;
dataString += dtostrf(ratio, 9, 4, s);
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();

// this method makes a HTTP connection to the server:
void sendData(String thisData) {
// if there’s a successful connection:
if (client.connect(“www.xively.com”, 80)) {
// send the HTTP PUT request.
// fill in your feed address here:
client.print(“Host: www.xively.com\n”);
// fill in your Pachube API key here:
client.print("Content-Length: ");
client.println(thisData.length(), DEC);

// last pieces of the HTTP PUT request:
client.print(“Content-Type: text/csv\n”);
client.println(“Connection: close\n”);

// here’s the actual content of the PUT request:

// note the time that the connection was made:
lastConnectionTime = millis();

else {
// if you couldn’t make a connection:
Serial.println(“connection failed”);


Can someone have a look to it and tell me what is wrong.

No. The code does something. You expect it to do something. You have not said what the code does. You have not said what you expect it to do.

You have not posted the code correctly, either.


Sorry. I will explain: i have a PPD42 dust sensor connected to an Arduini ethernet shield and he is reading the pis 8 for the data.

I want to feed the data to xilevy.

Also, can you explain me how to upload the sketch on the forum.



i have a PPD42 dust sensor connected to an Arduini ethernet shield and he is reading the pis 8 for the data.

Does that actually happen?

I want to feed the data to xilevy.

You don't need our permission.

The code does something. What does it do? What does it not do?

Also, can you explain me how to upload the sketch on the forum.

Frankly, I can see no good reason to repeat the information in the sticky that you were supposed to read before posting in the forum.

You may, or may not, have noticed that there are big orange pluses to the left of the forum descriptions, and that you can't start a new thread if you enter the forum that way.

If you are able to start a new thread, and you were, it is because you clicked on the actual forum name. That link took you to a page where the first threads are ones you are expected to read BEFORE you click the new topic link.

OK, first thing is that xively has an api that you should use - not cosm, not pachube. xively has “assimilated” (resistance is futile) cosm and pachube and standardized their interfaces and APIs so you should use those.

The concept is device->datastream->feed with appropriate functions to put and get data on a feed, datastream or device basis.

There are some decent but still primitive graphing functions available.

This is what works for me - (except the control part)

// *****************************************************************************
// *****************************************************************************
// ** 1-wire + DHT + SPI + Analog demo code for CATkit and Kitten long distance
// ** connectivity over CAT5 cabling (SmartGreenHouse.org)
// ** --------------------------------------------------------------------------
// ** Coding by: Don Gibson info@SmartGreenHouse.org
// ** Created: April 17 2013
// ** Version: 1.5
// ** Last update: added DHT sensor 2013/04/19
// ** Last update: added xively feed 2013/07/03
// ** Modified by: DG
// ** This code is public domain, but please always include the original header
// ** in any subsequent copies
// *****************************************************************************
// uses Arduino MEGA
// uses DS18B20 sensors (2)
// uses DHT11 humidity sensor (1)
// uses 2 channel relay board (1)
// uses 20x4 SPI display
// uses CATkit (1)
// uses Kitten boards (3)
// uses 1m, 5m, 20m CAT5 cables for long distance sensor connections
// *****************************************************************************

#include <OneWire.h>
#include <DallasTemperature.h>
#include “Wire.h”
#include “LiquidCrystal.h”
#include “DHT.h”
#include <SPI.h>
#include <HttpClient.h>
#include <Ethernet.h>
#include <Xively.h>

// ***************************************************************

#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect LCD via SPI protocol. Data pin is #1, Clock is #2 and Latch is #0
LiquidCrystal lcd(1,2,0); //D1, D2, D0

#define ONE_WIRE_BUS_A 7 // D7 for sensor string A (1-wire sensors can be daisy-chained)
#define ONE_WIRE_BUS_B 9 // D9 for sensor string B

#define redLED A0 // A0 to show xively update cycles
#define DHTPIN A1 // pin the DHT is connected to
#define blueLED A2 // A2 to test web i/o control
#define SSMSensor A3
#define lightSensor A5

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWireA(ONE_WIRE_BUS_A);
OneWire oneWireB(ONE_WIRE_BUS_B);

// Pass our oneWire references to Dallas Temperature. There are two channels of 1-Wire devices.
DallasTemperature sensorsA(&oneWireA);
DallasTemperature sensorsB(&oneWireB);

// instance the DHT11 sensor object

// variables

static int persistentData;
int LEDstatus = 0;
float blueLEDStatus = 0;
int maxSensorsA = 9;
int maxSensorsB = 9;
float temperatureA[9]; // assumes maximum 10 sensors on chain A
float temperatureB[9]; // assumes maximum 10 sensors on chain B

// ***************************************************************

byte mac = {0x90, 0xA2, 0xDA, 0x00, 0x10, 0x8f};

char xivelyKey = // “your xively API key here” //;
long xivelyFeed = // “your feed ID here” //;

char light1ID = “Light-1”;
char ssm1ID = “SSM-A”;
char humidity1ID = “Humidity”;
char temperature1ID = “InternalTemp”;
char blueLEDID = “Irrigation_Pump_A”;

XivelyDatastream datastreams = {
XivelyDatastream(light1ID, strlen(light1ID), DATASTREAM_FLOAT),
XivelyDatastream(ssm1ID, strlen(ssm1ID), DATASTREAM_FLOAT),
XivelyDatastream(humidity1ID, strlen(humidity1ID), DATASTREAM_FLOAT),
XivelyDatastream(temperature1ID, strlen(temperature1ID), DATASTREAM_FLOAT),
XivelyDatastream(blueLEDID, strlen(blueLEDID), DATASTREAM_FLOAT)

XivelyFeed feed(xivelyFeed, datastreams, 5);
EthernetClient client;

int sensorIndexA; //to specify which sensors in 1-wire chain A are addressed
int sensorIndexB; //to specify which sensors in 1-wire chain B are addressed
float dhtCorrection = -0.5; // DHT correction for temperature by experiment/calibration

void setup()
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
// no point in carrying on, so exit after message
} // end ethernet setup
//XivelyClient xivelyclient(client);
analogWrite(blueLED, 0);


void loop()
XivelyClient xivelyclient(client);
// problem here - the control code does not seem to return a parsed value.
// Raw JSON returned contains data then the value in xively resets to something else

int getReturn = xivelyclient.get(feed, xivelyKey);
if (getReturn > 0) {
Serial.println (“Blue LED :”);
Serial.println (feed[4]); // always returns ‘0’ JSON shows other values
} else {
Serial.println ("HTTP error ");

// update LED status from Xively - for debugging only
Serial.print("LED status before call: ");
int LEDstatus = feed[4].getFloat(); // expecting a value to adjust brightness here - debug only

analogWrite(blueLED, LEDstatus);
Serial.print("LED status ------ after call: ");

// ****************************************************************************************
// report sensor values to xively

// DHT temperature & humidity sensor
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
float DHTh = dht.readHumidity();
float DHTt = dht.readTemperature() + dhtCorrection;

blueLEDStatus = analogRead(blueLED);

// read light sensor
int lightPercentA = map(analogRead(lightSensor), 10, 1023, 0, 100);


// read stainless soil moisture sensor
int SSMPercent = map(analogRead(SSMSensor), 20, 1023, 100, 0);

// other sensor datastreams removed…

// uploading to Xively - put the entire feed at one time
int ret = xivelyclient.put(feed, xivelyKey);

// blink an LED to show cycles - yeah I know I could use digitalWrite … blah blah
analogWrite(redLED, 255);
delay(1500); // delay between calls to Xively
analogWrite(redLED, 0);