Hi, how do I get 300 RSSI samples from my esp8266 connected to a network? I need to average the 300 sample values for data. I tried this but it gives me a large amount of number
void setup() {
Serial.begin(115200);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
int test = 300;
int rssi = WiFi.RSSI();
for (int i=0;i<300;i++)
{
int average = rssi + average;
Serial.println(average);
}
}
redlop:
Hi, how do I get 300 RSSI samples from my esp8266 connected to a network? I need to average the 300 sample values for data. I tried this but it gives me a large amount of number
for (int i=0;i<300;i++)
{
int average = rssi + average;
Serial.println(average);
}
That code declares a new variable called "average" every time the loop body executes, which is definitely not what you want. It then goes away at the end of the loop body, and next time a new variable is created, only to be destroyed again... You need to be clear about the difference between declaring a variable (which brings it into existence), and assigning to a variable (which changes its value).
Many variables of the same name can exist at one, due to shadowing of names, be careful to understand this.
Here in the declaration of "average" in the loop body reference is made to the outer variable of the same name.
On the RHS of a declaration there can be no reference to the variable being declared as it doesn't exist at that point.
gcjr has given you code that will fix your issue, but you should try to understand why your code couldn't work as you hoped...
to get an average you would add up 300 results. then divide by 300 when your done.
I would use a long variable type because it can store a larger number.
long average = 0;
for (int i=0;i<300;i++)
{
average = average +WiFi.RSSI();
}
average = long(average / 300);
Serial.println(average);
}