Skip to content

Instantly share code, notes, and snippets.

@smulube
Last active December 11, 2015 12:58
Show Gist options
  • Save smulube/4603840 to your computer and use it in GitHub Desktop.
Save smulube/4603840 to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>
#define API_KEY "YOURAPIKEY" // your Cosm API key
#define FEED_ID YOURFEEDID // your Cosm feed ID
// MAC address for your Ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = 2;
unsigned long lastConnectionTime = 0; // last time we connected to Cosm
const unsigned long connectionInterval = 15000; // delay between connecting to Cosm in milliseconds
EthernetClient client;
CosmClient cosmclient(client);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Cosm Sensor Client Example");
Serial.println("==========================");
Serial.println("Initializing network");
while (Ethernet.begin(mac) != 1) {
Serial.println("Error getting IP address via DHCP, trying again...");
delay(15000);
}
Serial.println("Network initialized");
Serial.println();
}
void loop() {
// main program loop
if (millis() - lastConnectionTime > connectionInterval) {
// read data from Cosm
getData();
// update connection time so we wait before connecting again
lastConnectionTime = millis();
}
}
// get the value of the datastream from Cosm, then use this data somehow
void getData() {
Serial.println("Reading data from Cosm");
int ret = cosmclient.get(feed, API_KEY);
Serial.print("GET return code: ");
Serial.println(ret);
if (ret > 0) {
Serial.print("Datastream is: ");
Serial.println(feed[0]);
Serial.print("Sensor value is: ");
Serial.println(feed[0].getFloat());
}
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment