IoT-enabled Appointment Device
#include "Arduino.h"
#define DEBUG_ON
#define DEBUG_PRINT(receivedResponse) if(strcmp(receivedResponse, "") != 0){ Serial.print("\nWiFly Response: "); Serial.println(receivedResponse);}
#define BAUDRATE 19200
#define MAX_RESPONSE_LENGTH 250
#define NUMBER_SAVED_SSID 1
#define NETWORKS_SCAN_TIMEOUT 6000
#define WIFLY_RX 3
#define WIFLY_TX 4
// Functions
void initWifly();
boolean waitResponseFromWiFly();
boolean compareResponseFromWiFly(char *receivedResponse, const char *expectedResponse);
boolean enterWiFlyCommandMode();
boolean exitWiFlyCommandMode();
void sendSetCommandToWiFly(char *command);
void joinNetworkUsingWiFly(char *network);
boolean checkWiFlyConnection();
boolean scanNetworksUsingWiFly();
void connectToNetworkUsingWiFly();
// google.com
//byte server[] = { 66, 249, 89, 104 };
// httpbin.org
//byte server[] = { 202, 90, 132, 173 };
// STRIDE-ADMIN Network
//byte server[] = { 172, 16, 14, 55 };
WiFlyClient client;
SoftwareSerial serialWiFly = SoftwareSerial(WIFLY_RX, WIFLY_TX);
char wiFlyResponse[MAX_RESPONSE_LENGTH] = "";
//char SAVED_SSID[NUMBER_SAVED_SSID][13] = {"StrideBSN_AP"};
//char SAVED_PASSWORD[NUMBER_SAVED_SSID][15] = {"f0r4rdu1n00nly"};
//char SAVED_SSID[NUMBER_SAVED_SSID][21] = { "STRIDE-ADMIN_Network" };
//char SAVED_PASSWORD[NUMBER_SAVED_SSID][15] = { "50314b3154" };
//char SAVED_SSID[NUMBER_SAVED_SSID][21] = { "Squat" };
//char SAVED_PASSWORD[NUMBER_SAVED_SSID][15] = { "Jer291113" };
char SAVED_SSID[NUMBER_SAVED_SSID][21] = { "rdp" };
char SAVED_PASSWORD[NUMBER_SAVED_SSID][15] = { "12345678" };
char tempCommand[40];
char charToString[2] = { ' ', '\0'};
boolean isConnected = false;
char *stringPointer;
char *stringPointer1;
char *networkSsidPointer;
boolean returnValue = false;
unsigned short networksCompareCount = 0;
unsigned short tokenCount = 0;
unsigned short savedNetworksCount = 0;
unsigned int scannedNetworksCount = 0;
unsigned long waitStartTime;
unsigned long waitDurationTime;
void initWifly() {
// WiFly Software UART
serialWiFly.begin(BAUDRATE);
WiFly.setUart(&serialWiFly);
Serial.flush();
serialWiFly.flush();
Serial.read();
serialWiFly.read();
}
boolean waitResponseFromWiFly() {
serialWiFly.listen();
while(serialWiFly.available() > 0) {
charToString[0] = serialWiFly.read();
strcat(wiFlyResponse, charToString);
#ifdef DEBUG_ON
Serial.println("Waiting response from WiFly...");
#endif
}
}
boolean compareResponseFromWiFly(char *receivedResponse, const char *expectedResponse) {
stringPointer = strstr(receivedResponse, expectedResponse);
if(stringPointer != NULL)
return true;
else
return false;
}
boolean enterWiFlyCommandMode() {
returnValue = false;
serialWiFly.write("$$$");
delay(500);
strcpy(wiFlyResponse, "");
//Serial.print(wiFlyResponse);
serialWiFly.write("ver\r");
delay(500);
waitResponseFromWiFly();
#ifdef DEBUG_ON
DEBUG_PRINT(wiFlyResponse);
#endif
returnValue = compareResponseFromWiFly(wiFlyResponse, "<4.00>");
strcpy(wiFlyResponse, "");
#ifdef DEBUG_ON
if(returnValue)
Serial.println("WiFly Command Mode\n");
#endif
return returnValue;
}
boolean exitWiFlyCommandMode() {
returnValue = false;
serialWiFly.write("exit\r");
delay(500);
waitResponseFromWiFly();
#ifdef DEBUG_ON
DEBUG_PRINT(wiFlyResponse);
#endif
returnValue = compareResponseFromWiFly(wiFlyResponse, "EXIT");
strcpy(wiFlyResponse, "");
#ifdef DEBUG_ON
if(returnValue)
Serial.println("WiFly Data Mode\n");
#endif
return returnValue;
}
void sendSetCommandToWiFly(char *setCommand) {
//char *temp;
while(!enterWiFlyCommandMode());
returnValue = false;
//strcpy(temp, setCommand);
//strcat(temp, "\r");
while(!returnValue) {
#ifdef DEBUG_ON
Serial.print("Command: ");
Serial.println(setCommand);
//Serial.println(temp);
#endif
serialWiFly.write(setCommand);
//serialWiFly.write(temp);
delay(600);
waitResponseFromWiFly();
#ifdef DEBUG_ON
DEBUG_PRINT(wiFlyResponse);
#endif
returnValue = compareResponseFromWiFly(wiFlyResponse, "AOK");
strcpy(wiFlyResponse, "");
//delay(500);
//serialWiFly.flush();
}
//delay(1000);
returnValue = false;
#ifdef DEBUG_ON
Serial.println("Saving...\n");
#endif
while(!returnValue) {
serialWiFly.write("save\r");
delay(300);
waitResponseFromWiFly();
#ifdef DEBUG_ON
DEBUG_PRINT(wiFlyResponse);
#endif
returnValue = compareResponseFromWiFly(wiFlyResponse, "Storing");
strcpy(wiFlyResponse, "");
}
while(!exitWiFlyCommandMode());
}
void joinNetworkUsingWiFly(char *network) {
while(!enterWiFlyCommandMode());
strcpy(tempCommand, "join ");
strcat(tempCommand, network);
strcat(tempCommand, "\r");
#ifdef DEBUG_ON
Serial.print("Command: ");
//Serial.println(strcat("join ", network));
Serial.println(tempCommand);
#endif
serialWiFly.write(tempCommand);
delay(500);
waitResponseFromWiFly();
#ifdef DEBUG_ON
DEBUG_PRINT(wiFlyResponse);
Serial.println("Joining network...");
#endif
strcpy(wiFlyResponse, "");
while(serialWiFly.available() > 0) {
serialWiFly.read();
}
while(!exitWiFlyCommandMode());
}
boolean checkWiFlyConnection() {
//boolean wiFlyConnected = false;
while(!enterWiFlyCommandMode());
//returnValue = false;
serialWiFly.write("show c\r");
delay(300);
waitResponseFromWiFly();
#ifdef DEBUG_ON
DEBUG_PRINT(wiFlyResponse);
#endif
if(wiFlyResponse[2] == '3') {
//while(!exitWiFlyCommandMode());
//returnValue = true;
#ifdef DEBUG_ON
Serial.println("WiFly is associated!\n");
#endif
strcpy(wiFlyResponse, "");
while(!exitWiFlyCommandMode());
return true;
}
strcpy(wiFlyResponse, "");
while(!exitWiFlyCommandMode());
//returnValue = false;
return false;
}
boolean scanNetworksUsingWiFly() {
networksCompareCount = 0;
tokenCount = 0;
savedNetworksCount = 0;
scannedNetworksCount = 0;
waitStartTime = 0;
waitDurationTime = 0;
while(!enterWiFlyCommandMode());
serialWiFly.write("scan\r");
delay(500);
serialWiFly.listen();
waitStartTime = millis();
while(1) {
if(strlen(wiFlyResponse) < MAX_RESPONSE_LENGTH - 10) {
if(serialWiFly.available() > 0) {
charToString[0] = serialWiFly.read();
strcat(wiFlyResponse, charToString);
}
waitDurationTime = millis() - waitStartTime;
if(waitDurationTime > NETWORKS_SCAN_TIMEOUT) {
#ifdef DEBUG_ON
DEBUG_PRINT(wiFlyResponse);
#endif
break;
}
} else {
#ifdef DEBUG_ON
DEBUG_PRINT(wiFlyResponse);
#endif
break;
}
}
while(serialWiFly.available() > 0) {
serialWiFly.read();
}
if(wiFlyResponse[23] == '\r')
scannedNetworksCount = wiFlyResponse[22] - '0';
else
scannedNetworksCount = ((wiFlyResponse[22] - '0') * 10 ) + (wiFlyResponse[23] - '0');
#ifdef DEBUG_ON
Serial.print("Number of Networks Found: ");
Serial.println(scannedNetworksCount);
Serial.println();
#endif
if(scannedNetworksCount == 0) {
#ifdef DEBUG_ON
Serial.println("No network match found.");
Serial.println("Networks Scan Finished\n");
#endif
strcpy(wiFlyResponse, "");
while(!exitWiFlyCommandMode());
return false;
}
stringPointer = strstr(wiFlyResponse, "SCAN");
stringPointer1 = stringPointer;
stringPointer = strchr((char *)(stringPointer1), '\n');
if(stringPointer != NULL) {
*stringPointer = 0;
}
// Access first line of scanned networks.
while(networksCompareCount < scannedNetworksCount) {
stringPointer1 = stringPointer + 1;
stringPointer = strchr((char *)(stringPointer1), '\n');
if(stringPointer != NULL) {
*stringPointer = 0;
// Access SSID of each scanned network.
networkSsidPointer = strtok((char *)stringPointer1,",\r\n");
while (networkSsidPointer != NULL) {
tokenCount++;
if(tokenCount == 9) {
//scannedNetwork = networkSsidPointer;
//strcpy(scannedNetwork, networkSsidPointer);
#ifdef DEBUG_ON
Serial.print("Scanned Network ");
Serial.print(networksCompareCount + 1);
Serial.print(": ");
Serial.println((char *) networkSsidPointer);
#endif
while(savedNetworksCount < NUMBER_SAVED_SSID) {
#ifdef DEBUG_ON
Serial.print("Network Compare: ");
Serial.println(SAVED_SSID[savedNetworksCount]);
#endif
// Scanned network matched on of the known networks.
if(strcmp(networkSsidPointer, SAVED_SSID[savedNetworksCount]) == 0) {
//strcpy(matchedPASSWORD, SAVED_PASSWORD[savedNetworksCount]);
//savedNetworksCount = 0;
#ifdef DEBUG_ON
Serial.println("Network match found.");
Serial.print("Matched SSID: ");
Serial.println(SAVED_SSID[savedNetworksCount]);
Serial.print("Matched PASSWORD: ");
Serial.println(SAVED_PASSWORD[savedNetworksCount]);
Serial.println("Networks Scan Finished\n");
#endif
strcpy(wiFlyResponse, "");
while(!exitWiFlyCommandMode());
return true;
}
savedNetworksCount++;
}
tokenCount = 0;
savedNetworksCount = 0;
}
networkSsidPointer = strtok(NULL, ",\r\n");
}
tokenCount = 0;
}
networksCompareCount++;
}
#ifdef DEBUG_ON
Serial.println("No network match found.");
Serial.println("Networks Scan Finished\n");
#endif
strcpy(wiFlyResponse, "");
while(!exitWiFlyCommandMode());
// No networks found.
return false;
}
void connectToNetworkUsingWiFly() {
//while(!scanNetworksUsingWiFly());
sendSetCommandToWiFly("set ip dhcp 1\r");
sendSetCommandToWiFly("set wlan join 0\r");
//sendSetCommandToWiFly("set wlan auth 8\r");
sendSetCommandToWiFly("set wlan auth 4\r");
// Set SSID.
strcpy(tempCommand, "set wlan ssid ");
strcat(tempCommand, SAVED_SSID[savedNetworksCount]);
strcat(tempCommand, "\r");
#ifdef DEBUG_ON
Serial.print("Set SSID Command: ");
Serial.println(tempCommand);
#endif
sendSetCommandToWiFly(tempCommand);
// Set PASSWORD.
//strcpy(tempCommand, "set wlan key ");
strcpy(tempCommand, "set wlan passphrase ");
strcat(tempCommand, SAVED_PASSWORD[savedNetworksCount]);
strcat(tempCommand, "\r");
#ifdef DEBUG_ON
Serial.print("Set PASSWORD Command: ");
Serial.println(tempCommand);
#endif
sendSetCommandToWiFly(tempCommand);
while(!isConnected) {
joinNetworkUsingWiFly(SAVED_SSID[savedNetworksCount]);
isConnected = checkWiFlyConnection();
}
}