I am working on my home automation project. I have a server on digital ocean. I want to put my php code on that server and that server will connect to my Raspberry Pi. How can I pass messages between my server and Raspberry Pi. I want to control my website over internet.

Turn On Light (from internet) --> Digital Ocean Server--> My Home Router --> Raspberry Pi

And vice versa. So the reason I am writing this question is How to connect these things? Each time server receives message it has to pass message to Raspberry Pi (push) or Raspberry Pi has to check whether there is any message for me or not (poll). But how to do this? How to pass messages between intranet and internet.

UPDATE:I have found some stuff. Can anyone tell me whether it is useful or not. Because I am new to this.


3 Answers 3


It is said already in other answers that you should use MQTT in your case.

But why?

MQTT is The Protocol if your things are behind a firewall in a private network [1]. All tricks are an outbound rule for port 1833 or with some configuration maybe not even that [2].

How will the things change after taking MQTT and not http?

You will need one block more to your combo of

Turn On Light (from internet) --> Digital Ocean Server--> My Home Router --> Raspberry Pi

Your flow would be:

  1. Subscribe for Lights on event on Raspberry Pi (message between Broker and RP)

..later night:

  1. Publish Lights on event on Server (message goes Server -> Broker -> RP)

What is Broker?

Message Broker is a service, that can run in the Digital Ocean and it takes in Publish andSubscription requests. [3]

One such Broker is called Mosquitto and it is open source and easy to install. You install the service and run it. No coding involved, maybe little config. [4]

Publish and subscribe?

If you are familiar with php, you may use it also with Mosquitto [5]. Sample code at least looks straight forward, link contains more examples:


$c = new Mosquitto\Client;
$c->onConnect(function() use ($c) {
    $c->publish('mgdm/test', 'Hello', 2);


for ($i = 0; $i < 100; $i++) {
     // Loop around to permit the library to do its work

echo "Finished\n";


[1] https://mongoose-os.com/blog/why-mqtt-is-getting-so-popular-in-iot/

[2] https://stackoverflow.com/questions/32174272/how-mqtt-works-behind-the-firewall

[3] http://www.hivemq.com/blog/mqtt-essentials-part2-publish-subscribe

[4] https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-the-mosquitto-mqtt-messaging-broker-on-ubuntu-16-04

[5] https://github.com/mgdm/Mosquitto-PHP

  • 1
    If only more responses were as well written as this explanation.
    – gatorback
    Commented Jul 22, 2017 at 13:11

If you do decide to go along the route of MQTT, here's what you need to do on the Pi:

  • Choose a language to write the client code in. The libraries you linked in your edit are HTTP libraries for Node.js and Python, respectively, so your choice will depend on which one you're most comfortable in.

  • Find an MQTT client library for your language. I wouldn't recommend using an HTTP library directly; it's too much work to use MQTT over a raw connection. Instead, use a library like paho-mqtt for Python. There are some examples at HiveMQ.

  • Subscribe to a topic that indicates if the light should be on.

Here's an example of that using paho-mqtt:

import paho.mqtt.client as paho
LIGHTBULB_TOPIC = "lightbulb/state"

def lightbulb_on():
    # Put your code to turn the bulb on here

def lightbulb_off():
    # Put your code to turn the bulb off here

def on_message(client, userdata, msg):
    # Check that the message comes from the topic we expected
    if msg.topic == LIGHTBULB_TOPIC:
        if msg.payload == "on":
        elif msg.payload == "off":
            print("Invalid message. Expected 'on'/'off', got", msg.payload)

client = paho.Client()
client.on_message = on_message
client.connect("example.com", 1883)
client.subscribe(LIGHTBULB_TOPIC, qos=1)


You need only implement lightbulb_on and lightbulb_off, then send the message on or off to the MQTT topic lightbulb/state to toggle the lightbulb's state. This would be done from the device that you wanted to control the bulb from.


You might want to look at samples of MQTT protocol. It will help you connect server an Pi via a broker to enable pub-sub communication.

Try these: https://github.com/bluerhinos/phpMQTT and https://eclipse.org/paho/clients/java/ Just keep an eye on the MQTT spec version the clients/broker support. I am mentioning this, because these are projects from different repositories.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.