I have created a python flask script that runs on my raspberry pi, I have also configured rpi to switch to access point so when my laptop/mobile gets connected to the AP, I can navigate to the IP '192.168.4.1' to access the python flask web page. Now I want to do is set up a captive portal so that whenever I get connected to the AP, I get redirected to the IP of my pi and can access the flask web page. Can anyone give me an example/url to follow and understand what to do?
I have tried the following steps: Enable IP Forwarding:
nano /etc/sysctl.conf
Uncomment:
net.ipv4.ip_forward=1
Add these commands for the firewall and packet forwarding:
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
sh -c "iptables-save > /etc/iptables.ipv4.nat"
sudo nano /etc/rc.local
add these lines before Exit 0:
iptables-restore < /etc/iptables.ipv4.nat
ifconfig wlan0 192.168.4.1
1 Answer 1
I do not understand the problem. If you have switched on your access point, then there is a web server running on it. You simply connect to it by entering the name or ip address in the browser of a remote client (e.g. laptop). Any web server presents a start web page. That can be the page of your app.
To connect to the web server just use the network name of the RasPi and not its ip address. Try first http://raspberrypi.local
. But name resolution is not a problem of the RasPi. It is a problem of the network configuration. The Raspberry Pi OS uses mDNS (multicast DNS) by default that resolves the name raspberrypi.local
to its ip address. It may be required that you have to setup a local DNS server to have normal internet DNS name resolution available. Or you can map the ip address to the name in the /etc/hosts
file on every client.
To call direct the home page of the web app when starting the browser on the client you can define its start page.
-
The problem is I don't want to enter the network name or IP address anything. It should automatically (after connecting to AP) connect to web server. For example as they do for public wi-fi as soon as you connect to public wifi automatically a webpage opens up to enter details in order to continue.Aayush Lakkad– Aayush Lakkad2020年08月18日 03:48:09 +00:00Commented Aug 18, 2020 at 3:48
-
@AayushLakkad But with the configuration steps I have described you do what you want: starting the browser and then you are on the site of your app without doing anything others.Ingo– Ingo2020年08月18日 14:50:06 +00:00Commented Aug 18, 2020 at 14:50
-
without starting the browser manually, the browser should open up my site without doing anything elseAayush Lakkad– Aayush Lakkad2020年08月20日 12:01:58 +00:00Commented Aug 20, 2020 at 12:01
-
@AayushLakkad Just autostart the browser,Ingo– Ingo2020年08月20日 13:29:57 +00:00Commented Aug 20, 2020 at 13:29
-
How to autostart the browser? and by autostarting will it automatically open the webpage of my website?Aayush Lakkad– Aayush Lakkad2020年08月21日 07:26:45 +00:00Commented Aug 21, 2020 at 7:26
Explore related questions
See similar questions with these tags.
/etc/rc.local
has limitations due to Compatibility with SysV. We have seen many problems here on this site using it. Following the recommendation of the developers from systemd you should avoid using it.