Wireless Network Signal Strength with ESP32 with Arduino IDE

This example shows how to use the wireless network functionality of ESP32 to post data to ThingSpeak™. The ESP32 records and posts the signal strength of the wireless network that it is connected to. Press a button on the board to take three measurements of the wireless network signal strength and post the average result to a ThingSpeak channel. The device also posts a counter value so you can track measurements.

You can generate a heatmap overlay image to represent wireless signal strengths. This image shows wireless signal strengths super imposed over an office floorplan and the data used to generate the heatmap. To generate the heatmap overlay, see Create Heatmap Overlay Image.

Prerequisites

ESP32 Arduino Core and IDE Setup

First, set up your Arduino Core for ESP32. For instrutions, see Installation Instructions for Arduino Core for ESP32. You can test your Arduino® setup on the ESP32 using the “Blink” example sketch in File > Examples > 01.Basics. Define the LED_BUILTIN pin as pin 5 to use the onboard LED.

ThingSpeak Setup

To use ThingSpeak , you must have a user account and a created channel. Each channel has up to eight data fields, three location fields, and a status field. If you have a free account, you can send data to ThingSpeak every 15 seconds.

1) Sign up for new account as shown in Sign up for ThingSpeak.

2) Create a channel by selecting Channels > My Channels > New Channel.

3) Enable Field 1 and Field 2.

4) Enter RSSI as the Field 1 name, and Counter as the Field 2 name.

5) Name the channel. For example, ESP32 Signal Strength.

6) Save your channel.

7) Note the write API key on the API Keys tab.

Schematic and Connections

To complete this example, you need only devices that are built in to the ESP32 development kit provided by Sparkfun. However, using a portable micro-USB compatible battery can make your device portable for easier measurements.

Program the ESP32

Use the Arduino IDE to create an application for your device.

1) Connect the ESP32 to your computer using a micro-USB cable, and wait for it to connect successfully.

2) In the Arduino IDE select the ESP32 Dev Module board and the correct COM port.

3) Create the application. Open a new window in the Arduino IDE and save the file. Add the code provided in the Code section. Edit the wireless network SSID, password, and the write API key for your channel.

To visualize your results, you can generate the overlay image of an office map by using wireless network strengths with ThingSpeak. Since the simplified hardware records only signal strength, you must manually record the coordinates for each point. You can use most graphic editing programs to find the pixel coordinates on an image. For details on generating a heatmap, see Create Heatmap Overlay Image.

Code

1) Start by defining libraries, definitions, and global variables. Enter your wireless network SSID and password and the write API key for your channel.

#include <WiFi.h>

#define buttonPin 0 
#define LEDPin 5

// Network information
char* ssid = "WIFINAME";
const char* password = "PPPPPPPPP";

// ThingSpeak settings
char server[] = "api.thingspeak.com";
String writeAPIKey = "XXXXXXXXXXXXXXXX";

// Constants
const unsigned long postingInterval = 15L * 1000L;

// Global variables
unsigned long lastConnectionTime = 0;
int measurementNumber = 0;

2) In the setup, start the serial output, initialize input and output pins, and connect to the wireless network.

void setup(){
  
    Serial.begin(115200);
    pinMode(buttonPin,INPUT);
    pinMode(LEDPin, OUTPUT);
    connectWiFi();
    
}

3) In the main loop, first make sure that there is a wireless connection, and then check for a button press. If the button is pressed, check if enough time has elapsed to post data. After detecting a button press measure the wireless network strength, and then call the HTTP posting function.

void loop(){
const int numberPoints = 7;
float wifiStrength;

  // In each loop, make sure there is an Internet connection.
    if (WiFi.status() != WL_CONNECTED) { 
        connectWiFi();
    }

    // If a button press is detected, write the data to ThingSpeak.
    if (digitalRead(buttonPin) == LOW){
        if (millis() - lastConnectionTime > postingInterval) {
            blinkX(2,250); // Verify the button press.
            wifiStrength = getStrength(numberPoints); 
            httpRequest(wifiStrength, measurementNumber);
            blinkX(measurementNumber,200);  // Verify that the httpRequest is complete.
            measurementNumber++;
        }
        
    }
}

4) Connect your device to the wireless network using the connectWiFi function. After successfully connecting to the network, the device quickly blinks five times.

void connectWiFi(){

    while (WiFi.status() != WL_CONNECTED){
        WiFi.begin(ssid, password);
        delay(3000);
    }

    // Display a notification that the connection is successful. 
    Serial.println("Connected");
    blinkX(5,50);  
}

5) Connect to the ThingSpeak server and build the data strings for the HTTP POST command using the httpRequest function.

void httpRequest(float field1Data, int field2Data) {

    WiFiClient client;
    
    if (!client.connect(server, 80)){
      
        Serial.println("Connection failed");
        lastConnectionTime = millis();
        client.stop();
        return;     
    }
    
    else{
        
        // Create data string to send to ThingSpeak.
        String data = "field1=" + String(field1Data) + "&field2=" + String(field2Data); //shows how to include additional field data in http post
        
        // POST data to ThingSpeak.
        if (client.connect(server, 80)) {
          
            client.println("POST /update HTTP/1.1");
            client.println("Host: api.thingspeak.com");
            client.println("Connection: close");
            client.println("User-Agent: ESP32WiFi/1.1");
            client.println("X-THINGSPEAKAPIKEY: "+writeAPIKey);
            client.println("Content-Type: application/x-www-form-urlencoded");
            client.print("Content-Length: ");
            client.print(data.length());
            client.print("\n\n");
            client.print(data);
            
            Serial.println("RSSI = " + String(field1Data));
            lastConnectionTime = millis();   
        }
    }
    client.stop();
}

6) Take several measurements and return the average value to the main loop with getStrength.

// Take measurements of the Wi-Fi strength and return the average result.
int getStrength(int points){
    long rssi = 0;
    long averageRSSI = 0;
    
    for (int i=0;i < points;i++){
        rssi += WiFi.RSSI();
        delay(20);
    }

   averageRSSI = rssi/points;
    return averageRSSI;
}

7) Finally, use the blinkX function to make the device LED blink. The blinks allow the board to communicate with you when it is not connected to the computer via USB.

// Make the LED blink a variable number of times with a variable delay.
void blinkX(int numTimes, int delayTime){ 
    for (int g=0;g < numTimes;g++){

        // Turn the LED on and wait.
        digitalWrite(LEDPin, HIGH);  
        delay(delayTime);

        // Turn the LED off and wait.
        digitalWrite(LEDPin, LOW);
        delay(delayTime);
        
    }
}

See Also

|