Enable public access to connected devices in your local network
Tags: networking, iot, api, softwaredevelopment
Published: 2020-07-11 15:57:02

---
title: Enable public access to connected devices in your local network
published: true
date: 2020-07-07 14:43:17 UTC
tags: networking,iot,api,softwaredevelopment
canonical_url: https://codeburst.io/enable-public-access-to-connected-devices-in-your-local-network-c15338da5a3
cover_image: https://miro.medium.com/max/1400/1*iI4ABgRzpBDMSDi4Om8K9w.jpeg
---

#### Three options for public access to a Philips Hue light — go ahead, turn on the lights in my kitchen

In a recent livestream with my teammate Arlemi, we unboxed some lights and messed around with the [Philips Hue Lights API](https://developers.meethue.com/). We figured out how to turn on the light and change the colors using an API.

Then Arlemi queried a [weather API](https://openweathermap.org/) — he wanted to update the light based on the weather forecast.

{% youtube Nu0oMwld5Pk %}

But the light was with me in San Francisco. And Arlemi was in London. So I was the only one who could update the device during the livestream.

How can I let Arlemi, and the viewers, change the lights in my home from the comfort of their own pajamas?

> **Dear impatient readers** : If you don’t care how I did it, skip ahead to the end, and you too can turn on the lights in my kitchen **💡**

## Why can’t anyone else turn on the lights?

You can log in to the Philips Hue mobile app with your Philips Hue account for remote access to your lights.

To use the Philips Hue Lights API directly, there’s a couple options. For example, if I’m sending these API calls from [Postman](https://www.postman.com/) on my laptop, there’s really only two ways to do it.

### Local Area Network

I can send the request from within the same local network. Since my laptop and lights are connected to the same home WiFi, I can use Postman to hit the API. But if I go across the street and use my neighbor’s WiFi, then I’m no longer on the same local network.

![](https://cdn-images-1.medium.com/max/1024/1*ZJ1uSUevuJBmdaNRc3LA-A.png)<figcaption><strong>Local Area Network</strong>: devices connected to the same network can access the light</figcaption>

So unless I’m connected to my local network, I won’t be able to reach my lights without providing additional information.

### Remote access

The Philips Hue Lights API allows me to give additional information to authorize remote access, so my lights can be accessed from a different network.

![](https://cdn-images-1.medium.com/max/1024/1*hXcrAE-tnx2SI4u8TvJvww.png)<figcaption><strong>Remote access</strong>: additional info required to authorize</figcaption>

But I don’t want to share my private account information with anyone else.

Let’s walk through a few ways to enable public access to our light, without sharing personal credentials.

- [**Port forwarding**](#7790) — the most straightforward option, but don’t do it
- [**Proxy in the cloud**](#1cd0) — place a proxy in front of your light, in the cloud
- [**Proxy in the local network**](#a3db) ** ** — place a proxy in front of your light, in the local network

## Option 1 — port forwarding

The most straightforward option is to set up [port forwarding](https://whatismyipaddress.com/port-forwarding) for your connected light. Port forwarding enables external requests to your public IP on a specified port to be forwarded to a connected device.

![](https://cdn-images-1.medium.com/max/1024/1*_aiXzf_97c52ZiwI7IZUag.png)<figcaption><strong>Port forwarding</strong>: light is exposed directly to the internet</figcaption>

**Don’t do this.** Bots and malicious attackers scan IP addresses for exposed and vulnerable devices. And if my Philips Hue light has a vulnerability, an attacker can infiltrate my connected device and access other clients connected to my local network.

Also, you tell the users your public IP address for your home, which can reveal some personal details like your general location.

Next, let’s buffer against potential attacks and obscure our home IP.

## Option 2 — proxy in the cloud

This option places a proxy in the cloud between users and the connected device. This buffers against some unwanted attention. By directing requests through an app, we can build custom checkpoints like validating user inputs or rate limiting into our app.

[![](https://cdn-images-1.medium.com/max/1024/1*OuYeLXhYKO-ucb7pSjcmuA.png)](https://github.com/loopDelicious/hue-go)<figcaption>validate user inputs and rate limiting</figcaption>

Deploy your app on a cloud hosting provider like Digital Ocean or Heroku. This means you can invite users to send requests to the app hosted on the cloud, instead of your home IP address.

Now we control traffic from our friendly users. But we still need to set up port forwarding to route traffic from our proxy, so our light is still exposed to the internet.

![](https://cdn-images-1.medium.com/max/1024/1*PvZlKGOGlfpnM_piF_7Rww.png)<figcaption><strong>Proxy in the cloud</strong>: place a proxy in front of the light</figcaption>

Even though you’re now handling legitimate requests from your users more cautiously, the light is still exposed. There’s nothing standing between potential attackers and the light.

Next, let’s thwart attackers by moving the proxy inside our local network.

## Option 3 — proxy in the local network

The only improvement with this option is the placement of the proxy in the local network. If we move the same proxy from the cloud to the local network, we handle potential attackers more effectively.

This time we set up port forwarding to route traffic **_to our proxy._** Attackers can still hit our public IP, but now we have a barrier between the light and the internet.

Once again, we control the data passed through and returned, but this time **_for every user_**.

![](https://cdn-images-1.medium.com/max/1024/1*uSmYdQC_OowUMMA4GgYpJw.png)<figcaption><strong>Proxy in the local network</strong>: move the proxy inside</figcaption>

## Tradeoffs and additional considerations

We talked about three options to open up your connected device to the public, and a few more considerations to further fortify connected devices. As always, the option you choose will depend on your specific situation.

![](https://cdn-images-1.medium.com/max/1024/1*19myWMQFi2VjmYB7Ly5_6w.png)<figcaption>pros and cons for each option to enable public access to a connected device</figcaption>

### CDN

You can use a **content delivery network (CDN)** in front of any of these three options to obscure your home IP address. A CDN provider like [Cloudflare](https://www.cloudflare.com/ddos/#:~:text=All%20Cloudflare%20plans%20offer%20unlimited,associated%20with%20a%20distributed%20attack.) also offers free protection against distributed denial of service (DDOS) attacks to throttle an intentional, or unintentional, surge in traffic.

> **Rate limiting** : You can add rate limits at the infrastructure-level or application-level. Check out my tutorial [to build application-level rate limits with Node and Redis](https://dev.to/loopdelicious/api-rate-limiting-with-node-and-redis-3f87).

### NAT traversal

You can use a gateway to maintain a **network address translation (NAT)** connection by opening a two-way tunnel between a cloud proxy and your home server. This means you don’t need to configure your router for port forwarding and is secure from attackers who scan IPs.

### Dynamic DNS

You can set up a **dynamic DNS (DDNS)** service for any option that requires router configuration for port forwarding. Check out my tutorial for [using a free DDNS client like ddclient](https://codeburst.io/ddclient-c9a6ac1d8f81) to automatically update your DNS record when your IP address changes, so access to your home server is maintained.

## Here’s what I did

I wanted my Philips Hue light to be accessible to livestream viewers. So I ran an Express server on a Raspberry Pi in my local network (option 3). I used [Cloudflare](https://www.cloudflare.com/) for DNS and [KubeSail](https://kubesail.com/) for NAT traversal so that I didn’t need port forwarding and could direct users to a public domain: [https://light.meowsergirl.com](https://light.meowsergirl.com).

![](https://cdn-images-1.medium.com/max/1024/1*aVvcANbiHeIsWAN2qQDq0A.png)<figcaption>here’s what I did</figcaption>

## Go ahead, turn on the lights in my kitchen

Now, as promised, go ahead and turn on the lights in my kitchen.

- [**Example API calls in Postman**](https://explore.postman.com/templates/10360/turn-on-the-hue-light-in-joyces-kitchen) — to update the lights in my kitchen
- [**Code sample in GitHub**](https://github.com/loopDelicious/hue-go) — to enable public access to your own connected devices

[![](https://cdn-images-1.medium.com/max/1024/1*lKXBTfLghY1Xio58LKLpfQ.png)](https://explore.postman.com/templates/10360/turn-on-the-hue-light-in-joyces-kitchen)

Turning on these lights is more de-lightful when you can see them. So [follow Postman on Twitch](https://www.twitch.tv/getpostman) and tune in to a livestream to see the effects of your API calls in realtime.