Go Down

Topic: Standalone Datacollecting Webserver (Read 8 times) previous topic - next topic


Mar 09, 2013, 01:41 pm Last Edit: Mar 09, 2013, 03:05 pm by ArduinoGreenHouse Reason: 1
Im using the wifi shield at the moment, using the arduino wifi pachube example added an extra sensor
Code: [Select]
Add two reading lines at the start of loop(), ie

int sensorReading = analogRead(A0);
int sensorReading2 = analogRead(A1);

Pass the 2nd reading into the 'sendData' method at the end of loop()

sendData(sensorReading, sensorReading2);

Then change the sendData method to accept two values and package those up for sending also

// this method makes a HTTP connection to the server:
void sendData(int thisData, int thisData2) {
// if there's a successful connection:
if (client.connect(server, 80)) {
// send the HTTP PUT request:
client.print("PUT /v2/feeds/");
client.println(".csv HTTP/1.1");
client.println("Host: api.cosm.com");
client.print("X-ApiKey: ");
client.print("User-Agent: ");
client.print("Content-Length: ");

// calculate the length of the sensor reading in bytes:
// 8 bytes for "sensor1," + number of digits of the data:
// plus the same again and length of data 2
int thisLength = 8 + getLength(thisData) + 8 + getLength(thisData2);

// last pieces of the HTTP PUT request:
client.println("Content-Type: text/csv");
client.println("Connection: close");

// here's the actual content of the PUT request:
// and the 2nd sensor

else {
// if you couldn't make a connection:
Serial.println("connection failed");
// note the time that the connection was made or attempted:
lastConnectionTime = millis();

this code i found on the cosm forum

this is the message I get
and i get no values on my feed

CSV Parser Error: CSV is invalid. Incorrect number of fields
HTTP/1.1 400 Bad Request
Date: Sat, 09 Mar 2013 14:00:22 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 60
Connection: close
X-Request-Id: 94849a7b1e96abaa62c7145ff139221428ceddcb


I hesitate to say this because you are using WiFi and there may be something going on I don't understand. But.

I guess cosm doesn't know or care how you send the data, so long as the commands are right.


I see the word useragent

And you don't get a connection. So it would appear you  need to use the latest com libraries.  The code is incomplete, so the libraries you are using is unclear.


ahh ok cool thanks will look into that, I also have the Ethernet shield if you have any more advice or solutions/ code for that it would be great!


Mar 10, 2013, 01:01 am Last Edit: Mar 10, 2013, 06:03 am by Nick_Pyner Reason: 1
OK, now that you haven't summarily dismissed my comment, my guess is that it is definitely your problem and explains why your code looks like incomprehensible junk.

I get the impression that cosm underwent a palace revolution about last September and, unless you are one of the gold star brigade, anything you pick up on this matter that is older than that, is just going to cause you grief.

Check the cosm site, get the kosher libraries, cosm.h and httpclient, and you will probably be fine. You will see examples in the "It Worked!" section of the cosm forum, including one of mine, and there is stuff included with the libraries.

I do hope your ethernet shield is WIZ5100 based........


Mar 10, 2013, 12:14 pm Last Edit: Mar 10, 2013, 12:16 pm by ArduinoGreenHouse Reason: 1
Hi again thanks I got the wifi shield working and reads and graphs the sensors thank you for that needed the libraries

I do hope your ethernet shield is WIZ5100 based........

Yes it is!
Now I am trying out the ethernet  shield but this doesnt connect its fairly similar code to the wifi
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>

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

// Your Cosm key to let you upload data
char cosmKey[] = "YOUR_COSM_API_KEY";

// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = 2;

// Define the strings for our datastream IDs
char sensorId[] = "sensor_reading";
CosmDatastream datastreams[] = {
 CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
// Finally, wrap the datastreams into a feed
CosmFeed feed(15552, datastreams, 1 /* number of datastreams */);

EthernetClient client;
CosmClient cosmclient(client);

void setup() {
 // put your setup code here, to run once:
 Serial.println("Starting single datastream upload to Cosm...");

 while (Ethernet.begin(mac) != 1)
   Serial.println("Error getting IP address via DHCP, trying again...");

void loop() {
 int sensorValue = analogRead(sensorPin);

 Serial.print("Read sensor value ");

 Serial.println("Uploading it to Cosm");
 int ret = cosmclient.put(feed, cosmKey);
 Serial.print("cosmclient.put returned ");


obviously i add all the information that is needed to be added
In the serial display it now showing Error getting IP address via DHCP, trying again...
any solution to this would be great

Go Up