Kiilo-ArduinoEthernetShield-Read/Write
arduino + ethernetshield
OUTPUT send sensor data to pachube
/*
Pachube sensor client with Strings
This sketch connects an analog sensor to Pachube (http://www.pachube.com)
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
the Adafruit Ethernet shield, either one will work, as long as it's got
a Wiznet Ethernet module on board.
This example uses the String library, which is part of the Arduino core from
version 0019.
Circuit:
* Analog sensor attached to analog in 0
* Ethernet shield attached to pins 10, 11, 12, 13
created 15 March 2010
updated 4 Sep 2010
by Tom Igoe
This code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <SHT1x.h>
#include <stdlib.h>
//dtostrf(FLOAT,WIDTH,PRECSISION,BUFFER);
// Specify data and clock connections and instantiate SHT1x object
#define dataPin 6
#define clockPin 7
SHT1x sht1x(dataPin, clockPin);
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// assign an IP address for the controller:
byte ip[] = {
192,168,49,20 };
byte gateway[] = {
192,168,49,1};
byte subnet[] = {
255, 255, 255, 0 };
// The address of the server you want to connect to (pachube.com):
byte server[] = { 173,203,98,29 };
// initialize the library instance:
Client client(server, 80);
long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const int postingInterval = 10000; //delay between updates to Pachube.com
long lastReadingTime = 0;
const int readingInterval = 2000; //delay between readings
// read the SHT11sensor:
float temp_c;
float temp_f;
float humidity;
String dataString = "";
void setup() {
// start the ethernet connection and serial port:
Ethernet.begin(mac, ip);
Serial.begin(9600);
// give the ethernet module time to boot up:
delay(1000);
}
void loop() {
if (millis() - lastReadingTime > readingInterval) {
lastReadingTime = millis();
// Read values from the sensor
temp_c = sht1x.readTemperatureC();
//temp_f = sht1x.readTemperatureF();
humidity = sht1x.readHumidity();
// Print the values to the serial port
//Serial.print("Temperature: ");
//Serial.print(temp_c, DEC);
//((Serial.print("C / ");
//Serial.print(humidity);
//Serial.println("%");
// convert the data to a String to send it:
char out[20] = "";
dataString = dtostrf(temp_c, 5, 2, out);
dataString += ",";
dataString += dtostrf(humidity, 5, 2, out);
//Serial.println(dataString);
}
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
sendData(dataString);
Serial.println(dataString);
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server:
void sendData(String thisData) {
// if there's a successful connection:
if (client.connect()) {
Serial.println("connecting...");
// send the HTTP PUT request.
// fill in your feed address here:
client.print("PUT /api/2626.csv HTTP/1.1\n");
client.print("Host: www.pachube.com\n");
// fill in your Pachube API key here:
client.print("X-PachubeApiKey: 6e10b9a84989101b7135821003473d587edbd714062d1fea8eceb9c3682900d6\n");
client.print("Content-Length: ");
client.println(thisData.length(), DEC);
// last pieces of the HTTP PUT request:
client.print("Content-Type: text/csv\n");
client.println("Connection: close\n");
// here's the actual content of the PUT request:
client.println(thisData);
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
INPUT retrieve sensor data from pachube
/*
PachubeClientTest
Author: Arduinomstr
Date: 21-12-2010
Version: 1.1
Sketch to test updating Pachube feed using Official Arduino
Ethernet shield - Reset pin on Ethernet Controller needs bending
out of header and connecting to pin 8 to provide hardware reset
Updates feed with fixed data and displays response on serial monitor
Tested on Duemilanove, DFRobot Shield and Arduino 0022
*/
#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>
#include <Servo.h>
#define DEBUG_OUTPUT false
#define PACHUBE_FEED_ID 2626
#define PACHUBE_API_KEY "<YOUR API KEY HERE!>"
#define PACHUBE_UPDATE_INTERVAL 10000 /* Delay between updates, milliseconds */
#define RESET_PIN 8 /* Reset Ethernet using Arduino */
#define WAITING 2 /* Waiting state */
#define CONNECTING 3 /* Connecting state */
#define SENDING 4 /* Sending state */
#define FAILING 5 /* Failing state */
#define READING 6 /* Reading state */
#define DISCONNECTING 7 /* Disconnecting state */
#define PARSING 9
#define FIRST_LED WAITING /* First in consecutive LEDs */
#define LAST_LED DISCONNECTING /* Last LED */
byte mac[] = { 0xDE, 0xAD, 0xBA, 0xEF, 0xFA, 0xAD };
byte ip[] = { 192, 168, 49, 24 };
byte gateway[] = { 192, 168, 49, 1 };
byte subnet[] = { 255, 255, 255, 0 };
byte server[] = { 173, 203, 98, 29 }; /* www.pachube.com */
int state = 0; /* System state */
Client client(server, 80); /* Client listening on port 80 */
TextFinder finder( client);
Servo Pointer;
int pos = 0;
//String pachubeString = PACHUBE_TEST_DATA; /* Construct string using test data */
//float PachubeSensor[]= {0, 0, 0, 0, 0, 0 ,0 ,0};
// Set-up Arduino I/O and Ethernet Controller
// Includes fix to kick-start Ethernet on power-up /
// power-failure by using reset signal from Arduino
// to reset-pin on Ethernet Shield
void setup()
{
pinMode(RESET_PIN, OUTPUT); /* Configure I/O pins as outputs */
Pointer.attach(7);
// Start fix
digitalWrite(RESET_PIN, LOW); /* Take Ethernet reset line low */
delay(200); /* momentarily */
digitalWrite(RESET_PIN, HIGH); /* Bring it up again */
delay(2000); /* Allow Ethernet time to boot */
// End of fix
Serial.begin(9600); /* Initialise serial line on Arduino */
Ethernet.begin(mac, ip, gateway, subnet); /* Initialise Ethernet settings */
}
// Main function
void loop()
{
Serial.println("WAITING...");
waiting(); /* Provide delay between updates */
if (!client.connected()) {
Serial.println("CONNECTING...");
connecting(); /* Connect if no connection */
}
if (client.connected()) {
Serial.println("SENDING...");
sending(); /* Do PUT if connection is present */
//Serial.print("Data sent: ");
//Serial.println(pachubeString);
} else {
Serial.println("FAILING...");
fail(); /* Otherwise enter failure state */
}
if (client.available()) {
Serial.println("READING...");
reading(); /* Output response if there is one */
}
if (!client.connected()) {
Serial.println();
Serial.println("DISCONNECTING...");
disconnecting(); /* Stop client if disconnected */
}
} /* End of main loop */
// Set WAITING state and
// wait for update interval
void waiting() {
state = WAITING; /* Set state */
showStatus(); /* Turn on LED */
delay(PACHUBE_UPDATE_INTERVAL); /* Delay until net update */
}
// Set CONNECTING state and try to connect
void connecting() {
state = CONNECTING; /* Set state */
showStatus(); /* Turn on LED */
client.connect(); /* Try to connect */
}
// Set SENDING state
// build PUT request and send data
void sending() {
state = SENDING; /* Set state */
showStatus(); /* Turn on LED */
client.print("GET /api/feeds/");
client.print(PACHUBE_FEED_ID);
client.println(".csv HTTP/1.0"); /* update using csv format */
client.println("Host: www.pachube.com");
client.print("X-PachubeApiKey: ");
client.println(PACHUBE_API_KEY);
//client.print("Content-Length: ");
//client.println(pachubeString.length(), DEC); /* Calculate length of request */
client.println("Content-Type: text/csv");
client.println("Connection: close");
client.println();
//client.prinn(pachubeString); /* Output test data */
client.println();
}
// Set FAILING state and continue
void fail() {
state = FAILING; /* Set state */
showStatus(); /* Turn on LED */
}
// Set READING state and keep reading
// bytes from buffer until it is empty
// outputting bytes to serial monitor
void reading() {
state = READING; /* Set state */
showStatus();
//while (client.available()) { /* When buffer has data */
// char c = client.read(); /* Read byte */
// Serial.print(c);
while (client.available()) {
if( finder.find("HTTP/1.1")) {
int response = finder.getValue();
Serial.println(response);
}
if( finder.find("\r\n\r\n")) {
float temp = finder.getFloat();
float hum = finder.getFloat();
Serial.println(temp);
Serial.println(hum);
Pointer.write((int)(temp * 4.5));
}
client.flush();
}
}
// Set DISCONNECTING state
// and stop client
void disconnecting() {
state = DISCONNECTING; /* Set state */
showStatus(); /* Turn on LED */
client.stop(); /* Stop client */
}
// Turn off each LEDs unless it
// matches current status
void showStatus() { /* Turn off LED is pin does not */
delay(2000); /* Keep LED on for short while */
}