Wednesday, December 30, 2015

Installing a 3 phase energy meter in your home

About 2 years ago, I decided that it would be a good idea to log how much electricity I was using. If the endeavor resulted into an internet connected device, that would be even better.

So I set out looking for an energy meter. I was looking for one which had an interface which would all me to connect it to the computer (or Raspberry Pi or an OpenWrt device).

Since I have 3 phase supply coming into my house. There are 5 wires coming into my main electrical distribution panel: Red, Yellow, Blue, Neutral and Earth. The RMS voltage between any one of the 3 live wires and neutral is 230 volts.

I selected the Selec MFM383C which is a 3 phase meter with Modbus interface. I also used 3 nos. current transformers: Selec SPCT-62/40-100/5-1.5(1T)-0.5 one each for each phase line (Red, Yellow and Blue). These transformers are of the ratio 100:5.

The connection scheme is as shown below. One has to take care to make sure the direction of wire passing through the current transformer and the order of connection of terminals of current transformers is consistent across all three phases.

Here are photos of my distribution panel after I was done installing:
My house's main electrical distribution panel.
You can see that the various phases have been routed to different category of equipment.
The Red goes to Lights and Fans.
The Yellow one goes to the kitchen - Washing Machine, Microwave and Fridge and to Air Conditioning.
The Blue one powers the 3 water heaters in the three bathrooms.
I printed out the labels and a layout of my apartment to map the switches to the various electrical outlets for easy fault finding and isolation. The main protection device is an RCCB on the top left. There are MCBs for each rooms/ high power appliance.

Here are the detailed photos of front and back side of Select MFM383C.

Here is what the current transformers look like:

100:5 current transformers. each of the three phase cables:
Red, Blue and Yellow pass through each of the current transformer.

Finally I used the following to read the Modbus registers from the energy meter.

  1. Samsung Galaxy Tab 2 7.0
  2. Samsung EPL-1PL0BEGXAR USB Connection Kit
  3. USB to RS485 Serial Bridge (uses FT232RL and MAX485)
  4. Modbus Monitor Advanced app
Using the Modbus Monitor Advanced app to read registers from MFM383C

To know the addresses of the registers, you will need to consult the datasheet for the energy meter:
  1. Selec MFM383C
  2. Select MFM384C
  3. Selec SPCT-62/40-100/5-1.5(1T)-0.5 Current Transformers
Please note that the newer model MFM384C is similar in operation (and connection) to MFM383C but there is difference in the memory organization (addresses) of the various Modbus registers between the two.

Wednesday, December 16, 2015

OpenWrt 15.05 Chaos Calmer on TL MR3020 with rootfs on external usb drive

The latest version of OpenWrt is quite bulky. Although it feats neatly within TL-MR3020's 4MB flash, doesn't leave enough space to install the modules required to shift the root file system to an external drive.

My TL-MR3020 with FT232RL board to access its serial console
and a USB hub with a flash drive in one of its slots.

