lets boot up….
For p2p communication with devices, lets spinup the Husarnet client
in both Linux as well as ESP32
follow the guide here for setup https://docs.husarnet.com/docs/begin-linux
I have added my laptop running linux and ESP32 to the husarnet , which can be seen via the husarnet dashboard. https://app.husarnet.com
this blog is about exploring the docs from husarnet.
adding linux to husarnet
create a network in https://app.husarnet.com
get the join code from the add element button
join to husarnet from linux machine now
$ husarnet join fc94:b01d:1803:8dd8:b293:5c7d:7639:932a/xxxxxxxxxxxxxxxxxxxxxxx mydevice
lets add the esp32 device as well
Husarnet.join("fc94:b01d:1803:8dd8:b293:5c7d:7639:932a/xxxxxxxxxxxx", "esp32device");
Husarnet.start();
replace xxxxx with the code from join tab
lets demo the P2P communication from my linux and esp32
laptopServerUDP.py
import socketwith socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s:
s.bind((socket.gethostname(), 8002))
s.listen(1)
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data: break
print(data.decode('ascii'))
husarnet-esp32.ino
#include <WiFi.h>
#include <Husarnet.h>// WiFi credentials
const char* ssid = "<SSID-NAME>";
const char* password = "<SSID-PASSWORD>";// Husarnet credentials
const char* hostName = "esp32demo";
const char* husarnetJoinCode = "fc94:b01d:1803:8dd8:b293:5c7d:7639:932a/xxxxxxxxxxxxxxx";
const char* dashboardURL = "default";// Hostname of your laptop hosting a server
const char* laptopHostname = "mydevice";void setup()
{
Serial.begin(115200);/* Connect to WiFi */
Serial.printf("Connecting to %s", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}Serial.printf("done\r\nlocal IP: %s", WiFi.localIP());/* Start Husarnet */
Husarnet.selfHostedSetup(dashboardURL);
Husarnet.join(husarnetJoinCode, hostName);
Husarnet.start();
}void loop()
{
HusarnetClient client;
uint8_t buf[100];
int buf_s = 0;
int cnt = 0;/* Try to connect to a server on port 8002 on your laptop */
if (!client.connect(laptopHostname, 8002)) {
Serial.printf("connection to \"%s\" failed\r\n", laptopHostname);
delay(1000);
return;
}/* While connected send "Hello world!" + counter value */
while(client.connected()) {
buf_s = sprintf ((char*)buf, "Hello world! %d\r\n", cnt++);
client.write(buf,buf_s);
Serial.printf("sending: %s\r\n", (char*)buf);
delay(1000);
}
}
python3 laptopServerUDP.py
Connected by ('<ipV6_addr_esp32>', 50445, 0, 0)
Hello world! 0
Hello world! 1
Hello world! 2
Hello world! 3
Hello world! 4
Hello world! 5
Hello world! 6
Hello world! 7
Serial logs from arduino esp32
[10001133] initOnTcpThread
[10001134] bind -> 0
[10001138] ngsocket <ipaddrof esp32> listening on 5582
[10001146] Updating hosts file for esp32demo
[10001146] Updating hosts file for master
[10001149] write configdata (len: 381)
[10001221] sending join request to fc94:b01d:1803:8dd8:b293:5c7d:7639:932a
[10001224] waiting until base address is resolved...
[10001226] sending init request...
[10001235] sending join code request: [fc94:b01d:1803:8dd8:b293:5c7d:7639:932a] [xxxxxxxxxxxxxxxx>]
[10003375] HELLO from [<husarnetIP>]:5582 (id: fc94:b01d:1803:8dd8:b293:5c7d:7639:932a, active: YES)
[10003451] HELLO from [<husarnetIP>]:5582 (id: fc94:b01d:1803:8dd8:b293:5c7d:7639:932a, active: YES)sending: Hello world! 0sending: Hello world! 1sending: Hello world! 2sending: Hello world! 3sending: Hello world! 4sending: Hello world! 5sending: Hello world! 6sending: Hello world! 7
lets check the husarnet dashboard once
esp32demo network has 2 elements mydevice( linux machine ) , and esp32demo ( esp32 chip ).
NOTE: these names can be updated on the dashboard as well
Husarnet status
Troubleshootings
- udp connections might not work in some network carriers
all the above tests happened through mobile hotspots instead of router(broadband ) - in case of router, check in firewall.
- if mobile hotspot, try with different carriers, till successfull connections.
make sure to have a hands with this amazing chip ESP32….
that’s all folks take it easy…..