Tuesday, December 29, 2015

Connect iOS device to MySQL database on a server (Raspberry Pi Part)

This post shows how to create a PHP+MySQL service on Raspberry Pi for iPhone / iPad to access using the HTTP POST method.

Raspberry Pi Part


1. Make sure that Apache, MySQLPHP and phpMyAdmin have been installed.

2. Create some data in MySQL using phpMyAdmin or MySQL monitor commands.

3. Create the iosmysql.php file with this command:

sudo nano /var/www/iosmysql.php

4. Edit and save the file as below:                    

<?php

//check if database parameters are received correctly.
if (isset($_POST['username']) and isset($_POST['password']) and isset($_POST['tablename'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$tablename = $_POST['tablename'];
} else {
    die("Wrong request");
}

$servername = "localhost";

//connect to database
$connection = mysql_connect($servername, $username, $password) or die("No MySQL Connection: " . mysql_error());

mysql_select_db("mydatabase", $connection) or die("<br>No Database: " . mysql_error());

$query = "select * from $tablename";
$result = mysql_query($query) or die("Query Error: " . mysql_error());

$FieldTotal = mysql_num_fields($result);

//loop through column names
for ($count = 0; $count < $FieldTotal; $count++) {
    $field = mysql_fetch_field($result, $count) or die("Field error!");    
    //Remove "fruit_" string, which includes $tablename and "_".
    $ShortTitle = substr($field->name, strlen($tablename)+1);
    print "$ShortTitle\t\t";
}
$RowTotal = mysql_num_rows($result);

//loop through rows
for ($RowCount = 0; $RowCount < $RowTotal; $RowCount++) {
    $row = mysql_fetch_row($result) or die("Row error!");
    print "\n";
    //loop through columns
    for ($ColCount = 0; $ColCount < $FieldTotal; $ColCount++) {
        print "$row[$ColCount]\t";
        if (strlen($row[$ColCount]) < 7) {
            print "\t";
        }
    }
}

?>

In this example, parameters obtained from the HTTP POST method are username, password, and tablename, while servername is set to "localhost" locally.


iOS Part

1. Write a simple iOS app. See this:


2. Test the iOS app. The result:


This result is the same as the result obtained using MySQL monitor commands on Raspberry Pi:


References:
PHP Manual
PHP Basics
Connect iOS device to HTTP GET/POST PHP service (Raspberry Pi Part) (iOS Part)
Access and show content of a database in MySQL with PHP

Friday, December 25, 2015

Setup HTTPS / SSL for Apache server

This post shows how to setup HTTPS (Hypertext Transfer Protocol Secure) / SSL (Secure Sockets Layer) with a Raspberry Pi.

1. Raspberry Pi Requirements:

Apache HTTP server installed

Custom domain name configured for Raspberry Pi's IP address on a Mac or Raspberry Pi.

You should have an HTTP server working like this:




2. Confirm if OpenSSL has been installed with this command:

sudo apt-get install openssl



3. Create certificate and key files:

sudo mkdir /etc/apache2/ssl
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -out /etc/apache2/ssl/server.crt -keyout /etc/apache2/ssl/server.key

where -x509 refers to the X.509 standard, while rsa:2048 refers to 2048-bit RSA cryptosystem (cryptographic algorithms).

You may replace 365 days by 1095 days for a longer period of three years.

Enter the the names of your country, state / province, locality, organization, organizational unit and common name as well as your email address.


4. Create a symbolic link:


sudo ln -s /etc/apache2/sites-available/default-ssl /etc/apache2/sites-enabled/000-default-ssl

5. Edit the SSL configuration file:

sudo nano /etc/apache2/sites-enabled/000-default-ssl

Type Ctrl+W to search for "SSLCer" and make sure:

SSLEngine on

and modify these file paths:

SSLCertificateFile    /etc/apache2/ssl/server.crt

SSLCertificateKeyFile /etc/apache2/ssl/server.key



6. Restart the Apache server:

sudo service apache2 restart

or

sudo /etc/init.d/apache2 restart


7. Open a browser from a computer. This example uses Firefox on a Mac. Enter the HTTPS version of your custom domain name. The message of "This Connection is Untrusted" is shown because we did not pay for a SSL certificate.

Select Add Exception.



Select Confirm Security Exception.

Now a secure lock is shown at the left of the URL.

This also works with PHP.

Reference:

Enable SSL for apache server in 5 minutes

Thursday, December 24, 2015

Raspberry Pi Technique: How to configure an IP address as a custom domain name for a browser

If an Apache HTTP server is installed on a Raspberry Pi, entering the IP address obtained from the ifconfig command in the Epiphany web browser gives this result:




Or the result of a PHP page:




However, is it possible to use a custom domain name to access the above 192.168.xx.xx IP address with a personal Mac?

Yes. The solution is easy. And the good news is: there's no need to pay for the custom URL!!

Just modify the hosts file of Mac using this terminal command:

sudo nano /etc/hosts

And add this line:

192.168.xx.xx     yourdomain.com




Save this hosts file and enter your custom domain name in the browser:



This solution also works with PHP.



You may give your server an awesome domain name you like and then you no long need to enter the numbers of your server's IP address in the browser!

Note the different hosts file path:
Raspberry Pi:   /etc/hosts
Mac:                 /private/etc/hosts

Reference:

Wednesday, December 23, 2015

Connect iOS device to HTTP GET/POST PHP service (Raspberry Pi Part)

This post shows how to create a PHP service on Raspberry Pi for iPhone / iPad to access using HTTP GET / POST methods.

Raspberry Pi Part


1. Make sure that Apache and PHP have been installed.

2. Create the ios.php file with this command:

sudo nano /var/www/ios.php

4. Edit and save the file as below:

<?php
if (isset($_GET[language]) and isset($_GET[product])) {
    print "GET:";
    print "\nYour language: " . $_GET[language];
    print "\nYour product: " . $_GET[product];
} else if (isset($_POST['name']) and isset($_POST['age'])) {
    print "Post:";
    print "\nYour name: " . $_POST['name'];
    print "\nYour age: " . $_POST['age'];
} else {
    print "Wrong request";
}
?>

In this example, parameters used in the GET method are language and product, while parameters used in the POST method are name and age.

5. Test the GET method with the browser:

Invalid HTTP GET:
http://192.168.0.149/ios.php


Valid HTTP GET:
http://192.168.0.149/ios.php?language=objective-c&product=applewatch 

iOS Part

1. Write a simple iOS app. See this:


2. Test the app. The results:

GET:


POST:



Reference:
PHP Manual

Monday, December 21, 2015

Access and show content of a database in MySQL with PHP

This post shows how to access a database in MySQL and print the data of the database on a PHP web page:

1. Make sure that Apache+MySQL+PHP (LAMP) and phpMyAdmin have been installed.


3. Create the fruit.php file with this command:

sudo nano /var/www/fruit.php

4. Edit and save the file as below:

<html><head><title>Fruit</title></head>
<body>
<?php
$servername = "localhost";
$username = "myusername";
$password = "mypassword";

$connection = mysql_connect($servername, $username, $password) or die("No MySQL Connection: " . mysql_error());

print "MySQL Connection OK";
mysql_select_db("mydatabase", $connection) or die("<br>No Database: " . mysql_error());

print "<br>Database OK";

$query = "select * from fruit";
$result = mysql_query($query) or die("<br>Query Error: " . mysql_error());

$FieldTotal = mysql_num_fields($result);

print "<P>There are $FieldTotal columns in the table.</P>";
print "<Table border=1><tr align=center>";

//loop through column names
for ($count = 0; $count < $FieldTotal; $count++) {
    $field = mysql_fetch_field($result, $count) or die("Field error!");    
    print "<td width=120>$field->name</td>";
}
print "</tr>";
$RowTotal = mysql_num_rows($result);

//loop through rows
for ($RowCount = 0; $RowCount < $RowTotal; $RowCount++) {
    print "<tr align=center>";
    $row = mysql_fetch_row($result) or die("Row error!");

    //loop through columns
    for ($ColCount = 0; $ColCount < $FieldTotal; $ColCount++) {
        print "<td>$row[$ColCount]</td>";
    }
    print "</tr>";
}
print "</table>";

print "<br>end";
?>
</body></html>

5. Use the browser to open this URL:

http://192.168.xx.xx/fruit.php

The result:


Reference:

如何使用vim文字編輯器

1. 先安裝vim:

sudo apt-get install vim



2. 使用指令編輯檔案(以HTML檔為例,請先確定已安裝Apache HTTP伺服器):

cd /var/www
sudo vim test.html

3. 按下i或a或o鍵進入編輯模式

4. 輸入以下資料:

<html>
<title>My Title</title>
<body>
<P>Edited by Vim.
</body>

</html>

5. 按Esc鍵離開編輯模式

6. 按以下鍵存檔並退出vim:

:wq

如果要放棄編輯過的檔案:

:q!

注意:要按下:等指令退出vim時,一定要先按Esc退出編輯模式。

7. 以瀏覽器打開網址檢查結果:

Basic vim commands 基本指令:

highlight one line 選取單行: V
highlight area 選取區域: v
copy 複製: y
paste 貼上: p
undo 復原:u
redo 重做:Ctrl + r
search 搜尋: /

參考資料:
vi與Emacs的編輯器之戰
鳥哥的 Linux 私房菜 第九章、vim 程式編輯器
How to use the vim text editor(這篇文章的英文版本)

Friday, December 18, 2015

在樹莓派上安裝中文字型 Install Chinese Fonts with Raspberry Pi

在樹莓派上使用LibreOffice等需要字型變化的軟體時,如果能使用漂亮的中文字體,會令人賞心悅目!

以下將介紹如何用指令安裝一些不同的字型。

文泉驛正黑

sudo apt-get install ttf-wqy-zenhei


教育部標準楷書

sudo apt-get install fonts-moe-standard-kai

cwTex圓體

sudo apt-get install fonts-cwtex-yen

Noto Sans CJK (思源黑體) 

(No Tofu => Noto 不再有如豆腐方塊形狀的外文字)

1.先上官網(http://www.google.com/get/noto/)
複製DOWNLOAD ALL FONTS按鈕的網址,格式大概如下:

https://noto-website-2.storage.googleapis.com/pkgs/Noto-unhinted.zip



2.進入一個新增的資料夾:
mkdir myfont
cd myfont

3.下載字型壓縮檔:

wget 剛才複製的網址

例如:

wget https://noto-website-2.storage.googleapis.com/pkgs/Noto-unhinted.zip



檔案有點大,要稍等一下

4.解壓縮

unzip Noto-unhinted.zip


又要稍等一下

5.執行以下的指令

mkdir -p ~/.fonts
cp *otf *otc ~/.fonts
fc-cache -f -v



6.重新開機

7.在LibreOffice中使用正體中文字型時,請選擇Noto Sans CJK TC字型,有Thin、Light、DemiLight、Regular、Medium、Bold、Black七種粗細。





使用LibreOffice Writer測試字型

結果如下:

相關資訊


檢查SD卡的空間:
df -h /dev/root

更多字型:
Ubuntu環境下,一些字型套件 (可使用sudo apt-get install指令下載這個網站中所列出的字型檔,因Ubuntu和樹莓派的Raspbian作業系統都是基於Debian的Linux Distribution)

相關網站:

以iOS 9程式顯示並比較六種不同粗細變化的「蘋方」字體
Raspberry Pi 樹莓派的中文設定
在樹莓派上使用免費的辦公室軟體 - LibreOffice
Google 推新版字體網站,共 804 字體供大家免費取用!
Google Fonts
解決字體顯示問題!蘋果、Google、微軟、Adobe 攜手開發字體(inside.com.tw)
支援超過800種語言!Google、Monotype共同開發的「Noto Fonts」開放免費下載! (數位時代)

在樹莓派上使用免費的辦公室軟體 - LibreOffice

想使用樹莓派工作嗎?想開啟或編輯Office檔案?

方法很簡單,只要安裝LibreOffice即可!

方法1:前往Pi Store下載(此方法需以帳號登入)

1. 選擇Menu->網際網路->Pi Store

2. 搜尋"Office"

3. 點選"Free Download"

4. 需要用帳號登入後才可以下載

如果不想註冊帳號,可使用下面的方法安裝LibreOffice。

 方法2:使用指令下載

1. 在LX終端機輸入:

sudo apt-get install libreoffice


因為檔案有點大,要稍等一下才會完成安裝。

2. 選擇Menu->辦公->LibreOffice Writer或其他項目


開啟中...

切換中文輸入法,即可編輯~


這個時候LibreOffice,介面是英文的,以下將介紹更新中文介面的方法:

 更新為中文介面

1. 輸入以下指令:

sudo apt-get install libreoffice-l10n-zh-tw


2. 重新開啟LibreOffice,介面已更新為中文



相關資訊:
Raspberry Pi 樹莓派的中文設定
在樹莓派上安裝中文字型

Thursday, December 17, 2015

Raspberry Pi 樹莓派的中文設定(含如何編輯中文網頁)

雖然安裝好Raspberry Pi後,打開瀏覽器Epiphany Web Browser可以看到中文,卻不能輸入,而視窗介面也是英文的。

要如何在樹莓派中使用中文呢?
為避免忘記 ,自己記錄一下設定的步驟:

使用設定工具Software Configuration Tool (raspi-config)

1. 輸入指令:

sudo raspi-config

2. 選擇Internationalisation Options


選擇Change Locale

選擇zh_TW.UTF-8 UTF-8
也可以選擇zh_TW BIG5

選擇zh_TW.UTF-8

3. 重新開機,視窗裡的部分選項變成了中文。


安裝中文輸入法

1. 輸入以下指令:
sudo apt-get install scim scim-tables-zh scim-chewing

2. 同時按下Ctrl及空白鍵,即可切換中文輸入法。

系統所選到的輸入法會顯示在螢幕的右下角,以滑鼠在上面點選即可選擇新的輸入法或切換中英、半形/全形等設定。


3. 在這裡介紹另一種切換中文輸入法的方法:

如果使用Mac透過VNC連線到樹莓派,會發生按Control + Space無法切換輸入法的情形,因此這時候需要改用以滑鼠選擇輸入法的方法。

這時需要點選螢幕右上角的鍵盤按鈕:

若是使用VNC時,可能看不到這個鍵盤按鈕,因此需要把視窗往右拉大,或是按Toggle full screen按鈕,把樹莓派桌面放到最大。



然後就可以點選鍵盤按鈕,選擇輸入法。


4. 選擇輸入法後,即可輸入中文!



註:樹莓派的SCIM大易輸入法使用的是三碼的輸入方式

編輯中文網頁

在安裝Apache及PHP後,可以使用nano編輯中文網頁:

sudo nano /var/www/chinese.php

編輯這個PHP檔案如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>My Web Page我的網頁</title>
</head>
<body>
<P>English: Raspberry Pi<br>中文:樹莓派</P>
<P><?php print "測試PHP動態網頁";?></P>
<P>現在時刻:</P>
<P>
<?php
print Date("Y年m月d日");

if (Date(A) == "AM")
{
    $AMPM_Str = "上午";
}

else
{
    $AMPM_Str = "下午";
}
print "<br>$AMPM_Str" . Date("h:i:s");
?>
</P>
</body>

</html>

以瀏覽器打開網址http://192.168.xx.xx/chinese.php

成功顯示中文!

相關資訊:

在樹莓派上安裝中文字型
在樹莓派上使用免費的辦公室軟體 - LibreOffice