With using Spring, I've made an app on my RaspberryPi that always when I type in my browser "rspi_ip/light", it switches on the LED. It works perfectly but I want to turn this LED on from my another Android app via Web. It is working when both Raspberry and phone are in the same network but I want to switch it on from any network. How I can make it accessible from anywhear and make it secure? I do not want to make any rules on my router this system has to be plug and play, just connecting raspberry to any network and be able to connect to it from my phone without any other apps if it is possible.

1 Answer 1


This sounds like a perfect use case for MQTT, (like HTTP, but with publish/subscribe capability and perfect for IoT projects). You will need to use an MQTT "broker", which is a server that you can use as sort of a middle-man to communicate between devices. I'd suggest researching MQTT to get an overview of how it works.

It is like ngrok in that you shouldn't have to configure any firewall settings in order to have your phone and Raspberry Pi reach each other. The broker will need to be reachable from the public internet (if you use a public broker, then this is guaranteed).

MQTT client libraries are available for just about any common programming language, so you can use Java for your Raspberry Pi app, although it will probably be simpler to use Python (there's a great MQTT client called paho-mqtt).

Whatever language you choose, your program on your Raspberry Pi would "subscribe" to a "topic" from the MQTT broker. The topic is published from another device and contains a "message" of what you want to tell your LED (e.g. a topic of /directive/powerState with a message of ON).

The broker manages and transfers messages, and has the job of making sure messages get delivered. You typically set up an account through a dashboard on a website owned by the broker, and they usually require you to create a username and password (or sometimes an API token) that you can use to connect your devices to the broker with. You may need to create a "device" on the broker that will be uniquely used for the publish/subscribe topics and messages for your LED program on your Raspberry Pi. Should you ever decide to convert your project to use something lighter (e.g. ESP8266), you would be able to use the same credentials and device you created through your broker. See this page for a list of public brokers.

Your web browser uses HTTP which is a different protocol than MQTT, so you will need to use an app that can connect to MQTT (there are many of these apps available on both iOS and Android, as well as Siri/Google Assistant shortcuts/integrations). You can put the account and device information you created through your broker's website into the app and then you will be able to control the LED on your Raspberry Pi from your phone (anywhere!).

Concerning security, just like HTTP (using port 80) has a secure version (HTTPS over TLS using port 443), MQTT (using port 1883) also has a secure version over TLS using port 8883. MQTT shouldn't open you up to having your network/devices "hacked", but just like using plain HTTP, using unsecured MQTT would make it easier for a determined hacker to MITM your MQTT traffic (this is highly unlikely in my opinion).

  • Is this something like ngroc? Sep 2, 2021 at 8:51
  • Like ngrok, using MQTT does not require router configuration. On the free plan, ngrok's URLs are randomly generated and temporary. There are free options for MQTT that once set up, will allow you to continue its use without reconfiguring anything. Keep in mind that MQTT is a different protocol from HTTP.
    – Adamq
    Apr 7, 2022 at 4:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .