Avatar for espftw

espftw

Member since Feb 2018 • Last active Mar 2018
  • 4 conversations
  • 9 comments

Most recent activity

  • started
    • 4 comments
    • 418 views
  • in ESP32
    Avatar for espftw

    If the AP is setup before the WebSocket connection, it fails to connect.

  • in ESP32
    Avatar for espftw

    Sorry for the delayed response, I was dissecting the code and finding the exact moment where it goes wrong. I seem to have found that as soon as wifi.setAP() is called, it prevents execution of all socket transmissions, even those which are called previous to wifi.setAP().

    In the callbacks below, if you halt execution of Setup_AccessPoint() so that wifi.setAP() is never called, the WS.send() functions are successful. However, if Setup_AccessPoint() is called, no previous or subsequent WS.send() calls are successful.

    Seems weird that the previous ones don't work either, but the server never receives the data if wifi.setAP() is called.

    function onInit() {  
      /***************************************­*****************************
       * Import modules
       ****************************************­****************************/
      var 
        wifi = require('Wifi'),
        http = require('http'), 
        WebSocket = require("ws"),
        WebServer = require('WebServer');
      
      /***************************************­*****************************
       * Declare globals & functions
       ****************************************­****************************/
      var
        WS, // stores the WebSocket
        Setup_WiFi = function(callback) {
          console.log('Setup_WiFi is initiating.');
          wifi.connect('MY_WIFI', {password: 'MY_PASSWORD'}, Validate_WiFi(callback)); 
        },
        Validate_WiFi = function(callback) {
          if ( wifi.getIP().ip == '0.0.0.0' ) {
            console.log('[Notice][Validate_WiFi : Connection failed]');
            setTimeout(function(){
              Setup_WiFi(callback);
            } , 1500);
            return;
          } 
          
          console.log('[Notice][Validate_WiFi : Connection successful]');      
          callback();
        },
        Setup_Socket = function(callback) {
          console.log('Setup_Socket is initiating.');
          
          WS = new WebSocket( 'SOCKET_IP' , {
            port: SOCKET_PORT,
            keepAlive: 5000
          });
          
          WS.on('error', function() {
            console.log('Socket error has occurred');
          });
    
          WS.on('open', function() {
            console.log("Connected to socket");
            callback();
          });
    
          WS.on('message', function(data) {
            data = JSON.parse(data);
            console.log('Received data: ');
            console.log(data);
          });
          
        },
        Setup_AccessPoint = function(callback) {
          console.log('Creating AccessPoint.');
          
          
          // ###
          // As soon as the wifi.startAP() is called, no previous 
          // or subsequent socket transmission are successful.
          // ###
          
          wifi.startAP('Sample AP', {}, function(err) {
            // If this point is reached, socket will not work.
            if (err) throw err;
            console.log('AccessPoint is live.');
            callback();
          });
          
        };
    
      
      /***************************************­*****************************
       * Program procedure index
       * 1. Connect to WiFi
       * 2. Connect to WebSocket
       * 3. Create Access Point
       ****************************************­****************************/
      
      /***************************************­*****************************
       * Wait for WiFi connection before proceeding
       ****************************************­****************************/
      Setup_WiFi(function(){
        console.log('Setup_WiFi has finished.');
        Setup_Socket(function(){
          WS.send('Sample data');
          console.log('Setup_Socket has finished.');
          Setup_AccessPoint(function(){
            WS.send('Sample data #2');
            console.log('Setup_AccessPoint has finished.');
          });
        });
      });
      
    }  
    save();
    
  • in JavaScript
    Avatar for espftw

    I saw an old thread referencing Promises, but can't seem to find an official Promise module anywhere. I tried an implementation by @favo from this thread but a simple console.log() test had erroneous results where callbacks occurred before resolutions.

    Trying to save a bit of time myself and hoping this was already implemented.

  • in ESP32
    Avatar for espftw

    Just curious if there's a fundamental reason this isn't working. If I disable the WebServer that is served over an AccessPoint, the WebSocket transmission is successful. If I delay the initiation of the WebServer and AccessPoint, the WebSocket transmission is successful all the way up until the WebServer and AccessPoint are created.

    The main goal is to accept data from a non-Espruino device (plain Arduino) via an HTTP request to the WebServer on the Espruino board, and then relay that data via WebSockets to the remote server. I'm not sure if this is the best way of achieving this, any guidance is appreciated.

  • in ESP32
    Avatar for espftw

    Terrific, thanks so much. I was never getting an error at all, certainly not one saying the Wifi module was missing. Is there anything I can do to enable error reporting or something similar? I hate to bother you guys for such simple issues.

  • in ESP32
    Avatar for espftw

    When modifying some simple example code to try and figure out what is going wrong, I noticed that the callback function inside wifi.connect is not executing and no console output is shown after console.log('start').

    console.log('start');
    
    var wifi = require("Wifi");
    
    function onConnect(err) {
      console.log('begin'); // does not output
      if(err) {
        console.log("An error has occured :( ", err.message); // does not output
      } else {
        console.log("Connected with IP : ", wifi.getIP().ip); // does not output
      }
      console.log('end'); // does not output
    }
    
    wifi.connect("KNOWN_NETWORK", { password: "KNOWN_PASS" }, onConnect);
    
  • in ESP32
    Avatar for espftw

    doh!

    I am so sorry to waste everyone's time, especially yours. It was a matter of setting the proper baud rate in Settings > Communication.

Actions