Pages: [1]   Go Down
Author Topic: COSM + arduino ethernet  (Read 484 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Carissimi,
volevo sapere se avete problemi con i feed di cosm ?
io invio valori di temperatura e umidità da sensore DHT22 a cosm e tutto funziona bene finchè dopo un pò (variabile a volte 20 min. a volte 2 ore)...si frizza tutto !!!
non capisco ! vi allego il codice...
AIUTO !!!!

Code:
#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>

#define APIKEY         "xx"
#define FEEDID         ....
#define USERAGENT      "Cosm Arduino...name

#define DHTPIN         7      // Data pin for the HUMID module
#define DHTTYPE        DHT22  // Sensor used on the HUMID module

DHT dht(DHTPIN, DHTTYPE);

// 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};


///////Your next device could be 0xDE 0xAD 0xBE 0xEF 0xFE 0xEE, for example.
///////Then your third would end in 0xEF, the fourth in 0xF0, and so on.


// 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(173,203,98,29);    // numeric IP for api.cosm.com
//char server[] = "api.cosm.com";   // name address for cosm 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 = 30*1000; //delay between updates to Cosm.com
const unsigned long postingInterval = 60000; //delay between updates to Cosm.com

void setup() {
  // start serial port:
  Serial.begin(38400);
  // start the HUMID module:
  dht.begin();
  // 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() {
  
  
  // read the analog sensor:
  int sensorReading = analogRead(A0);  

  // 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 = client.read();
    Serial.print(c);
  }

  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
  
  if (millis() < lastConnectionTime) lastConnectionTime = millis();    // evita il blocco dopo 50gg poiché millis() si azzera

  // 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)) {
    float temperatureReading = dht.readTemperature();
    Serial.print("Temperature: ");
    Serial.println(temperatureReading);
    float humidityReading    = dht.readHumidity();
    Serial.print("Humidity: ");
    Serial.println(humidityReading);
    sendData(temperatureReading, humidityReading);
  }
  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();
}

// this method makes a HTTP connection to the server:
boolean sendData(float thisTemperature, float thisHumidity) {
  if (client.connected()) client.stop();

  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.print("PUT /v2/feeds/");
    client.print(FEEDID);
    client.println(".csv HTTP/1.1");
    client.println("Host: api.cosm.com");
    client.print("X-ApiKey: ");
    client.println(APIKEY);
    client.print("User-Agent: ");
    client.println(USERAGENT);
    client.print("Content-Length: ");

    /*
     work out the length of the string:
     "temperature," = 12 +
     digits of temperature value +
     "humidity," = 9 +
     digits of humidity value +
     "\r\n" = 2
     */
    int length = 12 + countDigits(thisTemperature,2) + 2 + 9 + countDigits(thisHumidity,2) + 2;

    // calculate the length of the sensor reading in bytes:
    client.println(length);

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

    // here's the actual content of the PUT request:
    client.print("temperature,");
    client.println(thisTemperature);
    client.print("humidity,");
    client.println(thisHumidity);
    client.println();
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
    Serial.println("data uploaded");

  }
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}

// Counts digits of a floating point number, to calculate content length
// for an HTTP call.
// Based on Arduino's internal printFloat() function.
int countDigits(double number, int digits)  {
  int n = 0;

  // Handle negative numbers
  if (number < 0.0)
  {
    n++; // "-";
    number = -number;
  }

  // Round correctly so that print(1.999, 2) prints as "2.00"
  double rounding = 0.5;
  for (uint8_t i=0; i<digits; ++i) {
    rounding /= 10.0;
  }
  number += rounding;

  // Extract the integer part of the number and print it
  unsigned long int_part = (unsigned long)number;
  double remainder = number - (double)int_part;

  while (int_part > 0) {
    int_part /= 10;
    n++;
  }
  // Print the decimal point, but only if there are digits beyond
  if (digits > 0) {
    n++; //".";
  }

  // Extract digits from the remainder one at a time
  while (digits-- > 0)
  {
    remainder *= 10.0;
    int toPrint = int(remainder);
    n ++; // += String(toPrint);
    remainder -= toPrint;
  }
  return n;
}
« Last Edit: January 04, 2013, 09:03:02 am by kove3374 » Logged

Cagliari, Italy
Offline Offline
Tesla Member
***
Karma: 103
Posts: 6572
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ciao,
potresti rieditare il tuo messaggio? dovresti includere il codice all'interno dei tag CODE
Premi modify (in alto a destra del messaggio). Seleziona tutto il codice e premi il tastino # nella barra dell'editor sopra le faccine.
Salva il mesaggio.
Dovresti otenere una cosa così:
Code:
codice
Molto più leggibile.

