Reputation: 595
I want to transfer sensor data from one device(with sensor and wifi hotspot. For now i'm using my laptop) to other devices connected via WiFi hotspot. For this i want to host a localhost website listening on port 80 on the sensor device(for now my laptop), and then just open this site on other devices(through WiFi hotspot set up by the sensor device(my laptop)) but when I tried this by putting URL: http://192.168.1.110, I got This site can't be reached http://192.168.1.110/ is unreachable.
How can I access the localhost webpage in the other devices?.
However,(why?)this works perfectly fine when the devices are connected to my home's WiFi router. here is the image(this is what it should look like):
This is the node.js
file index.js I'm running:
const app = require("express")();
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
var clients = 0;
io.on('connection', function(socket) {
clients++;
socket.emit('newclientconnect',{ description: 'Hey, welcome!'});
socket.broadcast.emit('newclientconnect',{ description: clients + ' clients connected!'})
socket.on('disconnect', function () {
clients--;
socket.broadcast.emit('newclientconnect',{ description: clients + ' clients connected!'})
});
});
port = 80
http.listen(port, () => {
console.log("listening to port",port)
})
and this is the html file index.html:
<html>
<head>
<title>
broadcasting_demo
</title>
</head>
<script src = "/socket.io/socket.io.js"></script>
<script>
var socket = io();
socket.on('newclientconnect', (data) => {
document.body.innerHTML = '';
document.write(data.description);
document.write("<h1><br>Hello world, this site works<br> on other devices<br> if you're connected to my home wifi!</h1>")
});
</script>
<script></script>
<body>
<h1>this is a demo</h1>
</body>
</html>
Upvotes: 0
Views: 2174
Reputation: 111
Does your IOT device have a DHCP server running?
If you are connecting through the home network, your router is providing an IP address to the phone/tablet/client device. If you connect to the IOT device directly, you need to make sure that the phone/tablet/client device has an IP address from the 192.168.1.0 subnet.
For testing purposes, you can manually set it in the phone/tablet/client device, but easiest way would be to have a DHCP server running on the IOT device.
Upvotes: 0
Reputation: 22
When you're on your home wifi , the ipaddr of your sensor is 192.168.1.110 and hence you are able to access it over the network,
but when you connect to the sensor's hotspot , it will have some other public ipaddr over which it is accessible , hence you need to use that ip instead
To know that you can simply use the node library public-ip
npm install public-ip
and then
const publicIp = require('public-ip');
(async () => {
console.log(await publicIp.v4());
//=> '46.5.21.123'
})
now you should be able to access the website over the sensor's hotspot at http://46.5.21.123
Upvotes: 1