Send command to turn screen off?


#1

1) Give a description of the problem
Don’t know if it’s possible to do or how to do it.

2) What is the expected behavior?
When Smartthings Home Security is set to disarmed, turn Raspberry Pi screen on, when Home Security is set to armed away or armed stay, turn Raspberry Pi screen off.

Could this be done using Webcore web request:
http://IPADDRESS:SOMETHING?

These are the commands in Raspberry that turns screen on and off:
vcgencmd display_power 0 to turn off
vcgencmd display_power 1 to turn on

Any ideas?


#2

Does it work if you send the command manually via a url or curl? If you can, then there’s a good possibility webCoRE can do it for you.


#3

That’s one of my problems… I do not know if there is a way to send that kind of commands to Raspberry pi. I’m not sure how or what kind of messages Raspberry listens.

EDIT: here might be something…
http://raspberrywebserver.com/cgiscripting/writing-cgi-scripts-in-python.html


#4

Ooh, I thought you had a working setup where you actually can turn it on/off already. I have a rpi but it’s headless, no way to even try it out :slight_smile:


#5

If you have some coding experience, you could easily write a nodeJS app that listens on a port on your rPi and have it respond to commands. So, something like http://xxx.xxx.xxx.xxx:xxxx/picmd/[cmd] could execute a system command like “vcgencmd display_power 0”. You could even get fancy and add parameters to your API: http://[ipaddress]:[port]/[cmd]/[arg1]/[arg2]. So, for your scenario, http://192.168.1.10:8900/display/0 || 1 (on or off).

This is a beginners guide to installing and using nodejs on the rPI (https://thisdavej.com/beginners-guide-to-installing-node-js-on-a-raspberry-pi/). From there, you’ll want to use Child Process (https://nodejs.org/api/child_process.html) to execute your command.

It might look WAY more complicated than it is. If you aren’t comfortable with nodejs, you could do the same thing with Apache/Nginx/LightHTTPd/etc and any programming language like PHP, Ruby, Python, etc.


#6

Ok, this is new thing for me but I will try. I installed nodejs and now I’m trying to figure out how to create child process and what to write there…and where … :smiley:


#7

What you want to do is follow any nodejs tutorial you can find. Here are ~25 of them: https://codeburst.io/25-node-js-tutorials-1db3b1da0260

The code below should get you started though. Ssh to your rPI and use the following:

This will install express, which is a wrapper around many of node’s basic http commands (https://expressjs.com/):

npm install express --save

nano ~/app.js (this will open your editor)
In your editor, type the following:

var exec = require('child_process').exec;
var express = require('express'),
  app = express(),
  port = 8001;

app.get("/cmd/display/:state", (req, res, next) => {
    exec('vcgencmd display_power ' + req.params['state'], function(error, stdout, stderr) {
        console.log('stdout: ' + stdout);
        res.json('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        if (error !== null) {
            console.log('exec error: ' + error);
            res.json('exec error: ' + error);
        }
    });
});

app.listen(port);

console.log('Command API server started on: ' + port);

Save that file and then in the command line type: node app.js

You should see a console line that states: ‘Command API server started on: 8001’. From there, on your desktop, open a browser to http://[your_pi_ipaddress]:8001/cmd/display/0 to turn off the display and http://[your_pi_ipaddress]:8001/cmd/display/1 to turn it back on.

This is a VERY basic example, and you might need to tweak the ‘vcgencmd’ command (if it’s not in your path or whatnot), but should start you on your way to what you want to do with your rPi.


#8

I’m amazed that there’s people who are so kind. Thank You Core_PHX very much. App seems to work just like it is supposed to.
Might be stupid question but is app.js just an application as any other application which needs to be manually started if Raspberry boots? So do I need to add it somewhere so that it runs at boot?

EDIT: and all the noobs like me… out there… if you have installed pm2 (auto process manager 2) to your Raspberry Pi for example to autostart magicmirror etc, you can add app.js to autostart by typing: “pm2 start app.js” and after that “pm2 save”. Now reboot and check if “pm2 list” shows you app.js process running.


#9

No problem! I’m glad I could help. Yeah, you answered your own question. :wink: PM2 or Forever will both restart the app after a reboot.

What’s funny is that in writing that, I came up with a ton of use cases for my own setup. :slight_smile:


#10

Instead of sending an on/off command, is it possible to trigger an app node.is file? I’m trying to utilize this node to change my security camera’s mode setting from within webcore with a get request. https://github.com/JanLoebel/eufy-node-client-examples