Google Ads

December 16, 2017

Ubuntu improve WIFI performance and reliability

If you need a stable internet connection through WIFI then it is important that WIFI performance remains consistent and WIFI connection is reliable at any given time. This is vital if you are running any kind of server or using torrents. Unfortunately, the power management feature exists for different devices within Ubuntu promotes unstable and slow WIFI connection which is enabled by default for WIFI devices. This creates problems such as automatic turning of WIFI and loosing of WIFI connectivity randomly while the system stays idle for a while or there is no inbound packets coming to your system. To check whether you have power management active for your WIFI device execute the following command into the terminal:

iwconfig | grep -w "Power Management"

Now if it is active then you can easily turn it off by executing following command:

sudo iwconfig wlan0 power off

where you have to replace wlan0 word with your WIFI device name which can be found by executing following command:

ifconfig -a

The above command will list all the network devices present in the system including WIFI devices. If the terminal says that command not found then install the program by installing the package net-tools. To do that, execute the following in the terminal:

sudo apt-get install net-tools

Now to make this setting persistent between the boots you can do several things. First, make changes to the /etc/pm/config.d/blacklist file. To do that, in the terminal execute the following to open the file:

sudo gedit /etc/pm/config.d/blacklist

Add the following line in the /etc/pm/config.d/blacklist file:

HOOK_BLACKLIST="wireless"

Second, make changes to the /etc/pm/power.d/wifi_pwr_off file. To do that, in the terminal execute the following to open the file:

sudo gedit /etc/pm/power.d/wifi_pwr_off

Now add the following lines in the /etc/pm/power.d/wifi_pwr_off file:

#!/bin/sh 
/sbin/iwconfig wlan0 power off

Do not forget to replace the word wlan0 with your WIFI device name and save the file. Now, execute the following command it make the file executable upon every boot:

sudo chmod +x /etc/pm/power.d/wifi_pwr_off

Third, make changes to the /etc/NetworkManager/conf.d/default-wifi-powersave-on.conf file. To do that, in the terminal execute the following to open the file:

sudo gedit /etc/NetworkManager/conf.d/default-wifi-powersave-on.conf

Now in the /etc/NetworkManager/conf.d/default-wifi-powersave-on.conf file, replace wifi.powersave = 3 with wifi.powersave = 2 and save the file.


That is it!

Cheers!
Imam

December 3, 2017

Yet another HTML cache busting technique

If you are coding your web application or web site from the ground up then you may be already facing problems where changing individual files does not get reflected immediately. This is due to browsers caching some of the files to speed up loading performance and also to save bandwidths. The process of avoiding caching in the browsers is called cache busting.  There are already a ton of solutions out there which can be applied to your HTML code to avoid caching where some are tedious to maintain and others are a breeze. Although not very well known technique here I am presenting how to use hashing technique to do HTML cache busting. This technique can be well applied to both client side or server side scripting languages as well as using any cryptographic hash functions. The method I have selected for my web development is client side PHP scripting language and SHA-1 hash function. Using this method the following HTML code shows how to import external CSS file with cache busting feature:

<link rel="stylesheet" <?php echo "href=\"style1.css?v=" . sha1_file('style1.css') . "\"" ?>>

In the above code, PHP script will inject HREF attribute into the link tag. The ?v=" . sha1_file('style1.css') code fragment will change each time the CSS file is modified as well as add a argument to the CSS file name which will force browsers to load the CSS file. As the SHA-1 hash function is unique each time for a particular CSS file data set the above code works well as intended.

The following code snippet shows that the same technique can be applied to import JavaScript files with cache busting feature:

<script <?php echo "src=\"load.js?v=" . sha1_file('load.js') . "\"" ?>></script>

Cheers!,
Imam

July 4, 2017

Stop Ubuntu from freezing completely with Intel Bay Trail

Ubuntu and Linux kernel seem to suffer from a cpu state bug which completely freezes the system when doing graphics intensive tasks such as playing games or videos. Many processors suffer from this bug including the following Intel processors:

Celeron J1900

Celeron N2940
Celeron N2840
Celeron N2930
Pentium N3520 
Pentium N3530
Pentium N3540

To see whether you have one of the above processor put the following command in the terminal:


cat /proc/cpuinfo | grep 'model name'


This bug can easily be fixed by installing proper Microcode files and changing c-state flag for Linux kernel.


First step is to make sure you have Ubuntu microcode package installed by putting the following command in the terminal:


sudo apt-get install intel-microcode


Then, remove the existing microcode files by running the following command in the terminal as they are not recent:


