Arduino Uno+Ethernet Shield+DHT 11 project

Ok, so i need help, im trying my first project. I want to acces my readings of the sensor(temperature and humidity) from web. How do i setup this? The board is connected to my laptop via LAN. I tried to use Cosm web site but even the first exemple(https://cosm.com/docs/quickstart/arduino.html) doesnt work, the site doesnt get any feed…, i get an error in serial monitor

Initializing network";

Error getting IP address via DHCP, trying again…

I am an absolut beginner, im trying to do this for my girlfriend, shes got a project at school, so i kinda need baby steps, whats the simple solution for this project? The terms are like this: Read the values from the sensor and then view them from distance using the web.

The board is connected to my laptop via LAN.

Does that mean you are connected to the internet via wireless with your computer, and the Arduino is connected to your ethernet port on the laptop?

Yes thats the setup i will want do make, my girlfriend will present the project, she will go with the arduino board and the laptop at school, there she will connect with the laptop wireless at the school wifi. I am kinda lost how can i make this work...

Don’t know what OS you have on the laptop, but you need “internet connection sharing”. A Google search should help you set up the laptop for your OS.

I run windows 7. I found an exmple on the net: http://stackoverflow.com/questions/15851776/arduino-php-webpage.. but my problem is how do i set up my hardware and software so i can send a feed on COSM.com in the first place :)

http://windows.microsoft.com/en-us/windows-vista/using-ics-internet-connection-sharing

http://answers.microsoft.com/en-us/windows/forum/windows_7-networking/how-to-enable-internet-connection-sharing-on/5a3e34ec-f0af-4a02-a588-e9f091711095

Ok, so after I share the connection the feed from arduino to cosm should work?

cylix: Ok, so after I share the connection the feed from arduino to cosm should work?

I won't guarantee that, but you should be able to connect to the internet with the Arduino/shield. I don't use Cosm.

Ok, thank you !! What do you use? Whats the best solution for this project to work? Any ideea will help :)

I use my own stuff. I have 3 servers online all the time, and all have php/mysql extensions enabled on the web servers. However, that would be harder to demonstrate from a remote location.

Ok so i did the internet sharing on the wireless network, on the LAN adapter tcpip4 and tcpip6 both have obtain an IP automatically but still doesnt work. i get the followin error when i try the example from cosm.com (https://cosm.com/docs/quickstart/arduino.html)

Cosm Sensor Client Example

Initializing network Error getting IP address via DHCP, trying again...

What ip/subnet is shown on your laptop ethernet adapter?

Ok, so I gave it another try and now the feed works, the example works.with no sensor attached i get a random value.

Cosm Sensor Client Example

Initializing network Network initialized

Read sensor value 426.00 Uploading to Cosm PUT return code: 200

Reading data from Cosm GET return code: 200 Datastream is: { "id" : "sensor_reading", "current_value" : "426.00" } Sensor value is: 426.00

now the question is how to change the code of the DHT11 sensor so i can view the temp and humidity on the cosm website. Now i can see the values on the lcd screen attached to the arduino board.

I have used the following code and it works, i can see the grapichs and the temp and humidity level on the cosm portal but ive got a big delay between measurements… :astonished:

/**

  • Cosm Arduino sensor client example.

`` * This sketch demonstrates connecting an Arduino to Cosm (https://cosm.com),

  • using the new Arduino library to send and receive data.

/**
DHT11 temp and humidity sensor added to the COSM example code
**/

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

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

#define API_KEY “plzg_Fpj1mEzSlbE37LGEbq1OwGSAKxwVEk0bFpZdk13OD0g” // your Cosm API key
#define FEED_ID 127936 // your Cosm feed ID

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

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

unsigned long lastConnectionTime = 0; // last time we connected to Cosm
const unsigned long connectionInterval = 15000; // delay between connecting to Cosm in milliseconds

// Initialize the Cosm library

// Define the string for our datastream ID
char sensorId = “sensor_reading”;
char sensorId2 = “DHT11_humidity_sensor_reading”;
char sensorId3 = “DHT11_temperature_sensor_reading”;
CosmDatastream datastreams = {
CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
CosmDatastream(sensorId2, strlen(sensorId2), DATASTREAM_FLOAT),
CosmDatastream(sensorId3, strlen(sensorId3), DATASTREAM_FLOAT),
};

// Wrap the datastream into a feed
CosmFeed feed(FEED_ID, datastreams, 3 /* number of datastreams */);

EthernetClient client;
CosmClient cosmclient(client);

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

Serial.println(“Cosm Sensor Client Example”);
Serial.println("==========================");

Serial.println(“Initializing network”);
while (Ethernet.begin(mac) != 1) {
Serial.println(“Error getting IP address via DHCP, trying again…”);
delay(500);
}

Serial.println(“Network initialized”);
Serial.println();
}

