COSM -ethernet shield

Hi All

I’ve setup a feed to COSM to display an LDR reading, temperature and humidity (using an DHT11) and a digital temperature sensor using an Arduino Ethernet shield.

Works OK for about 3 to 5 hours when running on a 9v power supply then flat lines…

When plugging the USB cable back into the shield to check the serial monitor messages the feed starts to work again. I believe the cosm heater files are using DHCP but I don’t have much knowledge in this area.

Any suggestions as to what might be causing the loss of feed to COSM??

Many thanks

#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <dht11.h>

dht11 DHT11;
#define DHT11PIN 3//pin DHT11 sensor is connected to

//one wire******************************************************************
#define ONE_WIRE_BUS 2 // Data wire is plugged into port 2 on the Arduino
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature. 
//one wire******************************************************************

// MAC address for your Ethernet shield
  byte mac[] = {xxxx}; 

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

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

// Define the strings for our datastream IDs
char sensorId1[] = "LDR_sensor_reading";
char sensorId2[] = "DHT11_humidity_sensor_reading";
char sensorId3[] = "DS18B20_digital_temp_sensor_reading";
char sensorId4[] = "DHT11_temperature_sensor_reading";
const int bufferSize = 140;
char bufferValue[bufferSize]; // enough space to store the string we're going to send
CosmDatastream datastreams[] = {
  CosmDatastream(sensorId1, strlen(sensorId1), DATASTREAM_FLOAT),
  CosmDatastream(sensorId2, strlen(sensorId2), DATASTREAM_FLOAT),
  CosmDatastream(sensorId3, strlen(sensorId3), DATASTREAM_FLOAT),
  CosmDatastream(sensorId4, strlen(sensorId4), DATASTREAM_FLOAT),
// Finally, wrap the datastreams into a feed
CosmFeed feed(118422, datastreams, 4 /* number of datastreams */);

EthernetClient client;
CosmClient cosmclient(client);

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

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

void loop() {
  int sensorValue1 = analogRead(sensorPinLdr1);
  int chk =;
  switch (chk)
    case 0: Serial.println("DHT11 OK"); break;
    case -1: Serial.println("Checksum error"); break;
    case -2: Serial.println("Time out error"); break;
    default: Serial.println("Unknown error"); break;
  int humidityDHT11 = ((float)DHT11.humidity);
  int tempDHT11 = ((float)DHT11.temperature);
//one wire******************************************************************
  sensors.requestTemperatures(); // Send the command to get temperatures
//one wire******************************************************************
  Serial.print("Read LDR sensor value - ");

  datastreams[1].setFloat(humidityDHT11); //DHT11 humidity value*******
  Serial.print("Read DHT11 humidity sensor value - ");
  datastreams[2].setFloat(sensors.getTempCByIndex(0)); //one wire value********
  Serial.print("Read Digital temp sensor value - ");
  datastreams[3].setFloat(tempDHT11); //DHT11 temp value********
  Serial.print("Read DHT11 temperature sensor value - ");
  Serial.println("Uploading it to Cosm");
  int ret = cosmclient.put(feed, cosmKey);
  Serial.print("cosmclient.put returned ");


DHCP usually drops after ~24 hours but this is to early.

I see that you are setting up the Cosm feed on boot. If the connection is dropped (which it will eventually) will the libraries detect this and reconnect to Cosm?

I have not looked at these libraries for more than a year (before 1.0 which fixed ethernet etc.) but then you needed to create the connection, do your stuff and then disconnect in the main-loop. Doing it just once would always result in a dropped connection sooner or later.

The USB-cable, is it causing a reset of the board? Then that might explain why you get it working when the cable is connected.

My advice is to move everything into the main-loop that has to do with internet. Or maybee even rebooting the board every 15-minutes as this would solve some network problems as well.