sudo rm /lib/firmware/intel-ucode/*


After then, install Intel Microcode files by downloading Microcode package from the following site:


https://downloadcenter.intel.com/download/26798/Linux-Processor-Microcode-Data-File


Download the microcode*.tgz file and extract it in the Home folder. Now copy the extracted microcode files to system folder by opening a terminal in intel-ucode folder and running the following command:


sudo * /lib/firmware/intel-ucode/


Now edit the grub file to change c-state kernel flag by opening grub file in /etc folder by putting the following command:


sudo gedit /etc/default/grub


In the grub file under the line containing GRUB_CMDLINE_LINUX_DEFAULT=, add the following intel_idle.max_cstate=2, so the line should look like the following line:


GRUB_CMDLINE_LINUX_DEFAULT="quiet splash intel_idle.max_cstate=2"


Now, save and close the grub file and put the following command in the terminal to update boot loader to reflect kernel flag:


sudo update-grub


Done! Now just reboot the system and check whether microcodes are loaded properly by running the following command in the terminal:


dmesg | grep microcode


You should be able to see something like following as output of above command:


[    3.168100] microcode: CPU0 sig=0x30678, pf=0x8, revision=0x829

[    3.168138] microcode: CPU1 sig=0x30678, pf=0x8, revision=0x829
[    3.168445] microcode: Microcode Update Driver: v2.01 <tigran@aivazian.fsnet.co.uk>, Peter Oruba

Cheers!

May 30, 2017

Assigning Binary Value to a Variable in C and C++

If you are programming in C and C++ then you may already know that C and C++ do not have native support for binary literal. So you can not simply write the following statement to give a variable a binary value:

int signal = 1110;

However, C and C++'s bitwise operators such as Left Shift operator << and Or operator | can be easily utilised to achieve binary value assignment like shown below:

int signal = 1 << 3 | 1 << 2 | 1 << 1;


The above statement assigns binary value 1110 to the variable signal. The way it works is the very first expression 1 << 3 creates a binary value 1000, and then the expression 1 << 2 creates a binary value 100 and finally the expression 1 << 1 creates a binary value 10. When all these three binary values are combined using the Or operators we get the binary value 1110 like shown below:

Expression            Binary equivalent
1 << 3            =     1000
1 << 2            =     0100
1 << 1            =     0010
                              1110 (after or operations of three expressions)

One advantage about this method of binary assignment is that there is no need for external libraries and such. The example program demonstrating the above method can be downloaded from the following link:

binary_assignment.c

There is also a convenience functions library called imamB which I wrote recently which can be used for getting binary values of variables as a string and vice versa. To use the functions from imamB all you need to do is to include header and source files from imamB package to your project directory. imamB can be download from the following link:

imamB-1.0.zip

Cheers!
Imam

March 19, 2017

Windows Install and use Python without admin rights

Sometimes it is useful to install Python in a preferred directory other than to a default Windows partition directory. The main reasons for doing this are given below:
  1. Have multiple Python versions and run them as per requirements (use Python versions 2.7 and 3 simultaneously on the same system)
  2. Have Python Interpreter on a portable disk (Python on the go)
  3. Tryout experimental and latest Python versions and Python packages/libraries (leave the default system Python Interpreter unaltered)
Installing Python without admin rights:

Step 1: Download Python installer(msi file) from official website https://www.python.org/


Step 2: Open up Windows Command Prompt where msi file is downloaded and execute the following command,

msiexec /a python-2.7.10.msi /qb TARGETDIR=G:\Python27

Here, TARGETDIR specifies the target folder where Python will be installed.

Once setup is finished you should be able to find python.exe (Python Interpreter) and IDLE (Python Integrated Development Environment) in the G:\python27 and G:\Python27\Lib\idlelib folders respectively.

Step 3: Install pip Python utility script for installing Python packages/libraries by downloading get-pip.py file and executing the following command from the G:\Python27 folder,

python.exe get-pip.py

Step 4: Install Python packages/libraries using pip by executing the following commands from G:\Python,

python.exe -m pip install numpy

Here, numpy is Python package/library name which will be download and installed to Python Interpreter directory by the pip utility. If a package/library is not available in the standard Python pip repository then you can download pip install-able (whl files) Python packages/libraries from pythonlibs and install them by executing following command from G:\Python27, assuming whl files are placed in the same folder,

python.exe -m pip install numpy‑1.11.3+mkl‑cp27‑cp27m‑win32.whl

Cheers!

February 22, 2017

Linux .desktop file run a executable file from the same directory

Desktop files in GNU/Linux define shortcuts for conveniently launching installed applications in the system from system menus and file managers. They can be found in many different places such as in /usr/share/applications directory for system wide applications and in ~/.local/share/applications for user applications. These files can be opened in any text editor program to edit application executable file names or for adding new options to application executables. A typical desktop file may look like below:

[Desktop Entry]

Name=Install
GenericName=Package Installer
Comment=Copy package files to your system
Exec=Install.sh
Terminal=false
Type=Application
Encoding=UTF-8

Each key and value pair defines different attributes of the application launcher. For example the Name key assigns a name for the application launcher which would appear in the system menus and file managers. The most important key is Exec which tells the name of the application executable file. The value of the Exec key can be either just the application executable file name or absolute address of the application executable file like shown below:


Exec=App.sh


Or

 
Exec=/opt/ApplicationX/App.sh

However, sometimes it is preferable to tell Exec key that the application executable file is located in the same directory as the desktop file. This can be easily achieved with the %k code which is available to the Exec key. The %k code in the Exec key value gives us the absolute location of the desktop file as either a URI (if for example gotten from the vfolder system) or a local filename or empty if no location is known. Now, to run a application executable in the same directory as desktop file, Exec key can have the following value:


Exec=xdg-open "%k"/App.sh


The above Exec key value also ensures portability across different GNU/Linux distributions and can be used for many different application scenarios such as shipping portable application packages.


That's all for today

Cheers!

January 1, 2017

Blender Game Engine aligning objects to standing surface using material settings

When using Blender Game Engine there are several methods for aligning objects orientations to sitting surface. One such popular method is to use Ray sensor and Python codes to align objects. With newer Blender for Dynamics objects, it is actually possible to align objects just by using Physics settings in object’s Material tab to orient sitting objects without using any Python codes. To do that you have to change values in the Physics of Material tab of the surface object you want objects to be orient to, especially values of Force, Damp and Distance like shown below. And for objects which should be oriented you have to tick Rotate from Normal in the Physics tab. If all set your objects will follow the orientation of the surface. Check out the sample blend file to see how it works, press A and D keyboard keys to move the object.

object_alignment.blend


Physics settings in the material tab