Ahh -that's quite likely - in fact if you called reset but didn't nest what came next then that would explain almost everything.
I had assumed that the totally unmodified example code didn't work for you... Or did it?
You probably know this anyway now, but Espruino isn't multi-tasking, so it relies on scheduling execution of small functions one after the other (which includes all the code that handles the ESP8266).
It's probably most apparent in the ESP8266 library - if you call wifi.reset, it'll return almost immediately (having sent the reset command to the ESP8266)... But that doesn't mean the ESP8266 has actually managed to reset - that takes a few seconds, during which Espruino can be doing other stuff. It's only actually reset when the callback function that was passed to wifi.reset gets executed.
It's really powerful, but it's quite easy to trip up. For instance:
setWatch(function() {
wifi.reset(function(err) {
if (err) throw err;
console.log("Connecting to WiFi");
wifi.connect("WiFi_Name","WPA2_Key", function(err) {
if (err) throw err;
console.log("Connected");
// Now you can do something, like an HTTP request
require("http").get("http://www.pur3.co.uk/hello.txt", function(res) {
console.log("Response: ",res);
res.on('data', function(d) {
console.log("--->"+d);
});
});
});
});
}, BTN, {repeat:true, edge:"rising"});
Looks absolutely fine, but if you tap the button twice in quick succession (or the button bounces) it'll break, because the ESP8266 was already in the middle of resetting, but it'll be told to reset again.
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.
Ahh -that's quite likely - in fact if you called reset but didn't nest what came next then that would explain almost everything.
I had assumed that the totally unmodified example code didn't work for you... Or did it?
You probably know this anyway now, but Espruino isn't multi-tasking, so it relies on scheduling execution of small functions one after the other (which includes all the code that handles the ESP8266).
It's probably most apparent in the ESP8266 library - if you call
wifi.reset
, it'll return almost immediately (having sent the reset command to the ESP8266)... But that doesn't mean the ESP8266 has actually managed to reset - that takes a few seconds, during which Espruino can be doing other stuff. It's only actually reset when the callback function that was passed towifi.reset
gets executed.It's really powerful, but it's quite easy to trip up. For instance:
Looks absolutely fine, but if you tap the button twice in quick succession (or the button bounces) it'll break, because the ESP8266 was already in the middle of resetting, but it'll be told to reset again.