Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
double temperature;
#define APIKEY "<YOUR API KEY>"
#define DEVICE_ID "<YOUR DEVICE ID>"
#define USERAGENT "Spark"
byte server[] = {88,190,31,53}; // api.opensensorcloud.com IP address
// initialize the library instance:
TCPClient client;
void setup() {
Serial.begin(9600);
// Connect the temperature sensor to A7 and configure it
// to be an input
pinMode(A0, INPUT);
}
void loop()
{
float voltage = ( analogRead(A0) * 3.3 *3.3/5.0)/4095;
temperature = voltage*100.0;
sendValue("temp",temperature);
while (client.available())
{
char c = client.read();
Serial.print(c);
}
if (!client.connected())
{
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
delay(10000);
}
void sendValue(String key,float value)
{
Serial.println("Will post...");
String data="[{\"name\":\""+key+"\",\"value\":"+String(value)+"}]";
sendData(data);
}
// this method makes a HTTP connection to the server:
void sendData(String &thisData) {
// if there's a successful connection:
Serial.println(thisData);
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.println("POST /device/"+String(DEVICE_ID)+" HTTP/1.1");
Serial.println("Sending:");
client.println("Host:api.opensensorcloud.com");
client.print("X-ApiKey: ");
client.println(APIKEY);
client.print("User-Agent: ");
client.println(USERAGENT);
client.print("Content-Length: ");
client.println(thisData.length());
// last pieces of the HTTP PUT request:
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println();
// here's the actual content of the PUT request:
client.println(thisData);
client.println();
Serial.println("Request done:"+thisData);
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.