ESP32 Espruino: Scanning WiFi Networks

The objective of this post is to explain how to scan the surrounding WiFi Access Points on Espruino, running on the ESP32. The tests were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.


Introduction

The objective of this post is to explain how to scan the surrounding WiFi Access Points on Espruino, running on the ESP32. We will use Espruino’s WiFi library to do it.

Note that since we are going to use Espruino, we will be programming in the JavaScript language. For an introduction on Espruino and how to install it on the ESP32, please check the previous post.

The tests were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.

If you prefer, you can check the video version of this tutorial at my YouTube channel.


The code

The code for this tutorial will be very short and simple, which is one of the advantages of using a higher level language, such as JavaScript.

We will start by importing the previously mentioned WiFi module. To import a module in Espruino, we simply use require(“name of the module”) and assign the result to a variable [1]. Note that since we are using JavaScript, we don’t specify the types of the variables [2].

var wifi = require('Wifi');

Next, to scan the surrounding access points, we simply need to call the scan function of the WiFi module. This function receives as input a callback function that will be executed when the scan finishes [3]. Thus, the execution of the code below this call will immediately continue instead of stopping until the scan is complete.

So, right before the previously mentioned call, we define the callback function, so it can be passed as input of the scan function. In terms of structure, this function needs to receive as input an argument, which will be an array of objects (one object per Access Point detected) with the following fields:

  • Signal strength;
  • Authorization mode;
  • Name of the network (SSID).

Note that this argument will be automatically passed to our function by the scan function and thus we don’t need to code anything related to its structure.

Inside the function, we will simply print the array to the console. You can check below the function definition.

function getScanResult(apList){
  console.log(apList);
}

Note that since we are on JavaScript programming, the argument of the function doesn’t have a type (just a name) and the function doesn’t have an explicit return type. You can read more about JavaScript functions here.

Since we already defined the callback function, we will then call the scan function of the WiFi module, passing as input our getScanResults function.

wifi.scan( getScanResult );

You can check the full source code below. Note that we added an extra print right after the scan function call, just to show that this print will be immediately executed and only later, when the scan finishes, the callback function is executed.

var wifi = require('Wifi');

function getScanResult(apList){
  console.log(apList);
}

wifi.scan( getScanResult );

console.log("right after scan");


Testing the code

To test the code, simply upload the previous script to your ESP32 using the Espruino IDE. You should get an output similar to figure 1, which shows the nearby access points detected being printed. Also, it is possible to confirm that our extra print is indeed printed before the scan result. Note that the code upload button of the IDE is highlighted on the figure.

ESP32 Espruino Scan WiFi networks.png

Figure 1 – Scanning the surrounding networks with Espruino on the ESP32.

 

Related posts


References

[1] https://www.espruino.com/Modules

[2] https://docs.microsoft.com/en-us/scripting/javascript/javascript-fundamentals

[3] http://www.espruino.com/Reference#l_Wifi_scan

Advertisements
This entry was posted in ESP32 and tagged , , , , , , , , , . Bookmark the permalink.

One Response to ESP32 Espruino: Scanning WiFi Networks

  1. Pingback: ESP32 Espruino: Connection to a WiFi network | techtutorialsx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s