logo elektroda
logo elektroda
X
logo elektroda

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

Quattro1928 2370 12
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 17068111
    Quattro1928
    Level 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
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
  • ADVERTISEMENT
  • #2 17068596
    przemwach
    Level 15  
    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  
    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  
    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  
    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.
  • #6 17069238
    przemwach
    Level 15  
    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  
    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  
    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  
    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  
    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?
  • ADVERTISEMENT
  • #11 17076916
    przemwach
    Level 15  
    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  
    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  
    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.
Summary generated by the language model.
ADVERTISEMENT