Created
August 1, 2013 08:46
-
-
Save indiejoseph/6129633 to your computer and use it in GitHub Desktop.
SenseCloud - Arduino code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <Ethernet.h> | |
// assign a MAC address for the ethernet controller. | |
// fill in your address here: | |
byte mac[] = { | |
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; | |
// fill in an available IP address on your network here, | |
// for manual configuration: | |
IPAddress ip(192,168,1,123); // Device IP | |
IPAddress myDns(8,8,8,8); // DNS Server | |
float tempc = 0; // temperature variables | |
int port = 5000; // Server port | |
// initialize the library instance: | |
EthernetClient client; | |
char server[] = "cloud.sensebox.co"; | |
unsigned 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 unsigned long postingInterval = (long)3*1000; // delay between updates, in milliseconds | |
void setup() { | |
// start serial port: | |
Serial.begin(9600); | |
// give the ethernet module time to boot up: | |
delay(1000); | |
// start the Ethernet connection using a fixed IP address and DNS server: | |
Ethernet.begin(mac, ip, myDns); | |
// print the Ethernet board/shield's IP address: | |
Serial.print("My IP address: "); | |
Serial.println(Ethernet.localIP()); | |
} | |
void loop() { | |
// 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)) { | |
httpRequest(); | |
} | |
// 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 httpRequest() { | |
tempc = analogRead(A8); //read the value from the sensor | |
tempc = tempc * 0.48828125; //convert the analog data to temperature | |
delay(1000); | |
// if there's a successful connection: | |
if (client.connect(server, port)) { | |
Serial.println("connecting..."); | |
// send the HTTP PUT request: | |
String data = "{\"datastreams\": [{"; | |
data += " \"name\": \"temperature\","; | |
data += " \"value\": "; | |
data += (int)tempc; | |
data += ","; | |
data += " \"unit\": \"Celsius\","; | |
data += " \"unit_symbol\": \"C\","; | |
data += " \"tags\": \"test\""; | |
data += "}]}"; | |
Serial.println(tempc); | |
Serial.println(data); | |
tempc = 0; | |
client.println("POST /api/apps/temp/datastreams HTTP/1.1"); | |
client.println("X-Device-Key: 42bc4a155b047ad001225bdacd1fbaa4"); | |
client.println("X-App-Key: 25317badef4e07d09d523069f0ebacf9"); | |
client.println( "Content-Type: application/json" ); | |
client.println( "Connection: close" ); | |
client.print( "Content-Length: " ); | |
client.println( data.length() ); | |
client.println(); | |
client.print( data ); | |
client.println(); | |
// note the time that the connection was made: | |
lastConnectionTime = millis(); | |
} | |
else { | |
// if you couldn't make a connection: | |
Serial.println("connection failed"); | |
Serial.println("disconnecting."); | |
client.stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment