-
• #2
That's an odd one - just tried here with:
var WIFI_NAME = "..."; var WIFI_OPTIONS = { password : "..." }; var wifi; function getPage() { require("http").get("http://www.pur3.co.uk/hello.txt", function(res) { res.on('data', function(d) { console.log("--->"+d); }); }); } function onInit() { wifi = require("Wifi"); wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) { if (err) { console.log("Connection error: "+err); return; } console.log("Connected!"); getPage(); }); }
then:
Upload... >save() =undefined Compacting Flash... Calculating Size... Writing.. Compressed 114368 bytes to 4481 Running onInit()... Connected! --->Hello World! >save() =undefined Compacting Flash... Calculating Size... Writing.. Compressed 114368 bytes to 8578 Running onInit()... Connected! --->Hello World! >
And it seems to work...
Could it be that in
setConfig
you're actually forcing a connect again, then saving?In that case, it'd be saving the current state (which is in the middle of a connection), but then
onInit
is firing off a second connection?Having said all that, saving the network details the way you're doing (re-saving all your code) isn't ideal. In 1v96 I added
Storage
module which allows you to save small amounts of data. For instance:// update wifi details function setConfig(name, password) { require("Storage").write("wifi",{ name:name, password:password}); } function onInit() { var WIFI_NAME = "..."; var WIFI_OPTIONS = { password : ".." }; var wifiConfig = require("Storage").readJSON("wifi"); if (wifiConfig) { WIFI_NAME = wifiConfig.name; WIFI_OPTIONS = { password : wifiConfig.password }; } console.log(WIFI_NAME,WIFI_OPTIONS); wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) { .... }); }
So now when you change your wifi details it modifies just the wifi details in a file called
wifi
, but leaves everything else the same (and doesn't require a reboot). -
• #3
Hi Gordon! Thanks for the reply. My bad for not noticing the
Storage
module. This works and it makes it much easier than callingsave()
every time. Thank you!
Hello,
I'm having trouble removing the previous instance of the Wifi connection when I call the
save()
function. (see attached image)I have a function
setConfig(options)
where I set variables like the Wifi SSID, password, device name, etc, and I'd like to be able to save these variables to the flash memory on the fly. This function has asave()
function at the end which callsonInit()
again. I noticed that the previous Wifi connection reconnects on top of the new Wifi connection. I tried explicitly disconnecting it and setting thewifi
variable to null, but it still does the same thing.Is there anything I can do to remove the previous instance? Or is this normal and I shouldn't be worried about it?
1 Attachment