#include #include #include #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 "TomahawkHOME" #define WLAN_PASS "some password" // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2 #define WLAN_SECURITY WLAN_SEC_WPA #define TIMEOUT_MS 500 // Amount of time in milliseconds to wait for // an incoming request to finish. Don't set this // too high or your server could be slow to respond. #define LISTEN_PORT 1234 // What TCP port to listen on for connections. Adafruit_CC3000_Server echoServer(LISTEN_PORT); #define BUFFER_SIZE 64 uint8_t buffer[BUFFER_SIZE+1]; #define NO_ACTION 0 #define BAD_ACTION 1 #define TOGGLE_DOOR1 2 // pin on arduino #define NUM_ACTIONS 1 typedef struct { const char* actionWord; int actionToken; } ActionArray; ActionArray actionArray[NUM_ACTIONS] { {"toggle1", TOGGLE_DOOR1} }; int parseRequest( uint8_t* buf, int bufSize); bool displayConnectionDetails(void); void setup(void) { pinMode(TOGGLE_DOOR1, OUTPUT); Serial.begin(115200); Serial.println(F("Hello, CC3000!\n")); Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC); /* 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); } /*********************************************************/ /* You can safely remove this to save some flash memory! */ /*********************************************************/ Serial.println(F("\r\nNOTE: This sketch may cause problems with other sketches")); Serial.println(F("since the .disconnect() function is never called, so the")); Serial.println(F("AP may refuse connection requests from the CC3000 until a")); Serial.println(F("timeout period passes. This is normal behaviour since")); Serial.println(F("there isn't an obvious moment to disconnect with a server.\r\n")); // Start listening for connections echoServer.begin(); Serial.println(F("Listening for connections...")); } void loop(void) { // Try to get a client which is connected. Adafruit_CC3000_ClientRef client = echoServer.available(); if (client) { Serial.println(F("Client connected.")); // Process this request until it completes or times out. // Note that this is explicitly limited to handling one request at a time! // Clear the incoming data buffer and point to the beginning of it. int bufindex = 0; memset(&buffer, 0, sizeof(buffer)); // Set a timeout for reading all the incoming data. unsigned long endtime = millis() + TIMEOUT_MS; int action = NO_ACTION; // Read all the incoming data until it can be parsed or the timeout expires. bool parsed = false; while (!parsed && (millis() < endtime) && (bufindex < BUFFER_SIZE)) { if (client.available()) { buffer[bufindex++] = client.read(); // no point checking unless we have some data action = parseRequest( buffer, bufindex ); } parsed = (action > NO_ACTION); } if( action == TOGGLE_DOOR1 ) { digitalWrite(TOGGLE_DOOR1, !digitalRead(TOGGLE_DOOR1)); delay(500); digitalWrite(TOGGLE_DOOR1, !digitalRead(TOGGLE_DOOR1)); client.fastrprintln( "action processed" ); } else { client.fastrprintln( "no matching action" ); } delay(100); Serial.println(F("Client disconnected")); client.close(); } } int parseRequest( uint8_t* buf, int bufSize ) { char* strBuff = (char*)buf; char* strEnd = strstr(strBuff, "\r\n"); if( (bufSize < 2) || (strEnd == NULL) ) return NO_ACTION; *strEnd = 0; for( int i=0;i