I'm trying to write a little python program for shutdown or Reboot my Raspberry PI, drived by a button connected to an GPIO. The program can show the current status of the raspberry PI (Booting,Running,Halting,Rebooting) via two leds. The python program is executed as daemon, started by a init.d bash script (written using the /etc/init.d/skeleton).
Now I can start/stop/verify the status of the daemon, and the daemon can check the input where the button is connected, to perform the command "shutdown -h now" or "shutdown -r now" .
For show the current status of the raspberry PI, I had thought of send messages to the daemon, using some script in the runlevels directorys, for change the status of the leds. But I don't know how receive message in the python program.
Someone can help me?
Thanks.
Send an SMS message in Python via the REST API. To send an outgoing SMS message from your Twilio account you'll need to make an HTTP POST to Twilio's Message resource. Twilio's Python library helps you to create a new instance of the Message resource, specifying the To, From, and Body parameters of your message.
If you are running any python script and want to send regular updates from your script to your mobile phone through SMS, you can use SinchSMS API to send SMS. Approach : Create an app on Sinch and get the key and secret of the app and use these credentials in the following script to send SMS to your mobile.
There are several methods to send a message from one script/app to another:
For you application a valid method is to use a named pipe. Create it using os.mkfifo, open it read-only in your python app and then wait for messages on it.
If you want your app to do another things while waiting, I reccomend you open the pipe in non-blocking mode to look for data availability without blocking your script as in following example:
import os, time
pipe_path = "/tmp/mypipe"
if not os.path.exists(pipe_path):
os.mkfifo(pipe_path)
# Open the fifo. We need to open in non-blocking mode or it will stalls until
# someone opens it for writting
pipe_fd = os.open(pipe_path, os.O_RDONLY | os.O_NONBLOCK)
with os.fdopen(pipe_fd) as pipe:
while True:
message = pipe.read()
if message:
print("Received: '%s'" % message)
print("Doing other stuff")
time.sleep(0.5)
Then you can send messages from bash scripts using the command
echo "your message" > /tmp/mypipe
EDIT: I can not get select.select working correctly (I used it only in C programs) so I changed my recommendation to a non-bloking mode.
Is not more convenient this version?
With the with
costruct inside the while true:
loop?
In this way, all other code inside the loop is executable even in case of error in the pipe file management. Eventually I can use the try:
costuct
for catching the error.
import os, time
pipe_path = "/tmp/mypipe"
if not os.path.exists(pipe_path):
os.mkfifo(pipe_path)
# Open the fifo. We need to open in non-blocking mode or it will stalls until
# someone opens it for writting
pipe_fd = os.open(pipe_path, os.O_RDONLY | os.O_NONBLOCK)
while True:
with os.fdopen(pipe_fd) as pipe:
message = pipe.read()
if message:
print("Received: '%s'" % message)
print("Doing other stuff")
time.sleep(0.5)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With