Sam Debruyn

Cloud Data Solution Architect

Specialized in Microsoft Azure, Fabric & modern data stack. Microsoft Data Platform MVP. Public speaker & meetup organizer. FOSS contributor.

Sam Debruyn

Wake up your computer(s) with a simple email using a Raspberry Pi

4 minutes

Introduction

I have a Spotify Premium subscription but I also have a few CDs which are not available on streaming services. I don’t like the whole process of syncing files, that’s why I took a Spotify subscription in the first place.  Google offers a solution with Play Music : you can upload up to 20,000 songs to their servers for free and stream them to all of your devices. I didn’t use this because the Google Play Music app was too slow for my previous phone (Google Nexus S ). So I decided to put my CDs on a computer and use that computer as a server with Tonido . This way I could stream my music over the Internet to all of my devices. As a plus I could also put all my photos on this device so that I could show them to friends or family wherever I am.

Because this was just a standard computer this would cost a lot of electricity while I wouldn’t use it 90% of the time. I would only need it when I wanted to listen to a song I own which isn’t available on Spotify. That’s why I came up with this idea.

I want my computer to be available when I need it and to be in standby mode when I don’t need it. This way the computer boots up in a few seconds when I wake it up. I want to be able to wake it up from all over the world. I have a Raspberry Pi which is always on and doesn’t require much power . So I created a way to send an email to a certain address which then wakes up my computer within the minute.

Requirements

Setup

On the computer

  1. Make sure the computer goes to sleep when you don’t use it. In Windows you can find this setting by searching for power. I have my computer set to go to sleep after 3 hours of inactivity. If you have trouble putting your computer to sleep you can analyze the problems using the powercfg command .
  2. Make sure the computer wakes upon receiving a magic packet. This is a special type of TCP or UDP unicast packet that is processed by the BIOS and wakes up a computer. This also works if the computer is not in standby mode. You probably have to change a setting in your BIOS (press one of the F-buttons at boot time) and in your NIC’s settings. You can view your NIC’s settings in Windows using Device Management .
  3. Find the MAC address of the connected network adapter.

In your Gmail account

Create a new filter for a specific email address. You can use yourusername**+anythingyouwant**@gmail.com

  1. Search for to:theaddressyouchoose
  2. Click on the little arrow left to the search button
  3. Click on the link to create a new filter in the pop-up that appears
  4. Check the following options: skip inbox, mark as read, label new label, never send to spam, never mark as important
  5. Enable IMAP (Settings > Forwarding & POP/IMAP > Enable IMAP)

On your Raspberry Pi

I assume you know how to log in to your Raspberry Pi in command line, over SSH or directly. There is a lot of documentation available on their website.

 1import imaplib
 2import os
 3mail = imaplib.IMAP4_SSL('imap.gmail.com')
 4mail.login('YOUR USERNAME@gmail.com', 'YOUR PASSWORD')
 5answer = mail.select('THE LABEL YOU CHOOSE')
 6if int(answer[1][0]) > 0:
 7  os.system('THE PATH TO THE FILE YOU CREATED IN STEP 2')
 8  mail.store("1:*",'+X-GM-LABELS', '\\Trash')
 9mail.close()
10mail.logout()

Done!

Now you can send an email to the address you choose in the Gmail part above and your computer will wake up within the minute!

You might also like

If you liked this article, follow me on LinkedIn, Bluesky, or other social media to stay up-to-date with my latest posts. You might also like the following 2 posts about related topics: