logo elektroda
logo elektroda
X
logo elektroda

How do I set up communication between the ESP8266 and the Raspberry Pi?

Quattro1928 2817 12
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 17068111
    Quattro1928
    Level 3  
    Posts: 63
    Rate: 3
    Hello
    I have to make a simple home automation based on ESP8266 and Raspberry. The principle is to be that through, for example, a web interface that runs on the Raspberry I should be able to change the state of individual outputs on the ESP8266. It is also supposed to work the other way around, i.e. e.g. giving a high state to the input of one esp8266 module should change the state of the output of another ESP, but going through the server on the Raspberry.
    If I didn't need to use a server I would use UDP communication between the two ESP modules, but yes I don't really have much idea how to do this. Could I ask for some guidance on how to do such communication and what protocols to use? I will be programming in the Arduino IDE
  • ADVERTISEMENT
  • #2 17068596
    przemwach
    Level 15  
    Posts: 69
    Help: 16
    Rate: 16
    First of all, on the ESP8266 chips you need to upload a software that reads or writes the state of the inputs/outputs. An example is given at http://blog.nettigo.pl/2015/esp8266/
    Then you write an application on the Raspberry, which periodically or after a refresh by the user sends requests to the ESP8266 about the state of inputs or changes the state of outputs. In the link provided, communication is over HTTP but this is a matter of choice and software.
    The main task is to write an HTTP server on the Raspberry to mediate between the users and the modules. You can use CGI modules, or you can write your own server to parse the HTTP protocol. It is all a question of whether you have an OS to dispose of or whether you write without an OS yourself.
  • ADVERTISEMENT
  • #3 17068767
    TvWidget
    Level 38  
    Posts: 4386
    Help: 471
    Rate: 691
    An HTTP server is not necessary. Implement Websocket in ESP. An HTML5 page loaded into a browser can behave like a classic application. That is, asynchronously receive messages to change inputs and send commands to change outputs.
  • #4 17068783
    Quattro1928
    Level 3  
    Posts: 63
    Rate: 3
    TvWidget wrote:
    The HTTP server is not needed. Implement Websocket in ESP. An HTML5 page loaded into a browser can behave like a classic application. That is, asynchronously receive input change messages and send commands to change outputs.
    .
    So instead of using the raspberry as an HTTP server, use one ESP module that will "manage" the others ?
  • ADVERTISEMENT
  • #5 17068826
    TvWidget
    Level 38  
    Posts: 4386
    Help: 471
    Rate: 691
    You wrote that it should all go through the RPi. I pointed out how to do this without an HTTP server. All you need to do is to prepare an HTML5+code page in JS. It will be an application written in JS, the code of which is designed to be executed in a browser, e.g. a browser on the RPi.
  • ADVERTISEMENT
  • #6 17069238
    przemwach
    Level 15  
    Posts: 69
    Help: 16
    Rate: 16
    Operating without an HTTP server is cumbersome to do for a trivial reason all asynchronous requests that will be executed in the background by JS will be blocked by the browser if they go out to an address other than the downloaded page. As part of testing my HTTP server, I made a small page with asynchronous communication. I ran this page directly from a file on my comp and, despite the presence of a server on the network with data that was being queried by the page asynchronously, the information was not fetched from an 'external' server. In JS, this is a way of protecting against attacks being carried out over HTTP (page code from server A, and data and maybe malicious code from server B, which the user of the page knows nothing about).
  • #7 17069318
    mpier
    Level 29  
    Posts: 817
    Help: 153
    Rate: 141
    Hi. I would do as the author of the first post described. It could be UDP, or it could be TCP. After all, instead of sending messages from ESP to ESP you can send to Raspberry. The only difference is the IP address. Further you can send the information to wherever it suits you. HTML wasn't something to describe the content of a page, transmitted later via e.g. http?
  • #8 17069371
    TvWidget
    Level 38  
    Posts: 4386
    Help: 471
    Rate: 691
    przemwach wrote:
    Service without an HTTP server is cumbersome to perform for the trivial reason of all asynchronous requests
    .
    In the case of Websocket, there are no queries. There is only the connection.

    przemwach wrote:
    will be blocked by the browser if they go to an address other than the downloaded page.
    .
    This is what the Egde browser does. I think the one on the RPi will let you download from a different domain.
  • #9 17069375
    przemwach
    Level 15  
    Posts: 69
    Help: 16
    Rate: 16
    Colleague "mpier" simplified my reasoning regarding communication between modules. The solution good and simple peripheral modules are ESP send cyclic input state to Raspberry and Raspberry in response can send output state to modules and then the response is an acknowledgement that the message was received. It's a cleanup on UDP to keep it simple. Which doesn't absolve you from writing programs to send and receive data over UDP.
    Now on the Raspberry you need to present this data somehow, either a screen and another application, or a small server such as HTTP with a simple page to visualise the data.
    Yes HTTP is a page description language, today also used for data transfer (data from a database is put inside HTTP and presented on a web page) this is how it works today.
  • #10 17072955
    Quattro1928
    Level 3  
    Posts: 63
    Rate: 3
    The issue is this. I can use some server under an open source licence.
    I also got a hint that I can use an openhub server for the project, which will run on Raspberry and do communication over MQTT. What would be the workflow of such a solution?
  • #11 17076916
    przemwach
    Level 15  
    Posts: 69
    Help: 16
    Rate: 16
    You can do it like this:
    - ESP periodically sends the state of the inputs to the Raspberry , where it is written to a file,
    - when the Raspberry gets a request for a web page it sends it with the current input states read from the file,
    - if the Raspberry gets information about a change in the state of the outputs it writes this information to a file,
    - Raspberry after receiving input state from ESP responds within confirmation with output state read from file.

    P.S..
    PHP or CGI scripts are used to read variables, external data in HTTP, depending on what the server has.
  • #12 17076996
    JacekCz
    Level 42  
    Posts: 8670
    Help: 760
    Rate: 1460
    przemwach wrote:
    .
    P.S..
    PHP or CGI scripts are used to read variables, external data in HTTP, depending on what the server has.


    Neither PHP excludes CGI, nor PHP with CGI exhausts all possibilities. As far as I observe the market, CGI has been dead for a good few years.

    E.g. it is very cool to do it in Python, which I have a strong position on the Raspberry. Unlike PHP it is a good general purpose language.

    przemwach wrote:
    .
    Yes HTTP is a page description language, today also used for data transfer (data from a database is put inside HTTP and presented on a web page) this is how it works today.


    HTTP is not, and never has been, a page description language. You have confused it with HTML.

    Something is ringing a bell for you, but you don't know where .... Let's make a working assumption that you are having a bad day.
  • #13 17077303
    przemwach
    Level 15  
    Posts: 69
    Help: 16
    Rate: 16
    JacekCz wrote:

    Neither PHP excludes CGI, nor PHP with CGI exhausts all possibilities. As far as I observe the market, CGI has been dead for a good few years.
    .

    I have only given possible examples. There are many paths to your goal, the choice is yours.

    JacekCz wrote:

    HTTP is not, and never has been, a page description language. You have confused it with HTML.
    .

    My mistake, you're right. Probably out of momentum. HTTP is a protocol for sending hypertext documents.
    Thanks for the comments. [/syntax].

Topic summary

✨ The discussion addresses establishing communication between ESP8266 modules and a Raspberry Pi for home automation, enabling control of ESP outputs via a web interface hosted on the Raspberry Pi and state changes on one ESP to affect another through the server. Suggested approaches include implementing an HTTP server on the Raspberry Pi to mediate requests and responses with ESP8266 devices, using HTTP or UDP protocols for communication. Alternatives such as WebSocket were proposed to allow asynchronous bidirectional communication without repeated HTTP queries, leveraging HTML5 and JavaScript on the client side. Challenges with cross-origin requests in browsers were noted, emphasizing the need for a server to avoid security restrictions. A practical solution involves ESP modules periodically sending input states to the Raspberry Pi, which stores and serves this data via a web interface, while output state changes are communicated back to ESPs with acknowledgments. The use of MQTT with an open-source server like OpenHAB on the Raspberry Pi was also mentioned as a workflow option for managing communication. Programming can be done in the Arduino IDE for ESP modules and Python or other languages on the Raspberry Pi. The discussion clarifies that HTTP is a protocol for data transfer, not a page description language, which is HTML's role.
Generated by the language model.

FAQ

TL;DR: To link ESP8266 and Raspberry Pi, use MQTT or WebSockets; MQTT’s fixed header is 2 bytes; "MQTT is a Client Server publish/subscribe messaging transport protocol." This FAQ shows setups, cross‑origin gotchas, and UDP/TCP fallbacks for Arduino IDE users. [OASIS, 2014]

Why it matters: It helps makers choose a reliable protocol and UI path to control ESP8266 GPIO via a Raspberry Pi without dead ends.

Quick Facts

What’s the simplest way to link ESP8266 GPIO to a Raspberry Pi?

Send the same UDP or TCP messages you’d use ESP‑to‑ESP, but target the Raspberry Pi’s IP. The payload stays the same. The Pi then forwards or visualises the states as needed. This keeps device code simple and centralises routing on the Pi. [Elektroda, mpier, post #17069318]

Do I need an HTTP server on the Pi to control ESP8266?

No. You can skip an HTTP server by using WebSockets from an HTML5+JS page run in a browser on the Pi. The JS app communicates directly with ESPs over WebSockets, enabling real‑time control and feedback without HTTP endpoints. [Elektroda, TvWidget, post #17068826]

How do WebSockets help with real‑time GPIO control?

WebSockets create one persistent connection, so updates push instantly. No repeated HTTP requests, headers, or polling loops. As one expert notes, "In the case of Websocket, there are no queries. There is only the connection." This fits fast I/O toggling and status updates. [Elektroda, TvWidget, post #17069371]

How do I avoid CORS or cross‑origin blocks in the browser UI?

Serve your UI from the same origin as the device API or Pi proxy. Browsers may block background requests if the page origin differs or is opened from a local file. Host the page via a server to prevent these blocks and ensure fetches work. [Elektroda, przemwach, post #17069238]

Can one ESP8266 manage other ESPs without using the Pi?

Yes, if you drop the “must go through Raspberry Pi” requirement. Use UDP or TCP between modules and target peer IPs. This mirrors an ESP→RPi setup, except each ESP directs messages to the other module instead of the Pi. [Elektroda, mpier, post #17069318]

What would an MQTT + openHAB workflow on Raspberry Pi look like?

Run an MQTT broker on the Pi. Each ESP publishes input states to topics and subscribes to command topics. Your automation app (e.g., openHAB) subscribes to states and publishes output commands. MQTT’s fixed header is only 2 bytes, reducing overhead on Wi‑Fi. [OASIS, 2014]

What’s a simple UDP pattern with acknowledgements?

Use a request‑reply‑ack flow.
  1. ESP periodically sends input states to the Pi.
  2. Pi replies with desired output states.
  3. ESP sends an acknowledgement after applying outputs. This keeps UDP simple while confirming delivery. [Elektroda, przemwach, post #17069375]

What minimal HTTP approach works on Raspberry Pi?

Serve a small page and endpoints using Python. It’s widely available on the Pi and suits general tasks. As one expert says, "it is very cool to do it in Python." You don’t need legacy CGI or PHP for this pattern. [Elektroda, JacekCz, post #17076996]

Is HTTP the same as HTML?

No. HTTP is the protocol that transports data between client and server. HTML is the page description language rendered by the browser. They work together but serve different roles in your architecture. [Elektroda, JacekCz, post #17076996]

What firmware should I flash on ESP8266 to expose GPIO control?

Upload an Arduino IDE sketch that reads inputs and sets outputs, then exposes them over your chosen protocol. A basic HTTP example pattern exists; you can adapt it for GPIO control and status reads. [Elektroda, przemwach, post #17068596]

How can I present input/output states to users on the Pi?

Host a small web page on the Pi. On request, read current input states and render them. When a user changes outputs, write the new states for devices to fetch or receive on next cycle. This keeps UI and device sync simple. [Elektroda, przemwach, post #17076916]

Can I serve the HTML UI from a file and still fetch device data?

Avoid opening the UI directly as a local file. Some browsers block asynchronous requests to other servers from file‑origin pages. Serve the UI via HTTP on the same host or domain as your API to prevent blockages. [Elektroda, przemwach, post #17069238]

Is WebSocket more efficient than HTTP polling for toggling pins?

Yes. A single persistent connection avoids repeated HTTP headers and polling delays. This reduces overhead and improves responsiveness when flipping GPIO or streaming state changes. It matches real‑time needs far better than periodic polling. [Elektroda, TvWidget, post #17069371]

What’s a file‑based HTTP workflow if I don’t want MQTT?

Implement a simple exchange on the Pi.
  1. ESPs periodically send input states; Pi stores them in a file.
  2. UI requests read the file and display current states.
  3. UI writes desired outputs; Pi returns them to ESPs in replies. This is straightforward and transparent. [Elektroda, przemwach, post #17076916]
Generated by the language model.
ADVERTISEMENT