Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9287bf2

Browse files
WiFi scanner
1 parent 8c78e4d commit 9287bf2

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <WiFi.h>
2+
#include "eloquent.h"
3+
#include "eloquent/networking/wifi/WifiScanner.h"
4+
5+
6+
String location;
7+
8+
9+
void setup() {
10+
Serial.begin(115200);
11+
delay(2000);
12+
Serial.println("Instructions:");
13+
Serial.println("\tEnter the name of the location to start scanning");
14+
Serial.println("\tEnter 'stop' to stop scanning");
15+
}
16+
17+
void loop() {
18+
// await user to input his current location or "stop" to abort scanning
19+
if (Serial.available()) {
20+
location = Serial.readStringUntil('\n');
21+
}
22+
23+
// if a location is set, perform scan
24+
if (location != "" && location != "stop") {
25+
// expected line format is `$location : $access_points_as_json`
26+
Serial.print(location);
27+
Serial.print(": ");
28+
wifiScanner.scan();
29+
wifiScanner.printAsJson(Serial);
30+
delay(3000);
31+
}
32+
}

‎src/eloquent.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef ELOQUENTARDUINO_ELOQUENT_H
2+
#define ELOQUENTARDUINO_ELOQUENT_H
3+
4+
// entry point for the Eloquent Arduino library
5+
6+
#endif //ELOQUENTARDUINO_ELOQUENT_H
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#pragma once
2+
3+
4+
namespace Eloquent {
5+
namespace Networking {
6+
namespace Wifi {
7+
/**
8+
* Abstract implementation of WiFi scanner
9+
*/
10+
class WifiScanner {
11+
public:
12+
13+
/**
14+
* Perform scan
15+
*/
16+
void scan() {
17+
i = 0;
18+
numNetworks = WiFi.scanNetworks();
19+
}
20+
21+
/**
22+
* Reset iterator
23+
*/
24+
void reset() {
25+
i = 0;
26+
}
27+
28+
/**
29+
* Test if we can iterate over the networks
30+
* @return
31+
*/
32+
bool hasNext() {
33+
return i < numNetworks;
34+
}
35+
36+
/**
37+
* Advance iterator
38+
*/
39+
void next() {
40+
i += 1;
41+
}
42+
43+
/**
44+
* Get SID of current network
45+
* @return
46+
*/
47+
String ssid() {
48+
if (hasNext())
49+
return ssidAt(i);
50+
51+
return "";
52+
}
53+
54+
/**
55+
* Get SSID of given network
56+
* @param i
57+
* @return
58+
*/
59+
String ssidAt(uint8_t i) {
60+
return WiFi.SSID(i);
61+
}
62+
63+
/**
64+
* Get RSSI of current network
65+
* @return
66+
*/
67+
int rssi() {
68+
if (hasNext())
69+
return rssiAt(i);
70+
71+
return 0;
72+
}
73+
74+
/**
75+
* Get RSSI of given network
76+
* @param i
77+
* @return
78+
*/
79+
int rssiAt(uint8_t i) {
80+
return WiFi.RSSI(i);
81+
}
82+
83+
/**
84+
* Print scan results as JSON
85+
* @tparam Stream
86+
* @param stream
87+
*/
88+
template<typename Stream>
89+
void printAsJson(Stream& stream) {
90+
stream.print('{');
91+
92+
for (uint8_t i = 0; i < numNetworks; i++) {
93+
stream.print('"');
94+
stream.print(ssidAt(i));
95+
stream.print('"');
96+
stream.print(':');
97+
stream.print(rssiAt(i));
98+
99+
if (i < numNetworks - 1)
100+
stream.print(',');
101+
}
102+
103+
stream.print('}');
104+
stream.print('\n');
105+
}
106+
107+
protected:
108+
uint8_t i = 0;
109+
uint8_t numNetworks;
110+
};
111+
}
112+
}
113+
}
114+
115+
static Eloquent::Networking::Wifi::WifiScanner wifiScanner;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /