Local Network Access via HTTP Server

Kedar Sovani
Oct 14, 2018 · 2 min read

So you have your smart device on your end-user’s home network. Now you would want your user to access your device over the local network. That’s what we will look at in this post.

Let’s say you are building a smart plug, and you want to toggle the power on or off on your plug. We will create a HTTP URI /power on your device.

  • An HTTP GET on this URI will give us the current state of the output of the plug.
  • An HTTP POST on this URI will update the output state of the plug.

We will create this URI with the following code:

The power_get_handler() and the power_post_handler() are the functions that will get called respectively, whenever an HTTP GET or HTTP POST will happen on the /power URI on the device.

Then in our application, we can start the web server and register these URIs with the web server as:

Now the power_get_handler() and the power_post_handler() functions could be implemented as:

In the power_get_handler() routine above, we just fetch the current output state from the driver, and return it in the HTTP GET response.

In the power_post_handler() routine above, we fetch the user’s request, and modify the driver’s state as the user requested it.

Using the API

For my testing purposes, I could use the following Curl commands to test this implementation:

For my production scenario, my phone app will make these API calls to query/modify the state of my device.

Security

Using the API as defined above, relies on the Wi-Fi network’s security to secure the data exchange between your phone and the device. So as long as the user has a secured Wi-Fi network, these exchanges will not be visible to entities outside of the user’s network.

In case of Wi-Fi networks with Open security, the data will be exchanged in plain-text. You could also use a security layer for exchanging these commands. That is a topic for another article.

The ESP Journal

Unofficial Blog for Best Practices and Announcements about Espressif

Kedar Sovani

Written by

Startups, Learning, Innovation, Internet-of-Things, Technology, Books, Yoga, Leadership, Teaching http://kedar.dumpstack.com

The ESP Journal

Unofficial Blog for Best Practices and Announcements about Espressif