Below is the sketch I created, essentially merging the basic temperature sketch with some code I found
online for the post request. At the time of writing I believe that Arduino cannot convert a float to a string and I was scratching my head over this for quite a while. I don't have a C programming background and so that didn't help. Anyway I found a patch which did exactly what I wanted here however please note that it is probably not a great idea to patch Arduino files because an update could break it. Please post other ways to tackle this.
#include "SPI.h"
#include "Ethernet.h"
void postData();
void getTemp();
//Data
float temperature = 0.00;
int temperaturePin = 0;
// Enter a MAC address of your Arduino board that should be stuck to the bottom of the board
byte mac[] = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX };
//Server to connect to
char serverName[] = "http://temp-monitor.appspot.com";
// Initialize the Ethernet client library
EthernetClient client;
void setup()
{
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0)
{
while(true);
}
// give the Ethernet shield a second to initialize:
delay(1000);
}
void loop()
{
delay(60000);
getTemp();
postData();
}
void postData()
{
String data = String("temperature="+ String(temperature,2));
Serial.println(data);
while(!client.connected())
{
client.connect(serverName, 80);
}
client.println("POST / HTTP/1.1"); # The first slash is the part of the url that the data will post to. The default slash will post to the default MainPage post method in your google app. This can be changed to whatever you want
client.println("Host: temp-monitor.appspot.com");
client.println("Connection: keep-alive");
client.print("Content-Length: ");
client.println(data.length());
client.println("Cache-Control: max-age=0");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Accept-Language: en-US,en;q=0.8");
client.println();
client.print(data);
client.println();
char c;
while(client.available())
{
c = client.read();
}
client.stop();
}
float getVoltage(int pin){
return (analogRead(pin) * .004882814);
}
void getTemp()
{
//temperature = (((temperature - 0.5) * 100)*1.8) + 32;
temperature = (getVoltage(temperaturePin) - .5) * 100;
}
Below is a simple diagram I took from another site Arduino Tutorial Bundle.
No comments:
Post a Comment