• 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();
    
About

Avatar for espftw @espftw started