Created
April 25, 2014 15:46
-
-
Save dmiddlecamp/11294083 to your computer and use it in GitHub Desktop.
WifiScanner example firmware for the core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This #include statement was automatically added by the Spark IDE. | |
#include "WifiScan.h" | |
WifiScan WifiScanner; | |
uint8_t scanning = 0; | |
int startTime = 0; | |
void setup() { | |
pinMode(D7,OUTPUT); | |
Serial.begin(115200); | |
} | |
int lastScan = 0; | |
int lastStep = 0; | |
void loop() { | |
unsigned int now = millis(); | |
if ((now - lastScan) > 60000) { | |
if (!scanning) { | |
WifiScanner.startScan(); | |
} | |
scanning = 1; | |
startTime = millis(); | |
lastScan = now; | |
} | |
//auto scan | |
if ((now - lastStep) > 100) { | |
scanNext(""); | |
lastStep = now; | |
} | |
} | |
int scanNext(String command) { | |
if ((millis() - startTime) > 60000) { | |
scanning = 0; | |
} | |
if (!scanning) { | |
return 0; | |
} | |
char scanResult[220]; | |
if (!WifiScanner.Next(scanResult, 220)) { | |
Serial.println("Hit the end, stopping for now..."); | |
scanning = 0; | |
} | |
Serial.println(scanResult); | |
return scanning; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "WifiScan.h" | |
#include "application.h" | |
void parseScanResultToJson(WifiScanResults_t result, char *line); | |
void bytesToHex(unsigned char *data, int length, char* buffer); | |
void wipeStr(char *str, int len) { | |
for(int i = 0; i < len ;i++) { | |
str[i] = 0; | |
} | |
} | |
int WifiScan::startScan() { | |
const unsigned long intervalTime[16] = { 2000, 2000, 2000, 2000, 2000, | |
2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000 }; | |
wlan_ioctl_set_scan_params(4000, 20, 100, 5, 0x7FF, -120, 0, 300, | |
(unsigned long * ) &intervalTime); | |
return 0; | |
} | |
bool WifiScan::Next(char *line, uint8_t len) { | |
WifiScanResults_t scanResult; | |
wipeStr(line, len); | |
wipeStr((char *)scanResult.ssid, 32); | |
long err = wlan_ioctl_get_scan_results(0, (uint8_t* ) &scanResult); | |
if(err == 0) { | |
parseScanResultToJson(scanResult, line); | |
} | |
else { | |
const char *msg = (String("Error ") + String(err)).c_str(); | |
strncpy(line, msg, strlen(msg)); | |
} | |
if (String(line).indexOf("00:00:00:00:00:00") >= 0) { | |
return false; | |
} | |
return true; | |
} | |
/** | |
* Format a SSID Scan result into a GeoLocation API Wifi Object | |
*/ | |
void parseScanResultToJson(WifiScanResults_t result, char *line) { | |
//uint8_t idx = 0; | |
uint8_t length = (result.security_ssidlen >> 2); | |
uint8_t rssi = (result.rssi >> 1); | |
char ssidStr[32]; | |
strncpy(ssidStr, (char *)result.ssid, length); | |
ssidStr[length] = 0; | |
char bssidStr[24]; | |
bytesToHex((unsigned char *)result.bssid, 6, bssidStr); | |
//more standard geolocation format | |
//String json = "{ \"macAddress\": \"" + String(bssidStr) + "\", \"signalStrength\": "+ String(rssi) +", \"age\": 0, \"name\": \""+String(ssidStr)+"\" }"; | |
//mozilla's location format | |
String json = "{ \"key\": \"" + String(bssidStr) + "\", \"signal\": -"+ String(rssi) +" }"; | |
strncpy(line, json.c_str(), json.length()); | |
} | |
void bytesToHex(unsigned char *data, int length, char* buffer) { | |
const char * hex = "0123456789ABCDEF"; | |
int i=0, a=0; | |
for(i=0;i<length;i++) { | |
unsigned char c = data[i]; | |
buffer[a] = hex[(c>>4) & 0xF]; | |
buffer[a+1] = hex[(c) & 0xF]; | |
buffer[a+2] = ':'; | |
a+=3; | |
} | |
buffer[a-1] = 0; //null | |
buffer[a] = 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "application.h" | |
typedef struct WifiScanResults { | |
uint32_t networks; | |
uint32_t status; | |
uint8_t rssi; | |
uint8_t security_ssidlen; | |
uint16_t time; | |
uint8_t ssid[32]; | |
uint8_t bssid[6]; | |
} WifiScanResults_t; | |
class WifiScan { | |
public: | |
int startScan(); | |
bool Next(char *line, uint8_t len); | |
}; | |
void wipeStr(char *str, int len); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment