-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp_wifi_bar.ino
165 lines (144 loc) · 3.66 KB
/
esp_wifi_bar.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 2 //pin on the ESP
#define NUMPIXELS 9
#define BRIGHTNESS 64
const float fraction = NUMPIXELS / 100;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#include "ESP8266WiFi.h"
const int RSSI_MAX = -50; // define maximum strength of signal in dBm
const int RSSI_MIN = -80; // define minimum strength of signal in dBm
const int displayEnc = 1; // set to 1 to display Encryption or 0 not to display
String myssid = "";
int32_t myrssi = -999999;
int32_t rssi;
void setup() {
Serial.begin(115200);
Serial.println("********** Wifi-O-Meter ********************");
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(2000);
while (myssid == "") {
Serial.println("Wifi scan started");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("Wifi scan ended");
if (n > 0) {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(") ");
Serial.print(WiFi.SSID(i));// SSID
rssi = WiFi.RSSI(i);
Serial.print(rssi);//Signal strength in dBm
Serial.println("dBm");
if (rssi > myrssi) {
myrssi = rssi;
myssid = WiFi.SSID(i);
}
delay(10);
}
}
Serial.println("Setup done");
Serial.print("myssid= ");
Serial.println(myssid);
};
pixels.begin();
}
void loop() {
Serial.println("Wifi scan started");
myrssi = -999999;
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("Wifi scan ended");
if (n == 0) {
Serial.println("no networks found");
setLedBar(0);
} else {
//Serial.print(n);
//Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
rssi = WiFi.RSSI(i);
//Serial.print(WiFi.SSID(i));
//Serial.print(" => ");
//Serial.println(rssi);
if ((WiFi.SSID(i) == myssid) and (rssi > myrssi)) {
myrssi = rssi;
}
//delay(10);
}
}
//Serial.print("myssid=>");
//Serial.println(myrssi);
setLedBar(dBmtoPercentage(myrssi));
//Serial.println("");
// Wait a bit before scanning again
//delay(500);
WiFi.scanDelete();
}// loop
int r;
int g;
int b;
void setLedBar(float val) {
//Serial.print("setledbar:");
//Serial.println(val);
if (val > 0.95) {
r = 0;
g = 0;
b = 255;
}
else if (val > 0.6) {
r = 0;
g = 255-(255*(val-.6)*3);
b = (255*(val-.6)*3);
}
else if (val > 0.3) {
r = 255-(255*(val-.3)*3);
g = 255;
b = 0;
}
else {
r = 255;
g = (255*(val-.3)*3);
b = 0;
};
//Serial.println(NUMPIXELS * val);
for (int i = 0; i < NUMPIXELS; i++) {
//Serial.print(i);
if (i <= (NUMPIXELS * val)) {
//Serial.println("*");
pixels.setPixelColor(i, r, g, b);
}
else {
//Serial.println(".");
pixels.setPixelColor(i, 0, 0, 0);
};
};
pixels.show();
}
float dBmtoPercentage(int dBm)
{
float quality;
if (dBm <= RSSI_MIN)
{
quality = 0;
}
else if (dBm >= RSSI_MAX)
{
quality = 1;
}
else
{ //so e.g. (-51dbm --80)= 29 / (-50--80) =30
//Serial.println(float( dBm - RSSI_MIN ));
//Serial.println(float(RSSI_MAX-RSSI_MIN));
quality = float( dBm - RSSI_MIN )/float(RSSI_MAX-RSSI_MIN);
//Serial.println(quality);
}
return quality ;
}//dBmtoPercentage