2

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.

3

Don't use HTTP, it is the wrong choice for this sort of thing.

Use a messaging based protocol (e.g. MQTT) that way updates are pushed to the device rather than having it poll for them. This cuts down on bandwidth and you get (near) real time notification.

The next question is where to run a message broker. Shared hosting (e.g. LAMP stacks) don't normally allow you to run brokers, but for something small moving the install to something like AWS lightsail will probably be cheaper anyway (but you will be responsible for setup/maintenance/security ).

6
  • Thanks for the answer! I know that a MQTT protocol is better, but I am trying to make a low cost solution with shared hosting. If I use a MQTT broker for example on VPS hosting, how does the MQTT broker communicate with my device, because the broker doesn't know the my public IP adress? Does the device sends the broker some kind of information so that the broker can connect to the device? May 1 '19 at 6:57
  • 1
    The device connects out to the broker, this also gets round working behind NAT routers. HTTP REALLY REALLY is the wrong way to do this. And as I said a AWS lightsail instance is probably cheaper than what you are paying for a shared hosting account.
    – hardillb
    May 1 '19 at 7:35
  • When I use MQTT on AWS lightsail can I there host a website where the user changes the device status and save the status to a database in case that the device losses it's power that it can establish a new MQTT connection and read the device status (before the device went out of power) from the database. May 1 '19 at 8:12
  • You can run anything you like on a lightsail instance it's a full VM (starting at $3 a month iirc). The down side is you are responsible for maintaining it all.
    – hardillb
    May 1 '19 at 8:17
  • Is there a way that the NodeMCU board can memorize it's pin status before it went out of power, so I don't need to save the values to a database May 1 '19 at 8:21

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.