quarta-feira, dezembro 28, 2011

The abusive price of electronics in Brazil

Last month, I've heard about the Raspberry Pi project. It's about a US$25~35 board, with an 700 MHz ARM processor (the Broadcom BCM 2835), 128/256 MB of RAM, USB, HDMI and Ethernet (depending of the model) connectors. Those boards are suposed to run some Linux distributions (initially Fedora and Debian) and programming languages such as Python, Java, C (and others), and it's aimed to educational proposes (but can you imagine the things that could be possible to do with such hardware?)





I've been very excited with the news that the Raspberry Pi final boards would be finished in December/2011 or January/2012, and they would be sold first in United Kingdom and maybe in the United States. Although, at the same time, I got worried about the possibility of that board never come to Brazil (the country I live) or to become a very expensive product that only few people could get access to. Since it's not being sold yet (until the publication of this post), I had a hope that all my worries could not become true.

Today, I knew that there is another similar project, but with a board (the BeagleBoard) that is more sophisticated than the Raspberry Pi, and is being sold for the recommended price of US$149 in the rest of the world, but in Brazil, due to a restriction of Texas Instruments that prohibits brazilians to buy that board directly from the United States through the Internet, it costs about R$850~975 !!!












Just for comparison, today US$1,00 is about R$1,90. So, we can conclude is that the BeagleBoard is being sold for, at least, 3 times it's recommended price.







I really hope that the Raspberry Pi can become a reality in brazilian education (from schools to universities), and I can't wait to get my own board. But if it costs much more than US$25~35, I'm sure that the dream is really, really over!

segunda-feira, novembro 21, 2011

Using a SSH connection as a proxy (SSH Tunneling)



For many people it is necessary to go the University to access many websites to download papers or journals. What many of those people don't know, is that is possible to create a socket (Windows/Linux) with a SSH (Secure Shell) server and then connect to the University server direct from your computer, using that SSH connection as a proxy server to your web browser (IE, Firefox, Chrome, Opera, etc). For that, you need:
*One client ssh (you can use Putty on Windows, for instance)
*Have a valid username and password to access the University server.

Bellow, there's one example about how it could be done using Windows (XP) platform.

In Putty's main screen we need to create a new "Saved Session". In this example, we called it "Tunneling". Create then save it. After that, in the same screen, set the host name and port.




Then in Category "Connection" click in SSH->Tunnels. Choose one random port (in this example we chose 8084) from your computer and type it in the "Source port" field. Before add, you need to set it as "Dynamic" (the default is "Local" as in the picture bellow, so change it).




After doing that, you can connect to the SSH server. Once connected is time to configure your browser. We are going to use Firefox web browser in this example, but it can be done in any other, like IE, Chrome, Opera, Safari, etc.

Open Firefox. At the upper menu click on Tools->Option, then choose "Advanced" and finally the "Network" tab. Click the "Settings..." button.

Select the "Manual Proxy Configuration", and set the IP "127.0.0.1" to SOCKS. The source port should be the same as you chose for your Tunneling configuration for Putty (in this example: 8084).


Click the "OK" button, then your IP address within the Internet is going to be the same as the server you are connected. To the Internet, it is as if you were accessing from your University (for example).

In fact, you are using the SSH server as a proxy server, and the data transmitted between you and that server are encrypted.

domingo, setembro 18, 2011

The Unix Time

Don't you know what is Unix Time?
According to Wikipedia: "Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds."

It's a linear representation of time, because we're just working with the amount of seconds that have passed from an event in the past (January, 1, 1970 00:00 - UTC time). Hence, it is widely used in computer systems because it's easy to store (only one "integer" variable is needed), and it makes some time calculations easier. Basically, you will work with dates the same way you work with integer numbers.


Bellow, we list some examples about how to get the Unix Time in different programming languages:

C#:

(DateTime.UtcNow - new DateTime(1970,1,1,0,0,0)).TotalSeconds;



Java:

System.currentTimeMillis()/1000;



JavaScript:

Math.round(new Date().getTime()/1000.0);



MySQL:

SELECT unix_timestamp(now())



Oracle PL/SQL:

SELECT (SYSDATE - TO_DATE('01/01/1970 00:00:00', 'MM-DD-YYYY HH24:MI:SS')) * 24 * 60 * 60 FROM DUAL



Perl:

time



PHP:

time()



PostgreSQL:

SELECT extract(epoch FROM now())



Python:

time.time()



R:

as.numeric(Sys.time())



Ruby:

Time.now



SQL Server:

SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE())



Unix or Linux shell:

date +%s

domingo, julho 03, 2011

How to send an E-mail using the Linux command line (The Mutt Mail User Agent)

Yesterday I needed to send an e-mail with a file attached using the Linux command line. The solution was quite simple but I spent some very important minutes to find it. So there is it:


$ mutt -s "Mail's subject" -a file_to_attach.rar user@provider.com


I hope it could be useful to you one day.

sexta-feira, abril 22, 2011

DD-WRT - A Linux-based firmware for wireless routers

Are you satisfied with configuration options of your wireless router?



If the answer is "No", you should know that there are many third-party firmwares that you can install on your device, and give it options that you would find only in high-end routers.
In this post, I'm going to introduce DD-WRT. It's a free third-party linux-based firmware, under the terms of GPL license. It include features like daemon-based services, IPv6 support, Wireless Distribution System (WDS), RADIUS, Quality of Service (QoS), radio output power control, and many others.

