• Hi,

    I am currently working on a school project in which we have to learn children how to program.
    For this project we are going to use Espruino on a ESP8266 microcontroller to send the code to the robot.

    To add a captive portal for the wifi connection i am using the Espruino-Wifi-Manager. I edited the code so the user can add a username in the form before adding the SSID and password (still a beginner with java-script so please let me know if i did something wrong).

    The problem that i have is that i want to have a connection to a web socket server after the microcontroller connects to the wifi network so we can send the saved username to the web socket server. I added the web socket code under the wifi connection code but after the microcontroller reboots with load() it won't execute the code of the web socket connection. the web socket connection will work if i send the whole code again after the right wifi credentials are saved on flash but i really want to have a web socket connection after sending the code once so it won't be to difficult for the children to use.

    My question is if it is possible to execute the web socket connection code before or after the microcontroller reboots with load(), and if so how i could do this? I tried out many different things but nothing seems to work.

    I will upload the code as a file since it is to long to upload with the code button and i hope anybody can help me out.


    1 Attachment

  • I'm not familiar with Espruino Wifi Manager, but looking at it, it looks like the wifi configuration is saved by first operating as an access point (192.168.4.1), after reboot the ESP8266 connects as a station to your LAN using the saved credentials.

    So first time round, before save/reboot, the ESP8266 has no connectivity to anything on your Wifi network, so you will not be able to communicate via websocket to anything other than the browser connection, through which you are adding the username and password.

    You will need to wait for your network connection before sending the credentials to a websocket server on your network, but this should be possible.

  • Actually, that's not quite right, after looking at your code file, you've got the module in your code and it would appear the Espruino Wifi Manager (I'm assuming the code belongs to that) checks the validity of the network credentials supplied by first test connecting to the Wifi network before writing to flash & rebooting (nice!) so if you bring your websocket code into there, below this line in the start_wifi function:

      console.log("Connected to: " + wifi.getIP().ip);
    

    and defer calling load() until the websocket sends the credentials (your test object) that should work.

    Given this would seem to be a one way, one time exchange of data, you could probably simplify your code again by making a HTTP POST with the data, and have a backend which can handle forms (assuming that's possible).

  • Hey @Gian were you able to move forward with your project? If I can help, I'm happy to try. Kids need this stuff :)

  • Thank you for the reply and my apologies for the delayed response, I am very glad that you want to help.

    I discussed the option of opening the web socket connection before calling load() with my project members but this doesn't seem for the solution we have been looking for.

    After brainstorming about what should work, the perfect solution would be that connection with the web socket server is called after load(), so we can keep this connection to the web socket server open. Sending the username to the server won't be a one-time thing and even if we manage to create a connection before load(), send the username to the server, close this connection and create a connection after load(), the microcontroller would connect to a different web socket.

    So we are searching for a way to call the connection to the web socket server after load() and keep this same connection open . I tried out to send some simple console logs after load() to see if the microcontroller does anything after it reboots and that seems to work, but it still won't execute the ws connection code so we are pretty much stuck now.

    Once again thank you for the reply and i hope you have a solution for this.

  • Yes, that makes sense, after load() allows you to use the websocket as you need rather than one-time.

    I don't think you are that far away. At the moment your websocket code is sort of orphaned but if you've got logging statements that appear after load() has run and when you're connected on your network that's a great start.

    Either bring your socket code into that part too, or maybe wrap the socket code in a function and just call it to set it up?

    Briefly looking at the README for the Espruino Wifi Manager the callback function is used on successful connection, so that's probably were you have your logging that works(?) and where you need to put your code (either pasted or the function call). I'm guessing a bit.

    If that does not help, post up your code as you have it now.

  • I will try to put the code into the callback or wrap in up in a function to call it. if that doesn't work i will upload the code that i have until now.

  • We are currently rewriting the whole code to see if we can fix the problem, we will keep you updated if we managed to find a working solution.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Create a connection with the web socket server after connecting to wifi and rebooting with load()

Posted by Avatar for Gian @Gian

Actions