Jump to content

How to create wireless access point and internet connection with NODEJS on Armbian board


Recommended Posts

Posted

Actually this is answer not a question .

 

How to drive things with the OrangePi board with smartphone?

 My GOAL was :
 1. create Access Point with Orange Pi Lite board.
 2. create wireless local network with Nodejs
 3. connect my smartphone to this wireless network
 

 
 So , I think almost all kind of boards could work , from Raspberry pi , Orange pi and many 
 others boards families with wireless adapter .  
 
 This is tested on OrangePiLite board with OS-Armbian 5.65 with node.js installed.
 First you have to create two files - server.js and index.html(see below)
 server.js  is your nodejs server script file who will handle index.html
 index.html  is your html file witch you will see on your smartphone.
 Save those files in directory by your choice
 
 Next , open terminal and write one magic command :
 
 nmcli dev wifi hotspot ifname wlan0 ssid test password "test1234"
 
 This will turn your board in access point(hotspot) and will assign static address on your board. 
 So you have to find out this static address. I found it when I start 
 Applications->Settings->Ambrian config , 
 on my board it was 10.42.0.1 , but in your board maybe will be different.
 Calling this static address from your smartphone browser you will be able to connect 
 to the wireless network "test" with pasword "test1234" (see magic command upstairs)
 Don't forget to change "wlan0". This is the name of your wireless adapter on board, 
 it maybe different like wlan1,wlan2 or somting else. So, find it out too.
 
 Next , go back to you server.js file and put this static address and port into it
 (see server.js file below, const hostname = '10.42.0.1'  const port = '3000')
 
 Next , open terminal , go to directory with server.js file and write command below:
 
 nmcli dev wifi hotspot ifname wlan0 ssid test password "test1234"
 
 Next , write : 

 node server.js
 
 this will start your HTTP nodejs server on the board
 
 Next , from your smartphone , connect to wireless network "test" with password "test1234" . 
 Then start web browser and write the address and the port of your board. For me it was :
 
 10.42.0.1:3000
 
 It should display index.html file on your phone.
 
 
 Below is the code for server.js and index.html files
 
 
 ----------server.js-----------------
 
//display web page with nodejs
//board-OrangePiLite, OS-Armbian 5.65
//start AP on orangepi -> nmcli dev wifi hotspot ifname wlan0 ssid test password "test1234"
//start server on orangepi -> node server.js
//write in browser from your smartfon-> http://10.42.0.1:3000


var http = require('http');
var fs = require('fs');

const hostname = '10.42.0.1';    //'localhost' or '10.42.0.1' -> this is the ip address of the board Access Point
const port = '3000';


// Create the server.

var server = http.createServer(    function (req, res)    //req->event emiter, IncomingMessage object created from Server object. this is Readible Stream
                                {                    //res->event emiter, ServerResponce object created from Server object. this is Writible Stream.
                                    if (req.method == 'GET' && req.url=='/')
                                    {    serve(__dirname + '/index.html', 'text/html');
                                        res.write(__dirname);
                                    }else if(req.method == 'GET' && req.url == '/css/index.css')
                                    {   serve(__dirname + '/css/index.css','text/css');
                                    }else if(req.method == 'GET' && req.url == '/includes/index.js')
                                    {   serve(__dirname + '/includes/index.js','text/javascript');
                                    }else
                                    {    res.writeHead(404);
                                        res.end('Not found');
                                        
                                    }

                                    function serve (path, type)
                                    {    res.writeHead(200, { 'Content-Type': type } );
                                        fs.createReadStream(path)
                                            .on('data',function(data)    { res.write(data); }  )    // "data" -> event
                                            .on('end', function()    { res.end(); } );                // "end" -> event
                                            //    |        |-> callback function assigned to event "end"
                                            //    |-> event
                                        //or with pipe below
                                        //fs.createReadStream(path).pipe(res);
                                        //server.close();
                                    }
                                }
                              );

// Listen.

server.listen(port, hostname, () => {
 console.log(`Server running at http://${hostname}:${port}/`);
});

----------index.html--------------------


<html>
    <input type="text" id="name" name="name" required
       minlength="4" maxlength="8" size="10">
</html>

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines