Page 1 of 1

Cosm online temperature, humidity reading

Posted: Mon Oct 22, 2012 9:41 pm
by nitram
I have set up a Cosm feed that reads temperature, humidity and light readings.
All seems to be ok apart from the humidity reading which does not display any numbers after the decimal point.
This is the code I have been using

Code: Select all

  Pachube sensor client
 This sketch connects an analog sensor to Pachube (
 using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
 the Adafruit Ethernet shield, either one will work, as long as it's got
 a Wiznet Ethernet module on board.
 This example has been updated to use version 2.0 of the API. 
 To make it work, create a feed with a datastream, and give it the ID
 sensor1. Or change the code below to match your feed.
 * Analog 0 has LDR input. 5V -> 10kR -> (A0) -> LDR -> Ground
 * DHT22 attached on pin 3
 * Etherten: Equivalent to Ethernet shield attached to pins 10, 11, 12, 13
 created 15 March 2010
 updated 16 Mar 2012
 by Tom Igoe with input from Usman Haque and Joe Saavedra
 This code is in the public domain.

#include <SPI.h>
#include <Ethernet.h>
#include <DHT22.h>
//#include <DHT.h>

#define APIKEY         "XXXXXXXXXXXXXX" // replace your pachube api key here
#define FEEDID         80365 // replace your feed ID
#define USERAGENT      "Conditions at Caboolture" // user agent is the project name
#define DHT22_PIN 3

// assign a MAC address for the ethernet controller.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// fill in your address here:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(10,0,1,20);
// initialize the library instance:
EthernetClient client;

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(64,94,18,122);      // numeric IP for
//IPAddress server(216,52,233,122);      // numeric IP for
//char server[] = "";   // name address for pachube API

unsigned 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 unsigned long postingInterval = 60000;   //milliseconds delay between updates to

// Setup a DHT22 instance
DHT22 myDHT22(DHT22_PIN);

void setup() {
  // start serial port:
  Serial.println("Cosm 3-sensor test");
 // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // DHCP failed, so use a fixed IP address:
    Ethernet.begin(mac, ip);

void loop()
  // This is where HTML is returned from the server and errors are reported.
  // 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 =;

  // 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)) {
    // read sensors
    int light = analogRead(A0);   
    int humidity = 0; //myDHT22.getHumidityInt()/10;
    int temperature = 0; //myDHT22.getTemperatureCInt()/10;
    if (getEnviron(temperature, humidity)) sendData(light, humidity, temperature);
    else Serial.println("Digital sensor read failed");
  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();

boolean getEnviron(int& temp, int& rh)
  DHT22_ERROR_t errorCode;
  //Serial.print("Requesting data...");
  errorCode = myDHT22.readData();
    case DHT_ERROR_NONE:
      Serial.println("   ***   getEnviron() got data ...");
      //Serial.print("C ");
      // Alternately, with integer formatting which is clumsier but more compact to store and
   // can be compared reliably for equality:
      char buf[128];
      sprintf(buf, "Integer-only reading: Temperature %hi.%01hi C, Humidity %i.%01i %%RH",
                   myDHT22.getTemperatureCInt()/10, abs(myDHT22.getTemperatureCInt()%10),
                   myDHT22.getHumidityInt()/10, myDHT22.getHumidityInt()%10);
      rh = myDHT22.getHumidityInt();
      temp = myDHT22.getTemperatureCInt();
      return true;
      Serial.print("check sum error ");
      Serial.print("C ");
      Serial.println("% ");
    case DHT_BUS_HUNG:
      Serial.println("BUS Hung ");
      Serial.println("Not Present ");
      Serial.println("ACK time out ");
      Serial.println("Sync Timeout ");
      Serial.println("Data Timeout ");
      Serial.println("Polled to quick ");
  return false;

// this method makes a HTTP connection to the server:
void sendData(int light1, int rh1, int temp1)
//  Serial.println("*** ENVIRON");
//  Serial.println(light1);
//  Serial.println(temp1);
//  Serial.println(rh1);
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("   ***   sendData() connecting...");
    // send the HTTP PUT request:
    client.print("PUT /v2/feeds/");
    client.println(".csv HTTP/1.1");
    client.print("X-PachubeApiKey: ");
    client.print("User-Agent: ");
    client.print("Content-Length: ");

    char data[128];
    sprintf(data, "light1,%hi\ntemp1,%hi.%01hi\nrh1,%i.%01i", light1, temp1/10, temp1%10, rh1/10, rh1%10);
    Serial.println("   ***   sendData() data...");
    int thisLength = -1;
    char* length = data;
    while(*(length++)) thisLength++;
    // 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:
    // if you couldn't make a connection:
    Serial.println("connection failed");
   // note the time that the connection was made or attempted:
  lastConnectionTime = millis();

// This method calculates the number of digits in the
// sensor reading.  Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:

int getLength(int someValue)
  // there's at least one byte:
  int digits = 1;
  // continually divide the value by ten, 
  // adding one to the digit count for each
  // time you divide, until you're at 0:
  int dividend = someValue /10;
  while (dividend > 0) {
    dividend = dividend /10;
  // return the number of digits:
  return digits;

Any ideas anyone??
Regards :?

Re: Cosm online temperature, humidity reading

Posted: Mon Oct 22, 2012 10:13 pm
by ngp99
Have a look at this and the code here ... ation.html
This was posted by Tronixstuff on my thread below. I thought this was the holy grail but I have despaired of adapting it to the Freetronics server sketch. However, 78283 is probably doing the same thing as you and using the exact same gear.
It works, so all should be revealed.

Re: Cosm online temperature, humidity reading

Posted: Thu Oct 25, 2012 12:42 am
by nitram
Have changed line no 196 to:
    Serial.println(" *** sendData() data...");

    int thisLength = -1;
    char* length = data;
    while(*(length++)) thisLength++;
    client.println(thisLength + 1); //Changed thisLength here to +1

    // last pieces of the HTTP PUT request:
    client.println("Content-Type: text/csv");
    client.println("Connection: close");
This seems to have fixed the humidity reading to show a number after the decimal point.
Perhaps someone with more programming knowledge than I can explain why I needed to do this because I am not too sure that what I have done is the correct thing.