Husarnet VPN Client can run not only on servers, laptops and mobile phones (soon), but also on microcontrollers with very limited computing power and memory. Thanks to that you can run a single VPN network containing not only computers, but also cheap and low-power Wi-Fi sensors/actuators without any additional server or bridge in between. In other words you can build an IoT network connected to your computer without any kind of IoT API cloud in between. You can send messages directly to your Wi-Fi chips and this guide will show you how to do that.
This quick start guide describes how to use Husarnet VPN Client software library on ESP32 Wi-Fi microcontrollers and how to configure a network using Husarnet Dashboard in a few easy steps.
Create a network
You need to create a network in order to connect your devices. In most of the setups one network is enough for all devices.
Log in to Husarnet Dashboard, click [Create network] button, name your network and click [Create] button.
Add other machines
If you want to connect to your ESP32 from your computer, you need to install a Husarnet client there too. Have a look at the tutorials in the "Getting started" section and find one for your platform. If you already created a network - use the same one!
Get a join code
Click [Add element] button, select [join code] tab and copy your join code which looks something like this:
Leave the tab open, you'll need to paste this token into your code in one of the following steps.
Get platformio and install it. No special plugins are required.
On the Platformio's home screen, click [New Project].
Fill in required data. Board must be from ESP32 family and the only supported framework right now is Arduino.
main.cpp and you'll see something like this:
Feel free to upload it to your board to test if everything works fine. This is optional though.
Now, let's find some examples you can modify.
Go to the [Libraries] tab on the Platformio's home screen.
Search for "Husarnet ESP32".
In the [Examples] tab you'll see a couple of examples we've prepared for you. There's a special one we need to use for all of the others though. Let's start with it.
Select the platformio example.
On the screenshot you'll see that there's an
env section selected. Copy it and paste it at the top of your project's
platformio.ini file. It should look something like this now:
Now you're free to choose any other example and copy it to your
src/main.cpp replacing the old content.
Fill in the required data
In the examples you'll find a section for some deployment specific settings. Things like WiFi name and password, but also some of the Husarnet's settings. Let's go through them.
This is a name of your device. Choose something descriptive as it'll be visible in our dashboard, where you can find IP addresses of your device.
This is the code that you generated at the dashboard earlier. Paste it here. You can use the same code multiple times for different devices as long as you don't generate a new one in the dashboard. If you do all previous ones will be invalidated.
This setting is for deployments were you are running your own Husarnet server. As we're using the public one in this tutorial, leave "
default" as the value.
Run the code
Feel free to compile and upload the code to your board now. Connecting to the network for the first time takes a couple of seconds and after that you should see your new device as "Online" in the dashboard. (Go to the "Networks" tab and then click on the network you created in the first step.)
If, for some reason, the code does not compile, look at the "Minimal setup" at the bottom of this tutorial and check whether your project has all the required settings and code.
Reminder - most of the examples here require that you run a Husarnet client on your computer too and that both devices are marked "Online" in our dashboard.
Let's go through available examples now.
Repository URL: simple-webserver
This example makes your ESP32 start a simple HTTP/web-server. It will be available both on the local network and via the Husarnet. You'll find the addres of your device in the dashboard. Put
http://[ip_of_your_device] in your browser's address bar (including the brackets - that's the way you tell IPv6 addresses to your browser) and… voila. You should get a message from your ESP32 now!
If you used one of the examples - everything has already been configured for you and you can skip this step.
In order to use Husarnet on ESP32 you need a special version of
arduino-esp32 framework we've prepared for you. (Hopefully this is a temporary requirement and we will be able to migrate to the mainline version soon.)
Add the following lines to your
The version in the URL may change in the future so have a look from time to time for new releases.
Minimal code to run Husarnet on your ESP32 goes like this: