The right / easy way for live two way commands

esp32noob
Posts: 3
Joined: Wed Jul 29, 2020 12:29 pm

The right / easy way for live two way commands

Postby esp32noob » Wed Jul 29, 2020 12:41 pm

Hello.
I am new to ESP32...

What is the right way to create a "live" connection to my server, so the board can receive (and send) live commands, e.g when I press a button in my web server web interface the ESP would trigger the relay.

Sending part (from esp to server) can be easy and would be reliable to trigger a http GET command like example.com/esp.php?cmd=foo
And for simplicity, I can do this part that way^.

But what is the best practice and method for receiving commands ? An "ugly" way would be create a web server on ESP32 and deal with port forwarding and other unreliable things. Or to make the ESP constantly ask for new commands from server using http GET (not great).

I know there is a "sockets" method, but I am confused because of many examples and libraries, and some create "web" sockets, other create "tcp sockets" and I am kind of lost.
Another example I found on youtube involved installing tons of libraries and servers on both ends, not great eider.

What is the simplest and reliable way (sample code or library) to do the ESP32 receiving part ?
Thank you.
Last edited by esp32noob on Wed Jul 29, 2020 2:53 pm, edited 1 time in total.

ESP_Sprite
Posts: 3758
Joined: Thu Nov 26, 2015 4:08 am

Re: The right / easy way for live two way commands

Postby ESP_Sprite » Wed Jul 29, 2020 2:27 pm

No such thing as 'the' right way to do something like this, but can I suggest looking into mqtt? Especially if your server is under your own control, this may be the way to go.

esp32noob
Posts: 3
Joined: Wed Jul 29, 2020 12:29 pm

Re: The right / easy way for live two way commands

Postby esp32noob » Wed Jul 29, 2020 2:59 pm

I cam across mqtt a few more times, so I will give that a try too, if anyone has more thoughts, please share.
I was able to create sockets with PHP too and from esp32 send data with:

Code: Select all

#include <WiFi.h>
[...]
client.connect(ip, port);
client.print("Hello from ESP32!\n\r");
This code seems simple enough, but not sure how to inject new text/commands while the established connection remains active.

ESP_Sprite
Posts: 3758
Joined: Thu Nov 26, 2015 4:08 am

Re: The right / easy way for live two way commands

Postby ESP_Sprite » Thu Jul 30, 2020 7:30 am

esp32noob wrote:
Wed Jul 29, 2020 2:59 pm
This code seems simple enough, but not sure how to inject new text/commands while the established connection remains active.
Assuming you're using PHP for a webpage, that is more-or-less impossible... your PHP program ends as soon as the webpage has finished loading, and the socket closes when your PHP program ends. You need some other more persistent server (e.g. an MQTT server) your ESP can connect to if you want persistent connections. (Well, technically there are ways for a php webpage to stay active for a longer time, but I'd say all of those are hacks that will end up with a lot of side effects. You really want something more persistent here.)

esp32noob
Posts: 3
Joined: Wed Jul 29, 2020 12:29 pm

Re: The right / easy way for live two way commands

Postby esp32noob » Thu Jul 30, 2020 10:29 am

I actually made some progress and am able to send data both ways with raw socket between ESP32 and a PHP script.
If it ends up well, I will post all the details.

I looked at MQTT for a while, but this also seems to involve too many software components.

The way I did it now and my objective is for ESP32 to connect to my site trough wifi and connect to the socket opened at the server end, then data can be sent both ways, so no socket server on the ESP32's end.

My only problem now is that socket is opened inside the Arduino loop() and it cannot run other code while communicating with the server.

How can I put this outside of the loop in order to also loop other code ? (e.g to also update value of a temperature sensor).

Code: Select all

void loop(){
    WiFiClient client;
 
    if (!client.connect(host, port)) {
        Serial.println("Connection to host failed");
        delay(1000);
        return;
    }
 
    Serial.println("Connected to server successful!");
 
    client.print("Hello from ESP32 ");
    client.print(random(100,999));
    client.print("\0");

    Serial.println("Disconnecting...");
    client.stop();
 
    delay(250);
}

Who is online

Users browsing this forum: No registered users and 20 guests