Saturday, November 11, 2017

Esprunio ESP8266 Wifi Setup

The Wifi parameters (SSID and password) can be configured programmatically, or they can be manually entered (using the IDE) and stored for later usage by programs. I'll describe the second option here, for usage as client with DHCP:
To manually configure Wifi execute the following command in the IDE (left side):
require("Wifi").connect("<ssid>", {password: "<password>"});
require("Wifi").getIP();
require("Wifi").getIP();
require("Wifi").getIP();

 

Replace <ssid> and <password> with your SSID and password. It'll take some time for the Wifi to connect, most likely you'll have to repepat the getIP command a few times (if you wait long enough only once, but if you're impatient even more than I did).

After you've got the IP address the configuration can be saved with the save command. This will save the SSID and password in clear text, and allows programs running on the ESP8266 to read this information with the command getDetails.

require("Wifi").save();
require("Wifi").getDetails();


To verify that everything works reset or restart (power cycle) the ESP8266, and recall the details with getDetails again.

reset();
require("Wifi").getDetails()
;
require("Wifi").getIP()
; 

In the ESPRUINO WEB IDE it'll automatically connect to the Wifi network.


To use Wifi in a program is now very simple, you don't have to hardcode SSID or password in your code but you can simply start a connection with the following code:

var wifi = require("Wifi");
wifi.connect(JSON.parse(require('Storage').read('.wificfg')).ssid,
{password: JSON.parse(require('Storage').read('.wificfg')).password}, function(err) { if (err) { console.log(err); } else { console.log(
wifi.getIP()); } });


That's it, almost.

To configure a fixed IP  address (e.g. when using an Android Hotspot as access point) and in cases where the Wifi access point doesn't allow to configure a fixed DHCP address you can set/change the IP address. Android Hotspot alway uses IP address 192.168.43.1 with the netmask 255.255.255.0. Use this code to change the IP address of the ESP8266 to 192.168.43.2:

require("Wifi").setIP(
  {
    ip:'192.168.43.2',
    gw:'192.168.43.1',
    netmask:'255.255.255.0'
  }, function(){}
);


That's it.

No comments:

Post a Comment