Jump to content

Smart home programming applications with Raspberry Pi

Hi,

 

I'm currently trying to set up some quality of life smart home applications with a raspberry pi 4 I recently got. Right now all it has on it is pi-hole to filter out all of the ads on my network. My goal is to be able to have the raspberry pi be able to send a wake on lan packet to my computer when I either tell my google home to turn on my computer and/or when it detects that my phone's mac address has reconnected to my home network. I would also like to be able to have the reverse happen, I want to send a command to my computer (running windows 10 home) to shut down on either of the above conditions (except obviously when my phone's mac address leaves the home network).

 

Here are my basic quesions:

  • What is the most effective way to send a wake on lan packet from a raspberry pi to another device on the same network?
  • Is there a Python library that would allow me to send WOL packets?
  • Would I have to enable remote registry on my windows machine or is it possible to write a custom windows application that listens for instructions from the pi and executes a batch file to shut down?

I can probably figure out how to listen for ifttt requests on the raspi and have the pi execute a command or script in response... but any suggestions on how to do this efficiently would be greatly appreciated!

 

Any other smart home ideas like this with the pi would be welcomed as well. I'm currently working on modifying the pi to act as a smart thermostat as well to work with an AWS server so I can control my apartments temperature from anywhere.

 

Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

You can have look at home assistant.

Magical Pineapples


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Link to comment
Share on other sites

Link to post
Share on other sites

You can use wol and embed it in a script. You do need to enable WoL functionality on your target computer. There's an article about this on the Arch wiki, it should apply to Raspbian too.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, Sauron said:

You can use wol and embed it in a script. You do need to enable WoL functionality on your target computer. There's an article about this on the Arch wiki, it should apply to Raspbian too.

Thanks!

 

I already have WOL enabled but i just use an app from my phone to send the packet. I just need to have it automate itself on certain conditions from a rpi

Link to comment
Share on other sites

Link to post
Share on other sites

From what you have described the windows machine power up and shut down trigger the same on the pie.

 

In which case just make windows turn on the pi after it boots and turn it off before it shuts down.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Hans Christian | Teri said:

Might want to take a look at this for a Python wake on lan script.

Thanks! I made a basic python script to implement that. I assigned my phone's mac address a static IP through DHCP on my router so I could still have the phone utilize pi-hole. Then I have the script ping that IP until it gets a successful ping, upon which it will send a magic packet to wake up the computer.

 

The only issue is that it will keep repeatedly sending magic packets while my phone is on the network, so I've just got to edit it to send once on a successful ping to phone.

 

I'll probably end up setting a variable to count how many wake on lan packets have been sent, and then increment it on each one. Then to make sure it actually wakes up the computer, I'll have the script ping my computer's IP and if the computer is still off it will reset the WOL packet counter to zero and try again.

 

Idk if thats the most effective but it seems like a good shot so far

 

Here's what I've got so far if you're curious:

import os
import time
from wakeonlan import send_magic_packet

hostname = "1.1.1.1" #My phone

while True:
  time.sleep(20)
  response = os.system("ping -c 1 " + hostname)
  if response == 0:
    print("Owner phone detected on network, sending magic packet...")
    send_magic_packet('aa:bb:cc:dd:ee:ff') #Sends WOL packet to computer
  else:
    print("Owner phone offline, retrying...")
    pass

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, SgtBot said:
  • What is the most effective way to send a wake on lan packet from a raspberry pi to another device on the same network?

not sure what you mean by effective. wol is a simple packet like any other but with a specific content and target. id start by looking into what a wol packet looks like and youll understand.

Quote
  • Is there a Python library that would allow me to send WOL packets?

im sure there are but they are not neccessarily neccesary

Quote
  • Would I have to enable remote registry on my windows machine or is it possible to write a custom windows application that listens for instructions from the pi and executes a batch file to shut down?

push2run , google it. does exactly that. triggered by a pushbullet message. can even configure over ifttt directly so no pi neccessary. but i would try looking into wether you can just send windows to sleep from another machine. with shell commands. i know you can from one windows machine to another but not sure about a pi. 

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

14 hours ago, cluelessgenius said:

not sure what you mean by effective. wol is a simple packet like any other but with a specific content and target. id start by looking into what a wol packet looks like and youll understand.

im sure there are but they are not neccessarily neccesary

push2run , google it. does exactly that. triggered by a pushbullet message. can even configure over ifttt directly so no pi neccessary. but i would try looking into wether you can just send windows to sleep from another machine. with shell commands. i know you can from one windows machine to another but not sure about a pi. 

Thanks, I'll look into that. I think I've got some pretty good baseline code for it. For the pushbullet one, I use that as well. I have my google nest hub send a pushbullet command to my phone, which uses Automagic on my phone to send a WOL packet to my pc. I used Automagic because Taskr didn't have native WOL support.

 

Here's the code I have so far, I'll look into implementing that push2run into the code if I can:

import os
import time
from wakeonlan import send_magic_packet

phone = "1.1.1.1" #Phone IP address
computer = "2.2.2.2" #Computer IP address

while True: 
  time.sleep(20) #Ping for computer and/or phone every 20 seconds
  phone_response = os.system("ping -c 1 " + phone) # Initial ping for phone
  if phone_response == 0:
    print("Owner phone detected on network, sending magic packet...") # Sends WOL packet if phone is detected
    send_magic_packet('aa:bb:cc:dd:ee:ff')							
  else:
    pc_response = os.system("ping -c 1 " + computer)  
    if pc_response != 0: # This block checks if waking up the computer was waked
      print("Owner computer still offline, resending magic packet...") # and continues to ping until successful
      send_magic_packet('aa:bb:cc:dd:ee:ff') 
    else:
      print("Owner phone and computer online, standing by...") # If owner and computer are both online, don't send any packets
      pass
  else:
    print("Owner phone offline, retrying...") # If phone is offline, start script over
    pass

 

Link to comment
Share on other sites

Link to post
Share on other sites

@SgtBot 

well i myself had it running like that for a while.

using push2run to shutdown/hibernate over ifttt and pushbullet

and using automate on my smartphone over pushbullet and ifttt to wol

but right now im looking for a way to exclude google and alexa completly from my home setup. looking at habot and similar but with no real luck yet.

o for now im sticking with having google/alexa commands send a pushbullet and then i got a bit of server software to catch pushes and do stuff depending on the command.

what im missing is the ability to turn it around and have my server trigger google/alexa to say stuff when events accour. i know it must be possible since skype and so on already let alexa ring and stuff so a bit more research needed here.

 

anyway i digress.

if you want to use pushbullet to forward google/alexa commands to a server like i do heres the most basic code to quickly catch pushes (although im writing in c# since my server is a surface 3 pro with windows 10 :D but maybe it helps) - but again written in C# and with the help of PushbulletSharp  and so on. im sure theres libraries for python or whatever you wanna use too. and pushbullet themselves have great documentation on their API

using PushbulletSharp;
using PushbulletSharp.Filters;
using PushbulletSharp.Models.Responses.WebSocket;
using System.Web.Script.Serialization;
using WebSocketSharp;

string apikey = "YOUR PUSHBULLET API KEY";
JavaScriptSerializer js = new JavaScriptSerializer();
Client = new PushbulletClient(apikey, TimeZoneInfo.Local);
lastChecked = DateTime.Now;
pushlist = new Dictionary<string, string>();

ws = new WebSocket(string.Concat("wss://stream.pushbullet.com/websocket/", apikey));
ws.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;
ws.OnMessage += (sender, e) => 
{
	WebSocketResponse response = js.Deserialize<WebSocketResponse>(e.Data);

	switch (response.Type)
	{
    	//ping to see if connection is still active, comes in in intervals
        case "nop":
        //Console.WriteLine(string.Format("Updated {0}", DateTime.Now));
        break;

        //notification that something has changed - device,push
        case "tickle":
        //Console.WriteLine(string.Format("Tickle recieved on {0}. Go check it out.", DateTime.Now));
        PushResponseFilter filter = new PushResponseFilter()
        {
        	Active = true,
            ModifiedDate = lastChecked
        };

        var pushes = Client.GetPushes(filter);

        foreach (var p in pushes.Pushes)
        {
        	if (!pushlist.ContainsKey(p.Body + "|" + p.Created))
            {
            	//Console.WriteLine(p.Body + " : " + p.Created);
                pushlist.Add(p.Body + "|" + p.Created, p.Body);

                //if a push is newer than the lastchecked flag
                if (DateTime.Compare(p.Created, lastChecked) > 0)
                {
                	lastChecked = p.Created;
                    
                  	//do whatever you want to do in here. 
                    //probably wanna switch between different push messages.
                    //p.body contains the message of the push for me
                }
            }
        }
        break;

      case "push":
      	break;

      default:
        break;
    }
};

 

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×