ETHERNET_FUNCTIONS.PDE
contains the functions required to initialize the Ethernet shield for Arduino (official version). In this sketch, as before, should not be altered in any way. The third sketch is the main one, the main:
PACHUBE_LM35.PDE
In the latter sees the function for reading the temperature with a LM35, already used in previous work. The data read by our function, however, is split into integer and decimal, and then at the meeting (with the decimal point of separation) from the function sprintf () into a single string, this is necessary because passing the value directly on Pachube This is not formatted correctly. The string thus generated is passed to pachube_in_out ().
The configuration parameters for Pachube was entrusted to symbolic labels:
SHARE_FEED_ID 8276
| update_interval 60000 # define | L ' SHARE_FEED_ID label that represents the ID Pachube assigned when creating your feed manually. In this case, the sketch mode only works in "sharing" (send data, not read remote sensors), and then REMOTE_FEED_ID REMOTE_FEED_DATASTREAMS be set to 0. The label PACHUBE_API_KEY must insert the API key that you are in the area. |
| . | Finally, the update interval: update_interval which is set to 1min (60,000 ms). |
| byte mac [] = {0xCC, 0xAC, 0xBE, 0xEF, 0xFE, 0x91}; | |
| subnet byte [] = {255, 255, 255, 0}; | bytes RemoteServer [] = { 209,40,205,190};
/ *
sketch * Name: PACHUBE_LM35.PDE
* (ETHERNET_FUNCTIONS.PDE, PACHUBE_FUNCTIONS.PDE)
* Grounds: Acquire an using a LM35 temperature sensor connected to the Arduino and
* through the same Ethernet module connected to an ADSL router,
* Pachube sends it on a server at regular intervals.
* Date: 30-Jun-2010
* Rev.: 1.1 * Author: Francesco Parisi, fparisi [at] gmail [dot] com, fparisi [at] tiscali [ dot] com * Arduino Ver: 18 (or higher)
* * Note: This sketch uses (and builds) su sketch sviluppati da usman (www.haque.co.uk), maggio 2009 * copy, distribute, whatever, as you like:
* http://community.pachube.com/node/112
* */ #include <Ethernet.h> #include <string.h> #include <stdio.h>
// per la funzione sprintf
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* Configurazione connessione al server Pachube */
#define
SHARE_FEED_ID 8276
// ID del feed locale Pachube (quello assegnato da Pachube)
REMOTE_FEED_ID
# define
0
/ / ID of the remote feed Pachube (set to 0 if you do not use any remote feed)
# define REMOTE_FEED_DATASTREAMS 0 / / Number of remote sensors associated with the remote feed (set to 0 if you do not use any remote feed) # define PACHUBE_API_KEY "INSERIRE_QUI_LA_PROPRIA_API_KEY"
# define
update_interval
60000 / / If the connection is valid expects 10s before a new update - should be not less than 5s
# define
RESET_INTERVAL
10000 / / If the connection fails / resets waiting 10s before trying again - it should be not less than 5s
float
remoteSensor [ REMOTE_FEED_DATASTREAMS
]
/ / If the data is different (eg, integers) to change the return type
///////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// / / /
/////////////////////////////////////////// ////////////////////////////////////////////////// ///////////////////////
/ * * Arduino hardware configuration /
# define
LM35PIN
5
/ / Number of analog input to which and 'connecting the sensor LM35
///////////////////////////// ////////////////////////////////////////////////// ///////////////////////////////////// /////////////////////////////////////////// ////////////////////////////////////////////////// /////////////////////// / * Network configuration of Ethernet module * / mac byte []= {
0xCC, 0xAC , 0xBE, 0
, 0xEF, 0xFE ,
0x91
} ; // assicurarsi che il MAC address sia unico sulla propria rete
byte ip
[] =
{
192 ,
168
,
0
,
10
}
;
// dipende dalla configurazione del ns. router
byte
gateway
[]
=
{
192
,
168
1
}
;
// dipende dalla configurazione del ns. router
=
{
255
255
,
0
}
;
// dipende dalla configurazione del ns. router
byte
remoteServer [] =
{
40
,
205, 190
};
/ / server IP pachube.com
() {
////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////
///////////////////////////////////////////// ////////////////////////////////////////////////// /////////////////////
/ * Function to read the temperature detected dall'LM35 * /
double
readLM35Temp
double temp =
0.0; / / value converted to temperature (° C)
int val =
0;
/ / value quantized by the ADC [0 .. 1023]
int
nread =
20
;
double sum =
0.0
;
/ / sum of the individual readings
(
int i =
;
i \u0026lt;nread
;
i + +) {
(
LM35PIN
)
/ / reads the converted data to the load pin LM35PIN
temp =
(100.0
* 1.1 * val)
/
1024.0
;
/ / Vref = 1.1 V (internal reference)
sum + = temp
;
/ / adds to the sum of the temperatures read
delay50);
(
/ / wait 50ms}
return
(
sum
/
nread
);
// restituisce la temperatura media
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
setup ()
{ Serial .
begin
( 57600 );
setupEthernet
();
analogReference (
INTERNAL ); // per l'ADC usiamo il Vref interno da 1,1V (migliore precisione)
analogRead
( LM35PIN );
// Prima lettura "a vuoto" (cfr. pag. 47 datashett ATmega168)
} ///////////////////////////////////// ////////////////////////////////////////////////// ///////////////////////////// ////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// / /
void
loop () {
char date
[ 20];
/ / String data to be sent to Pachube / / We read the temperature and the result assegnamone to 'temp'
double temp =
readLM35Temp
();
/ / We extract the integer part of temperature unsigned int int_part =
( unsigned int ) temp ;
// quindi estraiamo la parte decimale (con una cifra dopo la virgola) unsigned int frac_part =
10.0 * ( temp
-
( double ) int_part );
// Creiamo la stringa concatenando le due parti sprintf ( data
, "%d.%d" , int_part , frac_part )
/ / Call the function to put / get to / from Pachube
pachube_in_out (
date);}
///////////////////////////////////////////////// ////////////////////////////////////////////////// /////////////////
Here, the two sketches usman:
PACHUBE_FUNCTIONS.PDE
/ * sketch * Name: PACHUBE_FUNCTIONS.PDE * Justification : Manages the put / get data to / from the server Pachube * function through a single put / get. The data read by sensors * are printed on the serial remote. * Date: 30-Jun-2010 * Rev.: 1.1 * Autore: usman (www.haque.co.uk), maggio 2009
* http://community.pachube.com/node/112 * Modificato da: Francesco Parisi (fparisi * gmail . com, giugno 2010) * + La funzione pachube_in_out() ha un parametro di ingresso * + La stampa dei dati dei sensori remoti avviene con due cifre dopo la virgola
* Ver. Arduino: 18 (o superiore) * */ boolean found_status_200 = false ; boolean found_session_id = false ; boolean found_CSV
= false ; char * found ; unsigned int successes = 0 ; unsigned int failures = 0
; boolean ready_to_update = true ; boolean reading_pachube = false ; boolean request_pause = false ; boolean found_content = false ;
int content_length ;
unsigned long last_connect
;
void pachube_in_out ( char
data []
) { content_length = strlen (
data ); if ( millis
() < last_connect ) last_connect = millis
(); if ( request_pause )
{
if (( millis () - last_connect ) > interval )
{
ready_to_update = true ; reading_pachube = false ; request_pause = false ;
found_status_200 = false ; found_session_id = false ; found_CSV = false ; //Serial.print("Ready to connect: "); //Serial.println(millis());
} }
if ( ready_to_update ) { Serial . println
(
"Connecting ..." ); if ( local client .
Connect ()) {
Serial
.
println (
"GET request to retrieve" ); local client.
( "GET /api/" ); localClient . print
( REMOTE_FEED_ID ); localClient .
print ( ".csv HTTP/1.1 \n Host: pachube.com \n
X-PachubeApiKey: " ); localClient . print (
PACHUBE_API_KEY );
localClient . print
( " \n
User-Agent: Arduino (Pachube In Out v1.1)" ); localClient .
println
(
" \n " ); Serial .
println (
"finished GET now PUT, to update" ); localClient . print
( "PUT /api/" );
localClient . print ( SHARE_FEED_ID ); localClient . print
( ".csv HTTP/1.1
\n Host: pachube.com \n X-PachubeApiKey: " ); localClient . print ( PACHUBE_API_KEY ); localClient
.
( " \n User-Agent: Arduino (Pachube In Out v1.1)" ); localClient . print
(
" \n
Content-Type: text/csv \n Content-Length: " ); localClient
.
( content_length );
localClient .
(
"
\n
Connection: close
\n\n
"
);
localClient . print
( data );
localClient .
print (
" \\ n
" );
Serial . println
( "
\\ n --- updated:" ); Serial . println
( data );
ready_to_update = false ;
reading_pachube = true ;
request_pause = false ;
interval = UPDATE_INTERVAL ;
// Serial.print("finished PUT: ");
// Serial.println(millis());
} else
{ Serial .
print ( "connection failed!"
); Serial . print (++ failures
); found_status_200 = false ; found_session_id = false ;
found_CSV = false ; ready_to_update
= false ; reading_pachube = false
;
request_pause = true ; last_connect
= millis ();
interval = RESET_INTERVAL ;
setupEthernet ();
}
} while ( reading_pachube ) {
while
( localClient . available ())
{ checkForResponse (); } if (! localClient . connected
()) { disconnect_pachube ();
} } } void disconnect_pachube () { Serial .
println
( "disconnecting. \\ n
===== \\ n \\ n
" );
local client.
stop (); ready_to_update = false
; reading_pachube = false
; request_pause = true
; last_connect
=
Millis
( ); found_content = false ;
resetEthernetShield (); } void
checkForResponse () { char c = localClient . read
(); //Serial.print(c); buff [ pointer ]
=
c ; if ( pointer < 64
) pointer ++; if (
c == ' \n ' ) { found = strstr (
buff , "200 OK" ); if
( found != 0 ) { found_status_200 = true ;
//Serial.println("Status 200"); } buff [ pointer ]= 0 ; found_content
=
true ; clean_buffer (); }
if (( found_session_id ) && (! found_CSV
)) { found = strstr
( buff , "HTTP/1.1" ); if ( found != 0 )
{ char csvLine [ strlen
(
buff )- 9 ]; strncpy ( csvLine , buff ,
strlen ( buff )- 9 ); //Serial.println("This is the retrieved CSV:");
//Serial.println("---"); //Serial.println(csvLine); //Serial.println("---");
// Serial.println("\n--- updated: "); // Serial.println(pachube_data); Serial . println (
" \n --- retrieved: " ); char delims
[]
= "," ; char * result = NULL ;
char
* ptr ; result = strtok_r ( buff , delims ,
& ptr ); int counter = 0
; while
( result !=
NULL ) {
remoteSensor [ counter ++]
=
atof
( result ); result
=
strtok_r
( NULL , delims ,
& ptr ); } for
( int i = 0 ; i <
REMOTE_FEED_DATASTREAMS ; i ++)
{ Serial .
print (
remoteSensor [ i ],
2 );
// due cifre dopo la virgola Serial .
print ( "
\t " );
} found_CSV =
true ;
Serial .
print ( " \n successful updates="
); Serial . println (++ successes ); }
}
if
(
found_status_200 ) { found = strstr ( buff , "_id="
); if
( found
!= 0
)
{
clean_buffer (); found_session_id = true
; } } } ETHERNET_FUNCTIONS.PDE
/*
* Nome sketch: ETHERNET_FUNCTIONS.PDE
* Motivazione: Gestisce la configurazione
* e l'inizializzazione del dispositivo Ethernet
* Data: 30-giu-2010 * Rev.: 1.1
* Autore: usman (www.haque.co.uk), maggio 2009
* http://community.pachube.com/node/112
* Arduino Ver: 18 (or higher) *
* / Client localClient
( remoteServer , 80
) unsigned int interval ;
char buff [64 ] int pointer =
0 ; void setupEthernet () {
resetEthernetShield (); Client remoteClient ( 255
); delay ( 500 ); interval
= UPDATE_INTERVAL ; Serial
. println
(
"setup complete" ); }
void clean_buffer
() { pointer =
0
;
memset ( buff , 0 , sizeof
( buff )); } void resetEthernetShield
() { Serial . println ( "reset ethernet"
); Ethernet . begin ( mac , ip );}
.