Project

General

Profile

Statistics
| Branch: | Revision:

root / arduino-1.0 / libraries / Ethernet / examples / PachubeClient / PachubeClient.ino @ 58d82c77

History | View | Annotate | Download (4.22 KB)

1
/*
2
  Pachube sensor client
3
 
4
 This sketch connects an analog sensor to Pachube (http://www.pachube.com)
5
 using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
6
 the Adafruit Ethernet shield, either one will work, as long as it's got
7
 a Wiznet Ethernet module on board.
8
 
9
 Circuit:
10
 * Analog sensor attached to analog in 0
11
 * Ethernet shield attached to pins 10, 11, 12, 13
12
 
13
 created 15 March 2010
14
 updated 26 Oct 2011
15
 by Tom Igoe
16
 
17
 http://www.tigoe.net/pcomp/code/category/arduinowiring/873
18
 This code is in the public domain.
19
 
20
 */
21

    
22
#include <SPI.h>
23
#include <Ethernet.h>
24

    
25
// assign a MAC address for the ethernet controller.
26
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
27
// fill in your address here:
28
byte mac[] = { 
29
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
30

    
31
// fill in an available IP address on your network here,
32
// for manual configuration:
33
IPAddress ip(10,0,1,20);
34
// initialize the library instance:
35
EthernetClient client;
36

    
37
long lastConnectionTime = 0;        // last time you connected to the server, in milliseconds
38
boolean lastConnected = false;      // state of the connection last time through the main loop
39
const int postingInterval = 10000;  //delay between updates to Pachube.com
40

    
41
void setup() {
42
  // start serial port:
43
  Serial.begin(9600);
44
  // start the Ethernet connection:
45
  if (Ethernet.begin(mac) == 0) {
46
    Serial.println("Failed to configure Ethernet using DHCP");
47
    // no point in carrying on, so do nothing forevermore:
48
    for(;;)
49
      ;
50
  }
51
  // give the ethernet module time to boot up:
52
  delay(1000);
53
  // start the Ethernet connection:
54
  if (Ethernet.begin(mac) == 0) {
55
    Serial.println("Failed to configure Ethernet using DHCP");
56
    // Configure manually:
57
    Ethernet.begin(mac, ip);
58
  }
59
}
60

    
61
void loop() {
62
  // read the analog sensor:
63
  int sensorReading = analogRead(A0);   
64

    
65
  // if there's incoming data from the net connection.
66
  // send it out the serial port.  This is for debugging
67
  // purposes only:
68
  if (client.available()) {
69
    char c = client.read();
70
    Serial.print(c);
71
  }
72

    
73
  // if there's no net connection, but there was one last time
74
  // through the loop, then stop the client:
75
  if (!client.connected() && lastConnected) {
76
    Serial.println();
77
    Serial.println("disconnecting.");
78
    client.stop();
79
  }
80

    
81
  // if you're not connected, and ten seconds have passed since
82
  // your last connection, then connect again and send data:
83
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
84
    sendData(sensorReading);
85
  }
86
  // store the state of the connection for next time through
87
  // the loop:
88
  lastConnected = client.connected();
89
}
90

    
91
// this method makes a HTTP connection to the server:
92
void sendData(int thisData) {
93
  // if there's a successful connection:
94
  if (client.connect("www.pachube.com", 80)) {
95
    Serial.println("connecting...");
96
    // send the HTTP PUT request. 
97
    // fill in your feed address here:
98
    client.print("PUT /api/YOUR_FEED_HERE.csv HTTP/1.1\n");
99
    client.print("Host: www.pachube.com\n");
100
    // fill in your Pachube API key here:
101
    client.print("X-PachubeApiKey: YOUR_KEY_HERE\n");
102
    client.print("Content-Length: ");
103

    
104
    // calculate the length of the sensor reading in bytes:
105
    int thisLength = getLength(thisData);
106
    client.println(thisLength, DEC);
107

    
108
    // last pieces of the HTTP PUT request:
109
    client.print("Content-Type: text/csv\n");
110
    client.println("Connection: close\n");
111

    
112
    // here's the actual content of the PUT request:
113
    client.println(thisData, DEC);
114

    
115
    // note the time that the connection was made:
116
    lastConnectionTime = millis();
117
  } 
118
  else {
119
    // if you couldn't make a connection:
120
    Serial.println("connection failed");
121
  }
122
}
123

    
124

    
125
// This method calculates the number of digits in the
126
// sensor reading.  Since each digit of the ASCII decimal
127
// representation is a byte, the number of digits equals
128
// the number of bytes:
129

    
130
int getLength(int someValue) {
131
  // there's at least one byte:
132
  int digits = 1;
133
  // continually divide the value by ten, 
134
  // adding one to the digit count for each
135
  // time you divide, until you're at 0:
136
  int dividend = someValue /10;
137
  while (dividend > 0) {
138
    dividend = dividend /10;
139
    digits++;
140
  }
141
  // return the number of digits:
142
  return digits;
143
}
144