I just flashed an ESP32 I have with 2.07 and uploaded this code:
var ssid = 'Mi A3';
var password = 'parasquid';
var wifi = require('Wifi');
wifi.on('disconnected', () => {
console.log('disconnected!');
setInterval(connectToWifi, 1000);
});
wifi.on('connected', () => console.log('connected!'));
const connectToWifi = () => {
wifi.connect(ssid, {password: password}, function() {
console.log('Connected to Wifi. IP address is:', wifi.getIP().ip);
wifi.save(); // Next reboot will auto-connect
});
};
connectToWifi();
I then setup a hotspot on my phone to control the wifi connection and disconnection outside of the ESP32.
The callbacks are running, but it looks like the issue is that disconnect status isn't immediately detected. I can force the detection of the disconnect (and the callback) by doing something on the network (like getting a url)
Connected to Wifi. IP address is: 192.168.43.111
connected!
connected!
>require("http").get("http://www.pur3.co.uk/hello.txt")
Uncaught InternalError: Unable to locate host
at line 1 col 54
require("http").get("http://www.pur3.co.uk/hello.txt")
^
disconnected!
disconnected!
disconnected!
disconnected!
disconnected!
disconnected!
disconnected!
disconnected!
disconnected!
disconnected!
WARNING: Wifi:startMDNS - espressif
WARNING: Wifi:stopMDNS
Connected to Wifi. IP address is: 192.168.43.111
connected!
connected!
>require("http").get("http://www.pur3.co.uk/hello.txt")
=httpCRq: { type: 1,
res: httpCRs: { },
opt: {
protocol: "http:",
method: "GET",
host: "http://www.pur3.co.uk",
path: "/hello.txt",
pathname: "/hello.txt",
search: null, port: null, query: null },
dSnd: "GET /hello.txt HT" ... "ww.pur3.co.uk\r\n\r\n",
sckt: 56, cls: true }
>
Sometimes though, disconnection is immediately detected and the callback runs (again tested by turning on or off the hotspot on my phone). And if you wait long enough, the ESP32 detects the disconnection and then runs the callback.
So maybe the best solution for now is to have a setInterval that pings a known good url so as to trigger a disconnect if it goes down?
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
I just flashed an ESP32 I have with 2.07 and uploaded this code:
I then setup a hotspot on my phone to control the wifi connection and disconnection outside of the ESP32.
The callbacks are running, but it looks like the issue is that disconnect status isn't immediately detected. I can force the detection of the disconnect (and the callback) by doing something on the network (like getting a url)
Sometimes though, disconnection is immediately detected and the callback runs (again tested by turning on or off the hotspot on my phone). And if you wait long enough, the ESP32 detects the disconnection and then runs the callback.
So maybe the best solution for now is to have a
setInterval
that pings a known good url so as to trigger a disconnect if it goes down?