In previous versions of OpenWRT, you had 840KB of space left on internal 4MB flash to install the essential kernel modules for usb, usb mass storage and ext4 which are all essential for mounting USB flash drive. My Weather Station used TL-MR3020 flashed with OpenWrt 12.09 and used this methods.
In the latest version just about a few hundred kilobytes are left after flashing the image which aren't enough to install the modules required for mounting USB flash drives. So we have to resort to the following steps:

  1. Open up your TL-MR3020 and solder in a USB-UART (FT232RL) board to gain access to its serial port.
  2. Install the toolchain for compiling OpenWRT on your 64-bit Ubuntu Laptop
  3. Compile OpenWrt for TL-MR3020 on your ubuntu laptop. While doing so, we will include the modules required for mounting flash drives and leave out the bulky modules like LuCI web interface that can be installed later on the rootfs which has been relocated to USB flash drive)
  4. Connect your TL-MR3020 to your laptop using Ethernet cable and flash the newly compiled image
  5. Disconnect the ethernet cable between TL-MR3020 and your laptop and connect a USB cable to the soldered USB-UART board to gain access to TL-MR3020's Serial console
  6. Issue commands to TL-MR3020 using PuTTY over Serial and configure it to connect to your WiFi router as client.
  7. Issue commands to TL-MR3020 using PuTTY (over serial or over WiFi) and configure it to mount external USB flash drive as rootfs.
  8. Issue commands to TL-MR3020 using PuTTY (over serial or over WiFi) and configure it to install LuCI and other essential modules that had been left behind when you compiled the image for OpenWrt.
  9. Done. Now you can access your router using your browser as well.

  1. Open up your TL-MR3020 and solder in a USB-UART (FT232RL) board to gain access to its serial port. To do this refer to this image for the pin out. (You only need access to TX, RX and GND):
    Note the location of RX, TX and GND.
    Connect this to a SparkFun FTDI Basic Breakout - 3.3V Board
    Make sure to use only the 3.3V FTDI board
    Make sure you connect TX on TL-MR3020 to RX on FT232RL and
    RX on TL-MR3020 to TX on FT232RL.
    GND on MR3020 will connect to GND on FT232RL
  2. Install the OpenWrt toolchain on your 64-bit Ubuntu laptop.
    Instructions to do this are in my blog post here. The blog post covers how to install toolchain for Attitude Adjustment - a previous version of OpenWrt. In the current case we need to perform this process for the latest version - Chaos Calmer. So please replace all the URLs that point to tools for Attitude Adjustment to that of Chaos Calmer. The respective URLs for Chaos Calmer are at the end of the blog post.
  3. Compile OpenWrt for TL-MR3020 on your ubuntu laptop:
    1. Download to your 64-bit Ubuntu box
    2. Extract the archive:
      bzcat OpenWrt-ImageBuilder-15.05-ar71xx-generic.Linux-x86_64.tar.bz2  | tar -xvf -
    3. Go into the folder and run:
      make image PROFILE=TLMR3020 PACKAGES="blkid block-mount kmod-fs-ext4 kmod-usb2 kmod-usb-uhci kmod-usb-ohci kmod-usb-storage"
  4. Connect Router to laptop using LAN cable. (If you have a new router, use web console to first flash a generic OpenWRT image of some previous version which is known to work fine. Look here on how to do that. Once that is done, use the OpenWrt webpage to flash the new custom compiled image.) Flash the following image using OpenWrt's web console  ~/OpenWrt-ImageBuilder-15.05-ar71xx-generic.Linux-x86_64/bin/ar71xx/openwrt-15.05-ar71xx-generic-tl-mr3020-v1-squashfs-factory.bin
  5. Disconnect the ethernet cable and connect to your router using a USB cable from your laptop to FT232RL board that you soldered to your TL-MR3020. Now use PuTTY or minicom to gain access to console on your router. Look here on how to configure your router to connect to your home WiFi as a client.
  6. Once the router has successfully connected to your home WiFi, you can either continue using serial console or use PutTTY or SSH over WiFi. Issue commands to the router and mount the external flash drive and shift the rootfs to it:
    1. We need to check the path where MR3020 has mounted the flash drive. Execute the command
      ls /dev/sd*
      to know the path. In my case this was /dev/sdb1. In your case it could be /dev/sda1. Use this path in the following steps wherever required.
    2. The remaining steps required to move the rootfs to the flash drive have been taken from this page here. Mount the USB stick and copy the flash /overlay to the USB stick by executing the following four commands in succession:
      1. mkdir -p /mnt/usb
      2. mount /dev/sdb1 /mnt/usb
      3. tar -C /overlay -cvf - . | tar -C /mnt/usb -xvf -
      4. vi /etc/config/fstab
      5. The last of the above four commands will open a text editor. Press I to insert and modify the file. The should look like this:
        config 'mount'
                option target   /overlay
                option device   /dev/sdb1
                option fstype   ext4
                option options  rw,sync
                option enabled  1
                option enabled_fsck 0
      6. After editing the file press Escape on your keyboard followed by colon (:) and then type "wq!" and press Enter to save the file.
        Reboot the router and check if you have gained the extra space of the USB flash drive using the command df -h
         _______                     ________        __
        |       |.-----.-----.-----.|  |  |  |.----.|  |_
        |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
        |_______||   __|_____|__|__||________||__|  |____|
                  |__| W I R E L E S S   F R E E D O M
        CHAOS CALMER (15.05, r46767)
          * 1 1/2 oz Gin            Shake with a glassful
          * 1/4 oz Triple Sec       of broken ice and pour
          * 3/4 oz Lime Juice       unstrained into a goblet.
          * 1 1/2 oz Orange Juice
          * 1 tsp. Grenadine Syrup
        root@OpenWrt:/# df -h
        Filesystem                Size      Used Available Use% Mounted on
        rootfs                    7.2G     17.0M     
        6.8G   0% /
        /dev/root                 2.3M      2.3M         0 100% /rom
        tmpfs                    14.0M     72.0K     14.0M   1% /tmp
        /dev/sdb1                 7.2G     17.0M      6.8G   0% /overlay
        overlayfs:/overlay        7.2G     17.0M      6.8G   0% /
        tmpfs                   512.0K         0    512.0K   0% /dev
  7. Next install the packages which we had left out while compiling OpenWrt. Make sure your TL-MR3020 is connected to home WiFi and therefore the internet and issue these commands to it over PuTTY/SSH:

    opkg update
    opkg install luci
    opkg install kmod-usb-serial-ftdi kmod-usb-acm kmod-usb-serial curl coreutils-stty usbutils bash nano grep getopt ip-full 

    This will install LuCI the web interface as well as few other useful utilities for your IoT projects.
  8. Use web browser to visit from your laptop (use a LAN cable connected between your laptop and TL-MR3020 and your laptop's WiFi must be off)
    Configure whatever your TL-MR3020's settings to whatever you want to.

Friday, November 13, 2015

Hacking a Logitech MK220 wireless keyboard and mouse combo for a community computer

Recently during my stint as a Teach For India Fellow, I have been building Community Computers for use at my school. These involve creating an easy to carry box with projector, computer, speakers and wirless keyboard/mouse all stuffed in. After building one based around Intel NUC PC I decided to think of ways of decreasing cost of the Community Computers and make it more user friendly for our not so tech savvy teachers. Towards that, I thought of using an old non-HDMI VGA projector paired with a Single Board Computer APC 8750 which runs Android OS and has a VGA video output which makes pairing with an old projector possible..

If you think about it, a computer that runs Android would be more successful with teachers as compared to a Windows based PC. Teachers now days have access to smart phone so that are already comfortable with using Android. If you install appropriate apps on an Android based community computer, they can play all kind of content - videos, photos, PDFs etc. Also Android based SBCs boot fast and you don't really have to worry about viruses.

The APC 8750 supports playing of Videos and Photos from a USB flash drive that you plug into it. Here is what the result looked like:

If you notice, there is a small box with colored switches next to the wireless mouse. That box is actually a full fledged wireless keyboard hacked down to retain just the 4 arrow keys along with Esc, Space bar and Enter keys - because those are the only keys that you need to operate the Android OS on APC 8720 for playing videos and photos. It has proven to be a good idea because carrying a cumbersome keyboard to class when you only want to show videos and photos is not such a good idea. Big keyboards are prone to damage in a chaotic classrooms.

Here is how I went about hacking the Logitech MK220 wireless keyboard and mouse:

Step 1: Open up the keyboard and separate out the parts:
You can see the back side of the transmitter circuit and key membrane

Keyboard membrane (the one with silver traces on it), pressure pads sheet (made of rubber) and the transmitter.
Step 2: The membrane separates into two sheets - Top and Bottom. So the next step was to scan the sheet so as to highlight the traces for the keys that I wanted on my compact new remote. Since the length of the membrane sheets was more than what would fit in my A4 sized scanner, I had to perform the scan twice from both ends of the membranes and join the two images. While scanning the two images, I placed a black folder as their background to ensure that there was contrast between the traces of the membrane and the background which would otherwise have been white due to the white sheet usually stuck on glass facing side of the scanner lid.

Two sheets of the membrane lying on top of each other

Black folder providing contrast while scanning

Scanning in progress

Scanned the membranes from one side...

... and then from the other side because they did not fit all together at once in the scanner
Step 3: Next, I used Microsoft Paint to join the images. Then I used IrfanView to decrease their color depth to 2 colors (black and white) and then back to 16 million colors. This ensured that the contrast for the image had been maximized (each pixel was either white or black) and that I could now use the paint bucket tool of Microsoft Paint to trace the tracks on the images. The tracks were white and the background was black,

Decreasing the color depth (IrfanView) and then increasing it again (Microsoft Paint

Joined image. Decreased to depth of 2 colors only.
Step 4: Finally use the paint bucket tool to trace the tracks to the pads of keys of interest on the top as well as the bottom layer of the membrane. Then match the connecting pins to the pads on the keyboard transmitter PCB and make a list of what to pads on the transmitter PCB correspond to pressing of which key.

Step 5: Finally mount tactile switches on a PCB and assemble your own circuit for the wireless remote control. Solder wires from these tact switches to pads on the transmitter PCB corresponding the keys that the new tact switches need to proxy. Fit them all in a small door bell box, add batteries, close the box shut and you are done.

NOTE: Here is the list of various other Community Computers that I built using various other projectors and single board computers over the few months of my fellowship.

Thursday, November 12, 2015

Community Computer using Intel NUC PC

NOTE: Here is the list of various other Community Computers that I built using various other projectors and single board computers over the few months of my fellowship.

As I began my Teach For India fellowship, I got inspired by the K-Yan and decided to build my own community computer for my own class of 48 7th graders. I tried building one using a Raspberry Pi, but the results left much to be desired (Couldn't install Prezi, wasn't powerful enough, video player required commands to operate, no internal storage etc). That's when I came across Intel's Next Unit of Computing (NUC) PCs. These are small form factor PCs built for portable applications like interactive kiosks etc.

Since these PCs only have and HDMI output, I would need an HDMI capable projector. So I asked my friends for donations and went shopping and bought these:
  1. BenQ MS521p [Cheapest HDMI Projector]
    INR 30,000
  2. Intel DN2820FYKH [NUC PC]
    INR 9,340
  3. 8GB Low Voltage 1.35V SODIMM DDR3 RAM Kingston KVR16LS11/8 [For NUC PC]
    INR 3,300
  4. 2.5" SATA 500GB HDD Toshiba MQ01ABD050 [For NUC PC]
    INR 2,870
  5. HP S3000 USB Powered Speakers
    INR 729
  6. Logitech mk220 Wirless Keyboard/Mouse Combo
    INR 1,300
  7. HDMI Cable
    INR 790
So for under INR 48,500 I had managed to collect all the components:

I began by installing the HDD and RAM in the NUC PC and Installing Windows on it using a pen drive. Installing Google Drive on the PC (I have WiFi at school too), allowed me to sync stuff from my home PC to this Community Computer easily

Next I mounted all the components on a piece of plywood (I had to add in a powerstrip and phone charger to power the speakers) and voila!: I had an easy to carry community computer which took no time to setup and assemble at the start of the class because all the components were always wired together beforehand and did not need any connection and disconnection. The setup just had a single 3 core AC mains cable coming out of it. I could now go into my class, setup the projector, and show videos, photos, powerpoint slides, prezis and all kinds of content to my kids.

Carrying it around was still cumbersome and the plywood meant it wasn't sturdy enough.
Also, I had to carry the keyboard and mouse separately.

I was looking for a way to make it more compact and easy to carry.
That when Mr Rohit Garg, who owns a electronics goods and service shop here in Aundh, Pune suggested that I mount it in a casing meant for a voltage stabilizer. I gave him the current setup and this is what he came up with:

This made it immensely more sturdy. But the keyboard and mouse still had to be carried separately.
That and the ventilation was a big issue. I did not want the projector/PC's lives to be diminished just because of lack of ventilation. Rohit had made cut out for vents but somehow it did not seem enough for me. So I did away with the shell, and while I was at it, I added a lunch box to hold the projector's remote and wireless mouse. For the keyboard I added velcro (loop and hook) stickers to make it easy to stow inside the case itself. The Community Computer now had to be carried using two hands which was more comfortable. The keyboard, mouse and the projector remote are now stashed inside the community computer itself.

Community Computer frame with the keyboard and mouse stashed within it

Velcro hook stickers applied to the projector top to attach the keyboard

Velcro loops on back of wireless keyboard

Lunchbox lid attached to the community computer frame to hold the mouse and projector infrared remote
Front View

Intel has recently come up with the Intel Compute Stick. The compute stick is a cheaper replacement for the NUC PC. The stick has 32GB of eMMC storage (its like an SSD Hard Disk) and 2 GB of RAM already inside it. It retails for INR 9,800 on flipkart and comes pre-loaded with Windows 8.1:

NOTE: Here is the list of various other Community Computers that I built using various other projectors and single board computers over the few months of my fellowship.

Thursday, October 29, 2015

A Security Cam that pushes video clips to your phone: Motion Pie + PushBullet


Raspberry Pi running Motion Pie. USB Webcam is connected to it. A bash script pushes small snippets of videos (shot when motion is detected() to your phone.


  1. Signup for PushBullet and install the android app on your phone (and chrome extension on your browser)
  2. Take a Raspberry Pi and connect a Raspberry Pi Camera or USB Webcam to it. I used a UVC USB Webcam.
  3. Format a SD Card on your computer and write the Motion Pie image to it. Here is how to do it.
  4. Connect your Raspberry Pi to your home WiFi router using Ethernet cable. My house has CAT5 wire running through pipes concealed in the walls.
  5. Perform basic configuration on the Raspberry Pi using its web interface. 
    1. Administrator password. (Otherwise you wont be able to connect to it using SSH/PuTTY)
    2. Setting the timezone.
    3. Network setting - I used static IP address.
    4. Enable FTP write support.
    5. Video Device - Camera name or rotation etc.
    6. File storage: I used "network share" option to point back to the RPi's SD card folder shared over the network. Not sure why the default option of saving files to SD card did not work for me.
    7. Motion detection threshold - I set mine to 10% because I also have a motion activated light switch installed outside my door.
    8. Working Schedule - Make sure to cover all days and times of the week
  6. Use PuTTY to install a bash script ( on it.
    1. Make sure to set your PushBullet access token in the script.
    2. Before you can use nano to create a script file , you will need to remount the filesystem to make it writable. to do that issue the command: mount -o remount,rw /
      Read FAQ here.
    3. Mark the script as executable: chmod +x
  7. Configure the Raspberry Pi using its web interface to run the script when Motion is detected. This option is available under "Motion Notification"
  8. Put everything in an enclosure and install it near your door. In my case I used a lunch box to hold the RPi and its power supply (standard 5V USB smart phone charger). I installed the cam near the door and ran its USB came through my concealed wiring pipes embedded in the walls of my apartment.
  9. Wait for motion.


## Your PushBullet Access Token
## Fetch yours from your "Account Settings" Page:
## PushBullet API Documentation link:

## Following bash script function taken from
## It extracts value for a corresponding key from a JSON response.
function jsonval {
    temp=`echo $json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $json_key`
    echo ${temp##*|}

## Get the current date and time
DATESTAMP=$(date +"%Y-%m-%d")
TIMESTAMP=$(date +"%H-%M-%S")

## Get the name of the latest AVI clip shot and placed with a folder on your Raspberry Pi's SD card
LATESTAVI=$(ls -tr1 /home/ftp/sdcard/$DATESTAMP/*.avi | tail -1 | sed 's#.*/##')

## The latest AVI might still be open and being written to.
## So if we try to upload the file rightaway, the file size will be reported to be greater than 25 MB
## and PushBullet will reject it. So we will wait for 30 seconds to allow the system to finish writing the file.
## Ideally we should use lsof utility to wait until the file is done writing, but lsof command is not available on Motion Pie.
sleep 30

## Pushing a file is a 3 step process
## Step 1: Send a request for file upload.
##    PushBullet will respond with a URL to which you can upload your file. (upload URL)
##    PushBullet will also respond with a URL at which the file will be available after upload. (File URL)
##   No push message is sent in this step.
##   File is not uploaded in this step.
##   Documentation:
## Step 2: Upload the file to the URL which was assigned to you in Step 1
##   Documentation:
## Step 3: A push need to be sent for that file. This push can include a message as well as the File URL generated in Step 1.
##   Documentation:

## Step 1: Request file upload
json="$(curl \
--header 'Access-Token: '$ACCESSTOKEN \
--header 'Content-Type: application/json' \
--data-binary '{"file_name":"'"$LATESTAVI"'","file_type":"video/avi"}' \
--request POST \"

## Extract the JSON fields: espesially the Upload URL and File URL




## Step 2: Upload the file
echo "About to  upload $LATESTAVI to $UPLOAD_URL"
curl -i -X POST $UPLOAD_URL -F file=@/home/ftp/sdcard/$DATESTAMP/$LATESTAVI
echo "Done uploading. File now available at $FILE_URL"

## Step 3: Send a push message including a link to the file. 
## If the Push is received on a smart phone, the file will be automatically downloaded to it.
echo "Now pushing the file $LATESTAVI to Devices."
curl \
--header 'Access-Token: '$ACCESSTOKEN \
--header 'Content-Type: application/json' \
--data-binary '{"type":"file","body":"Motion detected at '"$DATESTAMP $TIMESTAMP"'","file_name":"'"$FILE_NAME"'","file_type":"'"$FILE_TYPE"'","file_url":"'"$FILE_URL"'"}' \
--request POST \

My web configuration settings:

Make sure to set admin password. Set the timezone too.
Network settings may be needed if you don't want to use DHCP - it is always
better to ensure static IP address if you want to access
the live video feed using VLC app on your phone or a
computer from inside your own home network.

Enable FTP and Samba write support

Nothing much to change here.

Name the camera. Since I had mounted my camera upside down,
I had to set the rotation to 180 degrees.

Set the storage to Network Share and point it to the IP address of RPi itself.
Enter the appropriate credentials as well.

I set the motion detection threshold to 10% and
turned the frame changes ON (Pink square around the changing parts
of frame where motion is detected)

Working schedule set to cover all days and times.
The location of is mentioned here under Motion Notifications - make sure to perform that setting only after writing the script to the SD card using PuTTY.

Motion Clips and Screenshots

Motion detected notification on phone

Motion clip being played on phone

Motion clip being played on phone

Photos of hardware

All in a lunch box. Switch added to make the thing easy to turn on and off.

5V smart phone charger acting as Power Supply.

There is a security door in field of view of my camera, perhaps I should install
the camera outside this door to get a clear view of the person ringing the door bell

I use a webcam, it has infrared lights, haven't bothered to figure out how
to activate the illumination infrared LEDs over USB.....
...because I have a motion switch which turns on the porch light
whenever someone comes near the door.


  1. motionPie has been renamed to motionEyeOS and lives now at
  2. Get the latest SD Card image from:
  3. Installation instructions can be found here: