Created
July 3, 2013 22:01
-
-
Save errordeveloper/5923258 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
#include <HttpClient.h> | |
#include <Xively.h> | |
#include <EEPROM.h> | |
#include "sha1.h" | |
#include "EEPROMAnything.h" | |
#include <MemoryFree.h> | |
//remove these - add programtic definition | |
//#define key "FFiHpFMcOQsoSMPbLCFutthSh9qSAKxoVGhSMkhBNWk4bz0g" // your Cosm API key | |
//#define FEED_ID 121601 // your Cosm feed ID | |
/////////////////SETUP//////////////////////////////////////////////////////////////////////////////////////// | |
// MAC address for your Ethernet shield | |
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x76, 0xFC }; | |
// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield) | |
int sensorPin = 2; | |
const unsigned long connectionInterval = 10000; // delay between connecting to Cosm in milliseconds | |
int eepromCheck = 2; //eeprom position which stores the check variable for beign registered | |
byte eepromRegistered = 245; //byte value asigned to eepromCheck when tthe device has been registered. | |
int eeConfK = 10; //eeprom start locaiton for key | |
int eeConfKe = 59; | |
int eeConfF = 60; //eeprom start loc for feed | |
int eeConfFe = 71; | |
char sensorId[] = "memory"; // default datastream id | |
//xively | |
//IPAddress server(216,52,233,121); | |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
//vars | |
int eepromad = 0; | |
unsigned long lastConnectionTime = 0; // last time we connected to Cosm | |
String xresp = ""; | |
String skey; | |
char ckey[49]; | |
unsigned long lfeed; | |
//sha vvars | |
uint8_t deviceKey[]={ | |
0x3a,0x47,0x68,0xc1,0x79,0x33,0x59,0x99,0x10,0x00, | |
0x93,0x43,0x5d,0x89,0x92,0xdf,0x8b,0xb3,0xd4,0xe0 | |
}; | |
String serialNumber = "bca"; | |
String activation_code; | |
/// | |
String code; | |
String returnString; | |
XivelyDatastream datastreams[] = { | |
XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT), | |
}; | |
EthernetClient client; | |
XivelyClient cosmclient(client); | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(19200); | |
Serial.println(freeMemory()); | |
Serial.println("Initializing network"); | |
Serial.println(freeMemory()); | |
boolean conn = Ethernet.begin(mac); | |
// if(conn == 0) { | |
// Serial.println("Error getting IP address via DHCP, trying again..."); | |
// //delay(15000); | |
// }else Serial.println("Network initialized"); | |
Serial.println(); | |
Serial.println(freeMemory()); | |
//check if product initialized | |
if(EEPROM.read(eepromCheck) != eepromRegistered){ | |
Serial.println("not regd"); | |
registerProduct(); | |
Serial.println(freeMemory()); | |
}else{ | |
Serial.println("eeprom check passed"); | |
String efeed = readEEPROM(eeConfF, eeConfFe); | |
char fbuf[11]; | |
efeed.toCharArray(fbuf, sizeof(fbuf)); | |
lfeed = atol(fbuf); | |
skey = readEEPROM(eeConfK, eeConfKe); | |
skey.toCharArray(ckey, sizeof(ckey)); | |
Serial.println(ckey); | |
Serial.println(lfeed); | |
} | |
} | |
void loop() { | |
// main program loop | |
if (millis() - lastConnectionTime > connectionInterval) { | |
// read a value from the pin | |
int sensorValue = freeMemory(); | |
// send it to Xively | |
sendData(sensorValue); | |
// update connection time so we wait before connecting again | |
lastConnectionTime = millis(); | |
//Serial.print(returnString); | |
} | |
} | |
// send the supplied value to Xively, printing some debug information as we go | |
void sendData(int sensorValue) { | |
// Wrap the datastream into a feed | |
XivelyFeed feed(lfeed, datastreams, 1 /* number of datastreams */); | |
datastreams[0].setFloat(sensorValue); | |
Serial.println(datastreams[0].getFloat()); | |
int ret = cosmclient.put(feed, ckey); | |
Serial.print("PUT return code: "); | |
Serial.println(ret); | |
Serial.println(); | |
} | |
void registerProduct(){ | |
Serial.println("computing hmac sha: "); | |
Sha1.initHmac(deviceKey,20); | |
Sha1.print(serialNumber); | |
//activation_code = | |
convertHash(Sha1.resultHmac()); | |
//delay(1000); | |
Serial.println(returnString); | |
//talk to cosm | |
httpRequest(); | |
while(!client.available()){ | |
delay(10); | |
} | |
while(client.available()) { | |
char c = client.read(); | |
Serial.print(c); | |
if(c == '{'){ | |
xresp = "{"; | |
while(c != '}'){ | |
c = client.read(); | |
xresp += c; | |
} | |
} | |
//xresp += c; | |
} | |
Serial.println("got resp"); | |
Serial.println(xresp); | |
//if (!client.connected()) { | |
Serial.println("disconnecting."); | |
client.stop(); | |
//} | |
//store to eeprom | |
if(xresp.indexOf("apikey") != -1){ | |
//key | |
int keyloc = xresp.indexOf("apikey")+9; | |
String pkey = xresp.substring(keyloc, keyloc+48); | |
Serial.println(pkey); | |
//feed | |
int feedlocs = xresp.indexOf(',')+11; | |
int feedloce = xresp.indexOf(',',feedlocs); | |
String pfeed = xresp.substring(feedlocs, feedloce); | |
Serial.println(pfeed); | |
//set key and feed | |
pkey.toCharArray(ckey, sizeof(ckey)); | |
char fbuf[11]; | |
pfeed.toCharArray(fbuf, sizeof(fbuf)); | |
lfeed = atol(fbuf); | |
//eeprom | |
writeEEPROM(eeConfK, pkey); | |
writeEEPROM(eeConfF, pfeed); | |
EEPROM.write(eepromCheck, eepromRegistered); | |
} | |
} | |
String convertHash(uint8_t* hash) { | |
//returnString = ""; | |
int i; | |
for (i=0; i<20; i++) { | |
returnString += ("0123456789abcdef"[hash[i]>>4]); | |
returnString += ("0123456789abcdef"[hash[i]&0xf]); | |
} | |
//Serial.println(returnString); | |
//code = returnString; | |
return returnString; | |
} | |
// this method makes a HTTP connection to the server: | |
void httpRequest() { | |
// if there's a successful connection: | |
if (client.connect("api.xively.com", 80)) { | |
Serial.println("connecting..."); | |
client.print("GET /v2/devices/"); | |
client.print(returnString); | |
client.println("/activate HTTP/1.1"); | |
client.println("Host: api.xively.com"); | |
client.println("User-Agent: arduino-ethernet"); | |
//client.print("Content-Length: "); | |
client.println("Connection: close"); | |
client.println(); | |
} | |
else { | |
// if you couldn't make a connection: | |
Serial.println("connection failed"); | |
Serial.println("disconnecting."); | |
client.stop(); | |
} | |
} | |
void writeEEPROM(int adr, String input){ | |
//int cbytes = 0; | |
for(int i=0; i <= input.length(); i++){ | |
EEPROM.write(adr, byte(input.charAt(i))); | |
adr++; | |
} | |
} | |
String readEEPROM(int adr, int endr) { | |
String str; | |
int value; | |
while(adr <= endr ) { | |
value = EEPROM.read(adr); | |
str += (char)(value); | |
adr++; | |
} | |
return str; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment