Introduction: KiKi the Keyboard Bot

About: All round geek, love tinkering with microcontrollers. pcs. and just making stuff

I quite often need to attach to my PC from work, but I don't like to leave it on all the time. Problem is my old pc doesn't support WOL. It can however be put to sleep and woken back up with the power key on the keyboard. However with no one at home but the dog to prod it into life and so...

KiKi the KeyboardBot was born.

It will link to home assistant over MQTT and has a very simple standalone web page built in as well, it ain't pretty but it gets the job done.

Supplies

3D Printer
PLA Filament

Wire Cutters

Wire Strippers

Soldering Iron

Wemos D1 Mini

Micro 9G Servo

Step 1: Printing the Case the Case and the Servo Horn Extension.

First off start by printing the case, I provide the SKP file should you wish to tinker with it in Sketchup (Maker Edition). I've also packaged the STL file if you just want to go ahead and load it into your slicer and get it printed unmodified.

I Printed the case without supports as the first one I printed it was a bugger to remove them, lack of supports worked fine though, there was a tiny bit of sagg but once its all mounted up its fine.

I printed with a bed temp of 50 and an extruder temp of 200 which was the recommended for the filament I was using.

As with the case I also provide the SKP and STL files for the servo horn, the servo horn didn't print properly for me, in the end I just cut the mounting ferrule off and just glued, cable tied it onto one of the original horns as I was impatient to get this working.

Step 2: Assembly

Remove the 3 Pin header from the end of the Servo lead.

Poke the lead through the servo recess in the case and out through the exit hole in the side.

Now carefully lay the lead down the side of the servo and sit it over the case so that the lead will sit in the recess inside of the hole for the servo, pull the lead tight and gently push the servo home, it should be a nice snug fit.

Dress the lead into the recess on the outside of the case and poke it through the recess for the D1 Mini. Roll the cable back over and poke it back again so that it's looking quite tidy in the case.

Slide the D1 Mini into its housing with the WiFi module to the top. The lead should sit over the top of the module and then feed back under the PCB.

You can now cut the servo lead to an acceptable length.

Remove the D1 Mini by giving it a push from the opposite side of the PCB to the USB socket.

Once it is removed, seperate, strip and tin the three wires of the lead.

Solder them to the under side of the pcb in the following order.

Red -> 5V

Brown -> GND

Orange -> D4

The three pins are all adjacent to one another so it's easy to keep the lead tidy.

Now gently fold the lead under the board toward the centre keeping it nice and flat, the roll it over the top of the board and gently slide the board home. Don't push the D1 all the way in, leave it poking out far enough so it's easy to plug the USB cable in and you can get to the reset button.

If you managed to get the horn printed so it will fit over the capstan on the servo, fit it now so that the top of the horn is more or less parallel with the work top. Fix it in place with the small screw that came with the servo.

If you couldn't get the horn printed, cut off the ferrule and fix it to one of the supplied horns, super glue and a couple of small cable ties did the job for me.

Step 3: Flashing

Dowload the ino file to your arduino projects file into a folder calle KiKi.

Open the file up in the Arduino IDE.

Edit the following lines to suit your Home Assistant setup..

25 const PROGMEM uint16_t MQTT_SERVER_PORT = 1883; (if you have changed the default port)

26 const PROGMEM char* MQTT_CLIENT_ID = "HostName";

27 const PROGMEM char* MQTT_USER = "MQTTUSER";

28 const PROGMEM char* 28 MQTT_PASSWORD = "MQTTPASS";

For the MQTT STATE and COMMAND topics I like to order it location/device/(status/switch) but you can use whatever convention you like.

29 const char* MQTT_ROBOT_STATE_TOPIC = "location/pc/status"; const char*

30 MQTT_ROBOT_COMMAND_TOPIC = "location/pc/switch";

Now edit these further lines to set your devices host name.

159 wifi_station_set_hostname("HOSTNAME");

169 if (!wifiManager.autoConnect("HOSTNAME")) {

Set the Fully qualified domain name of your MQTT server.

175 WiFi.hostByName("MQTTSERVER", MQTT_SERVER_IP);

Plug the D1 Mini into your PC and select the COM port it mounted on.

Open the Serial Monitor.

Upload the file.

Once the upload is complete check the serial monitor.

If it is a new board there will be no settings and you will have to connect to the wireless AP it creates then select the correct Wireless network and enter the password.

When you save the details back it will write them to the device and re-use them each time it restarts.

Now it is connected to your wireless network, your will see in the serial monitor a line similar to ...

http://192.168.1.xxx or what ever the range of your network is.

If you connect to this with a browser you will see a very simple web page with a single button which when clicked will activate the servo arm.

Step 4: Adjusting the Servo Home and Activated Position

I stick a large lump of bluetack to the underside of the case and then squidge it to my desktop so the servo horn sits above the center of the key to be pressed.

Initially I line up on one of the other keys, not the power one as every time I test it will put the pc to sleep.

For my keyboard the Logitech K260 I set the home position to 135 and this sits the servo horn just above the key.

I set the activated location to 120 which pushes the button far enough down to make sure its actually activated.

Obviously you will have to tinker with these settings to get them set up nicely for your keyboard, its just a case of adjusting first the home position setting, uploading the sketch and eyeballing it.

Once your happy with the home position go ahead and set you activated position, for my keyboard its around 15 degrees down from the home position. If you alter your home position try first of all adjusting your activated position to home position - 15. Upload to code to the D1 and see how it works out. It doesn't take much time at all to work it out.

The benefit of using the bluetack is that it will keep the device in place perfectly once you have adjusted your positions properly but if the activated position is too low it will simply pop the device off your desk and do no damage.

Step 5: Hooking It Up to Home Assistant.

On your home assistant instance, open up the file you store your switch settings in and add the following.

- platform: mqtt

name: "YOURDEVICE"

state_topic: "location/device/status"

command_topic: "location/device/switch"

payload_on: "ON"

payload_off: "OFF"

optimistic: false

Making sure you edit YOURDEVICE to something suitable, and that the state/command topics match up with what you set in ino code from the previous step.

You will now want to add the device into you config so it appears on the HA interface.

I list out all my groups, and group definitions in configuration.yaml so simply add the device into there along the lines of...

study:
name: Study

entities:

- switch.study_pc

Check your config is safe by entering

/srv/homeassistant/bin/hass --script check_config -c /home/homeassistant/.homeassistant/

If you see no errors go ahead and restart HA

systemctl restart home-assistant@homeassistant.

Once everything has loaded you should see a new button on you HA interface that will turn your pc on or off via the magic of KiKi.

PS, if you access a machine over ssh you can activate KiKi with wget...

wget -qO- http://ip of kiki/?Req=1 > /dev/null

or stick it in a bash script file call the script, saves having to remember the arguments.