Wednesday, January 10, 2018

MNO LAN: Use same MNOs SIM card in your IoT devices like GL-MiFi

One of the big issues with IoT is somehow enabling two end nodes to communicate with each other in the presence of Network Address Translation (NAT)
Now days most ISPs (and Mobile Network Operators or MNOs) use NAT by default and assign you local IP addresses in 10.*.*.* subnet. You usually have to pay an extra fee to get static global IP addresses assigned to you - this could be a security concern.
If your end nodes run embedded linux, the following solutions can be used:


  1. Use DDNS and UPnP together
    DDNS is not usually free, but the manufacturer of your embedded linux device (routers from goodlife of Vstarcam's IP camera) might provide you this service for free as it uses almost nil resources on the server side. Most ISPs have UPnP support disabled on their gateway. In a home environment, if you have your own static global IP for your home router, you can use UPnP and DDNS to your advantage. An end node will use UPnP to request your WiFi Router to open ad forward certain outward facing ports to it and then. It will then detect the global IP address of its parent's gateway (in your case, your own home router) and register it with its DDNS server. You can then use the URL assigned to the device to access it from anywhere in the world.
  2. Use Virtual Private Networking
    Pay for a virtual private networking service and use OpenVPN clients on your end devices and bring them all on the same LAN
  3. Use yaler.net
    Its a paid reverse SSH relay service. Works well for SSH and HTTPS connections to your device
  4. Use a server with a global IP address
    Rent virtual VM from digital ocean or amazon or google and develop software for your devices to send data to or receive data from. this is what most manufacturers do for their IoT appliances.

There is a 5th option if:

  1. You are working on a DIY project - handful of embedded linux devices with SSH access.
  2. Your devices are located in the same geographical area (i.e. within the same telecom circle)
  3. Your devices have 3G/4G modem
The trick is to stick the same network operators SIM card in all your devices. All these devices will be assigned IP addresses on the same subnet and would be able to ping each other.

The 10.*.*.* only allows around 16 million unique IP addresses and so I have reason to believe that in India, different MNO circles would be on different logical LANs

Here are the results of my tests - I used two GL-MiFi with 4G modems in them with Reliance Jio 4G SIM cards. Both the devices were located within Maharashtra (one in Pune and other in Talegaon)

I opened the outward facing ports on GL-MiFi and deactivated the firewall on them.
I then conducted iperf tests and here is what I got. It seems that we can easily transfer data between the devices at around 1-2 Mbps while on the Jio network.

Results. PDF is here.


TurboPower

We all want our smartphone to charge quickly. And different phone manufacturers have either created their own propriety method of charging (Oppo VOOC or MediaTek PumpExpress+ or Motorola's TurboPower or OnePlus's Dash) or have opted to comply with a industry wide standard like Qualcomm's Quick Charge series of standards).
These technology work my allowing a phone to tell the charger to step up the voltage beyond 5 volts so that the power available to the phone for charging the battery is more than what a good 5 volt / 2 ampere wall/car charger can supply.

I have a Moto G4 plus and I wanted to make sure that I had access to fast charging at home/office, in car or while travelling. So I went ahead and got the following accessories

My Moto G4 plus along with the various charging accessories

Voltmeter

USB Voltmeter Stick

  • This small device is what allowed me to check at what voltage/current the various chargers were charging my phone or regular devices.
  • It has a USB male and female receptacle so that it can sit between the charger and the USB cable.
  • It shows the voltage, current as well total mAh consumed during the past few hours - all these 4 parameters are shown on its OLED display. It ofcourse discounts the current it itself requires.
  • A reset button will allow you to reset the energy and time counter - The energy and time counters are stored in its non volatile memory and their values are preserved across power cycles so you need to reset them manually.
  • ₹777.00 at Amazon.in

Original TurboPower Charger that cam with your mobile phone

Wall Charger
  • Comes in the box when you buy your phone
  • There re two varieties of these chargers - one has a USB female socket in it and comes with a  detachable microUSB cable while others have microUSB cable connected to it permanently. I have both of these but prefer the one with USB female socket
  • When I used the voltmeter to investigate the charger I discovered the following:
    • It charges regular devices at 5.0 volts
    • It charges Moto G4 plus and Moto G5s plus at around 9.0 volts
    • It charges Qualcomm QC 3.0 compatible devices at around 11.5 volts
  • Thankfully, its 2 pin has the correct diameter and distance between them to fit snugly in Indian AC main sockets - unlike those sad chargers which have europlugs which hang loosely when used with Indian sockets.
  • ₹1,364.00 at Amazon.in 
Charging a regular device at 5.0 volts

Charging Moto G4 plus at around 9.0 volts


Charging a QC 3.0 Power Bank at around 11.5 volts

TurboPower Car Charger


Motorola Turbo Power 15W Qualcomm 2.0 Quick Charge Car Charger (Black)
Car Charger
  • This charger is small and compact and is an original Motorola accessory which is bought separately.
  • Supports QC 2.0 devices (Qualcomm QuickCharge)
  • When I used the voltmeter to investigate the charger I discovered the following:
    • It charges regular devices at 5.0 volts
    • It charges Moto G4 plus and Moto G5s plus at around 9.0 volts
    • It charges Qualcomm QC 3.0 compatible devices at around 11.5 volts
  • ₹990.00 at Amazon.in 
Charging a regular device at 5.0 volts
Charging Moto G4 plus at around 9.0 volts
Charging a QC 3.0 Power Bank at around 11.5 volts

LCARE Qualcomm QC 3.0 Quick Charge Power Bank


  • Now that I had charger for fixed sockets (Car and AC Mains) I wanted a portable power bank with fast charging technology as well. Motorola's TurboPower seemed compatible with QC 2.0/3.0 so I tried this particular powerbank and was pleasantly surprised.
  • When I used the voltmeter to investigate the charger I discovered the following:
    • It charges regular devices at 5.0 volts
    • It charges Moto G4 plus and Moto G5s plus at around 9.0 volts
  • The QC 3.0 technology is used in input as well as output sides. i.e. when you try to charge the power bank itself using any of motorola's original accessory - the car charger and the wall charger - you notice that those chargers themselves are able to charge the powerbank at around 11.5 volts - so basically you have a power bank that not only charges other devices quickly, but also itself gets charged quickly.
  • A backlit LCD display shows the charge level remaining in percentage numbers and also shows the charging technology being used
  • One downside - the powerbank cannot charge mobile devices when it is itself being charged.
  • ₹1,755.00 at Amazon.in
Powerbank charging  moto G4 plus at around 9.0 volts

Powerbank charging a regular bluetooth headset at 5.0 volts

Sunday, January 7, 2018

Food Silicone food savers + Glass bottles

Plastics! Cancer! Correlation!
And so we move away from plastic bottles to glass bottles.
But glass bottles pretty but fragile.
So what do we do?

We get food huggers!!!
Food huggers seemed onle somewhat useful to me all this while until now
The silicone rubber food huggers will protect the bottom of the bottles - I was worried that I might end up banging them on the floor when I have a sip in the middle of the night.


Color Glass bottles
 +
Silicone food hugger


 =



Amazon.in links:
  1. Food huggers/savers
  2. Glass bottles
Note:

  1. The food huggers come as a set of 4. Only the two larger ones have diameters large enough for the glass bottles linked to above. The smaller two won't fit.
    So if you get 6 bottles, you will need to order 3 sets of food huggers. Of the 12 pieces of food huggers, you will end up using only 6 of them.
  2. Various sized food huggers seem to be colored differently to match the color of the vegetable/fruit they are supposed to protect. Yellow is for lemons, Purple for onions, Green for capsicum, Red for tomatoes. This is a good thing - food huggers come in various colors and so do the glass bottles. So there is a very high chance that you would end up getting matching cushions for each bottle except for the blue one.
  3. These silicone rubber huggers can also be used for green tea flasks as well.
  4. Also, here are some photos on how these huggers can protect half cut vegetables from drying up when left in the fridge.







Friday, January 5, 2018

Adding new elements to OpenWrt's LuCI on GL-MiFi

If you are an IoT enthusiast who uses devices that run OpenWrt (routers from Goodlife, or MT7688), you would have considered creating a plugin for LuCI so that you can expose the functionality of any sensors/actuators that you may have connected to your OpenWrt box via OpenWrt's builtin web console.

This article on openwrt.org's wiki will tell you exactly how to do that.
As maybe obvious from the page, if you want to create dynamically generated web content, you will need to write lua scripts.

When trying out the steps from the wiki article for my GL-MiFi, I had to modify the file locations for placing various script like so:


  1. new_tab.lua had to be placed in:
    /usr/lib/lua/luci/controller/myapp
  2. cbi_tab.lua had to be placed in:
    /usr/lib/lua/luci/model/cbi/myapp-mymodule
  3. cbi_file can be put in the same place as mentioned:
    /etc/config
  4. view_tab.htm had to be placed in:
    /usr/lib/lua/luci/view/myapp-mymodule
Once I had the placed the files in the right location, I could see them in the web UI:



My next step is to modify the lua scripts to run executables in C to query the various sensors that I have connected to my OpenWrt devices and display the latest values within a tab in WebUI.

I use the device specifiy SDK from OpenWrt to compile my C programs into executables.

Later on I will package my lua scripts and C programs into a simple .ipkg packages that one can install on any OpenWrt device

Saturday, August 26, 2017

Build system comparison for C shared library: Manual vs. Makefile vs. Bazel

Here is a simple code for shared library (.so) and an executable that uses it:

Library Source files (libhello.c and libhello.h):


Source for the executable that uses the above library (main.c):

There are many ways to build and execute these sources. Lets look at three of them. The first one is a collection of manual commands that you need to execute in a particular order. The other two uses some form of build system. Before you proceed, make sure to have the C compiler installed on your Ubuntu PC. To do this, execute the following at the command prompt:

sudo apt-get install build-essential git


Manual build:

(Reference: http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html)
Place the three source files in the same directory. Open the command prompt, switch to the same directory and execute the following commands to compile, execute and clean:
  1. Compilation
    1. Compiling the library sources into object files with Position Independent Code:
      gcc -c -Wall -Werror -fpic libhello.c
    2. Creating a shared library from an object file
      gcc -shared -o libhello.so libhello.o
    3. Linking with a shared library
      (prefix "lib" will be automatically added to library name, -L is used to specify search folder)
      gcc -L$(pwd) -Wall -o main.exe main.c -lhello

      if there are any issues, use the command to debug linker: ld -lhello --verbose
  2. Running the executable
    1. Making the library available at runtime
      export LD_LIBRARY_PATH=$(pwd):$LD_LIBRARY_PATH
    2.  Run the binary
      ./main.exe
  3. Cleaning
    1. Execute:
      rm -rf *.o *.so *.exe

Building using Makefile:

Place the three source files in the same directory. Also create and place a new file "Makefile" with the following contents in the same directory:

Open the command prompt, switch to the same directory and execute the following commands to compile, execute and clean:
  1. Compilation
    • make all
  2. Running the executable
    1. Making the library available at runtime
      export LD_LIBRARY_PATH=$(pwd):$LD_LIBRARY_PATH
    2.  Run the binary
      ./main.exe
  3. Cleaning
    • make clean

Building using Bazel:

To use Bazel (The build system from Google), you need to first install it. Do so using following commands:

  1. Install JDK 8
    • sudo apt-get install openjdk-8-jdk
  2. Add Bazel distribution URI as a package source (one time setup):
    • echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
    • curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
  3. Install and update Bazel
    • sudo apt-get update && sudo apt-get install bazel
Next, create a directory structure like so:

Current folder
├────WORKSPACE (Blank file)

└───main (folder)
     ├───BUILD (Bazel build script)
     ├───libhello.c
     ├───libhello.h
     └───main.c

The content of the BUILD file should be as follows:

Now carry out the following commands to build the file using Bazel from the command prompt after changing to the directory with blank "WORKSPACE" file:
  1. Compilation
    • bazel build //main:all
  2. Running the executable
    • ./bazel-bin/main/main.exe
  3. Cleaning
    • bazel clean
Compiling, running and cleaning using Bazel

Output files created in the working directory by Bazel
By default, (when linkstatic option is not specified in BUILD file), Bazel links libraries to executables statically. To disable that, specify linkstatic = 0

Monday, July 17, 2017

My favourite alias in .bashrc on Ubuntu


Colorizing and handling system/kernel logs
  • alias logread='cat /var/log/syslog | ccze -A'
  • alias logclear='sudo truncate -s 0 /var/log/syslog'
  • alias logsave='cat /var/log/syslog | ccze -h > ~/log.html'
  • alias dmesgread='dmesg | ccze -A'
  • alias dmesgclear='sudo dmesg -c'
  • alias dmesgsave='dmesg | ccze -h > ~/log.html'
You will have to install ccze (sudo apt-get install ccze)


Thursday, July 13, 2017

Replacing batteries in Black and Decker DP240 2.4V Direct-Plug Rechargeable Screwdriver

I have owned a Black and Decker DP240 Cordless Electric Screwdriver for 5 years now. Its battery had started losing its capacity to hold charge, so I looked online to see if it was possible to change its batteries. I couldn't find any blog/DIY guide on which batteries to replace the internal ones with and how to go about pry open the screwdriver and putting it back. So here are some photos outlining how I changed the batteries in mine and turned it into new.

To open the screwdrive, you will need a set of "Torx Pin 6 lobe" screwdriver set. You cant use regular torx screwdriver bits. You need ones with holes in the center of the bits so that the center pin on the screw can fit into those holes. You can buy just a set of just the bits here.

Torx pin 6 lobe Screwdrivers to open the shell

Screwdriver with the shell opened

It seems that the screwdriver comes with two AA size
1.2V NiCd 800 mAh cells installed


I replaced the 800mAh batteries with 2550 mAh NiMH cells
eneloop by Panasonic is the best brand of NiMH cells


Soldering wires onto the cells

And putting the screw driver back together


Thursday, May 11, 2017

Come C tricks

Swapping the values of two variables with out using a third!!
x = x ^ y;
y = x ^ y;
x = x ^ y;
All hail the XOR operation
Reference: https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/xor.html


int c;
c &= (c-1);
check if c == 0

This allows you to check if C is a power of 2 (i.e. 0,1,2,4,8...)



Reading sentences (including spaces) from stdin:

scanf("%s",str); can't read spaces in input string.
To do that, you have to: scanf(" %[^\n]s",str);

Note the space before % within the quotes.

Reference: https://gpraveenkumar.wordpress.com/2009/06/10/how-to-use-scanf-to-read-string-with-space/

Monday, May 1, 2017

Set Elon's latest tweet as your desktop wallpaper

If you have an Ubuntu box, you can use a script to to set your wallpaper's to Elon's Latest Tweet.

Here is how you setup:
  1. Install required packages.
  2. Download tweet.sh - an OAuth based twitter client implemented completely as a bash script.
  3. Get Token keys for from your Twitter account and save it in the config file. You need this for OAuth that twee.sh uses.
  4. Get a free account on http://www.page2images.com/ and fetch your API keys. This service allows you to generate screenshot of any supplied URL - you can use this service to to fetch PNGs's of Elon's tweets. We do screenshots because aren't just interested in tweet's text but also any images that Elon chooses to attach with his tweets.
  5. In the batch script (elon_tweet.sh) appended below, configure the pages2images API key
  6. Setup the batch script (elon_tweet.sh) as a cron job which runs once every minute.
You can set this script up on a Raspberry Pi running Raspbian connected to your TV as well. Its always nice to have some inspiration trickling in while you work.



Here is how my desktop looks like
Here's how to go about putting the pieces together:
  1. Open a terminal window and install the required software on your ubuntu box:
    sudo apt-get install jq imagemagick pngcrush nkf
  2. Clone https://github.com/piroor/tweet.sh and place tweet.sh and tweet.client.key in your home directory
  3. While logged into Twitter, visit https://apps.twitter.com/ and create a new app and generate the required Token and Keys and place them in tweet.client.key
  4. Mark tweet.sh as executable:
    sudo chmod +x tweet.sh
  5. Register on http://www.page2images.com/ and get your API key
  6. Create a new script called elon_tweet.sh in your home folder and copy and paste the code snippet appended below.
  7. Edit elon_tweet.sh and insert the pages2image API key at the appropriate location.
  8. Mark elon_tweet.sh as executable:
  9. Set the script to run once every minute:
    crontab -e
    and insert the following line:
    * * * * * bash ~/elon_tweet.sh
The script will run once every minute and will check if there is any new tweet from Elon since last check. If there is, it will submit the URL for screenshot generation and exit. When the script is run again at the next consecutive minute, it will fetch the image, crop it and set it up as wallpaper. If Elon has put up a new tweet, the URL of this new tweet is submitted for screenshot and the script exits. In such situation, you will miss out the 2nd tweet if the time difference between 1st and 3rd tweet is less than 2 minutes - this can be fixed in the script, but it seemed to be a bit much. Thank God Elon doesn't go crazy on twitter. All the tweets are saved in the Pictures folder.

Tweet screenshots are saved in Pictures folder
This is what the cropped tweet looks like

This is what the cropped tweet looks like


elon_tweet.sh

Wednesday, April 26, 2017

Classroom Timer V1

If you have ever been a teacher at any point in your life, you would have realized the importance of keeping your students engaged and on task at all times during the short period every day that you get to spend time teaching them. If you have just 30 minutes per day, you want to maximize the learning by having a structured lesson plan that moves smoothly from Introduction to New Material to Guided Practice and into Independent Practice. And if you have planned for the lecture properly, you would know how much time you want to spend at each stage doing each activity with the kids to ensure that the lesson is completed on time and doesn't spill over onto the next lecture.

You may have a clock in your class, but having a countdown timer makes more sense in competitive and group activities. When the kids see the time ticking down, and they rush to try to finish the activity on time, it can give them an adrenalin rush and bring excitement to the activity.

I am an engineer by profession and passion. So during as a fellow at Teach For India, I was always looking for ways to enhance teaching through use of technology. And so I made a small classroom timer based on Arduino. The first version had a dot matrix display and switches. Soon, I figured that adding WiFi and controlling the timer using a mobile phone would allow the time to be permanently installed above the blackboard in the classroom and avoid the need to touch the timer repeatedly and hence avoid any breakage due to accidentally dropping the timer.

The first version based on Arduino UNO and 8x8 Matrix display
For the first version I used an Arduino UNO along with a 4 panels of 8x8 LED Matrix Displays. The left most 2 displays displayed the time remaining in minutes and the right most two displays were for the two digits which reported the seconds remaining. These display modules are powered by MAX7219 which allows you to control the displays using just 3 pins of the Arduino UNO. I connected the pins 2,3,4 of Arduino UNO R3 respectively to the DataIn_Pin, Clock_Pin, Load_Pin of the 4 module display. I powered the device using a portable power bank and put everything in a box. There are four switches connected to pins 8, 9, 10 and 11 of the Arduino which allows you to start / pause / stop the countdown as well as set the time that you want to countdown from in increments of 30 seconds.

There are four switches. When you power up the timer, it starts counting down from 1:00 (1 minute). You can pause this by pressing the stop button once, and continue the countdown when you press start button. If you press stop twice, the timer is reset to the last set value (1:00 by default). When the timer is stopped (press stop twice), you can use the up and down buttons to set a new reload time. Once you have set the time, you can press start to start counting down. the switches are wired in active low mode. There are pullups enabled on the 4 points. I have also added external pullup resistors. When the switches are presses, they short the corresponding arduino pin to ground.

Here is a video and photo of the internals:




The first time I set about assembling the device, I used a plastic box. Soon after the plastic box broke and the power bank mal-functioned and so I got a switch box and transferred all the circuitry to the new box. the new box was sturdier and looked neater!

Classroom Timer V1 in a better box


Code

I have made use of the following 3 libraries:

  1. TimerOne - for using a Timer Interrupt to update the display once every second.
  2. LedControl - This one is used for interfacing to MAX7219 displays. I had to modify this library because I used the modules upside down.
  3. PinChangeInt - Since the 4 buttons are connected to interrupt pins, I have sued Pin Change interrupts to respond to button presses. 
Many of these libraries are continuously updated, and the newer version might not work with my existing code. Also I had to modify LedControl library as mentioned above - so I have created a github repository for the code as well as a library here: https://github.com/lithiumhead/ClassroomTimerV1
In case you want to use the code, download the 3 library folders to your Documents\Arduino\libraries folder, then download the .ino file and put it in a new folder on your computer. make sure this folder has the same name as the .ino file. double click on the .ino file and use Arduino to download the code onto your Arduino UNO R3.
The ClassroomTimerV1.ino file is present in the github repo above and is also embedded as a gist below: