Skip to content

2. Setup Cloudflare Tunnel

Nikos Fotiou edited this page Mar 20, 2022 · 4 revisions

Here we explain how you can access securely your Raspberry Pi using a domain you control, e.g., myiot.is-always.online

Cloudflare account setup

First you need to add your domain to your Cloudflare account. You can do that following the instructions in this article

Setup Cloudflare tunnel

Install Cloudlare tunnel in your Raspberry Pi following the instructions in this article.

For the Raspberry Pi you need to use the ARM binary file. Download it (using for example wget) and rename to cloudflared (e.g., using mv cloudflared-linux-arm cloudflared). Then execute:

cloudflared tunnel login

open the prompted URL, and select the domain you transferred to Cloudflare with the previous step. Finally, a certificate will be created and installed in your Raspberry Pi.

Tunnel creation

Run the command

cloudflared tunnel create <NAME>

to create a tunnel named . The command will output a tunnel ID. You can always view the IDs and the NAMEs of your tunnels by executing

cloudflared tunnel list

Note that by creating a tunnel, a credential file is created in the location: /home/pi/.cloudflared/<NAME>.json.

Tunnel verification

We will now use the tunnel to interact with the server.py script located in the REST-server folder in the code repository. This script accepts requests in localhost:9000. Create the following configuration file and store it in /home/pi/.cloudflared/config.yml

url: http://localhost:9000
tunnel: <tunnel ID>
credentials-file: /home/pi/.cloudflared/<tunnel ID>.json

Then, we should create a sub-domain that will be used for accessing our IoT device through the tunnel. For example, in order to use ``myiot.is-always.online` the following command is used

cloudflared tunnel route dns <NAME> myiot.is-always.online

Finally, execute the server script (i.e., python3 server.py) and run the tunnel by executing:

cloudflared tunnel run <NAME>

At this point you can interact with the server script through any browser, e.g., by visiting https://myiot.is-always.online/hello?name=world Note that we are using HTTPS, which is enabled by default when accessing a resource over a Clouldflare tunnel!

Clone this wiki locally