Contemplating the best way to connect an arduino to a website

Hello,
I have a Uno controlling our irrigation system. It works well but the only way to change the timing is to disconnect it, bring it to the computer, and reprogram it. So I’m building a web interface to adjust timing parameters. I want the website to send any changes to the Uno and I want the Uno to send changes in status (i.e. “strawberry sprinklers ON”) to the web for display. Sending from the Uno to the website using POST/GET is easy. But because my Uno is connected via a home router I’m not sure of the best way to send data from the website. Here are the options as I see them:

  • Configure my router to allow the Uno to be seen from the web. This is where I’m leaning right now, though I’m not relishing the task.
  • Have the Uno poll the website for changes. This could be as infrequently as every five minutes. For some reason this offends me as a waste of bandwidth even though I know it’s common.
  • Explore the use of websockets. This is enticing but I don’t know enough about it. For instance, can I establish a connection and leave it open 24/7 without using it?

Any thoughts would be helpful. I’m sure there are issues I haven’t considered. And possibly other methods.

Thank you in advance

If you don't want to poll, try MQTT. Be aware that it has some web traffic for keeping the connection alive behind the scenes.

Thanks wildbill, I’d never heard of MQTT. It looks pretty easy on the Arduino side. Might be a learning curve on the PHP side.

Do you think my reluctance to poll is justified? The more I think about it the better that plan looks.

Polling seems fine to me.

Okay. Thanks!

Instead of an Uno get a NodeMCU or WeMO D1 Mini board. Those boards come with an ESP8266 controller, which has WiFi built in. Makes life a lot easier.

wvmarle:
Instead of an Uno get a NodeMCU or WeMOS D1 Mini board. Those boards come with an ESP8266 controller, which has WiFi built in. Makes life a lot easier.

I had a mind to suggest that earlier, but it appears on reading the OP that mmesford already has connection to his router via some secret interface.

If you suggest the WeMOS D1 in place of the "UNO" as most of us would of course do, he will most likely raise the spectre of requiring more I/O which we would then address by recommending a port expander.

It seems his concern is more the web coding.

Paul__B:
it appears on reading the OP that mmesford already has connection to his router via some secret interface.

Could be... but it's not what I read at first, and upon re-reading I'm definitely not sure whether that's the case. OP is perfectly ambiguous about this part!

And of course it can result in other issues.

In order to receive MQTT Broker subscriptions with the PuSubClient MQTT library, which works very well, client.loop() must be ran. Client.loop() is polling.

Sorry folks, I didn’t think it mattered how I connected to the router. In this case it will be using an Ethernet shield. I think I’ve settled on polling the external website because I haven’t heard (here or in any other research) any reason not to.

mmesford:
Sorry folks, I didn’t think it mattered how I connected to the router. In this case it will be using an Ethernet shield. I think I’ve settled on polling the external website because I haven’t heard (here or in any other research) any reason not to.

MQTT over WiFi will make your life so much easier. If you use Node Red as the controller/UI, you don't even need to make a website.

If you want to use a website, then you can use a Wemos D1 Mini for the web server and it can send or receive data over MQTT. Or you could use a Raspberry Pi as your MQTT broker and as an HTTP server.

Disclaimer: I haven't written much HTML in the past two years, and PHP in the past three years. I have some 50 devices in my home and most of them communicate with my Node Red or Home Assistant by MQTT. (If I can figure out how to use MQTT, then it is dirt simple.)

I toyed with a similar project some time ago, and used an ESP32 with the http server library.
It worked OK (but be careful to have a 5V supply that can supply sufficient current otherwise the ESP32 "browns out" whenever the wifi powers up).

In the router, you can map an external port on the router (called port-mapping) to route that incoming IP port to the (static) IP address of the ESP32 web server.
But the issue that you will have to address is that of security. Basically, opening up a router port to connect to an http device presents some security challenges. You don't really want external (hacker) traffic to access your home network.
You could try using an ESP32 https library which also works, but the ssl certificate is non-validated so you will always get a security warning on the remove client (PC) 0 which is OK actually.
Having said that, the level of capability you code into the esp32 sketch is will be quite limited.

In summary - a web server is fine and opening a port in the router is trivial but the security aspects are less so. I would suggest you carefully understand and resolve those security issues before proceeding.
You might want to check the "DMZ" functions of your router (although for a home router they are usually not that good anyway).

Alternatively, what you could do is use an Ethernet shield (of any ethernet/wifi interface) and use email to send data between the outside world and the arduino. It means the arduino has to poll to check for new email. But in this way, you are providing a degree of isolation for security.
Plus, if you use a gmail service, then you can apply filters to prevent anything but the email traffic you want. i.e. filter emails only from your own address.
The nice thing here is that the arduino could also send you emails to tell how how wet your strawberries are.
Plus - email is a well supported protocol, accessible from any PC or smartphone. Implementing other protocols such as MQTT is a fine suggestion, but you are adding complexity to what is in reality a simple requirement. And you don't need any MQTT services running for email.

If it were me I would strongly consider using email for this.

Well, I was planning to have email alerts anyway...

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.