Last active
August 29, 2015 14:23
-
-
Save AgustinPelaez/2e0750971c2825d706e3 to your computer and use it in GitHub Desktop.
A test code for the Adafruit CC3000 breakout, based on their "WebClient.ino" example at: https://github.com/adafruit/Adafruit_CC3000_Library
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
/*************************************************** | |
This is an example for the Adafruit CC3000 Wifi Breakout & Shield | |
Designed specifically to work with the Adafruit WiFi products: | |
----> https://www.adafruit.com/products/1469 | |
Adafruit invests time and resources providing this open source code, | |
please support Adafruit and open-source hardware by purchasing | |
products from Adafruit! | |
Written by Limor Fried & Kevin Townsend for Adafruit Industries. | |
BSD license, all text above must be included in any redistribution | |
****************************************************/ | |
//Modified by Agustin Pelaez on Jun 15 2015 for Ubidots, Inc. | |
#include <Adafruit_CC3000.h> | |
#include <ccspi.h> | |
#include <SPI.h> | |
#include <string.h> | |
#include "utility/debug.h" | |
// These are the interrupt and control pins | |
#define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin! | |
// These can be any two pins | |
#define ADAFRUIT_CC3000_VBAT 5 | |
#define ADAFRUIT_CC3000_CS 10 | |
// Use hardware SPI for the remaining pins | |
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11 | |
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, | |
SPI_CLOCK_DIVIDER); // you can change this clock speed | |
Adafruit_CC3000_Client www; | |
#define WLAN_SSID "xxxxxx" // cannot be longer than 32 characters! | |
#define WLAN_PASS "xxxxxx" | |
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2 | |
#define WLAN_SECURITY WLAN_SEC_WPA2 | |
// Ubidots information: | |
#define WEBSITE "things.ubidots.com" | |
char* token = "YOUR-UBIDOTS-TOKEN"; | |
char* idvariable = "YOUR-VARIABLE-ID"; | |
/**************************************************************************/ | |
/*! | |
@brief Sets up the HW and the CC3000 module (called automatically | |
on startup) | |
*/ | |
/**************************************************************************/ | |
uint32_t ip; | |
void setup(void) | |
{ | |
Serial.begin(115200); | |
Serial.println("Hello, CC3000!\n"); | |
/* Initialise the module */ | |
Serial.println(F("\nInitializing...")); | |
if (!cc3000.begin()) | |
{ | |
Serial.println(F("Couldn't begin()! Check your wiring?")); | |
while(1); | |
} | |
Serial.print(F("\nAttempting to connect to ")); Serial.println(WLAN_SSID); | |
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { | |
Serial.println(F("Failed!")); | |
while(1); | |
} | |
Serial.println(F("Connected!")); | |
/* Wait for DHCP to complete */ | |
Serial.println(F("Request DHCP")); | |
while (!cc3000.checkDHCP()) | |
{ | |
delay(100); // ToDo: Insert a DHCP timeout! | |
} | |
/* Display the IP address DNS, Gateway, etc. */ | |
while (! displayConnectionDetails()) { | |
delay(1000); | |
} | |
Serial.print(F("Checking WiFi connection ...")); | |
if(!cc3000.checkConnected()){while(1){}} | |
Serial.println(F("done.")); | |
} | |
void connect_socket() | |
{ | |
ip = 0; | |
// Try looking up the website's IP address | |
Serial.println(F("connecting socket")); | |
while (ip == 0) { | |
if (! cc3000.getHostByName(WEBSITE, &ip)) { | |
Serial.println(F("Couldn't resolve IP!")); | |
} | |
delay(1000); | |
} | |
} | |
void loop(void) | |
{ | |
save_value(String(analogRead(A0))); | |
delay(2000); | |
} | |
void save_value(String value) | |
{ | |
connect_socket(); | |
www = cc3000.connectTCP(ip, 80); | |
if(!(www.available())) | |
{ | |
delay(500); | |
} | |
Serial.println(("TCP connection established.")); | |
String var = "{\"value\":"+ value + "}"; | |
if (www.connected()) { | |
Serial.print(F("\nSending data to ubidots ...")); | |
www.println("POST /api/v1.6/variables/"); | |
www.print(idvariable); | |
www.println("/values HTTP/1.1"); | |
www.println("Content-Type: application/json"); | |
www.print("Content-Length: "); | |
www.println(String(var.length())); // The length of the body of the http request | |
www.print("X-Auth-Token: "); | |
www.println(token); | |
www.println("Host: things.ubidots.com"); | |
www.println(); | |
www.println(var); // The JSON formatted body: {"value":xx} | |
www.println(); | |
} | |
else { | |
Serial.println(F("Connection failed")); | |
} | |
// Read the response | |
while (www.available()) { | |
char c = www.read(); | |
Serial.print(c); | |
} | |
www.close(); | |
} | |
/**************************************************************************/ | |
/*! | |
@brief Tries to read the IP address and other connection details | |
*/ | |
/**************************************************************************/ | |
bool displayConnectionDetails(void) | |
{ | |
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv; | |
if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) | |
{ | |
Serial.println(F("Unable to retrieve the IP Address!\r\n")); | |
return false; | |
} | |
else | |
{ | |
Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress); | |
Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask); | |
Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway); | |
Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv); | |
Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv); | |
Serial.println(); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment