Alert via SMS and shutdown the power

I’ve started monitoring the temperature of my Raspberry Pi computer some time ago. I’ve also set up a Chatbot that can give me this information whenever I want. So next, I was thinking to write a Python script that can shut down the power of my Raspberry Pi (in fact, the smart plug where this device is connected ) when a threshold is reached and also alerts before this, via SMS.

The project is made of multiple python scripts:
– this is the main app where all other python scripts/functions are used
–, used to send a message via Twilio sms service using an api
–, used to shutdown the TP link smart plug
I already have the temperature logging in the LogDNA service so it would be easy to use the API to get this info. From here I only have to check if it has reached the threshold( 60 C in my case ) and then take action. Action would mean to call the send SMS function, wait 30 seconds and then call the shutdown function for my smart plug(I have a TP link smart plug ) .

import requests
from datetime import datetime
from datetime import timezone
from sendSMS import sendSMS
from tpplug import send_cmd
import time
from vault import apikey

def main():
    dt =
    timestamp = dt.replace(tzinfo=timezone.utc).timestamp()
    times = int(timestamp-600)
    api_url = '' % (times,int(timestamp))
    headers = {"servicekey": apikey}
    response = requests.get(api_url,headers=headers)
    text = f"Shutting down in 30s due to Pi Temperature: {x} C"
    if int(x) >= 60:

if __name__ == '__main__':
For SMS I’ve used the Twilio SMS service. They provide a phone number and also a Python library and API. They also have examples of how to send an SMS via a python script, so this was pretty easy to use.

# Download the helper library from
from import Client
from vault import account,token,from_number,to_number

def sendSMS(message):
# Your Account Sid and Auth Token from
# and set the environment variables. See
  account_sid = account
  auth_token = token
  client = Client(account_sid, auth_token)

  message = client.messages \

I am using a TP-link smart plug for my Raspberry Pi. I’ve also found a way to use python and run commands on this smart plug. (More info here: So I’ve used some of the python functions and created a python script that executes a command(‘off’) to shutdown the power. The smart plug does not shut down, it only stops powering my Raspberry Pi.

import socket
from struct import pack


commands = {'on'       : '{"system":{"set_relay_state":{"state":1}}}',
            'off'      : '{"system":{"set_relay_state":{"state":0}}}',
            'info'     : '{"system":{"get_sysinfo":{}}}'}

def encrypt(string):
    key = 171
    result = pack(">I", len(string))
    for i in string:
        a = key ^ ord(i)
        key = a
        result += bytes([a])
    return result

def decrypt(string):
    key = 171
    result = ""
    for i in string:
        a = key ^ i
        key = i
        result += chr(a)
    return result

def send_cmd(string):
    sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock_tcp.connect((ip, int(port)))
    data = sock_tcp.recv(2048)
    decrypted = decrypt(data[4:])
    return decrypted

Next I’ve scheduled the script to run every 5 min.
Here is a screenshot from my tests. I had to drop the threshold to 40 C for testing only.

And that’s all folks.

You can find the scripts here:

About the author

Mihai is a Network Aficionado with more than 10 years experience