Last active
August 29, 2015 14:27
-
-
Save dwblair/d290c91c1c39a94d1c25 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
/*************************************************** | |
Adafruit CC3000 Breakout/Shield TCP Chat Server | |
This is a simple chat server which allows clients to connect | |
with telnet and exchange messages. Anything sent by one | |
client will be written out to all connected clients. | |
See the CC3000 tutorial on Adafruit's learning system | |
for more information on setting up and using the | |
CC3000: | |
http://learn.adafruit.com/adafruit-cc3000-wifi | |
Requirements: | |
This sketch requires the Adafruit CC3000 library. You can | |
download the library from: | |
https://github.com/adafruit/Adafruit_CC3000_Library | |
For information on installing libraries in the Arduino IDE | |
see this page: | |
http://arduino.cc/en/Guide/Libraries | |
Usage: | |
Update the SSID and, if necessary, the CC3000 hardware pin | |
information below, then run the sketch and check the | |
output of the serial port. After connecting to the | |
wireless network successfully the sketch will output | |
the IP address of the server and start listening for | |
connections. Once listening for connections, connect | |
to the server from your computer using a telnet client | |
on port 23. | |
For example on Linux or Mac OSX, if your CC3000 has an | |
IP address 192.168.1.100 you would execute in a command | |
window: | |
telnet 192.168.1.100 23 | |
Connect multiple clients and notice that whatever one client | |
sends will be echoed to all other clients. Press ctrl-] and | |
type quit at the prompt to close the telnet session. | |
On Windows you'll need to download a telnet client. PuTTY | |
is a good, free GUI client: | |
http://www.chiark.greenend.org.uk/~sgtatham/putty/ | |
License: | |
This example is copyright (c) 2013 Tony DiCola ([email protected]) | |
and is released under an open source MIT license. See details at: | |
http://opensource.org/licenses/MIT | |
This code was adapted from Adafruit CC3000 library example | |
code which has the following license: | |
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 | |
****************************************************/ | |
#include <Adafruit_CC3000.h> | |
#include <SPI.h> | |
#include "utility/debug.h" | |
#include "utility/socket.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 | |
#define WLAN_SSID "MIT" // cannot be longer than 32 characters! | |
#define WLAN_PASS "myPassword" | |
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2 | |
#define WLAN_SECURITY WLAN_SEC_UNSEC | |
#define LISTEN_PORT 23 // What TCP port to listen on for connections. | |
void setup(void) | |
{ | |
Serial.begin(115200); | |
Serial.println(F("Hello, CC3000!\n")); | |
Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC); | |
pinMode(9, OUTPUT); | |
} | |
void loop(void) | |
{ | |
Adafruit_CC3000_Server chatServer(LISTEN_PORT); | |
Adafruit_CC3000_Client client; | |
char server[] = "data.sparkfun.com"; // name address for data.sparkFun (using DNS) | |
///////////////// | |
// Phant Stuff // | |
///////////////// | |
const String publicKey = "yAYowXLMgOFDMqyMmJzM"; | |
const String privateKey = "4WqazJv0YrTdwgKwzG2w"; | |
const byte NUM_FIELDS = 1; | |
const String fieldNames[NUM_FIELDS] = {"temp"}; | |
String fieldData[NUM_FIELDS]; | |
/* 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!")); | |
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); | |
} | |
float temp = 20 + random(10); | |
fieldData[0] = String(temp); | |
digitalWrite(9, HIGH); // turn the LED on (HIGH is the voltage level) | |
delay(100); // wait for a second | |
digitalWrite(9, LOW); // turn the LED off by making the voltage LOW | |
delay(100); | |
// Make a TCP connection to remote host | |
if (client.connect(server, 80)) | |
{ | |
// Post the data! Request should look a little something like: | |
// GET /input/publicKey?private_key=privateKey&light=1024&switch=0&name=Jim HTTP/1.1\n | |
// Host: data.sparkfun.com\n | |
// Connection: close\n | |
// \n | |
client.print("GET /input/"); | |
client.print(publicKey); | |
client.print("?private_key="); | |
client.print(privateKey); | |
for (int i=0; i<NUM_FIELDS; i++) | |
{ | |
client.print("&"); | |
client.print(fieldNames[i]); | |
client.print("="); | |
client.print(fieldData[i]); | |
} | |
client.println(" HTTP/1.1"); | |
client.print("Host: "); | |
client.println(server); | |
client.println("Connection: close"); | |
client.println(); | |
} | |
else | |
{ | |
Serial.println(F("Connection failed")); | |
} | |
// Check for a response from the server, and route it | |
// out the serial port. | |
while (client.connected()) | |
{ | |
if ( client.available() ) | |
{ | |
char c = client.read(); | |
Serial.print(c); | |
} | |
} | |
Serial.println(); | |
client.stop(); | |
cc3000.disconnect(); | |
Serial.println("Sleeping ..."); | |
delay(60000); | |
} | |
/**************************************************************************/ | |
/*! | |
@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