Skip to content

Instantly share code, notes, and snippets.

@circuitsenses
Created August 29, 2014 14:48
Show Gist options
  • Save circuitsenses/5d594ad8e397f48cdb62 to your computer and use it in GitHub Desktop.
Save circuitsenses/5d594ad8e397f48cdb62 to your computer and use it in GitHub Desktop.
WaveForm
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { "Your MAC" };
IPAddress server(216,52,233,120);
IPAddress ip( "Your IP" );
unsigned int randomdata = 0;
EthernetClient client;
#define WEBSITE "api.xively.com"
#define API_key "Your APIKey"
#define feedID "Your FeedID"
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// DHCP failed, so use a fixed IP address:
Ethernet.begin(mac, ip);
}
}
void Randomize()
{
randomdata = (randomdata == 0) ? 50 : 0;
}
void loop() {
Randomize();
// Prepare JSON for Xively & get length
int length = 0;
String data = "";
data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" : \"WaveForm\",\"current_value\" : \"" + String(randomdata) + "\"}]}";
length = data.length();
if (client.connect(server, 80)) {
Serial.println("Client Connected");
if (client.connected())
{
Serial.println("Connected!");
client.println("PUT /v2/feeds/" + String(feedID) + ".json HTTP/1.0");
client.println("Host: api.xively.com");
client.println("X-ApiKey: " + String(API_key));
client.println("Content-Length: " + String(length));
client.print("Connection: close");
client.println();
client.print(data);
client.println();
digitalWrite(STAT1, LOW); //Blink stat LED
}
else {
Serial.println(F("Connection failed"));
client.stop();
return;
}
while (client.connected()) {
while (client.available()) {
char c = client.read();
// Serial.print(c);
}
client.stop();
}
delay(1000);
}
else {
Serial.println("Client Failure");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment