root / arduino-1.0 / libraries / Ethernet / examples / PachubeClientString / PachubeClientString.ino @ 58d82c77
History | View | Annotate | Download (3.64 KB)
1 | 58d82c77 | Tom Mullins | /* |
---|---|---|---|
2 | Pachube sensor client with Strings |
||
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 | This example uses the String library, which is part of the Arduino core from |
||
10 | version 0019. |
||
11 | |||
12 | Circuit: |
||
13 | * Analog sensor attached to analog in 0 |
||
14 | * Ethernet shield attached to pins 10, 11, 12, 13 |
||
15 | |||
16 | created 15 March 2010 |
||
17 | updated 26 Oct 2011 |
||
18 | by Tom Igoe |
||
19 | |||
20 | This code is in the public domain. |
||
21 | |||
22 | */ |
||
23 | |||
24 | #include <SPI.h> |
||
25 | #include <Ethernet.h> |
||
26 | |||
27 | // assign a MAC address for the ethernet controller. |
||
28 | // fill in your address here: |
||
29 | byte mac[] = { |
||
30 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; |
||
31 | // fill in an available IP address on your network here, |
||
32 | // for manual configuration: |
||
33 | IPAddress ip(10,0,1,20); |
||
34 | |||
35 | // initialize the library instance: |
||
36 | EthernetClient client; |
||
37 | |||
38 | long lastConnectionTime = 0; // last time you connected to the server, in milliseconds |
||
39 | boolean lastConnected = false; // state of the connection last time through the main loop |
||
40 | const int postingInterval = 10000; //delay between updates to Pachube.com |
||
41 | |||
42 | void setup() { |
||
43 | // start serial port: |
||
44 | Serial.begin(9600); |
||
45 | // give the ethernet module time to boot up: |
||
46 | delay(1000); |
||
47 | // start the Ethernet connection: |
||
48 | if (Ethernet.begin(mac) == 0) { |
||
49 | Serial.println("Failed to configure Ethernet using DHCP"); |
||
50 | // Configure manually: |
||
51 | Ethernet.begin(mac, ip); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | void loop() { |
||
56 | // read the analog sensor: |
||
57 | int sensorReading = analogRead(A0); |
||
58 | // convert the data to a String to send it: |
||
59 | String dataString = String(sensorReading); |
||
60 | |||
61 | // you can append multiple readings to this String if your |
||
62 | // pachube feed is set up to handle multiple values: |
||
63 | int otherSensorReading = analogRead(A1); |
||
64 | dataString += ","; |
||
65 | dataString += String(otherSensorReading); |
||
66 | |||
67 | // if there's incoming data from the net connection. |
||
68 | // send it out the serial port. This is for debugging |
||
69 | // purposes only: |
||
70 | if (client.available()) { |
||
71 | char c = client.read(); |
||
72 | Serial.print(c); |
||
73 | } |
||
74 | |||
75 | // if there's no net connection, but there was one last time |
||
76 | // through the loop, then stop the client: |
||
77 | if (!client.connected() && lastConnected) { |
||
78 | Serial.println(); |
||
79 | Serial.println("disconnecting."); |
||
80 | client.stop(); |
||
81 | } |
||
82 | |||
83 | // if you're not connected, and ten seconds have passed since |
||
84 | // your last connection, then connect again and send data: |
||
85 | if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { |
||
86 | sendData(dataString); |
||
87 | } |
||
88 | // store the state of the connection for next time through |
||
89 | // the loop: |
||
90 | lastConnected = client.connected(); |
||
91 | } |
||
92 | |||
93 | // this method makes a HTTP connection to the server: |
||
94 | void sendData(String thisData) { |
||
95 | // if there's a successful connection: |
||
96 | if (client.connect("www.pachube.com", 80)) { |
||
97 | Serial.println("connecting..."); |
||
98 | // send the HTTP PUT request. |
||
99 | // fill in your feed address here: |
||
100 | client.print("PUT /api/YOUR_FEED_HERE.csv HTTP/1.1\n"); |
||
101 | client.print("Host: www.pachube.com\n"); |
||
102 | // fill in your Pachube API key here: |
||
103 | client.print("X-PachubeApiKey: YOUR_KEY_HERE\n"); |
||
104 | client.print("Content-Length: "); |
||
105 | client.println(thisData.length(), DEC); |
||
106 | |||
107 | // last pieces of the HTTP PUT request: |
||
108 | client.print("Content-Type: text/csv\n"); |
||
109 | client.println("Connection: close\n"); |
||
110 | |||
111 | // here's the actual content of the PUT request: |
||
112 | client.println(thisData); |
||
113 | |||
114 | // note the time that the connection was made: |
||
115 | lastConnectionTime = millis(); |
||
116 | } |
||
117 | else { |
||
118 | // if you couldn't make a connection: |
||
119 | Serial.println("connection failed"); |
||
120 | } |
||
121 | } |