Repository navigation
2. Setup Cloudflare Tunnel
Here we explain how you can access securely your Raspberry Pi using a domain you control, e.g., myiot.is-always.online
First you need to add your domain to your Cloudflare account. You can do that following the instructions in this article
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.
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.
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!