-
• #2
I'm pretty sure the problem is that JavaScript executes asynchronously. Obviously I don't know what is in your startAP/enableWifiScan functions, but usually when you execute a command
startAP
, it returns immediately and calls a callback when the AP is connected.For instance for Espruino Wifi you need:
wifi.startAP(ssid, options, function() { // this is executed when connected and should start the server }); // this code is executed before it is connected
Also, which device are you using the Wifi on?
-
• #3
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)
-
• #4
There is a promise library, at least in later versions of Espruino.
createServer
will create a second server in JS which will confuse matters, but presumably the ESP8266 doesn't check for overlapping sockets, or you're not calling.listen
twice.Moving this to the ESP8266 forum though - folks there might know what happens with
connected
. -
• #5
Gordon, pthieu,
I seem to have a related problem. I am trying to get a web server running on boot up, I've used samples from the forums to test with. Here's the code I'm using:
>dump()
E.on("init", function () { var http = require("http"); http.createServer(function (req, res) { res.writeHead(200); res.end("Hello World"); }).listen(8080); });
As you can see, the code was saved(). I've also saved the wifi settings (which do work btw)! However, when booting I get:
Loading 1836 bytes from flash...
ERROR: Not connected to the internetAnd, when I try to browse to port 8080 on the IP address of my ESP8266, the web browser can't load the page, however, if I type:
>load()
=undefined
Loading 1836 bytes from flash...Voila, it starts working! I am assuming that the http server is starting before the wifi interface, thus producing the Not connected to the internet error and when I load the code manually after the ESP8266 has finished booting, it works. This isn't very handy!
Any ideas chaps? Oh and pthieu, I think I have the same ESP8266 as you...
Julian
-
• #6
@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. -
• #7
Hi pthieu,
Wowzers, yes that works! The error has gone and the http server is running after a hard reboot. That link makes sense to me now, good find!
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:
i.e.
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.