• It looks like there's a problem with servers not getting cleaned up when the board is reset (for example, when you click "send to Espruino" in the IDE):

      "VERSION": "1v99",
      "GIT_COMMIT": "f0d66ba",
      "BOARD": "ESP32",
      "FLASH": 0, "RAM": 524288,
      "SERIAL": "30aea40f-0074",
      "CONSOLE": "Telnet",
      "MODULES": "Flash,Storage,fs," ... "r,crypto,neopixel",
      "EXPTR": 1073485304 }

    First time I upload and try to fire up the server, it works. The second time:

    ERROR: Socket bind failed
    Uncaught InternalError: Unable to create socket
     at line 11 col 60

    It isn't clear to me yet exactly what the key factor is that makes this fail, but the code below readily reproduces it for me. Upload code once.

    Request in browser:
    (board IP address):7348/status?un=testuser&pw=test­pass

    Upload code again. Watch for error in console.

    var http = require("http");
    function startup() {
    function fetchTime() {
    	require("http").get("http://drazzy.com/t­ime.shtml", function(res) {
      		var contents = "";
      		res.on('data', function(data) { contents += data; });
      		res.on('close', function() { setTime(contents); });
    	}).on('error', function(e) {
      		console.log("ERROR", e);
    var CORS={'Access-Control-Allow-Origin':'*'}­;
    // Network
    function onPageRequest(req, res) {
      var a = url.parse(req.url, true);
      var resu=0;
      if (a.pathname=="/favicon.ico") {
      } else {
        resu = handleCmd(a.pathname,a.query,res);
      if (resu > 0) {
      	  if (resu==200) {res.write('{"error":false,"code":200,"t­ext":"OK"}');}
      	  else if (resu==404) {res.write('{"error":true,"code":404,"te­xt":"Not Found"}');}
      	  else if (resu==403) {res.write('{"error":true,"code":403,"te­xt":"Unauthorized Client"}');}
      	  else if (resu==400) {res.write('{"error":true,"code":400,"te­xt":"Bad Request"}');}
      } else if (resu===0) {
      	  res.write('{"error":true,"code":500,"tex­t":"Unknown Error"}');
    function handleCmd(path,query,res) {
    	if (query.un!="testuser" || query.pw!="testpass") { 
    		return 403;
    	if (path=="/status") {
            return -1;
    	} else {
    		return 404;
    var cs={ //cs=Current State
    cs.Fargo=new Uint8Array(8);
    function onInit() {

    Powercycling the board fixes it.

    Also, sometimes when I click send to Espruino, the following is printed to console:

    >WARNING: --- gap_setScan 0
    WARNING: Scan stop failed
    WARNING: set rssi scan not implemeted yet
    WARNING: has central connection not implemented yet

    Much thanks to everyone for all the work they've put into the ESP32 support.

  • The issue is the the build is compiled with 10 sockets.
    When you reset, the sockets are not closed. Part of the reason for this, is one of the sockets was the telnet server socket - you would loose your connection to the board.

    If you saved the result of you your server connect as a global:

    var server=require("http").createServer(onPa­geRequest).listen(7348);

    Then before the reset - you could do server.close()

    This will shut down the connect and close the socket.

    By the way, are using using a telnet connection? It is so much faster than the com port connection.

    Oh - and your call to get the time - rather than have a page that returns the time - you can get the time out of the header..

  • When you reset, the sockets are not closed.

    Something seems odd there - if you call reset() it should call jswrap_net_kill which should shut all the open sockets (except the Telnet one, because it's handled separately). It works on ESP8266, so I think something might be broken on the ESP32 port.

  • Following the calls from jswrap_net_kill it ends up here in the ESP32 case - so it has been implemented, so I think something else is going on.


    I have also observed this behaviour in the ESP8266 build.

  • Hmm, yeah - it seems like something that would be good to fix, since it is a nuisance during development to have to power cycle the board after each upload.

    I will try manually doing server.close()

    I am using the telnet connection - I think this is one of the most compelling features of the ESP8266/ESP32. It's so much more convenient than having to fiddle with wires to upload.

    For doing the time with the header, how do I access the headers from within Espruino? It doesn't appear to be supported (based on my reading of the reference) As I understand it, the callbacks provide an httpCRs and httpCRq object - but the API reference for these does not mention any method to get the headers.

  • I think httpCRq has a headers field on it?

  • >    require("http").get("http://drazzy.com/t­ime.shtml", function(res) {
            var contents = "";
            res.on('data', function(data) { contents += data; });
            res.on('close', function() {
    setTime(contents); });
     }).on('error', function(e) {
    :        console.log("ERROR", e);
    :    });
      "GMT": "Wed, 13 Jun 2018 11:22:54 GMT"
    Wed Jun 13 11:22:54 2018
  • Thanks.

    I've created an issue for the incomplete API documentation. https://github.com/espruino/EspruinoDocs­/issues/441

  • @DrAzzy
    The initial problem you had should be largely sorted by this fix:


    It allows the port to be reused.

  • I think I got this issue on an ESP32 with 2v01 when trying to use MQTT / tinyMQTT. reset(1), reset button, power cycling does not solve the problem. The board can connect to wifi, ping works.
    What really puzzles me is that I have an ESP32 with tinyMQTT that works.

    With MQTT the error message is ERROR: Connect failed (err 104)
    With tinyMQTT the error message is:

    ERROR: Connect failed (err 104)
    Uncaught InternalError: Unable to create socket

    at this call: a.cl=require("net").connect({host:a.svr,­port:a.prt},b)

    Edit: A simple http get does work.
    Edit2: A simple http server works:

    function onPageRequest(req, res) {
      print('page req', req);
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Unable to bind socket errors if ESP32 not powercycled between uploads, strange error messages

Posted by Avatar for DrAzzy @DrAzzy