Connecting an ESP32 via websocket API

Has anybody used the websocket API to connect an ESP32 to HA? I want to build one ESP32-based device to put in my girl-friend’s apartment but I don’t want to start the hassle with VPNs as neither of us has a VPN-capable router. That would be just too much for one ESP32… I’m looking for libraries, example code, etc.

1 Like

Use the ESPHome integration to natively connect ESP devices to HA.

Did you not read the part about the ESP being in a completely seperate house to Home Assistant?

Yes, I read that.

The communications between ESPHome and HA are encrypted, and HA can connect to ESPHome devices exposed to the Internet.

I assume your fear is that you don’t want to expose the ESPHome to the public internet?

Why don’t you have the same fear about exposing the HA websockets port to the internet?

I mean, unless you have a site to site VPN going, one of these two will have to be accessible to the Internet, so that its counterparty can connect to it.

Read the post again. They do not have the capability of connecting via a VPN at either site.

I read that from the very beginning, Tom. It is why I have not outright suggested they get a site to site VPN.

Exactly how are you proposing that the ESP API be routed over the internet then?

The side with the ESP device needs to port forward incoming TCP connections at the ESP port to the ESP device.

The side with HA needs to use the public IP address of the side with the ESP device. Alternatively it can use a dynamic hostname updated periodically with the public IP on the other side.

None of this requires VPN. It will not work, however, if the side with the ESP is behind carrier grade NAT. In such a circumstance, something like Tailscale would be absolutely necessary — I don’t make the rules.

(Same exact plan would be necessary in the hypothetical case that ESPs could connect to HA’s websocket port and actually use its data feed meaningfully — which it cannot — but everything about the config would be the other way around.)

To clarify:
My gf will not have ANY kind of additional device for pure network purposes, be it a VPN device, a DDNS sensor or whatever. My HA is reachable from the internet through a fixed hostname via https (which is a prerequisite for websockets).
The only viable alternative is a connection originating from the ESP32 device in question. Yes, ESPHome would theoretically be possible with port forwarding but that would require additional devices or services which provide some kind of DDNS solution, so my ESPHome knows the address to connect to.

1 Like

Thanks for the link, Tom, but the answer within is not correct / lacks context.

It is not true that MQTT is the only way to link HA and ESPHome in different networks. You can in fact connect HA using the native protocol to ESPHome devices on separate networks just fine; I do this all the time and it just works.

That said, MQTT is the only way to indirectly link up HA to ESPHome via a third service whose IP is accessible to both sides. Of course, that assumes you already have an MQTT server with a public IP address going. We’ll see if that is what OP already has.

OK then.

What you want (connecting ESPHome to HA websocket) is not possible. ESPHome is not designed to consume the websocket pipe. Sorry.

Look into running a publicly-exposed MQTT and having both HA and ESPHome connect to it. It’s harder but doable — and given your constraints (which rule out anything like a RPi running Tailscale) it is the only option.

I never wrote that I want ESPHome… My question was “Has anybody used the websocket API to connect an ESP32 to HA?”. I believe, you are the one who brought ESPHome into it :smiley:

Edit:
Please note that this is the development section of the forums and I also wrote “I’m looking for libraries, example code, etc.”

1 Like

Ah OK. If you are willing to roll your own Arduino code then, sure, you can do whatever you want. I don’t know many people working directly with HTTPS streams on Arduino, but in principle it should be doable.

You’ll forgive me for assuming that the goal was to do domotics and therefore ESPHome was a reasonable assumption which is what most of us use to hook up ESP devices to HA.

Yes :laughing: Some people actually think about in which section they post their questions… :rofl:

I’m working already for a while on a steampunk-style “analog” thermometer with an ESP32 heart controlling a servo motor with the hand. The hardware works and I got it pulling observation data from the Finnish Meteorological Institute but I’d rather have it pull data from my garden sensor through HA.

1 Like

You should be able to poll a REST endpoint in HA to get that data, using the long term API key for the user you have created. /api/states/entity_id is the endpoint you want.

Jep I know :slight_smile: . I was explicitly looking for websocket examples because of the pub/sub/realtime aspect :slight_smile:

Rest <> websocket.
But I wonder how important RT is between 2 houses.
If you don’t want to build much I would suggest maybe MQTT. You can then use either esphome or tasmota.

Little late to the game, but did you get a resolution to this?