Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to get IP address #637

Open
pankleks opened this issue Feb 17, 2024 · 5 comments
Open

how to get IP address #637

pankleks opened this issue Feb 17, 2024 · 5 comments
Assignees

Comments

@pankleks
Copy link

Hello,

How to get IP address after wifi is connected?

I was looking through docs but could not found it.

Thank you

@communix communix self-assigned this Mar 3, 2024
@communix
Copy link
Collaborator

communix commented Mar 4, 2024

IP address is network address. So you need to connect to the network.
You can see the local IP address in the net module, Socket class.

https://kalumajs.org/docs/api/net#socketlocaladdress

@krish-ag
Copy link

Collaborator

I tried as u asked, but I get undefined as output.

image

image

@pankleks
Copy link
Author

you need to put checking code after Connected to Wi-fi... else it's called immediatelly - not after wifi is really connected.

@tricoos
Copy link

tricoos commented Apr 2, 2024

Still doesn't work, why is there no such function? This is such an essential functionality and should be part of the connection info:

const WiFi = require('wifi').WiFi,
    net = require('net'),
    wifi = new WiFi();

console.log("Connecting to WiFi....");
wifi.connect((err) => {
    if (err) {
        console.error(err);
    } else {
        wifi.getConnection((err, connInfo) => {
            console.log("Connected to Wifi: "+connInfo.ssid);
            const sock = new net.Socket();
            console.log('Address:', sock.localAddress);
        });
    }
});
Connecting to WiFi....
Connected to Wifi: MyWifi
Address: null

@tricoos
Copy link

tricoos commented Apr 2, 2024

With some issue digging I found this: #554

That lead me to this code:

console.log("Connecting to WiFi....");
wifi.connect((err) => {
    if (err) {
        console.error(err);
    } else {
        wifi.getConnection((err, connInfo) => {
            console.log("Connected to Wifi: "+connInfo.ssid);
            console.log('Creating HTTP server simply to get my IP...');
            const server = http.createServer();
            server.listen(80, () => {
                console.log(`Yay, my IP is ${server._dev.ip}`);
                console.log('Shutting down server...');
                server.close();
                console.log('On with my code....');
            });
        });
    }
});

Is this really the only way? Spinning up an HTTP server simply to get the local IP address? Even in LUA for ESP8266 something like this has been available forever: https://nodemcu.readthedocs.io/en/release/modules/wifi/#wifistagetip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants