10

Can I send data to an ESP8266 without creating a web server?

I am accessing the GPIO pins of ESP8266 through a web server. Now I want to make an Android app for that. So I want to send data to the 8266 without making a web server—is this possible?

  • By using an Android App, would you be on the same network? – Rohan Nov 7 '18 at 18:03
10

Yes, you can send data to an ESP8266 without using a web server, but you might want use one, or use something functionally related to one.

An ESP8266 is a fairly general purpose computing device with a WiFi radio and a network stack, hence, you can implement just about any reasonable protocol you care to describe in code.

However, it has become quite popular to implement protocols which look and act a lot like miniature web pages intended for human consumption.

ie, instead of your client connecting and doing something like

GET /index.html HTTP/1.1

it might say

GET /gpio/15/value HTTP/1.1

Where the URL refers not to a specific document, but to some piece of data on the device you wish to access. You can do similar things for POST, PATCH, DELETE etc requests.

Unless you are building a page for human consumption typically the data you exchange wouldn't be HTML pages. Often it might be something like JSON instead. So for example

GET /gpio/15/value HTTP/1.1

might trigger a response like

{"gpio": 15, "direction": "in", "value": 0}

Similarly, you can make an endpoint where your client might set a GPIO by saying

POST /gpio/15 HTTP/1.1
{"direction": "out", "value": 1}

It's to some degree a semantic or implementation specific question if the program answering such queries is a "web server" - it could be a web server which runs various helper tasks to deal with the data and gpios (much as a server serving pages might dynamically generate some of their content from database queries), or it could be a dedicated program which both deals with the data and knows how to talk HTTP.

And of course using HTTP to exchange JSON payloads is only one of many ways of doing things - it just happens to be a currently popular one which re-uses many webserver-like concepts, and may to a degree even permit usage of a web browser for testing.


It's also worth keeping in mind that such a scheme tends to work best locally, when the phone and the ESP8266 are clients of the same home WiFi network. If the phone isn't "at home" or it is, but is only on a mobile network, allowing it to reach the ESP8266 would mean allowing external requests into the home network, something preferably avoided. In that case, it's quite popular to use a protocol where both the ESP8266 device and the phone independently reach out to an external relay server, which passes messages between them. MQTT is an example of a scheme often used for a system with a relay-server based architecture.

  • Also, I am curious about implementing DELETE for a port ;-) – Arjan Jul 19 '17 at 21:37
  • 1
    On many linux systems you have to "export" a GPIO before you can use it with the /sys/class/gpio interface. I don't know off the top of my head if you can "unexport" one, but conceptually that could match a DELETE :-) – Chris Stratton Jul 19 '17 at 23:04
2

Yes, you can write a custom TCP server. Or, for lighter weight, use a UDP server.

Either way, get to define your own application protocol on top of TCP/UDP, and have your application send it. And you save on the overhead of HTTP. (HTTP can have around 1000 bytes of overhead per message!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.