Created
June 9, 2012 12:38
-
-
Save tacoe/2900840 to your computer and use it in GitHub Desktop.
::open behavior sketch
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
// ... | |
SoftwareSerial wifiSerial(8,9); | |
WiFly wifly; | |
// ... | |
void setupWifi() { | |
wifiSerial.begin(9600); | |
wifly.begin(&wifiSerial, NULL); | |
if (!wifly.isAssociated()) { | |
Serial.println("Joining network"); | |
wifly.setSSID(WIFISSID); | |
wifly.setPassphrase(WIFIPASSWORD); | |
wifly.enableDHCP(); | |
if (wifly.join()) { | |
Serial.println("Joined wifi network"); | |
} else { | |
Serial.println("Failed to join wifi network"); | |
reboot(); | |
} | |
} else { Serial.println("Already joined network"); } | |
wifly.setIpProtocol(WIFLY_PROTOCOL_TCP); | |
if (wifly.isConnected()) { | |
Serial.println("Old connection active. Closing"); | |
wifly.close(); | |
} | |
} | |
// ... | |
void loop() | |
{ | |
// ... | |
sendData("Temperature", temp); | |
// ... | |
} | |
// ... | |
void sendData(char *datastream, float datapoint) { | |
char buf[20]; | |
if(wifly.open(server)){ | |
wifly.print("PUT /v2/feeds/"); | |
wifly.print(FEEDID); | |
wifly.print("/datastreams/"); | |
wifly.print(datastream); | |
wifly.println(".csv HTTP/1.1"); | |
wifly.println("Host: api.cosm.com"); | |
wifly.print("X-ApiKey: "); | |
wifly.println(APIKEY); | |
wifly.print("User-Agent: "); | |
wifly.println(USERAGENT); | |
wifly.print("Content-Length: "); | |
wifly.println(snprintf(buf, 20, "%f", datapoint)); | |
wifly.println("Content-Type: text/csv"); | |
wifly.println("Connection: close"); | |
wifly.println(); | |
wifly.println(datapoint); | |
wifly.println(); | |
wifly.close(); | |
} | |
else | |
{ | |
// ... | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment