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.

https://cosm.com/feeds/118422

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 DHT11;
#define DHT11PIN 3//pin DHT11 sensor is connected to
//DHT11*********************************************************************

//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:
  Serial.begin(9600);
 
 //one wire******************************************************************
 sensors.begin();
 //one wire******************************************************************
  
  
  Serial.println("Starting multiple datastream upload to Cosm...");
  Serial.println();

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

void loop() {
  int sensorValue1 = analogRead(sensorPinLdr1);
  
//DHT11*********************************************************************
  int chk = DHT11.read(DHT11PIN);
  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);
//DHT11*********************************************************************  
  
//one wire******************************************************************
  sensors.requestTemperatures(); // Send the command to get temperatures
//one wire******************************************************************
  
  
  datastreams[0].setFloat(sensorValue1);
  Serial.print("Read LDR sensor value - ");
  Serial.println(datastreams[0].getFloat());

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

  Serial.println();
  delay(5000);
}

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.