Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Wireless Network Signal Strength with ESP32 in Arduino

This example demonstrates wireless network functionality of the ESP32 and how to use it to post data to ThingSpeak™. Your ESP32 records and posts the wireless network signal strength of the network that it is connected to. Press a button on the board to make 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.

The signal strengths were measured for several offices in an office building. The wireless network strength has been superimposed over an office map using tools available with ThingSpeak (Create Heatmap Overlay Image). The center image shows a portable version of the signal strength measurement prototype. The right image shows ThingSpeak data from the offices at The MathWorks®.

Prerequisites

ESP32 Arduino Core and IDE Setup

See Installation Instructions for Arduino Core for ESP32. You can test your Arduino® setup on the ESP32 using the “Blink” example sketch in Files > Examples > 01.Basics. Be sure to define the LED_BUILTIN pin as pin 5 to use the on board LED.

ThingSpeak Setup

ThingSpeak  requires a user account and a channel. Each channel has up to eight data fields, location fields, and a status field. A free account holder can send data every 15 seconds to ThingSpeak.

  1. Sign up for new account.

  2. Create a channel by selecting Channels > My ChannelsNew Channel.

  3. Enable Field 1 and Field 2.

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

  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

This example requires only devices built in to the ESP32 development kit provided by Sparkfun. Using a portable microUSB compatible battery can make your device portable for easier measurements.

Programming 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 here. Edit the wireless network SSID, password, and the Write API Key for your channel.

    You can generate the overlay image of an office map with 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. To generate a heatmap, see Create Heatmap Overlay Image.

     Expand for Copy and Paste Code

    1. Start with 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 to see if the button is pressed. If the button is pressed, check if enough time has elapsed to post data. If so, 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 always 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. The device blinks five times fast once connected.

      void connectWiFi(){
      
          while (WiFi.status() != WL_CONNECTED){
              WiFi.begin(ssid, password);
              delay(3000);
          }
      
          // Show the user a 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 a number of measurements of the WiFi 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 flash the device LED. The flashes allow the board to communicate with you when not connected to the computer via USB.

      // Flash the LED 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

|