Introduction
The objective of this post is to explain how to connect to a WiFi network on Espruino running on the ESP32. As mentioned in previous posts, we will be using JavaScript, since Espruino is a JavaScript interpreter for microcontrollers.
The tests were performed using a DFRobot’s ESP32 Module device integrated in a ESP32 FireBeetle board.
The code
First of all, we will need to include the WiFi module, so we have access to all the WiFi functionality needed to connect to the network.
var wifi = require('Wifi');
Next we will declare two variables to hold our network credentials, more precisely the name of the network (SSID) and the password.
var ssid = "yourNetworkName";
var password = "yourNetworkPassword";
To connect to the WiFi network, we simple need to call the connect function of the WiFi module. This function receives as input the SSID, a JavaScript object of options and a callback function that will be executed when the connection is established.
The options argument is an object that may contain a property called password, in which we specify the password in string format, and a property called dnsServers in which we can pass an array of strings with up to two DNS servers [1]. However, we will just make use of the password property.
Regarding the callback function, although we can use an arrow function or an anonymous function for a more compact syntax, we will actually define a named function before the call to the connect function of the WiFi module, for simplicity.
This function will receive no parameters and inside it we will simply print the IP address that was assigned to the ESP32 on the network.
To get the IP of the ESP32, we simply call the getIP function of the WiFi module. We will call this function with no arguments, but note that it can accept a callback function that is executed with an object with the IP information passed as input [2].
Nonetheless, this function also returns the previously mentioned IP object, which contains the IP and the MAC addresses, amongst other properties [2]. Since we have the MAC available, we will also print it.
function onWiFiConnect(){
var IPobject = wifi.getIP();
var IP = IPobject.ip;
var MAC = IPobject.mac;
console.log("IP:");
console.log(IP);
console.log("MAC:");
console.log(MAC);
}
Finally, after defining our callback, we will call the connect function of the WiFi module to effectively connect to the network. So, as first input we pass the SSID we have defined in our global variable, as second we pass the object with the options, which will only contain a property with the password (also defined on a global variable) and finally the callback function.
wifi.connect(ssid, {password: password}, onWiFiConnect);
You can check below the full source code for this tutorial.
var wifi = require('Wifi');
var ssid = "yourNetworkName";
var password = "yourNetworkPassword";
function onWiFiConnect(){
var IPobject = wifi.getIP();
var IP = IPobject.ip;
var MAC = IPobject.mac;
console.log("IP:");
console.log(IP);
console.log("MAC:");
console.log(MAC);
}
wifi.connect(ssid, {password: password}, onWiFiConnect);
Testing the code
To test the code, simply upload it to your ESP32 board using the Espruino IDE. You should get an output similar to figure 1, which shows both the IP address assigned to the ESP32 on the WiFi network and its MAC address being printed. The IDE upload button is also highlighted.
Figure 1 – Connection to WiFi on Espruino.