/* Rev 1, 5th Jan 2016. added timeout parameter to loadNetworkConfigFromEEPROM pfodWifiConfig AdafruitCC3000 example WiFi Web Server -- using pfodWifiConfig to configure for connection to the network To start config, short D4 to ground and then apply power. When config finished remove short to D4 and turn off and turn on again see http://www.forward.com.au/pfod/pfodWifiConfig/AdafruitCC3000/pfodWifiConfig_AdafruitCC3000.html for details For an example QR code image look in the directory this file is in. */ /** * pfodWifiConfig for Arduino Compatibles * http://www.forward.com.au/pfod/pfodWifiConfig/index.html * * (c)2015 Forward Computing and Control Pty. Ltd. * This code may be freely used for both private and commerical use. * Provide this copyright is maintained. */ #include #include #include #include "utility/debug.h" #include "utility/socket.h" #include "pfodWifiConfig.h" #include "pfodWifiConfig_AdafruitCC3000.h" #define DEBUG // 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 // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2 #define WLAN_SECURITY WLAN_SEC_WPA2 Adafruit_CC3000_Server server(80); Adafruit_CC3000_ClientRef client = NULL; pfodWifiConfig_AdafruitCC3000 pfodWifiConfig; // =============== start of pfodWifiConfigionV1 settings ============== // update this define with the password from your QR code // http://www.forward.com.au/pfod/pfodWifiConfig/pfodQRpsk.html #define pfodWifiConfigPASSWORD "plyWtEDk6uZ0yfmAEM5wMc" // the ssid is pfodWifiConfigV1 and the port is 23 -- set by pfodQRpsk program // note pfodSecurity uses 19 bytes of eeprom usually starting from 0 so // start the eeprom address from 20 for configureWifiConnect int eepromAddress = 20; int wifiSetup_pin = 4; // name the input pin for setup mode detection // =============== end of pfodWifiConfigionV1 settings ============== int LED = 6; unsigned long timer = 0; boolean timedOut = true; unsigned long timeout = 0; unsigned long CONNECTION_TIMEOUT = 2000; // close connection after 2 secs regardless void setup() { #ifdef DEBUG Serial.begin(115200); for (int i = 10; i > 0; i--) { Serial.print(i); Serial.print(' '); delay(500); } Serial.println(F("Starting Setup")); #endif pinMode(LED, OUTPUT); //starts low == off #ifdef DEBUG Serial.print(F("Free RAM: ")); Serial.println(getFreeRam(), DEC); #endif #ifdef DEBUG Serial.println(F("Hello, CC3000!\n")); #endif // pfodWifiConfig.setDebugStream(&Serial); // add this line is using DEBUG in pfodWifiConfig_AdafruitCC3000 library code //============ pfodWifiConfigV1 config ==================== // see if config button is pressed pinMode(wifiSetup_pin, INPUT_PULLUP); if (digitalRead(wifiSetup_pin) == LOW) { digitalWrite(LED, HIGH); // show we are in setup mode #ifdef DEBUG Serial.println(F("Starting pfodWifiConfigV1")); #endif // connect to temporary wifi network for setup // the features determine the format of the {set...} command uint16_t ipSources = pfodFeatures::DHCP; // bit or these together pfodFeatures::DHCP|pfodFeatures::STATIC_IP if both are available uint16_t security = pfodFeatures::WPA2; // bit or these together e.g. pfodFeatures::OPEN | pfodFeatures::WPA pfodWifiConfig.configureWifiConfig(&cc3000, eepromAddress, "pfodWifiConfigV1", pfodWifiConfigPASSWORD, 23, pfodFeatures::SERVER, security, ipSources ); // configureWifiConfig never returns. Need to reboot afterwards } //============ end pfodWifiConfigV1 config ==================== // else button was not pressed continue to load the stored network settings digitalWrite(LED, LOW); //else use configured setttings from EEPROM // use these local vars char ssid[pfodWifiConfig::MAX_SSID_LEN + 1]; // allow for null char password[pfodWifiConfig::MAX_PASSWORD_LEN + 1]; char staticIP[pfodWifiConfig::MAX_STATICIP_LEN + 1]; uint16_t portNo = 0; uint16_t security = 0; uint16_t ipSource = 0; byte mode = 0; pfodWifiConfig.loadNetworkConfigFromEEPROM(eepromAddress, &mode, (char*)ssid, pfodWifiConfig::MAX_SSID_LEN + 1, (char*)password, pfodWifiConfig::MAX_PASSWORD_LEN + 1, &security, &portNo, &ipSource, (char*)staticIP, pfodWifiConfig::MAX_STATICIP_LEN + 1, &timeout); server = Adafruit_CC3000_Server(portNo); // Initialise the module #ifdef DEBUG Serial.println(F("\nInitializing...")); #endif if (!cc3000.begin()) { #ifdef DEBUG Serial.println(F("Couldn't begin()! Check your wiring?")); #endif while (1); } #ifdef DEBUG Serial.println(F("Connecting to AP")); Serial.print("ssid '"); Serial.print(ssid); Serial.println("'"); Serial.print("password '"); Serial.print(password); Serial.println("'"); #endif if (!cc3000.connectToAP(ssid, password, 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); } // Start listening for connections #ifdef DEBUG Serial.println(F("Start Server")); #endif server.begin(); #ifdef DEBUG Serial.println(F("Server Started")); #endif Serial.println(F("Listening for connections...")); client = server.available(); } int count = 0; boolean alreadyConnected = false; boolean currentLineIsBlank = false; void loop(void) { if ((!timedOut) && ((millis() - timer) > CONNECTION_TIMEOUT)) { // if connection held for more the 3sec forcefully drop it Serial.println(F("connection held too long close it")); closeConnection(); // let someone else connect } if (!client) { // Try to get a client which is connected. client = server.available(); } // else if (client) { if (!alreadyConnected) { alreadyConnected = true; currentLineIsBlank = false; timer = millis(); // start connection timer timedOut = false; count++; digitalWrite(LED, HIGH); Serial.print(F("Got new Connection ")); Serial.println(count); } // loop here while there is data, until get blank line or timeout connection while ( (!timedOut) && ((millis() - timer) > CONNECTION_TIMEOUT)) { int c = 0; if (client.available()) { c = client.read(); //Serial.print((char)c); } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; break; // process request } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // Handle the request if found terminating blank line. if (currentLineIsBlank) { Serial.println(F("Processing request")); // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); // the connection will be closed after completion of the response client.println("Refresh: 5"); // refresh the page automatically every 5 sec client.println(); client.println(""); client.println(""); // this next line avoids second http: request for favicon.ico client.println(""); client.print("Request count is "); client.print(count); client.println("
"); client.println(""); client.println(); //client.flush(); // make sure it is sent Serial.print("sent reply count:"); Serial.println(count); delay(100); closeConnection(); // this actually flushes output to socket currentLineIsBlank = false; } } void closeConnection() { Serial.println(F("close connection ")); alreadyConnected = false; timedOut = true; digitalWrite(LED, LOW); if (!client) { return; } // else client.stop(); client = server.available(); // evaluates to false if no connection } // 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)) { #ifdef DEBUG Serial.println(F("Unable to retrieve the IP Address!\r\n")); #endif return false; } else { #ifdef DEBUG 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(); #endif return true; } }