void loop() {
// main program loop
if (millis() - lastConnectionTime > connectionInterval) {

//check DHT11 sensor is working OK
int chk = DHT11.read(DHT11PIN);
Serial.print("Read DHT11 sensor: ");
switch (chk)
{
case 0: Serial.println(“OK”); break;
case -1: Serial.println(“Checksum error”); break;
case -2: Serial.println(“Time out error”); break;
default: Serial.println(“Unknown error”); break;
}

sendData(); // send data to Cosm
getData(); // read the datastream back from Cosm
lastConnectionTime = millis(); // update connection time so we wait before connecting again
}
}

// send the supplied values to Cosm, printing some debug information as we go
void sendData() {
int sensorValue = analogRead(sensorPin);
int humidityDHT11 = ((float)DHT11.humidity);
int tempDHT11 = ((float)DHT11.temperature);
datastreams[0].setFloat(sensorValue);
datastreams[1].setFloat(humidityDHT11); //DHT11 humidity value*******
datastreams[2].setFloat(tempDHT11); //DHT11 temp value********
Serial.print("Read sensor value ");
Serial.println(datastreams[0].getFloat());
Serial.print("Read DHT11 humidity sensor value ");
Serial.println(datastreams[1].getFloat());
Serial.print("Read DHT11 temperature sensor value ");
Serial.println(datastreams[2].getFloat());

Serial.println(“Uploading to Cosm”);
int ret = cosmclient.put(feed, API_KEY);
Serial.print("PUT return code: ");
Serial.println(ret);

Serial.println();
}

// get the value of the datastream from Cosm, printing out the value we received
void getData() {
Serial.println(“Reading data from Cosm”);

int ret = cosmclient.get(feed, API_KEY);
Serial.print("GET return code: ");
Serial.println(ret);

if (ret > 0) {
Serial.print("Datastream is: ");
Serial.println(feed[0]);
Serial.print("Sensor value is: ");
Serial.println(feed[0].getFloat());

Serial.print("Datastream is: ");
Serial.println(feed[1]);
Serial.print("Sensor value is: ");
Serial.println(feed[1].getFloat());
/*
Serial.print("Datastream is: ");
Serial.println(feed[2]);
Serial.print("Sensor value is: ");
Serial.println(feed[2].getFloat());
*/
}

Serial.println();
}

I have used the following code and it works, i can see the grapichs and the temp and humidity level on the cosm portal but ive got a big delay between measurements..

Big delay? Like 15 to 20 seconds? Or a couple hours?

edit: Is that big delay between measurements? Or from the time the measurement is sent to the time it is displayed on the webpage?

Between Reading data from Cosm and the GET Return code...like a minute or so..its because the internet connection?

Reading data from Cosm GET return code: 200 Datastream is: { "id" : "sensor_reading", "current_value" : "236.00" } Sensor value is: 236.00 Datastream is: { "id" : "DHT11_humidity_sensor_reading", "current_value" : "89.00" } Sensor value is: 89.00

That would be a delay out of your control. It is the time it takes to establish the connection, verify your account stuff, insert/retrieve the data into the database, then send a response.

I thought so, anyway the minimum refresh on the cosm graphs is 5 minute…a little big but its ok.

Many thanks for your help SurferTim, you where of great help!!! :slight_smile:

