Tuesday, 31 May 2016

Enable wifi in OSMC on Raspberry Pi.

Recently, I got myself in a situation where I had to use Wifi module with my OSMC setup.
As you might be knowing that in the current version, enabling wifi on OSMC in GUI mode is not supported and it can only be done using a few super commands on your OSMC terminal.

Let me take you through those super commands:
connmanctl
then
technologies

If wifi is off use:
enable wifi

then
scan wifi

then
agent on
then
services
This will list found networks, then use connect with the long string to the right of the network you're trying to connect to - for example

connect wifi_c83a35ca4314_535454657374_managed_psk

Tip: you can use tab completion so you don't have to type every character.
Once connected the connection will be remembered and automatically used in the future.
Network settings will soon be added in the OSMC settings addon so this manual method (or entering the details during install) won't be needed for the final version of OSMC.

Hope this post will help you, if you got stuck! 

Saturday, 14 May 2016

Remote Raspberry Pi display on desktop using Xming & Putty.

We all know it is very irritating to work on raspberry pi standalone coz you need a keyboard, a mouse and a monitor to connect. But you can get rid of all these by getting a display on your host machine like your desktop or laptop.
The following steps help you to display the desktop of the Raspberry Pi on the host machine, like a Windows laptop.
1. Download http://sourceforge.net/projects/xming/ and start it with the command below, so that the Raspberry Pi desktop will take over a small part of your screen :
"C:\Program Files (x86)\Xming\Xming.exe" :0 -clipboard -screen 0 700x500+125+100@1
At this point, Xming should be started. It provides an empty desktop screen into which Putty will pass the Raspberry Pi desktop.
2. Start the Raspberry Pi and make sure you can connect to it from your host machine (otherwise there's no point in using Putty in the first place, since it exists as an SSH client in this context).
3. For this setup to work, you must know your pi's IP address. If you do not have an ip address associated to pi, please assign one like below in the cmdline file present in your raspbian os installed on your SD card. Remember that there is an empty line in this file. Do not delete that line.
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait ip=169.254.0.2::169.254.94.145
4. Start up Putty. Connect to the Raspberry Pi using the IP of the Raspberry Pi. Before making the connection, go to Connection/SSH/X11 in Putty and then put a check the "Enable X11 forwarding" checkbox.
5. Now you see the Raspberry Pi login screen in Putty and you can use pi/raspberry login.
6. The final step is to type 'startlxde' in the Raspberry Pi, Putty session, and press Enter. 
It initiates the session like below and then Xming shows you your Raspberry Pi desktop.
pi@raspberrypi:~ $ startlxde
** Message: main.vala:99: Session is LXDE
** Message: main.vala:100: DE is LXDE
** Message: main.vala:131: log directory: /home/pi/.cache/lxsession/LXDE
** Message: main.vala:132: log path: /home/pi/.cache/lxsession/LXDE/run.log


And there it is, a nice and niche desktop for your tasks. Now that you can see your desktop, you can play with the awesome stuff as I am playing with Node-red above.

Do leave a comment if you face any issues in connecting to RPI and getting display correctly.
Thanks and stay tuned for more blogs on my experiments with raspberry pi.

Happy learning :)

Saturday, 20 February 2016

How to optimize Sorting using HashMap/HashTable?

Compression Technique to sort 10,000 integers in an array ranging from 1-10.

This is a favorite coding interview question in sorting and searching algorithm optimization.

One can solve this using Merge Sort which gives O(nlog n)  complexity. But is there any way to implement with reduced complexity?

Well I tried to figure out a way using Java's HashMap.

Also there are specific sorts available to sort the numbers in linear time complexity:

Code Implementation :

package com.practice.algorithms;

import java.util.Map.Entry;
import java.util.Random;
//import java.util.TreeMap;
import java.util.HashMap;

public class HashMapSortTest {

public static void main(String[] args) {
int size = 10000;
int[] array = new int[size];

//TreeMap<Integer, Integer> sorted = new TreeMap<Integer, Integer>();
HashMap<Integer, Integer> sorted = new HashMap<Integer, Integer>();

double startTime = System.nanoTime();

for(int i=0; i<array.length; i++){
Random random = new Random();
array[i] = random.nextInt(10-1+1) + 1;

switch(array[i]){
case 1:
if(sorted.containsKey(1)){
int count = sorted.get(1);
sorted.remove(1);
sorted.put(1, count+1);
}
else{
sorted.put(1, 1);
}
break;
case 2:
if(sorted.containsKey(2)){
int count = sorted.get(2);
sorted.remove(2);
sorted.put(2, count+1);
}
else{
sorted.put(2, 1);
}
break;
case 3:
if(sorted.containsKey(3)){
int count = sorted.get(3);
sorted.remove(3);
sorted.put(3, count+1);
}
else{
sorted.put(3, 1);
}
break;
case 4:
if(sorted.containsKey(4)){
int count = sorted.get(4);
sorted.remove(4);
sorted.put(4, count+1);
}
else{
sorted.put(4, 1);
}
break;
case 5:
if(sorted.containsKey(5)){
int count = sorted.get(5);
sorted.remove(5);
sorted.put(5, count+1);
}
else{
sorted.put(5, 1);
}
break;
case 6:
if(sorted.containsKey(6)){
int count = sorted.get(6);
sorted.remove(6);
sorted.put(6, count+1);
}
else{
sorted.put(6, 1);
}
break;
case 7:
if(sorted.containsKey(7)){
int count = sorted.get(7);
sorted.remove(7);
sorted.put(7, count+1);
}
else{
sorted.put(7, 1);
}
break;
case 8:
if(sorted.containsKey(8)){
int count = sorted.get(8);
sorted.remove(8);
sorted.put(8, count+1);
}
else{
sorted.put(8, 1);
}
break;
case 9:
if(sorted.containsKey(9)){
int count = sorted.get(9);
sorted.remove(9);
sorted.put(9, count+1);
}
else{
sorted.put(9, 1);
}
break;
case 10:
if(sorted.containsKey(10)){
int count = sorted.get(10);
sorted.remove(10);
sorted.put(10, count+1);
}
else{
sorted.put(10, 1);
}
break;
}
}

int[] sortedArray = new int[size];
int current = 0;

for(Entry<Integer, Integer> entry : sorted.entrySet()){
int key = entry.getKey();
int value = entry.getValue();
//System.out.println(key+" "+value);

value=current+value;
for(int i=current; i<value; i++){
sortedArray[i] = key;
}
current=value;
}

double timeElapsed = System.nanoTime()-startTime;
System.out.println("Execution Time : "+timeElapsed/1000000000+"s");

for(int k:sortedArray){
System.out.print(k+" ");
}
}
}

Friday, 18 December 2015

How To Setup OSMC with Raspberry pi for Media Automation.

Hello readers, 
In this blog I am gonna share the steps which you need to follow to setup Open Source Media Center (OSMC) also known as Kodi on Raspberry Pi and configure your own Home Media Center.
Step1 - Setting up OSMC on Raspberry Pi. ->Download OSMC from this link https://osmc.tv/download/ 
  • Install OSMC on the SD card, setup wireless connection by connecting Raspberry Pi with your home network (Wifi hotspot) at the time of installation. 
  • Insert the SD card in the Pi, connect the Pi with ethernet cable or wifi dongle, keyboard, mouse and HDMI cable before powering it on.

