0

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
3
  • You wrote: "I have also configured rpi to switch to access point". What does switching mean? Normally you configure an access point and have a web application running on it. There is no need to switch anything, just connect to ip address of the AP and the port of the web app. Why do you use ip_forwarding, NAT and fiddle with ip addresses? – Ingo Aug 14 '20 at 16:43
  • And please take note that using /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. – Ingo Aug 14 '20 at 16:43
  • @Ingo by switching to access point means i can turn on and off the access point through my python program. I am successfully able to connect to the AP and access the web application by manually entering the IP address. but what I want to do is that the user doesn't have to remember the IP address he just connects to AP and is automatically redirected to the webapp without manually entering the IP address in web browser. – Aayush Lakkad Aug 17 '20 at 14:38
2

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.

6
  • 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 Aug 18 '20 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 Aug 18 '20 at 14:50
  • without starting the browser manually, the browser should open up my site without doing anything else – Aayush Lakkad Aug 20 '20 at 12:01
  • @AayushLakkad Just autostart the browser, – Ingo Aug 20 '20 at 13:29
  • How to autostart the browser? and by autostarting will it automatically open the webpage of my website? – Aayush Lakkad Aug 21 '20 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.