/* pfodWifiConfig LinkIt ONE 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/LinkItONE/pfodWifiConfig_LinkIt.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 #include "pfodWifiConfig.h" #include "pfodWifiConfig_LinkIt.h" #define WIFI_AUTH LWIFI_WPA #define DEBUG LWiFiServer server(80); // this portNo overridden below by config LWiFiClient client; pfodWifiConfig_LinkIt 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 = 13; unsigned long timer = 0; boolean timedOut = false; unsigned long CONNECTION_TIMEOUT = 2000; // close connection after 2 secs regardless void setup() { #ifdef DEBUG Serial.begin(115200); for (int i = 0; i < 10; i++) { delay(1000); } Serial.println(F("Starting Setup")); #endif pinMode(LED, OUTPUT); //starts low == off //pfodWifiConfig.setDebugStream(&Serial); // add this line is using DEBUG in pfodWifiConfig_LinkIt 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::WPA; // bit or these together e.g. pfodFeatures::OPEN | pfodFeatures::WPA pfodWifiConfig.configureWifiConfig(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); server = LWiFiServer(portNo); // set correct portNo LWiFi.begin(); #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 // keep retrying until connected to AP while (0 == LWiFi.connect(ssid, LWiFiLoginInfo(WIFI_AUTH, password))) { delay(1000); } #ifdef DEBUG printWifiStatus(); Serial.println(F("Start Server")); #endif server.begin(); #ifdef DEBUG Serial.println(F("Server Started")); #endif client = server.available(); // evaluates to false if no connection } int count = 0; boolean alreadyConnected = false; boolean currentLineIsBlank = true; // loop forever accepting connections void loop() { if (!client) { // see if a client is available client = server.available(); // evaluates to false if no connection } else { // have client if (!client.connected()) { if (alreadyConnected) { // client closed so clean up closeConnection(); } } else { // have connected client if (!alreadyConnected) { alreadyConnected = true; currentLineIsBlank = true; timer = millis(); // start connection timer timedOut = false; count++; #ifdef DEBUG Serial.print(F("Got new Connection")); Serial.println(count); #endif } if ((!timedOut) && ((millis() - timer) > CONNECTION_TIMEOUT)) { // if connection held for more the 2sec forcefully drop it #ifdef DEBUG Serial.println(F("connection held too long close it")); #endif closeConnection(); // let someone else connect } else { digitalWrite(LED, HIGH); // indcate we have a connection // an http request ends with a blank line while (client.available()) { // we basically ignores client request, but wait for HTTP request end int c = client.read(); #ifdef DEBUG Serial.print((char)c); #endif if (c == '\n' && currentLineIsBlank) { // 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 #ifdef DEBUG Serial.print(F("sent reply count:")); Serial.println(count); #endif closeConnection(); break; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } } } } void closeConnection() { #ifdef DEBUG Serial.println(F("close connection ")); #endif alreadyConnected = false; timedOut = true; digitalWrite(LED, LOW); if (!client) { return; } // else client.stop(); client = server.available(); // evaluates to false if no connection } void printWifiStatus() { #ifdef DEBUG // print the SSID of the network you're attached to: Serial.print(F("SSID: ")); Serial.println(LWiFi.SSID()); // print your WiFi shield's IP address: IPAddress ip = LWiFi.localIP(); Serial.print(F("IP Address: ")); Serial.println(ip); Serial.print(F("subnet mask: ")); Serial.println(LWiFi.subnetMask()); Serial.print(F("gateway IP: ")); Serial.println(LWiFi.gatewayIP()); // print the received signal strength: long rssi = LWiFi.RSSI(); Serial.print(F("signal strength (RSSI):")); Serial.print(rssi); Serial.println(F(" dBm")); #endif }