|
| 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 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 25 July 2010 |
| 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 | +// assign an IP address for the controller: |
| 32 | +byte ip[] = { |
| 33 | + 192,169,1,20 }; |
| 34 | +byte gateway[] = { |
| 35 | + 192,168,1,1}; |
| 36 | +byte subnet[] = { |
| 37 | + 255, 255, 255, 0 }; |
| 38 | + |
| 39 | +// The address of the server you want to connect to (pachube.com): |
| 40 | +byte server[] = { |
| 41 | + 209,40,205,190 }; |
| 42 | + |
| 43 | +// initialize the library instance: |
| 44 | +Client client(server, 80); |
| 45 | + |
| 46 | +long lastConnectionTime = 0; // last time you connected to the server, in milliseconds |
| 47 | +boolean lastConnected = false; // state of the connection last time through the main loop |
| 48 | +const int postingInterval = 10000; //delay between updates to Pachube.com |
| 49 | + |
| 50 | +void setup() { |
| 51 | + // start the ethernet connection and serial port: |
| 52 | + Ethernet.begin(mac, ip); |
| 53 | + Serial.begin(9600); |
| 54 | + // give the ethernet module time to boot up: |
| 55 | + delay(1000); |
| 56 | +} |
| 57 | + |
| 58 | +void loop() { |
| 59 | + // read the analog sensor: |
| 60 | + int sensorReading = analogRead(0); |
| 61 | + // convert the data to a String to send it: |
| 62 | + String dataString = String(sensorReading); |
| 63 | + |
| 64 | + // you can append multiple readings to this String if your |
| 65 | + // pachube feed is set up to handle multiple values: |
| 66 | + int otherSensorReading = analogRead(1); |
| 67 | + dataString += ","; |
| 68 | + dataString += String(otherSensorReading); |
| 69 | + |
| 70 | + // if there's incoming data from the net connection. |
| 71 | + // send it out the serial port. This is for debugging |
| 72 | + // purposes only: |
| 73 | + if (client.available()) { |
| 74 | + char c = client.read(); |
| 75 | + Serial.print(c); |
| 76 | + } |
| 77 | + |
| 78 | + // if there's no net connection, but there was one last time |
| 79 | + // through the loop, then stop the client: |
| 80 | + if (!client.connected() && lastConnected) { |
| 81 | + Serial.println(); |
| 82 | + Serial.println("disconnecting."); |
| 83 | + client.stop(); |
| 84 | + } |
| 85 | + |
| 86 | + // if you're not connected, and ten seconds have passed since |
| 87 | + // your last connection, then connect again and send data: |
| 88 | + if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { |
| 89 | + sendData(dataString); |
| 90 | + } |
| 91 | + // store the state of the connection for next time through |
| 92 | + // the loop: |
| 93 | + lastConnected = client.connected(); |
| 94 | +} |
| 95 | + |
| 96 | +// this method makes a HTTP connection to the server: |
| 97 | +void sendData(String thisData) { |
| 98 | + // if there's a successful connection: |
| 99 | + if (client.connect()) { |
| 100 | + Serial.println("connecting..."); |
| 101 | + // send the HTTP PUT request. |
| 102 | + // fill in your feed address here: |
| 103 | + client.print("PUT /api/YOUR_FEED_HERE.csv HTTP/1.1\n"); |
| 104 | + client.print("Host: www.pachube.com\n"); |
| 105 | + // fill in your Pachube API key here: |
| 106 | + client.print("X-PachubeApiKey: YOUR_KEY_HERE\n"); |
| 107 | + client.print("Content-Length: "); |
| 108 | + client.println(thisData.length(), DEC); |
| 109 | + |
| 110 | + // last pieces of the HTTP PUT request: |
| 111 | + client.print("Content-Type: text/csv\n"); |
| 112 | + client.println("Connection: close\n"); |
| 113 | + |
| 114 | + // here's the actual content of the PUT request: |
| 115 | + client.println(thisData); |
| 116 | + |
| 117 | + // note the time that the connection was made: |
| 118 | + lastConnectionTime = millis(); |
| 119 | + } |
| 120 | + else { |
| 121 | + // if you couldn't make a connection: |
| 122 | + Serial.println("connection failed"); |
| 123 | + } |
| 124 | +} |
0 commit comments