How can the PI be a Wifi Access Point???
The Linksys hardware pictured above is synonymous with wifi routers. Under the covers its just a dedicated Linux box and some fancy software. Turns out your PI can be that “dedicated Linux box” piece. In this post, we will cover turning your Raspberry PI into something that provides Wireless access to its internet connection to other computers within its radio’s range. (Sorry no fancy guis or admin tools here – that’s what you pay Linksys for. 😉
Get my repo…
git clone https://github.com/mamacker/kidpi.git
Then cd into the kidpi/apmagic directory:
cd kidpi cd apmagic
Install a few things…
You’ll need nodejs to use the code. There are a couple of ways to get that:
Installing NodeJS on a Raspberry PI
Or try running the node.sh script:
chmod 755 node.sh sudo ./node.sh
That should do most of the work for you. After its complete try:
If it installed correctly you should see a version greater than 6.10.0. If you see 0.10 or something similar – you’re running a version that is too old.
Linux utilities you’ll need…
There are several software chunks you’ll need to make this thing work:
- Takes care of routing the packets from one internet interface to another
- This is the chunk of software that uses the Raspberry’s wifi hardware as a hotspot
- This hands out IP addresses to any would-be client on your network.
So lets install those:
sudo apt-get install -y iptables hostapd dnsmasq
After a successful install, go ahead and reboot your PI.
Now install the pre-reqs for the node program through npm (from within the apmagic directory):
Setup the PI…
The Raspberry PI should now be connected to the internet through the Ethernet Port.
That’s the internet connection its going to share with all of its wireless clients, its also the one, if you are ssh’ing, you should be connected through… because the wireless connection is about to drop!
Running the software…
Now that you have all of the prerequisites… lets run the NodeJS program as root, and see if you’ve got the AP (again from within the apmagic directory):
sudo node index.js
You should see a couple of debug output statements, then there should be a shiny new Wifi AP called: MattWuzHere
Try to connect to it with the password “easypeazie” visible in the code.
If everything worked…
Congrats! You’ve successfully created an AP! Now lets make it happen every time the PI starts up. There is a SystemD configuration file there. Install that config:
sudo systemctl enable /home/pi/kidpi/apmagic/apmagic.service sudo systemctl start apmagic.service
Now reboot your PI:
When the PI fully reboots, your Access Point should automatically restart, and you can join it and get to the internet.
Now that you’ve successfully set this up using the default options. Open up the index.js file. Then look for the lines containing:
ssid: "MattWuzHere', password: 'easypeazie'
Change the MattWuzHere to the name you’d prefer for your new access point, then change the password too. Save the file.
Reboot the PI again, and your new name and password should take hold.
If something goes wrong…
If you have trouble – leave a comment! I’ll do my best to address any bugs or missed details.
7 thoughts on “Easily turn your PI3 into a Wifi Router!!! (Raspberry PI 3 as a Host AP – in code!)”
I can connect to the network using a mac but cannot connect using a PC. It says it cannot connect to this network. What should i be changing to get this to work?
I’m not entirely sure what is happening in your case. The networking stack on windows is very different, and there are soooo many things that can be configured funny when working with networking.
Does it say it cannot connect for a particular reason? For instance, the things I’d check are:
1) Did the wireless connect, but you weren’t given an IP
2) You were given an IP, but were not given an IPV6 address, and your system only wants IPV6
3) You were given an IP, but were not given a DNS server.
4) The given DNS doesn’t work for windows?
Check your network settings for the wireless and see if you can see anything different between this connection (to the macback) and your connection to a “professional” AP. That might provide some clue on what to attack on your system.
I am newbie to PI, Linux and node JS. 🙂
Very educational for me, thanks a lot. I loved the way of using tmp files 🙂
But … I can’t get it work. 😦 I can’t see the wifi ap. I put some lines to read tmp files,
otherwise your code is untouched … except some callbacks from fs,write functions.
May be because I am newbie, I can see static takeDown in apexecs.js but not
static takeUp function ? Do I need to UP wlan0 interface ?
Again, thanks a lot, I see the way of thinking in js 🙂
Waiting 30 seconds while everything else comes up…
Trying to bring down wlan0. Err? null
Err setting the ip address? null
Err setting the broadcast address? null
Err setting the mask address? null
Commands being executed:
Done setting up hostap.
File hostapd => /tmp/tmp-9628SIzo3UvIrjf0.tmp
Commands being executed:
Done setting up dnsmasq.
ip_forward flag written.
File dnsmasq => /tmp/tmp-9628Md6jzvpy0Raq.tmp
ip_forward = 1
Traffic forwarding setup.
It looks like everything went well there. I’ll bring it up again and try it with the latest to see if find anything. Do you know if the device you are trying to see the ap with runs on 2.4ghz? This config specifically brings up channel 6 which is in the 2.4 spectrum. If your device is 5g only then it won’t see it. Also, what is the output of ifconfig?
Sorry, my mistake. 🙂
Indeed this tmp files are very useful! I started manually :
pi@raspberrypi:~/fob_ap/kidpi/apmagic $ sudo hostapd /tmp/tmp-7674HcpvaxgPH1g7.tmp
And I got the mistake :)) Sorry, my password was too short.
Configuration file: /tmp/tmp-7674HcpvaxgPH1g7.tmp
Line 7: invalid WPA passphrase length 5 (expected 8..63) <<<<<< <<<<<<<<<<<<<< !
WPA-PSK enabled, but PSK or passphrase is not configured.
2 errors found in configuration file '/tmp/tmp-7674HcpvaxgPH1g7.tmp'
Failed to set up interface with /tmp/tmp-7674HcpvaxgPH1g7.tmp
Failed to initialize interface
I will try to get rid from all throw errors by my self, should be a good exercise ….
Again, thanks a lot for your code and explanations 🙂
I was stuck in the following situation:
I have to add “denyinterfaces wlan0″ in dhcpcd.conf and restart.
Then I can get AP mode with this code.
To return to CLIENT mode I have to delete or comment this line. And restart.
I have this on my PI:
pi@raspberrypi:~/fob_ap/kidpi/apmagic $ cat /etc/os-release
PRETTY_NAME=”Raspbian GNU/Linux 9 (stretch)”
Is there a way to avoid this with temp file serving dhcpcd.conf ?