143 lines
4.2 KiB
C++
143 lines
4.2 KiB
C++
/****************************************************************
|
|
WebClient.ino
|
|
CC3000 WebClient Test
|
|
Shawn Hymel @ SparkFun Electronics
|
|
March 1, 2014
|
|
https://github.com/sparkfun/SFE_CC3000_Library
|
|
|
|
Manually connects to a WiFi network and performs an HTTP GET
|
|
request on a web page. Prints the contents of the page to
|
|
the serial console.
|
|
|
|
The security mode is defined by one of the following:
|
|
WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA, WLAN_SEC_WPA2
|
|
|
|
Hardware Connections:
|
|
|
|
Uno Pin CC3000 Board Function
|
|
|
|
+5V VCC or +5V 5V
|
|
GND GND GND
|
|
2 INT Interrupt
|
|
7 EN WiFi Enable
|
|
10 CS SPI Chip Select
|
|
11 MOSI SPI MOSI
|
|
12 MISO SPI MISO
|
|
13 SCK SPI Clock
|
|
|
|
Resources:
|
|
Include SPI.h, SFE_CC3000.h, and SFE_CC3000_Client.h
|
|
|
|
Development environment specifics:
|
|
Written in Arduino 1.0.5
|
|
Tested with Arduino UNO R3
|
|
|
|
This code is beerware; if you see me (or any other SparkFun
|
|
employee) at the local, and you've found our code helpful, please
|
|
buy us a round!
|
|
|
|
Distributed as-is; no warranty is given.
|
|
****************************************************************/
|
|
|
|
#include <SPI.h>
|
|
#include "SFE_CC3000.h"
|
|
#include "SFE_CC3000_Client.h"
|
|
|
|
// Pins
|
|
#define CC3000_INT 2 // Needs to be an interrupt pin (D2/D3)
|
|
#define CC3000_EN 7 // Can be any digital pin
|
|
#define CC3000_CS 10 // Preferred is pin 10 on Uno
|
|
|
|
// Connection info data lengths
|
|
#define IP_ADDR_LEN 4 // Length of IP address in bytes
|
|
|
|
// Constants
|
|
char ap_ssid[] = "hangar_lab"; // SSID of network
|
|
char ap_password[] = "labinteractius"; // Password of network
|
|
unsigned int ap_security = WLAN_SEC_WPA2; // Security of network
|
|
unsigned int timeout = 1000; // Milliseconds
|
|
char server[] = "data.smartcitizen.me"; // Remote host site
|
|
|
|
// Global Variables
|
|
SFE_CC3000 wifi = SFE_CC3000(CC3000_INT, CC3000_EN, CC3000_CS);
|
|
SFE_CC3000_Client client = SFE_CC3000_Client(wifi);
|
|
|
|
void setup() {
|
|
|
|
ConnectionInfo connection_info;
|
|
int i;
|
|
delay(2000);
|
|
// Initialize Serial port
|
|
Serial.begin(115200);
|
|
Serial.println();
|
|
Serial.println("---------------------------");
|
|
Serial.println("SparkFun CC3000 - WebClient");
|
|
Serial.println("---------------------------");
|
|
|
|
// Initialize CC3000 (configure SPI communications)
|
|
if ( wifi.init() ) {
|
|
Serial.println("CC3000 initialization complete");
|
|
} else {
|
|
Serial.println("Something went wrong during CC3000 init!");
|
|
}
|
|
|
|
// Connect using DHCP
|
|
Serial.print("Connecting to SSID: ");
|
|
Serial.println(ap_ssid);
|
|
if(!wifi.connect(ap_ssid, ap_security, ap_password, timeout)) {
|
|
Serial.println("Error: Could not connect to AP");
|
|
}
|
|
|
|
// Gather connection details and print IP address
|
|
if ( !wifi.getConnectionInfo(connection_info) ) {
|
|
Serial.println("Error: Could not obtain connection details");
|
|
} else {
|
|
Serial.print("IP Address: ");
|
|
for (i = 0; i < IP_ADDR_LEN; i++) {
|
|
Serial.print(connection_info.ip_address[i]);
|
|
if ( i < IP_ADDR_LEN - 1 ) {
|
|
Serial.print(".");
|
|
}
|
|
}
|
|
Serial.println();
|
|
}
|
|
|
|
}
|
|
|
|
void loop() {
|
|
|
|
if ( !client.connect(server, 80) ) {
|
|
Serial.println("Error: Could not make a TCP connection");
|
|
}
|
|
else
|
|
{
|
|
// Make a HTTP GET request
|
|
client.print("GET /datetime HTTP/1.1 \n");
|
|
client.print("Host: ");
|
|
client.println(server);
|
|
client.print("User-Agent: SmartCitizen \n\n");
|
|
// client.println("Connection: close");
|
|
// client.println();
|
|
Serial.println("ei");
|
|
|
|
// If there are incoming bytes, print them
|
|
unsigned long time = millis();
|
|
while ( client.available()&&((millis()-time)<1000) ) {
|
|
char c = client.read();
|
|
Serial.print(c);
|
|
}
|
|
if ( !client.close() ) {
|
|
Serial.println("Error: Could not close socket");
|
|
}
|
|
// If the server has disconnected, stop the client and wifi
|
|
// if ( !client.connected() ) {
|
|
// Serial.println();
|
|
// // Close socket
|
|
// if ( !client.close() ) {
|
|
// Serial.println("Error: Could not close socket");
|
|
// }
|
|
// }
|
|
}
|
|
delay(1000);
|
|
}
|