Hello again, im back, with a problem =( :roll_eyes:…I started the project again yesterday, to my amaze it didnt work anymore, because in the meantime cosm.com redefined as xively.com so they updated the libraries, so i started to modify my sketch for the new xively site. Ok after all that the feeds and graphs were working fine. everything was working last night. today i started the arduino again it was working fine again, feeds were working. i stopped the board, unplugged the wires. Now i started it again and its not working anymore…im getting the first error again out of sudden :(((.

Error getting IP address via DHCP, trying again…

im going nuts here, i dont know what changed of a sudden…

This is the code and the network settings.
Can anyone help?

/* Cosm Arduino sensor client example.
 *
`` * This sketch demonstrates connecting an Arduino to Cosm (https://cosm.com),
 * using the new Arduino library to send and receive data.

/**
DHT11 temp and humidity sensor added to the COSM example code
**/

#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Xively.h>
#include <dht11.h>

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


#define API_KEY "oJ5SwctwmO8GNd4HJDch8iyiQyojNRBnUqQLdb79mMuRcVQL" // your Cosm API key
#define FEED_ID 459662990 // your Cosm feed ID

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

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

unsigned long lastConnectionTime = 0;                // last time we connected to Cosm
const unsigned long connectionInterval = 15000;      // delay between connecting to Cosm in milliseconds

// Initialize the Cosm library

// Define the string for our datastream ID
char sensorId[] = "sensor_reading";
char sensorId2[] = "DHT11_humidity_sensor_reading";
char sensorId3[] = "DHT11_temperature_sensor_reading";
XivelyDatastream datastreams[] = {
  XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
  XivelyDatastream(sensorId2, strlen(sensorId2), DATASTREAM_FLOAT),
  XivelyDatastream(sensorId3, strlen(sensorId3), DATASTREAM_FLOAT),
};

// Wrap the datastream into a feed
XivelyFeed feed(FEED_ID, datastreams, 3 /* number of datastreams */);

EthernetClient client;
XivelyClient xivelyclient(client);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  Serial.println("Cosm Sensor Client Example");
  Serial.println("==========================");

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

  Serial.println("Network initialized");
  Serial.println();
}

void loop() {
  // main program loop
  if (millis() - lastConnectionTime > connectionInterval) {

//check DHT11 sensor is working OK    
    int chk = DHT11.read(DHT11PIN);
    Serial.print("Read DHT11 sensor: ");
  switch (chk)
  {
    case 0: Serial.println("OK"); break;
    case -1: Serial.println("Checksum error"); break;
    case -2: Serial.println("Time out error"); break;
    default: Serial.println("Unknown error"); break;
  }

    sendData(); // send data to Cosm
    getData(); // read the datastream back from Cosm
    lastConnectionTime = millis(); // update connection time so we wait before connecting again
  }
}

// send the supplied values to Cosm, printing some debug information as we go
void sendData() {
  int sensorValue = analogRead(sensorPin);
  int humidityDHT11 = ((float)DHT11.humidity);
  int tempDHT11 = ((float)DHT11.temperature);
  datastreams[0].setFloat(sensorValue);
  datastreams[1].setFloat(humidityDHT11); //DHT11 humidity value*******
  datastreams[2].setFloat(tempDHT11); //DHT11 temp value********
  Serial.print("Read sensor value ");
  Serial.println(datastreams[0].getFloat());
  Serial.print("Read DHT11 humidity sensor value ");
  Serial.println(datastreams[1].getFloat());
  Serial.print("Read DHT11 temperature sensor value ");
  Serial.println(datastreams[2].getFloat());

  Serial.println("Uploading to Xively");
  int ret = xivelyclient.put(feed, API_KEY);
  Serial.print("xivelyclient.put returned");
  Serial.println(ret);

  Serial.println();
}

// get the value of the datastream from Cosm, printing out the value we received
void getData() {
  Serial.println("Reading data from Xively");

  int ret = xivelyclient.get(feed, API_KEY);
  Serial.print("xivelyclient.get returned ");
  Serial.println(ret);

  if (ret > 0) {
    Serial.print("Datastream is: ");
    Serial.println(feed[0]);
    Serial.print("Sensor value is: ");
    Serial.println(feed[0].getFloat());

    Serial.print("Datastream is: ");
    Serial.println(feed[1]);
    Serial.print("Sensor value is: ");
    Serial.println(feed[1].getFloat());

    Serial.print("Datastream is: ");
    Serial.println(feed[2]);
    Serial.print("Sensor value is: ");
    Serial.println(feed[2].getFloat());
    
   }

  Serial.println();
}

It looks like your ethernet port on the laptop did not start correctly. It is showing a 169.254.46.153 and 255.255.0.0.

Are you still using Internet Connection Sharing with the Arduino connected to the laptop ethernet port? Is that service still running on the laptop?