Most recent activity
-
@julian: Yes, it will take some time for the network interface to establish a connection with the access point; the
E.init
will trigger immediately after a power cycle (orsave()
invocation).Change your code to this:
var Wifi = require('Wifi'); E.on("init", function () { Wifi.on('connected', function(){ var http = require("http"); http.createServer(function (req, res) { res.writeHead(200); res.end("Hello World"); }).listen(8080); }); });
The doc for the
Wifi
class has a number of events that are emitted on specific situations that you can utilize to your advantage. -
ESP8266 NodeMCU 1.0 (ESP-12E)
Yes, the issue is the event-driven nature of JS. I was looking for a way to write simpler code since there isn't a reliable
async
orPromise
flow control library to use; I'll have to work with flags then. Some questions:Do you know if
http.createServer()
returns a new instance with a listener every time it's invoked like node.js? Or is there code beneath the hood that will check for an existence of ahttp.Server
object? I did a quick test to invokehttp.createServer()
twice in succession and there was no namespace conflicts orEADDRINUSE
errors, nor did the callback trigger twice upon a POST to my ESP8266.Does the Wifi class trigger an event when it has connected to a network? Something like
Wifi.on('connected', function(){...})
-- Nevermind, found the docs: http://www.espruino.com/Reference#t_l_Wifi_connectedIs there a way to save a key/value pair to flash? (or just any variable)
-
Want to invoke http.createServer outside of connecting to a network or creating access point. But I get this error:
Error: Not connected to the internet
My code follows this logic in event loop:
- Start AP
- Try to connect to network
- Start web server
- Stop AP when successfully connected to a network
i.e.
E.on('init', function() { APCheck = enableWifiScan(); startAP(); // If we try to connect to wifi at the same time we scan, there will be // conflicts and scan will return nothing setTimeout(function() { connectToWifi(); // If successful, stops AP }, 2000); // Create server anyway as we'll allow for control of the MCU through AP // if it has not connected to a network http.createServer(getPage).listen(80); });
The reason for this is I want to be able to have the MCU serve a configuration page which will allow user to configure the network to connect to, making the device portable without hardcoding a network.
I could probably start the server after access point has been created, add a check to see if a server has been instantiated after connecting to a network, but wondering if there's an easier way.
- Start AP
GH:
https://github.com/pthieu
NPM:
https://www.npmjs.com/~pthieu