~ubuntu-branches/debian/experimental/arduino/experimental

« back to all changes in this revision

Viewing changes to libraries/Ethernet/examples/PachubeClientString/PachubeClientString.ino

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2012-03-11 18:19:42 UTC
  • mfrom: (1.1.5) (5.1.14 sid)
  • Revision ID: package-import@ubuntu.com-20120311181942-be2clnbz1gcehixb
Tags: 1:1.0.1~rc1+dfsg-1
New upstream release, experimental.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 has been updated to use version 2.0 of the Pachube.com API. 
 
10
 To make it work, create a feed with two datastreams, and give them the IDs
 
11
 sensor1 and sensor2. Or change the code below to match your feed.
 
12
 
 
13
 This example uses the String library, which is part of the Arduino core from
 
14
 version 0019.  
 
15
 
 
16
 Circuit:
 
17
 * Analog sensor attached to analog in 0
 
18
 * Ethernet shield attached to pins 10, 11, 12, 13
 
19
 
 
20
 created 15 March 2010
 
21
 updated 27 Feb 2012
 
22
 by Tom Igoe with input from Usman Haque and Joe Saavedra
 
23
 
 
24
 http://arduino.cc/en/Tutorial/PachubeClientString
 
25
 This code is in the public domain.
 
26
 
 
27
 */
 
28
 
 
29
#include <SPI.h>
 
30
#include <Ethernet.h>
 
31
 
 
32
 
 
33
#define APIKEY         "YOUR API KEY GOES HERE" // replace your pachube api key here
 
34
#define FEEDID         00000 // replace your feed ID
 
35
#define USERAGENT      "My Project" // user agent is the project name
 
36
 
 
37
// assign a MAC address for the ethernet controller.
 
38
// fill in your address here:
 
39
  byte mac[] = { 
 
40
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
 
41
// fill in an available IP address on your network here,
 
42
// for manual configuration:
 
43
IPAddress ip(10,0,0,20);
 
44
 
 
45
// initialize the library instance:
 
46
EthernetClient client;
 
47
 
 
48
unsigned long lastConnectionTime = 0;        // last time you connected to the server, in milliseconds
 
49
boolean lastConnected = false;      // state of the connection last time through the main loop
 
50
const unsigned long postingInterval = 10000;  //delay between updates to Pachube.com
 
51
 
 
52
void setup() {
 
53
  // start serial port:
 
54
  Serial.begin(9600);
 
55
  // give the ethernet module time to boot up:
 
56
  delay(1000);
 
57
  // start the Ethernet connection:
 
58
  if (Ethernet.begin(mac) == 0) {
 
59
    Serial.println("Failed to configure Ethernet using DHCP");
 
60
    // DHCP failed, so use a fixed IP address:
 
61
    Ethernet.begin(mac, ip);
 
62
  }
 
63
}
 
64
 
 
65
void loop() {
 
66
  // read the analog sensor:
 
67
  int sensorReading = analogRead(A0);   
 
68
  // convert the data to a String to send it:
 
69
  
 
70
  String dataString = "sensor1,";
 
71
 dataString += sensorReading;
 
72
 
 
73
  // you can append multiple readings to this String if your
 
74
  // pachube feed is set up to handle multiple values:
 
75
  int otherSensorReading = analogRead(A1);
 
76
  dataString += "\nsensor2,";
 
77
  dataString += otherSensorReading;
 
78
 
 
79
  // if there's incoming data from the net connection.
 
80
  // send it out the serial port.  This is for debugging
 
81
  // purposes only:
 
82
  if (client.available()) {
 
83
    char c = client.read();
 
84
    Serial.print(c);
 
85
  }
 
86
 
 
87
  // if there's no net connection, but there was one last time
 
88
  // through the loop, then stop the client:
 
89
  if (!client.connected() && lastConnected) {
 
90
    Serial.println();
 
91
    Serial.println("disconnecting.");
 
92
    client.stop();
 
93
  }
 
94
 
 
95
  // if you're not connected, and ten seconds have passed since
 
96
  // your last connection, then connect again and send data:
 
97
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
 
98
    sendData(dataString);
 
99
  }
 
100
  // store the state of the connection for next time through
 
101
  // the loop:
 
102
  lastConnected = client.connected();
 
103
}
 
104
 
 
105
// this method makes a HTTP connection to the server:
 
106
void sendData(String thisData) {
 
107
  // if there's a successful connection:
 
108
  if (client.connect("api.pachube.com", 80)) {
 
109
    Serial.println("connecting...");
 
110
    // send the HTTP PUT request:
 
111
    client.print("PUT /v2/feeds/");
 
112
    client.print(FEEDID);
 
113
    client.println(".csv HTTP/1.1");
 
114
    client.print("Host: api.pachube.com\n");
 
115
    client.print("X-PachubeApiKey: ");
 
116
    client.println(APIKEY);
 
117
    client.print("User-Agent: ");
 
118
    client.println(USERAGENT);
 
119
    client.print("Content-Length: ");
 
120
    client.println(thisData.length(), DEC);
 
121
 
 
122
    // last pieces of the HTTP PUT request:
 
123
    client.print("Content-Type: text/csv\n");
 
124
    client.println("Connection: close\n");
 
125
 
 
126
    // here's the actual content of the PUT request:
 
127
    client.println(thisData);
 
128
 
 
129
    // note the time that the connection was made:
 
130
    lastConnectionTime = millis();
 
131
  } 
 
132
  else {
 
133
    // if you couldn't make a connection:
 
134
    Serial.println("connection failed");
 
135
    Serial.println();
 
136
    Serial.println("disconnecting.");
 
137
    client.stop();
 
138
    lastConnected = client.connected();
 
139
  }
 
140
}
 
141
 
 
142