Step2 - Configuring unique ip address for listening on OSMC.
Step4 - Installing Kore Android/IOS application in your smart phone to control the media on OSMC.
Step5 - Connecting hard disk with OSMC installed Raspberrypi.
Step6 - Configure Kore app to listen on configured IP address.
With this step, your OSMC should be up & Running & you can seamlessly enjoy your movie experience. The next step talks further about how to automate your media center to be in sync with the local system media files, so that you do not have to manually push media files to your hard disk. Basically it gives you a plug and play feature! 
You need a Winscp client on your computer machine. The scripts are simple Winscp scripts which can be run from the command line. This is a part of my Open Source Project HoMeAu, you can check the scripts at this link: https://github.com/ak1801/HoMeAu
Step7 - Run winscp script - media_push.txt to push any media file to desired location on the hard disk. Pass the filepath as parameter while executing the script. Execute following command in cmd from winscp installation directory: winscp.com /script=media_push.txt /parameter // filepath\filename
Step8 - Automate the script for scheduled file transfer & sync local media directory with remote media directory using the script media_sync.txt.

IMPORTANT NOTE:

User osmc is not a root user, it does not have root permissions. To write copy any media in your hard disk, you need to have root level access. For this you need to add permissions in sudoers file located at (/etc/sudoers).
To can make changes to sudoers from putty/bash:
Create a back up of this file before editing using -> cp filename{,.bak}

Type visudo and press enter.

Navigate to the place you wish to edit using the up and down arrow keys.

Press insert to go into editing mode.

Make your changes - for example: user ALL=(ALL) ALL.

Note - it matters whether you use tabs or spaces when making changes.

Once your changes are done press esc to exit editing mode.

Now type :wq to save and press enter.

You should now be back at bash.

Now you can press ctrl + D to exit the session if you wish.

Tuesday, 7 July 2015

Ideas that will drive the future!

With due regards to the makers of IronMan 1/2/3, Her, Total Recall (both old and new), here are some Project Ideas on IoT which I feel is required to be built. 

Project Ideas:
  1. Emotion Control
    1. Benefits: More and more people are suffering from stress related ailments and diseases. Stress leads to changes in emotional responses like anger, fear/excitement to daily events. An IoT device that estimates one's stress level and provides primary alerts and secondary alerts either to indicate the stress level or to take precautionary measures will be useful.
    2. Implementation: The device will eventually boil down to a wearable (watch, etc.) that will sense/measure/track body vitals like breath, blood pressure, heart rate and maybe even get a visual of the person. Based on these data, an estimate of the person's stress level will be made using algorithms and then alert the primary and secondary users (doctors, parents, friends) when the person is getting stressed. A doctor's help would be needed in estimating the stress level.
    3. Challenges: Device power consumption requirements, form factor i.e. can it be designed to be seamlessly integrated into your daily life, customization, how to manage false alerts?, cost, etc.
  2. Jarvis
    1. Benefits: A personal assistant/virtual friend that understands your mood and customizes your home environment to suite your taste. Jarvis will be a robot like device that will control your home modem/WiFi router, lights, fans, AC, appliances using IoT enabled devices to create the best environment as per an individuals personal preferences. Jarvis will also be able to talk to you and maybe even see you to understand your mood.
    2. Implementation: 10-15 IoT enabled devices to control various electrical appliances and equipment in your home. 1 IoT enabled central command center either a server or PC that will talk to each IoT device and run machine learning algorithms to estimate your mood to create the required ambiance.
    3. Challenges: Machine learning algorithms, how is data security managed? cost of such a platform.
  3. Women & Child Safety
    1. Benefits: Provide a safe environment and means of protection for women, children and senior citizens by designing a device that will track movements, location, means of alerting primary and secondary users (parents, friends, others)
    2. Implementation: An IoT device that has a push button to send an alert and which can connect to a mobile device either via Bluetooth, WiFi. The device design can be based on Arduino. Device to bluetooth connectivity was demonstrated at an earlier hackathon. Will probably need geofencing to provide connectivity to the devices or other means of detecting device movements in a school environment. Device can also capture audio. Some colleges and schools have tried using IoT enabled ID cards that track entry and exit into premises.
    3. Challenges: How to manage device charging? How much battery power will be required? Can a cheap wearable from China be hacked to get some know how?