Pushing to Arduino from PHP

Hi Everyone, I am looking for a way to push information from a (web) server (in a data center) to an Arduino.

  1. Arduino (f.ex. Yun) connects to the server. -> Green light -> Connected.
  2. The Server can f.ex. control an LED by sending 1 or 0. (f.ex. with Ajax? or any API?)
  3. In case of disconnect, retry connection. -> 1. (No port forwards, so no "regular" server. I guess.)

Basically I am trying to replace HTTP requests once a minute - with an open (listening) connection.

The only thing similar I could find so far is this,.. Unfortunately without explanation! https://www.youtube.com/watch?v=HlGH-uv3kts Does anyone have an Idea on how to go about this? I would be really thankful. Cheers Alex

What sort of connection do you have between the PHP webapp and the Arduino? If you have a USB serial connection available, that would be simple to use and there are lots of examples demonstrating that.

PeterH: What sort of connection do you have between the PHP webapp and the Arduino? If you have a USB serial connection available, that would be simple to use and there are lots of examples demonstrating that.

Hi Peter, I am actually talking about a "real" server. So it's located in a datacenter - there is no USB Connection available. Thank you, I will add more detail to me question. Alex

If you need to use a network connection, there are two basic approaches.

The best one is to have your Arduino act as a web server hosting a web service which enables it to receive push notifications, and have your PHP webapp act as a client for that web service to send it push notifications.

The alternative is to steal a leaf from the AJAX book and have the Arduino submit a long-running query to the PHP webapp, with some logic to time this out and resubmit periodically. This is much less good for various reasons, but if your service provider won't allow you to take the first approach it may be the only option.

One issue you are going to need to deal with, unless you have a fixed IP address (which is not common on domestic Internet connections), is the IP address of your router will change, possibly from day to day.

So you will need to use some sort of dynamic IP address lookup. You can pay for a dynamic IP lookup (i.e as on online service), but I think its probably easier in this case for the Arduino to make an occasional request to the server, and the server can note the IP address of the request i.e your Arduino.

Also..

You'll need to make sure you can configure your router to allow incoming unsolicited IP traffic and route it to your Arduino, i.e you will need to change the routing / firewall settings on your router

Hi Peter thanks again for your answer. Using the Arduino as a server makes life more difficult when trying to connect. Like I said, I don't want to use port forwarding and also finding the Arduino's IP Adress (or the routers IP better said) then is a problem (DynDNS or alike would help). Not to mention what to do if I want multiple servers running at the same time.

Long story short, I prefer Option 2: A long-running query to a PHP Webapp. So the Arduino connects (1.), then the open connection is used by the server to control the Arduino (2.). If the connection is lost the Arduino reconnects(3.) and the cycle repeats (1.). Thanks a lot!


Hi rogerClark, as mentioned I don't want to use port forwarding. Thereby no Dynamic IP lookup - Or having the Arduino periodically update. (I could also keep using HTTP requests every Minute - but it is not as fast and it causes a lot of traffic) Thanks to you too!

PS: I heard that PushingBox should work, but unfortunately there is not example that proves this,...