I tried DD-WRT in my D-Link DIR-300 wireless router and I got many new functionalities, such as iptables, traffic graphs, better QoS support, blocking websites by URL or keyword, and others. One interesting thing is that now I can use my Wi-Fi router as a Client or as a Repeater (even the other wireless routers of the network doesn't support WDS). I present you some printscreens from its configuration pages:


Wireless Modes



Used CPU and Memory



Traffic Chart of this Month


DD-WRT can be downloaded at: http://www.dd-wrt.com. Note that firmware upgrades have a risk of damaging your device. Please read carefully all about it on the website. There you will find if your wireless router is compatible with DD-WRT or not.

String format for DateTime in C#

In C# language, there are the following custom format specifiers for date and time:
y - year
M - month
d - day
h - hour 12
H - hour 24
m - minute
s - second
f - second fraction
F - second fraction, trailing zeroes are trimmed
t - P.M or A.M
z - time zone


Some examples on how to use them (the output is commented at the end of each line):


/*
In this example, we create an DateTime object (whose name is "dt") that representes
the date and time: 2008-03-09 16:05:07.123
*/

DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year
String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month
String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24
String.Format("{0:m mm}", dt); // "5 05" minute
String.Format("{0:s ss}", dt); // "7 07" second
String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" sec.fraction
String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" without zeroes
String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M.
String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone

segunda-feira, abril 18, 2011

Some important Taylor/Maclaurin series

Have you ever asked yourself about how your scientific calculator or your computer "know" the sine or the cosine of a number?

Maybe you thought that those devices had predefined tables inside their memories to calculate those functions. But in fact those tables don't exist! So, how those devices are able to give you the correct answer with great precision?

The truth is that they use algorithms that calculate the results of Maclaurin functions. According to Wikipedia: "A Taylor series is a representation of a function as an infinite sum of terms calculated from the values of its derivatives at a single point. Taylor series were formally introduced by the English mathematician Brook Taylor in 1715. If the series is centered at zero, the series is also called a Maclaurin series, named after the Scottish mathematician Colin Maclaurin who made extensive use of this special case of Taylor series in the 18th century."

This is an important subject of Calculus, but the objective of this post is to show some of those functions, so we won't make demonstrations here. If you really want to study it further, I recommend you to check the book: "Calculus - Volume 2, by James Stewart". Bellow you can appreciate that interesting way to calculate some well known functions:

The exponential function:


Sine and Cosine (and hyperbolic ones):








We can tell you those are ways to approximate the functions values, and precision depends only of the value of n. Note that those sommations go from zero to the infinity, then the value of those sommations converge to the real values of those functions, but computers can't calculate them to infinity. Thus, we calculate each sommation to a finite (and relatively "small") value of n, in a manner that satisfies the desired precision that we need. One example is given bellow:


Here we calculate the exponential function for x = 2 (that is: e^2)


For n = 1 , the sommation is: 1.0
For n = 2 , the sommation is: 3.0
For n = 3 , the sommation is: 5.0
For n = 4 , the sommation is: 6.33333333333
For n = 5 , the sommation is: 7.0
For n = 6 , the sommation is: 7.26666666667
For n = 7 , the sommation is: 7.35555555556
For n = 8 , the sommation is: 7.38095238095
For n = 9 , the sommation is: 7.3873015873
For n = 10 , the sommation is: 7.38871252205
For n = 11 , the sommation is: 7.38899470899
For n = 12 , the sommation is: 7.38904601571
For n = 13 , the sommation is: 7.38905456683
For n = 14 , the sommation is: 7.38905588239
For n = 15 , the sommation is: 7.38905607033
For n = 16 , the sommation is: 7.38905609538
For n = 17 , the sommation is: 7.38905609852
For n = 18 , the sommation is: 7.38905609888
For n = 19 , the sommation is: 7.38905609893
For n = 20 , the sommation is: 7.38905609893

According to the built-in function of Python's math library, e^2 is: 7.38905609893

In this example, we conclude that n equal or greater than 19 gives a good precision (if compared with Python's built-in function) for x = 2. But for greater values of x, we should increase the number of iterations. Let's check another example, for x = 5:


For n = 1 , the sommation is: 1.0
For n = 2 , the sommation is: 6.0
For n = 3 , the sommation is: 18.5
For n = 4 , the sommation is: 39.3333333333
For n = 5 , the sommation is: 65.375
For n = 6 , the sommation is: 91.4166666667
For n = 7 , the sommation is: 113.118055556
For n = 8 , the sommation is: 128.619047619
For n = 9 , the sommation is: 138.307167659
For n = 10 , the sommation is: 143.68945657
For n = 11 , the sommation is: 146.380601025
For n = 12 , the sommation is: 147.603848505
For n = 13 , the sommation is: 148.113534955
For n = 14 , the sommation is: 148.309568205
For n = 15 , the sommation is: 148.37958008
For n = 16 , the sommation is: 148.402917371
For n = 17 , the sommation is: 148.410210275
For n = 18 , the sommation is: 148.412355247
For n = 19 , the sommation is: 148.412951072
For n = 20 , the sommation is: 148.413107868
For n = 21 , the sommation is: 148.413147067
For n = 22 , the sommation is: 148.4131564
For n = 23 , the sommation is: 148.413158522
For n = 24 , the sommation is: 148.413158983
For n = 25 , the sommation is: 148.413159079
For n = 26 , the sommation is: 148.413159098
For n = 27 , the sommation is: 148.413159102
For n = 28 , the sommation is: 148.413159102
For n = 29 , the sommation is: 148.413159103
For n = 30 , the sommation is: 148.413159103
According to the built-in function of Python's math library, e^5 is:148.413159103


Now note that n = 19 isn't enough to give the same precision as before. And the precision for n = 19 will get worse for greater values of x. Thus, the greater is x, the greater is the number of necessary iterations to get a good approximation of the real value of the function.