8

In my college project (Smart Home System) there's a functionality in which if someone knocks on the door an image has to be displayed on a monitor (in a browser). I am implementing the door knock sensor (Piezo) using an Arduino which somehow has to send commands to the Raspberry Pi to take a photo, which is to be sent to a different computer's browser. There are several other modules like this. Everything is connected to a same WiFi network.

Now I can hopefully make it work somehow using PHP and MySQL and several Ajax requests running constantly, but that's probably not a very neat way to do it. I've heard of node.js and web sockets but I am not sure I have time to learn it. (I can if it's absolutely necessary)

Anyway can anyone tell me which is the right way to implement this type of system? It would be really helpful.

  • 3
    I think we will struggle to identify the best way for you to do this, since so much depends on what you already know - unless someone can point you to an existing codebase (but then you may not learn much). – Sean Houlihane Feb 27 '17 at 10:49
  • For a real-time alert to be triggered in the browser for a change in the server or database, NodeJS would be necessary. The rest of the functionality can be managed by PHP...! – Prashanth Benny Feb 27 '17 at 13:39
9

Push vs Poll

Your proposed solution of sending frequent AJAX requests sounds a lot like polling - you're sending a request every so often to check if the state has changed. It would make far more sense to push changes to the server when the piezo sensor detects a change.

It's the difference between this:

Server: Is there someone at the door? Sensor: No.
Server: Is there someone at the door? Sensor: No.
Server: Is there someone at the door? Sensor: No.
... repeat ad infinitum ...

And this:

Sensor: There's someone at the door!

The first example is polling, and the second is pushing. You can tell which one will have lower power usage, less complex code and reduced network usage.

HTTP or Something Else?

An AJAX request is sent over HTTP, so it's quite heavyweight and requires several TCP handshakes per connection (unless you use Keep-Alive).

It may be worth considering alternative protocols such as MQTT (there's some good explanation in the question 'When and why to use MQTT protocol?', which has a very similar problem to yours).

A message broker like MQTT might be a little bit more powerful than you really need in your current situation, but one MQTT broker could easily be expanded if you chose to add more devices to your smart home network, whereas your current system of AJAX requests would quickly fall apart. Imagine four or five different devices polling each other; it'd quickly lead to your network becoming overloaded and it would be a massive drain on power usage.

Node and Web Sockets

Using web sockets and Node would solve the issue of using push instead of poll, so it would be a good idea in my opinion. However, I suspect polling would work if you really didn't want to learn Node.

If you want an extensible solution that will work when you expand your smart home, definitely go with pushing - it'll save a lot of trouble and tears. If you just want a quick proof of concept, polling will probably work.

My personal advice is that you should either learn web sockets or investigate using a message broker like MQTT. You could use a client library like Mosquitto-PHP (with a guide by HiveMQ) to simplify using MQTT in PHP, or just go with Node and web sockets. I suspect the learning resources for Node and web sockets will be better, but MQTT tends to be favoured for smart home/IoT environments.

|improve this answer|||||

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.