-
• #2
I suggest you post a simplied version of your code.
-
• #3
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 towifi.setAP()
.In the callbacks below, if you halt execution of
Setup_AccessPoint()
so thatwifi.setAP()
is never called, theWS.send()
functions are successful. However, ifSetup_AccessPoint()
is called, no previous or subsequentWS.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();
-
• #4
Why don't you call
Setup_AccessPoint()
first, and then set up the sockets? -
• #5
If the
AP
is setup before theWebSocket
connection, it fails to connect.
Just curious if there's a fundamental reason this isn't working. If I disable the
WebServer
that is served over anAccessPoint
, theWebSocket
transmission is successful. If I delay the initiation of theWebServer
andAccessPoint
, theWebSocket
transmission is successful all the way up until theWebServer
andAccessPoint
are created.The main goal is to accept data from a non-Espruino device (plain Arduino) via an HTTP request to the
WebServer
on theEspruino
board, and then relay that data viaWebSockets
to the remote server. I'm not sure if this is the best way of achieving this, any guidance is appreciated.