Created
January 6, 2014 13:38
-
-
Save AdamMagaluk/8282999 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
#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_DIV2); // you can change this clock speed | |
#define WLAN_SSID "Kulagam" // cannot be longer than 32 characters! | |
#define WLAN_PASS "muskrat!" | |
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2 | |
#define WLAN_SECURITY WLAN_SEC_WPA2 | |
#define LISTEN_PORT 7000 // What TCP port to listen on for connections. The echo protocol uses port 7. | |
Adafruit_CC3000_Server server(LISTEN_PORT); | |
bool networkReady = false; | |
unsigned long tNetworkStart; | |
bool lastLedState = false; | |
#define LED_PIN 2 | |
void setup(void) | |
{ | |
Serial.begin(115200); | |
Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC); | |
pinMode(LED_PIN, OUTPUT); | |
/* Initialise the module */ | |
if (!cc3000.begin()) | |
{ | |
Serial.println(F("Couldn't begin()! Check your wiring?")); | |
while(1); | |
} | |
} | |
void loop(void) | |
{ | |
digitalWrite(LED_PIN, !lastLedState); | |
lastLedState = !lastLedState; | |
wifi_state(); | |
/* | |
if(millis()-tNetworkStart > 30000){ | |
Serial.println("Shutting down network its been too long."); | |
cc3000.disconnect(); | |
tNetworkStart = millis(); | |
} | |
*/ | |
delay(100); | |
} | |
void wifi_state(){ | |
status_t status = cc3000.getStatus(); | |
Serial.print(millis()); | |
Serial.print(" State:"); | |
Serial.println(status); | |
switch(status){ | |
case STATUS_DISCONNECTED: | |
wifi_connect(); | |
break; | |
case STATUS_SCANNING: | |
break; | |
case STATUS_CONNECTING: | |
break; | |
case STATUS_CONNECTED: | |
check_network(); | |
break; | |
}; | |
} | |
void wifi_connect(){ | |
//digitalWrite(LED_PIN, LOW); | |
if(networkReady){ | |
Serial.print(F("Network Failed! - ")); | |
Serial.println(millis()-tNetworkStart); | |
//server.close(); | |
} | |
networkReady = false; | |
cc3000.disconnect(); | |
cc3000.connectSecure(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); | |
} | |
void check_network(){ | |
if(!networkReady){ | |
if(cc3000.checkDHCP()){ | |
tNetworkStart = millis(); | |
Serial.println(F("Network Became Alive!")); | |
displayConnectionDetails(); | |
server.begin(); | |
//digitalWrite(LED_PIN, HIGH); | |
networkReady = true; | |
} | |
} | |
} | |
/**************************************************************************/ | |
/*! | |
@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("\n\rIP Addr: ")); cc3000.printIPdotsRev(ipAddress); | |
Serial.print(F("\n\rNetmask: ")); cc3000.printIPdotsRev(netmask); | |
Serial.print(F("\n\rGateway: ")); cc3000.printIPdotsRev(gateway); | |
Serial.print(F("\n\rDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv); | |
Serial.print(F("\n\rDNSserv: ")); 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