/* pfodWifiConfig Roving Networks WiFly example using FioV3 and WiFly XBee daughter board 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/RN_WiFly/pfodWifiConfig_WiFly.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 "pfodWifiConfig.h" #include "pfodWifiConfig_WiFly.h" //#define DEBUG pfodWifiConfig_WiFly 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 ============== const int CONFIG_LED = 7; // use D7 to indicate in config mode, only set as output after reading wifiSetup_pin LOW unsigned long timer = 0; boolean timedOut = true; unsigned long CONNECTION_TIMEOUT = 2000; // close connection after 2 secs regardless Stream *client = NULL; // the connection to WiFly &Serial1 in this case void setup() { Serial.begin(9600); Serial1.begin(9600); // UART connection to WiFly bard #ifdef DEBUG for (int i = 10; i > 0; i--) { Serial.print(i); Serial.print(' '); delay(1000); } Serial.println(F("Starting Setup")); #endif client = &Serial1; // 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) { pinMode(CONFIG_LED, OUTPUT); digitalWrite(CONFIG_LED, LOW); // 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|pfodFeatures::STATIC_IP; // 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(client,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 //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); #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 pfodWifiConfig.setupWiFi(client, ssid, password, staticIP, portNo); timedOut = true; } int count = 1; // for first one boolean currentLineIsBlank = false; void loop() { // 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(client); timedOut = true; count++; 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; } } } /* * This code forces the RN wifi Xbee module to drop the connection. * * @param io -- the stream that talks to the RN WiFly */ void closeConnection(Stream *io) { // code what ever is needed to force the link to disconnect // typically get into cmd mode and either reboot or close the link // for RN wifi Xbee module need to wait >250mS send $$$ and then wait >250mS // then send close and exit // should get EXIT back char EXIT[6] = {'E', 'X', 'I', 'T', 13, 10}; boolean foundExit = false; while (!foundExit) { delay(300); io->write('$'); io->write('$'); io->write('$'); delay(300); io->print("close\r"); delay(300); io->print("exit\r"); // wait for EXIT foundExit = io->find(EXIT, 6); } }