Building smart mirror with Android Things.

Krzysztof Król
nomtek
Published in
4 min readNov 28, 2018

Story by Krzysztof Król, Software Developer at Nomtek

We’ve heard that smart mirror building is the new cool thing. There is nothing strange about it, these mirrors look amazing, straight from science fiction. We decided that our office must have one too. Then we had a choice to make. If we wanted to build smart mirror as effortlessly as possible we would use Raspberry Pi 3 with MagicMirror² project. MagicMirror² is great platform that lets user easily customize their mirror looks. It even lets them integrate Google Assistant if they wish to do it. However, since in Nomtek we love Android so much, we decided to use Raspberry Pi 3 with Android Things to build our mirror.

Smart mirror basics

First things first. To build our mirror we needed to get parts:

  • Raspberry Pi 3 with a power supply
  • LCD Monitor
  • HDMI cable
  • MicroSD card
  • Wooden slats
  • Two-way mirror
  • Frame

After buying all the supplies we were ready to go! Fortunately we’ve got an old, not used by anyone monitor in our server room so we made a use out of it. We started with removing monitor from the case. Then we attached Raspberry Pi to it.

Monitor before and after first step. You can see how we attached Raspberry Pi to the monitor in the picture on the right

Our next step was to build a frame that will hold monitor and two-way mirror together.

Monitor in the frame

When the frame was done, it was the moment to focus on the software.

Tell me more about Android Things

Android Things is an operating system designed for low powered IoT devices. It was great choice for us since Things apps are basically the same as smartphone Android applications that we create everyday.

Android Things setup on Raspberry Pi is fairly straightforward. All you need to do is to flash the Android Things image onto the microSD card and then insert this card on the underside of the Raspberry Pi. This guide from Google explains everything in detail.

After we finished Android Things setup we could finally start working on the application that will be displayed by our smart mirror. We decided that our app would show time, weather forecast and interesting quotes. The repository can be found here. The application isn’t much different from any other Android smartphone app so we won’t go into implementation details. We had only one major problem to solve. Android Things operating system doesn’t support screen rotation and our application was displayed in landscape orientation. To remedy this we created simple layout that rotated everything by 90 degrees.

class RotatedFrameLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

init {
rotation = 90f
}

override fun onLayout(changed: Boolean,
left: Int,
top: Int,
right: Int,
bottom: Int) {
super.onLayout(changed, left, top, right, bottom)
val width = width
val height = height
val offset = Math.abs(width - height) / 2
translationX = (offset).toFloat()
translationY = offset.toFloat()
}

@Synchronized
override fun onMeasure(widthMeasureSpec: Int,
heightMeasureSpec: Int) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec)
val offset = Math.abs(measuredWidth - measuredHeight) / 2
val newWidth = measuredHeight
val newHeight = measuredWidth + 2 * offset
setMeasuredDimension(newWidth, newHeight)
}
}
Screen in the frame during app development

After that we attached mirror to the monitor in the frame. And Voila!

Damian Kwaśniak our main smart mirror engineer

We had a lot of fun while building our smart mirror. We are planning to add Google Assistant integration in the future so we can control lights in our office by voice.

Do you have your own smart mirror or maybe you are planning to build one? What kind of features do you wish for your smart mirror to have? Please share your thoughts in the comments.

--

--