EDIT: Cosi com'è lo sketch non compila.
« Last Edit: January 04, 2013, 08:55:41 am by PaoloP » Logged

Code fast. Code easy. Codebender --> http://codebender.cc/?referrer=PaoloP

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Carissimi,
volevo sapere se avete problemi con i feed di cosm ?
io invio valori di temperatura e umidità da sensore DHT22 a cosm e tutto funziona bene finchè dopo un pò (variabile a volte 20 min. a volte 2 ore)...si frizza tutto !!!
non capisco ! vi allego il codice...
AIUTO !!!!

Code:
#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>

#define APIKEY         "xx"
#define FEEDID         ....
#define USERAGENT      "Cosm Arduino...name

#define DHTPIN         7      // Data pin for the HUMID module
#define DHTTYPE        DHT22  // Sensor used on the HUMID module

DHT dht(DHTPIN, DHTTYPE);

// 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};


///////Your next device could be 0xDE 0xAD 0xBE 0xEF 0xFE 0xEE, for example.
///////Then your third would end in 0xEF, the fourth in 0xF0, and so on.


// 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(173,203,98,29);    // numeric IP for api.cosm.com
//char server[] = "api.cosm.com";   // name address for cosm 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 = 30*1000; //delay between updates to Cosm.com
const unsigned long postingInterval = 60000; //delay between updates to Cosm.com

void setup() {
  // start serial port:
  Serial.begin(38400);
  // start the HUMID module:
  dht.begin();
  // 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() {
  
  
  // read the analog sensor:
  int sensorReading = analogRead(A0);  

  // 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 = client.read();
    Serial.print(c);
  }

  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
  
  if (millis() < lastConnectionTime) lastConnectionTime = millis();    // evita il blocco dopo 50gg poiché millis() si azzera

  // 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)) {
    float temperatureReading = dht.readTemperature();
    Serial.print("Temperature: ");
    Serial.println(temperatureReading);
    float humidityReading    = dht.readHumidity();
    Serial.print("Humidity: ");
    Serial.println(humidityReading);
    sendData(temperatureReading, humidityReading);
  }
  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();
}

// this method makes a HTTP connection to the server:
boolean sendData(float thisTemperature, float thisHumidity) {
  if (client.connected()) client.stop();

  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.print("PUT /v2/feeds/");
    client.print(FEEDID);
    client.println(".csv HTTP/1.1");
    client.println("Host: api.cosm.com");
    client.print("X-ApiKey: ");
    client.println(APIKEY);
    client.print("User-Agent: ");
    client.println(USERAGENT);
    client.print("Content-Length: ");

    /*
     work out the length of the string:
     "temperature," = 12 +
     digits of temperature value +
     "humidity," = 9 +
     digits of humidity value +
     "\r\n" = 2
     */
    int length = 12 + countDigits(thisTemperature,2) + 2 + 9 + countDigits(thisHumidity,2) + 2;

    // calculate the length of the sensor reading in bytes:
    client.println(length);

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

    // here's the actual content of the PUT request:
    client.print("temperature,");
    client.println(thisTemperature);
    client.print("humidity,");
    client.println(thisHumidity);
    client.println();
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
    Serial.println("data uploaded");

  }
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}

// Counts digits of a floating point number, to calculate content length
// for an HTTP call.
// Based on Arduino's internal printFloat() function.
int countDigits(double number, int digits)  {
  int n = 0;

  // Handle negative numbers
  if (number < 0.0)
  {
    n++; // "-";
    number = -number;
  }

  // Round correctly so that print(1.999, 2) prints as "2.00"
  double rounding = 0.5;
  for (uint8_t i=0; i<digits; ++i) {
    rounding /= 10.0;
  }
  number += rounding;

  // Extract the integer part of the number and print it
  unsigned long int_part = (unsigned long)number;
  double remainder = number - (double)int_part;

  while (int_part > 0) {
    int_part /= 10;
    n++;
  }
  // Print the decimal point, but only if there are digits beyond
  if (digits > 0) {
    n++; //".";
  }

  // Extract digits from the remainder one at a time
  while (digits-- > 0)
  {
    remainder *= 10.0;
    int toPrint = int(remainder);
    n ++; // += String(toPrint);
    remainder -= toPrint;
  }
  return n;
}
Logged

Pages: [1]   Go Up
Jump to: