Page 1 of 1

Raspberry Pi as web server and ESP8266

Posted: Tue Jun 25, 2019 12:05 pm
by nguterresn
Hello, this is my first time here I hope you can help.
Im using: Raspberry Pi 3 and a NodeMCU. Raspberry Pi 3 has ubuntu server installed and is running a web server. NodeMCU is programmed on Arduino IDE.
So here the thing, I have this plan in my mind:
Image,
but I have some doubts about it.
The plan is to, when a client clicks for example a button on the web page, the server sends some data to the ESP8266. When the ESP8266 receives, replies with a success response and does something (like turning on a LED).
I managed to do this by opening a server in ESP8266

Code: Select all

WiFiServer server(80);
and communicate with Raspberry Pi using TCP.

Code: Select all

client.println("It is open!");
In the server side, Im using PHP to create a TCP connection after some database conditions.
Something like this:

Code: Select all

$fp = fsockopen("192.168.1.150", 80, $errno, $errstr, 30);
            if (!$fp) {
                echo "$errstr ($errno)<br />\n";
             } else {
                fwrite($fp, "open");
                if (!feof($fp)) {
                    $string = fgets($fp, 128);
                    if ($string = "It is open") {
                        header("Location: http://localhost:8080/successunlock.html");
                    } else echo "Error on TCP connection! Server side (MCU)!";
                }
                fclose($fp);
            }
This works, sends "open" to ESP8266, receives "It is open" and redirects user to another web page. But I think it lacks security by using (ALL) ESP8266 as a server and maybe it could be done using ESP8266 as a client or even other way around.

Does anybody has an better idea to establish connection between Raspberry and NodeMCU ?

Re: Raspberry Pi as web server and ESP8266

Posted: Tue Jun 25, 2019 12:27 pm
by scotty101
Worth considering a dedicated Internet of Things protocol like MQTT for this.

On your Pi would install an MQTT "broker" which handles any data. Your webserver publishes messages to the MQTT broker which then sends the messages on to the appropriate ESP module based on the topics that they subscribe to
Each MQTT module can subscribe to specific topics.
Then either you webserver can publish/subscribe to MQTT topics or using WebSockets your browser can directly subscribe/publish.

Re: Raspberry Pi as web server and ESP8266

Posted: Wed Jun 26, 2019 2:39 pm
by nguterresn
Thanks for the reply!
I will try to update this soon to those (maybe) in the same situation :)