Created
August 4, 2014 03:58
-
-
Save anoochit/3d4aa6c94c78fd04d4d5 to your computer and use it in GitHub Desktop.
CC3000 WiFi chip & Xively
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
/*************************************************** | |
This is a sketch to use the CC3000 WiFi chip & Xively | |
Written by Marco Schwartz for Open Home Automation | |
Modified code for read temperature from LM35 | |
****************************************************/ | |
// Libraries | |
#include <Adafruit_CC3000.h> | |
#include <ccspi.h> | |
#include <SPI.h> | |
#include <avr/wdt.h> | |
// Define CC3000 chip pins | |
#define ADAFRUIT_CC3000_IRQ 3 | |
#define ADAFRUIT_CC3000_VBAT 5 | |
#define ADAFRUIT_CC3000_CS 10 | |
// LM35 sensor pin | |
#define SENSOR_PIN 0 | |
// Create CC3000 instances | |
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, | |
SPI_CLOCK_DIV2); // you can change this clock speed | |
// WLAN parameters | |
#define WLAN_SSID "YOUR-SSID" | |
#define WLAN_PASS "YOUR-WPA-PASSWORD" | |
#define WLAN_SECURITY WLAN_SEC_WPA2 | |
// Xively parameters | |
#define WEBSITE "api.xively.com" | |
#define API_key "XIVELY-API" | |
#define feedID "FEED-ID" | |
uint32_t ip; | |
void setup(void) | |
{ | |
// Initialize | |
Serial.begin(115200); | |
Serial.println(F("Initializing WiFi chip...")); | |
if (!cc3000.begin()) | |
{ | |
Serial.println(F("Couldn't begin()! Check your wiring?")); | |
while(1); | |
} | |
} | |
void loop(void) | |
{ | |
// Connect to WiFi network | |
cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); | |
Serial.println(F("Connected to WiFi network!")); | |
/* Wait for DHCP to complete */ | |
Serial.println(F("Request DHCP")); | |
while (!cc3000.checkDHCP()) | |
{ | |
delay(100); | |
} | |
// Start watchdog | |
wdt_enable(WDTO_8S); | |
ip = 0; | |
// Try looking up the website's IP address | |
while (ip == 0) { | |
if (! cc3000.getHostByName(WEBSITE, &ip)) { | |
Serial.println(F("Couldn't resolve!")); | |
} | |
delay(500); | |
} | |
float temperature = readTemperature(); | |
Serial.print("Temp -> "); | |
Serial.println(temperature); | |
// Prepare JSON for Xively & get length | |
int length = 0; | |
// JSON beginning | |
String data_start = ""; | |
data_start = data_start + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ "; | |
// JSON for temperature & humidity | |
String data_temperature = "{\"id\" : \"Temperature\",\"current_value\" : \"" + String(temperature) + "\"}]}"; | |
// Get length | |
length = data_start.length() + data_temperature.length(); | |
// Reset watchdog | |
wdt_reset(); | |
// Send request | |
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80); | |
if (client.connected()) { | |
Serial.println(F("Connected to Xively server.")); | |
// Send headers | |
Serial.print(F("Sending headers")); | |
client.fastrprint(F("PUT /v2/feeds/")); | |
client.fastrprint(feedID); | |
client.fastrprintln(F(".json HTTP/1.0")); | |
Serial.print(F(".")); | |
client.fastrprintln(F("Host: api.xively.com")); | |
Serial.print(F(".")); | |
client.fastrprint(F("X-ApiKey: ")); | |
client.fastrprintln(API_key); | |
Serial.print(F(".")); | |
client.fastrprint(F("Content-Length: ")); | |
client.println(length); | |
Serial.print(F(".")); | |
client.fastrprint(F("Connection: close")); | |
Serial.println(F(" done.")); | |
// Reset watchdog | |
wdt_reset(); | |
// Send data | |
Serial.print(F("Sending data")); | |
client.fastrprintln(F("")); | |
client.print(data_start); | |
Serial.print(F(".")); | |
wdt_reset(); | |
client.print(data_temperature); | |
Serial.print(F(".")); | |
wdt_reset(); | |
client.fastrprintln(F("")); | |
Serial.println(F(" done.")); | |
// Reset watchdog | |
wdt_reset(); | |
} else { | |
Serial.println(F("Connection failed")); | |
return; | |
} | |
// Reset watchdog | |
wdt_reset(); | |
Serial.println(F("Reading answer...")); | |
while (client.connected()) { | |
while (client.available()) { | |
char c = client.read(); | |
Serial.print(c); | |
} | |
} | |
// Reset watchdog | |
wdt_reset(); | |
// Close connection and disconnect | |
client.close(); | |
Serial.println(F("Disconnecting")); | |
Serial.println(F("")); | |
cc3000.disconnect(); | |
// Reset watchdog & disable | |
wdt_reset(); | |
wdt_disable(); | |
// Wait 10 seconds until next update | |
delay(10000); | |
} | |
// get temperature from LM35 | |
float readTemperature(void) | |
{ | |
int reading = analogRead(SENSOR_PIN); | |
float voltage= reading * 5 / 1024.0; | |
float temperatureC = (voltage - 0.5) * 100 ; | |
return temperatureC; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i am bulding this, though i want it to be connected to my web-based database and not xively.. can you teach me how to do that?