1

I have a NodeMCU (ESP8266) board that I want to control over the internet. I am trying to find a solution where I don't have to set up any configurations on my router like port forwarding. I came up with the following solution:

enter image description here

I have a website where the user changes the device status (with status I mean for example GPIO5 pin value HIGH or LOW) which is then saved to a database on a shared hosting server. The NodeMCU sends periodically (for example every 5 seconds) a HTTP GET request to the database. According to the value that is received from the database the NodeMCU board changes the pin value to HIGH or LOW. If NodeMCU changes it's status (for example a pin value from HIGH to LOW) the new device status is sent to the database with a HTTP POST request. The device also sends a HTTP POST request periodically (for example every 60 seconds) so the user can monitor the device status on the website.

There are a few problems with this configuration:

  1. There is no real-time connection between NodeMCU and the user (there is always a delay in the device response)
  2. The device sends thousand of queries every day that are a load to the shared hosting server. For example if the GET request is sent every 5 seconds, that gives 17280 queries per day for one device.

So my question is how practical is this configuration on shared hosting or any kind of hosting, what are the alternatives or improvements to this configuration and how to establish a connection with the NodeMCU so that the device sends a GET request to the database only when the device status is changed in the database by the user.

  • 1
    a HTTP request can wait until the server has something to send as a response – Juraj May 1 at 4:02
  • @Juraj please check out my question at stackoverflow stackoverflow.com/questions/55932766/… – Slaven Tojic May 1 at 13:53
  • 1
    you can check if the client is still connected and if not, send the request again. but I don't know how to handle the php side – Juraj May 1 at 14:56
  • 1
    just use websockets; a 2-way always-connected low-overhead protocol supported by webserver platforms and ESPs. You could also use server-sent events, which almost any php box can support, and for which you can semi-easily mod the existing ESP libs to handle. – dandavis May 1 at 17:28
  • @dandavis can I use websockets without the need to configure my router (for example setting port forwarding) – Slaven Tojic May 1 at 18:32
2

Use MQTT.

You need:

  1. The PubSubClient.h library
  2. An MQTT server

For MQTT there are some free ones around on the internet (ask Google). Or if you have a Linux server available for use (e.g., a cheap VPS server from one of many many providers online) then you can run your own with mosquitto.

MQTT works kind of how you describe, but it's not making constant GET requests - it opens a single connection to the MQTT server and realtime requests are sent down it to the client, and back from the client to the server.

Near realtime communication.

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.