Saturday, December 23, 2017

Orvibo S20 Alexa skill

Looking around, I saw Alexa skills to control the Orvibo S20 switch which need a device that's always on in the network.
My skill was created to get around that, and control the S20 directly. It also allows you to configure a timer to automatically shut the S20 down after X minutes (not through the S20's internal timers though).

The code can be found here: https://github.com/itaybia/OrviboS20AlexaSkill.

Everything runs on AWS using lambda, and it also allows for winding a timer to shut down the S20. NOTICE: everything, including the shut down timer requires an external internet connection, since this skill doesn't use the S20's internal timer, but one from AWS.

The guide below was mostly copied from this link https://developer.amazon.com/alexa-skills-kit/alexa-skill-quick-start-tutorial. I recommend using both, Amazon's has screen shots.


Step 1 (Create the IAM policy and role for the timer feature access permissions)

    Creating the IAM policies for the Lambda function:

  1. Navigate to IAM in your management console: https://console.aws.amazon.com/iam/home#/roles
  2. Select "Policies" in the sidebar.
  3. Click "Create Policy".
  4. Select "Create Your Own Policy".
  5. Enter an appropriate policy name and description like "wiwo_timer".
  6. Paste the contents of \AlexaSkillKit_Code\policy.txt
    • Notice that you need to edit the "Resource" item after you create the lambda function below with the TIMER_ARN.
  7. Click "Create Policy".
  8. Select "Create Your Own Policy".
  9. Enter policy name "lambda_logging"
  10. Paste the contents of \AlexaSkillKit_Code\logs_policy.txt

    Creating the IAM role for the Lambda function:

  1. Select "Role" in the sidebar.
  2. Click "Create New Role".
  3. Enter an appropriate role name ("wiwo_timer") and click "Next Step".
  4. Select "AWS Lambda" within the AWS Service Roles.
  5. Change the filter to "Customer Managed", check the box of the 2 policies you created above, and click "Next Step".
  6. Click "Create Role".


Step 2 (Create your AWS Lambda function that your skill will use)

  1. Download or clone my OrviboS20AlexaSkill github project https://github.com/itaybia/OrviboS20AlexaSkill
  2. Gather the information about your Orvibo S20 socket.
    • Connect the S20 to the power
    • Find the S20's MAC address and IP:
      • In your router, you can probably find the devices connected to it. The S20 is probably going to be written as: HF-LPB100.
      • On a computer in the same network, you can run: "ping HF-LPB100", and see the IP that was resolved. Then run "arp -a", and find the MAC address corresponding to the IP.
    • Setup port forwarding on your router to reach the S20's IP from above, and UDP port 10000. Remember which external port you configured. If you're already using the Wiwo app from the internet, then you probably already have the external port configured to 10000. If you need help with configuring port forwarding, look here http://www.wikihow.com/Set-Up-Port-Forwarding-on-a-Router.
    • Get Your External IP, https://whatismyipaddress.com/ or a hostname that you have for your External IP (through noip, dyndns, etc.).
  3. If you do not already have an account on AWS, go to Amazon Web Services and create an account.
  4. Log in to the AWS Management Console and navigate to AWS Lambda.
  5. Click the region drop-down in the upper-right corner of the console and select either US East (N. Virginia) or EU (Ireland). Lambda functions for Alexa skills must be hosted in either the US East (N. Virginia) or EU (Ireland) region.
  6. If you have no Lambda functions yet, click Get Started Now. Otherwise, click Create a Lambda Function.
  7. Enter a name for the lambda function. "wiwo" for example.
  8. Under Runtime, select Python 2.7.
  9. Select "choose an existing role", and below that select the role you created in Step 1.
  10. Add an "Alexa skills kit" trigger.
  11. Add a "CloudWatch Events" trigger.
    1. Under Rule name give something like "wiwo_timer"
    2. Rule type should be "Schedule expression"
    3. Under Schedule expression write: cron(50 22 28 12 ? 2000). Which is essentially a time in the past, just as a placeholder.
    4. Disable the trigger.
    5. Click Add.
    6. Remember the event's arn (let's call it TIMER_ARN).
      • Copy the TIMER_ARN into the resource field in the wiwo_timer policy created in step 1.6
  12. Click the name of your lambda function (from step 2.7 above). Under the Lambda function code section (leave as Edit code inline and then copy in my code. The code can be found under \AlexaSkillKit_Code\lambda_function.py You will need to edit some variables in the code.
    • WIWO_mac: Is the MAC address of your S20 as you found in step 2.
    • WIWO_port: Your External IP as found in step 2.
    • WIWO_ip: The external port which was configured in the router's port forwarding in step 2.
    • DEFAULT_TIMEOUT: if positive, then when turning the S20 on, it will automatically turn off after this many minutes.
    • WIWO_CLOUDWATCH_TIMEOUT_EVENT_ARN: the TIMER_ARN from step 2.11.6.
    • CHECK_APP_ID: true if you want to add security and allow the function to be used only from your alexa skill, according to the ALEXA_SKILL_APP_ID below.
    • ALEXA_SKILL_APP_ID: if CHECK_APP_ID is true, then this ID will be matched against intent for this lambda function. You will be able to fill it after we create the skill in step 3.
  13. You can test your function by using the Configure test event. Change the name of Hello World as needed and pasted the contents of \AlexaSkillKit_Code\OrviboS20Lambda_TurnOnWithTimeoutTestEvent.xml and then Save and Test.
    • If all works you should see the socket turn on, and then turn off after 15 minutes.
    • You should probably set CHECK_APP_ID in the lambda function to False to test this. At least until you create the Alexa skill and have the actual App ID.
  14. Notice the ARN written on the top right of the page. We'll call it LAMBDA_ARN. It should be something like: arn:aws:lambda:-1:#:function:wiwo.

Step 3 (Create your Alexa Skill and link your Lambda function)

  1. Sign in to the Amazon developer portal. If you haven’t done so already, you’ll need to create a free account. https://developer.amazon.com/edw/home.html#/
  2. From the top navigation bar, select Alexa.
  3. Under Alexa Skills Kit, choose Get Started >.
  4. Choose Add a New Skill.
  5. Name your skill. This is the name displayed to users in the Alexa app. Wiwo, Kitchen socket, Orvibo S20 are all good choices.
  6. Create an invocation name. This is the word or phrase that users will speak to activate the skill. Something like socket, Kitchen socket, Orvibo S20 and so on (wiwo did not work well for me, Alexa did not really understand it). Click Save.
  7. Choose Next to continue to development of the new skill.
  8. In the Intent Schema box, paste the JSON code from \AlexaSkillKit_Code\IntentSchema.txt
  9. Skip over the Custom Slot Types section.
  10. Under Sample Utterances paste in contents of \AlexaSkillKit_Code\Utterances.txt
  11. Choose Next and wait until the interaction model finishes loading, in no more than a few seconds
  12. Select the Endpoint AWS Lambda ARN then paste your LAMBDA_ARN code from step 2.12. Then choose Next.
  13. Under Service Simulator you can test the skill. Write what you would have said to alexa to operate the skill. Logs can be found here https://console.aws.amazon.com/cloudwatch
  14. There is no need to Publish the skill.

Examples:

  • "Alexa, Tell INVOCATION_NAME to turn on"
  • "Alexa, Tell INVOCATION_NAME to turn on for 15 minutes"
  • "Alexa, Tell INVOCATION_NAME to open"
  • "Alexa, Tell INVOCATION_NAME to stop"

Tuesday, June 13, 2017

WOL Proxy - Wake On Lan through a router

I usually put my PC to sleep to conserve power. But I also sometimes need to access it from afar. In order to do this, I use WOL (Wake On Lan) from my phone over the internet.

The problem with WOL over the internet is that some routers (mine included) do not allow port forwarding of the WOL packet to a broadcast IP on my subnet. And since my PC is asleep, it loses its IP binding. Certain routers can get around this by configuring the ARP table to hold on to the IP, but mine doesn't do that as well.

To tackle the issues above, I developed WOL Proxy which is an app that can be installed on an Android device and forwards/broadcasts incoming WOL packets on the subnet. Since I had an old Android phone lying around the house, I put the app on it, and configured the port forwarding of the WOL to reach the phone. The phone in turn broadcasts the port-forwarded WOL packets and wakes the PC.

Although configuring the port forwarding itself may be difficult in some routers, the app is really easy to configure.

Let me know what you think about the app, or if you think any other features may complement it.



Tuesday, June 28, 2016

Windows 10 upgrade issues and fixes

I've recently upgraded to Windows 10 from Windows 7, while the upgrade itself was pretty quick, some issues were found that took some time to deal with. I recommend anyone considering the upgrade to go through the following first.

General Windows 10 issues

Privacy

Windows 10 has issues and by issues I mean some major privacy issues. I recommend anyone upgrading to Windows 10 to go through this site and more or less disable everything shown there (except of course for things you actually use, Cortana for instance, if you use her). As stated, it would probably be wise to visit the site once in a while for updates.

Bloatware

Microsoft seems to have made some money from 3rd party apps in exchange for them being part of the Windows 10 environment, even though you really don't need them. That includes Grooveshark, a Minecraft/Photoshop/Flipboard store links, and a whole bunch of other useless things that I could've downloaded myself if I wanted to.
Thing is, not all of these can be removed easily, and require the use of Window's power shell.

For most apps, the instructions in here are good enough. For the Minecraft link I actually downloaded the game, entered it, and then uninstalled it. That seemed to remove the link completely. No such luck with the Photoshop Express. Although I guess it's only a link, so you could right-click and unpin it from the start panels.


Windows 10 driver/new settings issues

Slow boot

For me, the Bios boot seemed to slow considerably. I use an SSD HD, and still it would take about 20 seconds to get the Windows logo. The fix here was an update to the BIOS itself. I, having an Asus Mobo, downloaded it from Asus's own download links for my Mobo. Unfortunately, the BIOS upgrade did not go all nicely, leading to the next issue.

WOL stopped working

This, I believe has to do with both my BIOS and my Mobo's NIC driver in Windows 10. First off, the BIOS no longer had an "enable" flag in the power options for WOL. But fortunately, it looks like enabling the "Wake by PCI/E" was enough for me.
As for the network adapter's configuration, something seemed to have changed in Windows 10, and having all options enabled in the network adapter's advanced and power options as I had them in Windows 7, no longer worked. The fix seemed to be to disable these two options:
  • Shutdown Wake On Lan
  • Wake on pattern match
and only enabling the "Wake on magic packet" option.
In the "Power management" tab, I had all options enabled. It looked like disabling "Allow the computer to turn off this device to save power" would be a good idea, but for some reason, it didn't work that way.

Wrong resolution on TV screen (not necessarily an issue)

I normally have both a normal monitor and a TV screen connected directly to my PC. It seems that after the upgrade, Windows decided that on the TV screen it would be preferable to have larger text and app images. So far so good, but it seems that in order to do so, it lowered the screen size. In the display settings, my TV screen would still show 1080p as it should, but using something like the following python code, would show that the screen size is 720p:

    import ctypes
    user32 = ctypes.windll.user32
    screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
    print screensize

The best way to see it was to just create a canvas in MSPaint with a height of 720 pixels and watching it fill the TV's screen although it shouldn't have.
Moving the "change the size of text apps and other items" bar all the way to the left fixed the issue and restored the actual 1080p display I wanted to see (the 720 pixel canvas no longer filled the whole screen).
This by itself is probably not an issue as long as the apps you're using are DPI aware. But since all I'm using my TV for is to watch movies, and don't really care about DPI, I kept it as 1080p to be on the safe side.

PlexConnect (AppleTV3)

In another room, I use my AppleTV3 to watch my media with Plex by using PlexConnect on a PC. On Windows 10, PlexConnect had an issue with listening on port 80 because of Window's "World Wide Web Publishing Service" (W3SVC). This service seems to open the port for itself and doesn't allow PlexConnect to connect as it should. Since I don't host any web servers, disabling the service fixed the issue.
Another issue with PlexConnect seems to be that its service doesn't start after a system restart, even in delayed automatic state. To fix this, I added a recovery option for it to try again after 2 minutes.

League Of Legends

My favourite game also had a minor issue. When trying to start it, I received the message: "PVP.net Patcher Kernel has stopped working". To fix this issue I had LOL start with admin rights, which fixed the issue. Other workarounds can be found here.

Friday, December 4, 2015

Useful Programs for the PC

Every now and then, you want something done automatically, which can't be done natively on Windows. That's when a few Google searches find you some free programs which serve the purpose very nicely. For me, Most of these had to be found after Radeon dropped functionality from their Control center in Radeon Crimson.

MultiMonitorTool - http://www.nirsoft.net/utils/multi_monitor_tool.html
This tool allows you to control the arrangement of your displays. This was especially helpful for me since it has command line options to do what's needed. And also, something I like very much, it needs no installation.

OpenHardwareMonitor - http://openhardwaremonitor.org/
No installation free software that shows all the fan/temperature/Load/Clocks in your system. It shows it from anything connected to your pc very neatly with some filtering options.
Still in the Beta phase, but is very promising.

FolderSize - http://www.mindgems.com/products/Folder-Size/Folder-Size-Download.htm
Free software that shows you the size of any file or folder while being able to drill down each folder to find whatever it is that is taking a lot of your storage. Also has a portable, non-installation needed version.
The Pro version can even analyze network drives and has command line options.

Friday, July 3, 2015

Adding audio delay keyboard shortcuts to PLEX

I've recently learned that there is a possibility to add audio delay keyboard mappings for the Plex Home Theater on windows.

To do this, edit the file located in:
C:\Program Files (x86)\Plex Home Theater\system\keymaps\keyboard.xml

Add these entries:
<keymap>
  <global>
    <keyboard>
      <j>AudioDelayMinus</j>
      <k>AudioDelayPlus</k>
      <j mod="ctrl">SubtitleDelayMinus</j>
      <k mod="ctrl">SubtitleDelayPlus</k>
    <keyboard>
  <global>
<keymap>

to the file. That is, add he AudioDelayMinus and AudioDelayPlus under the existing "keyboard" tag if it exists, or create a new keyboard.xml file and write the above in it.

This will assign the keyboard buttons j and k to the AudioDelayMinus and AudioDelayPlus respectively.

To put this together with my Plex + Harmony + FLIRC configuration, I then assigned the channel up and channel down buttons of my "Plex" device (actually configured as a Panasonic TC-P65VT30, as mentioned in: http://programming-headbanging.blogspot.co.il/2014/08/configuring-plex-home-theater-with.html) on the harmony remote to the j and k keyboard keys to be able to change the audio delay while watching a movie through Plex.

There are other short-cuts available as well, generally, all those that were in XBMC's keymap:
http://kodi.wiki/view/keymap

Saturday, June 13, 2015

Connecting to a DHCP disabled router

Recently I had a need to connect 2 routers together to create a single LAN. The main reason for me was actually laziness, since I didn't want to pull another Ethernet cable through the walls.

In order to configure them correctly I followed the instructions found in:

What I didn't follow correctly, was step (3), which is to change the secondary router's IP to one in the main router's IP range.
All was working well, except when I wanted to reconfigure the secondary router, and was unable to do so.

You see, my main router was in the 10.0.0.X range, and the secondary was in the 192.168.2.X range. Since my PC was now receiving an IP in the main router's range, it couldn't connect to the secondary router's web service.

An easy solution would've been to reset the router to the factory settings where the DHCP is enabled. Unfortunately, the reset button on my router didn't work.

To get around that I had to get my PC to change to the secondary router's subnet:
  1. Disconnect the secondary router from the main router
  2. Go into my PC's network adapter
    1. Search for Network Connections
    2. Right-click the Local Area Connection
    3. Click Properties
    4. Click Internet Protocol Version 4 and go into its properties
    5. Change to "Use the following IP address:"
    6. Enter
      1. IP address: an IP in the secondary router's range (for instance 192.168.2.5 in my example) which isn't the IP of the router
      2. Subnet mask: in my case it was 255.255.255.0, but even doing 255.255.0.0 works
      3. Default gateway: the IP of the secondary router in its domain
    7. Press OK until you get back to the Local Area Connection
    8. Right-click the Local Area Connection
    9. Click Disable
    10. Double-click the connection to re-enable it
Now, all that remained was to type the router's IP in my browser and I could now enter it as I should.

To not have to do this again every time I want to access my secondary router, I then changed the IP to be one in the main router's range, and rebooted the secondary router. I then undid the changes above (changing the adapter's option to "Obtain and IP address automatically"), connected the secondary router to the main one's LAN port, and connected the PC to the secondary's LAN port.

At this point, I could enter the secondary router's web service through the new IP I gave it.


Saturday, June 6, 2015

AutoHotKey

A while ago I wanted to find a way to create keyboard short-cuts that weren't available out of the box for my windows PC. Looking around, I came by AutoHotKey (AHK) which seems to not only allow for keyboard short-cuts, but much more. AHK allows you to control all sorts of things on your PC, be it through keyboard inputs, or through scripts running on your machine that pick up changes on the PC to be used in any way you like. 

AutoHotKey can be downloaded here:
After installation it is a simple matter of right clicking a script file and selecting “compile script” to create an executable from it.

The main reason I started using AHK was for helping me use my harmony universal remote to control my PC, and mainly, to use it to open and control my PLEX client. More on this can be found here:

This brings me to my first script, which was naturally, opening the PLEX home theater:
This one uses a SHIFT+ALT+F10 key combination to open the file in:
“C:\Program Files (x86)\Plex Home Theater\Plex Home Theater.exe”, if the PLEX Home Theater executable isn't already running.

Here are the rest of the scripts I've written since then:

KeyPad-Letters: This had to do with my problem finding a way to enter letters in the PLEX search box from my remote, which had only a numpad.
So, with the help of this script, I essentially created a phone-like numpad. The script sends letters while pressing the number buttons of the remote repeatedly like you usually do on the TV or older phones with no keyboard.
So, for instance, if I want to write abg3 I'll be able to do that with the remote by: pressing 2, wait a second (or press Alt-Shift-F8), pressing 2 twice, pressing 4 once, pressing 3 four times.
the Keypad-Letters script can be found at:
This script also uses Alt-Shift-F8 (which I mapped through FLIRC, as explained in the other blog post I mentioned earlier) to stop the 1 second timer so that you could write other characters from the same number immediately without having to wait a second between each letter.

Close Display: By far my most used keyboard short-cut, this one uses WIN-q to close my PC’s display screen just as it would when the computer usually goes to sleep or shuts down. This is extremely useful when you've left your PC for a little while, and don’t want it to go to sleep or shut down. It saves power used by the display, and also protects it from being “burned” if your screen-saver takes a while to start. In general, it makes me feel better knowing that my display isn't sitting there waiting for someone who is not around.

Change Resolution: This script uses WIN-w to immediately set your current display to use a resolution of 1920 x 1200 (which is what my PC display should be using). This was useful to me when using PLEX, as it was run on my 1080p TV, and would sometimes keep my other display in this same resolution after I closed PLEX.

Change PC volume by resolution: This script listens to a resolution change event from the windows operating system, and sets the PC’s volume accordingly. As it happened, the volume I heard on my PC’s headphones was much louder than that heard from my other audio output to my receiver, which was used when watching PLEX on my 1080p TV. Instead of always increasing the volume manually, I created this script to automatically change the volume when my resolution was set to 1080p (which automatically happens when starting PLEX in my case).

One script to include them all (and in a single executable run them):
In order to run all these scripts together, and not as separate executables, I created one other script which essentially includes all the rest. This is simply done by creating another AHK script file that has this:
#Include c:\full\path\to\ahk\script\file\to\be\included.ahk

And to finish this whole thing off, I’ll give you a small tip on how to run scripts that need admin rights. Sometimes, you want a keyboard shortcut in AHK to run a program that needs admin rights. Instead of having to authorize to use it with admin rights every time you run the keyboard shortcut for it, you could run a VBS script that would require the admin rights only once (on startup for instance). After doing it once, it will keep those rights as long as it runs. Saving you unnecessary clicks and hassles. Of course, be wary not to give your admin rights away to any third party executable you know nothing about.
Just create a file with a “vbs” extension, such as ahk_scripts.vbs and add these lines:

Set UAC = CreateObject("Shell.Application") 
UAC.ShellExecute " c:\full\path\to\ahk\executable\to\run\with\admin\rights.exe", "", "", "runas", 1


 As you can see, AHK lets you have a lot more power over your PC. Almost anything you want to do with your PC has a way to do it, and I hope the examples above will give you a few good tools to start building the scripts you need.