Wednesday, December 16, 2015

PHP Basics

This post shows how to create a PHP file with echo/print, variables, if condition, arrays, functions, and server date/time.

Requirements for this post:
1. Install Apache HTTP server
2. Install PHP

Type this command in the terminal:

cd /var/www
sudo nano basics.php

Edit the basics.php file as below:

<html>
<head><title>My Title</title></head>
<body>
<?php
//Use echo or print for printing
?>
<P><?php echo "Hello World!!" ?></P>
<P><?php print "Test<br>new line" ?>
<P><?php

//Variables

$my_str = "This is my string.";
$my_val = 100;

print $my_str;
print $my_val;

?>
<P>1+2+3=<?php print 1+2+3 ?></P>
<P><?php
$my_bool = true;

//if condition
if ($my_bool)
{
    print $my_bool;
}
else
{
    print "Noooooo";

}

$my_number = 123;
//A dot is required before $my_number:
print "<br>number = " . $my_number;
print "<br><br>";

//Array
$my_array = Array("Adam", "Tom", "Peter", "David");

for ($x=0; $x<=3; $x++){
    print "$my_array[$x]<br>";
}

//Function
function MyFunc() {
print "<br>MyFunc() is called!!";
}
print "<br>Before";
MyFunc();
print "<br>After";

//Server Time
print "<br><br>Now is " . Date("h:i:s A D Y/m/d");

?></P>
</body></html>


After editing, press Ctrl and X keys at the same time. Then select Y to save the php file.

========

Open the browser with this URL:

http://192.168.xx.xx/basics.php

The result:

Tuesday, December 15, 2015

phpMyAdmin - Create a new database and its contents

Create Database

1. Select Databases. Enter the database name and select utf8_unicode_ci or other format. Click the create button.





The database is created as below:

Create Table of Database

1. Select the database just created.

2. Now create a table of the database. Enter the table name and number of columns. Select Go.

3. Enter or select column items such as column, type, length/values, and collation.
Select save.

4. Select the table just created.

5. Since there is a "No index defined!" warning, let's add one column by adding it to the beginning of the table. Select Go.

6. For the new column, select INT type and PRIMARY as index.

Now the index column has been added to the table. One row of index column has been added under "Indexes:".

Create Values of Database Table

1. Click Insert. Enter the values. Click Go.

The result shows that one row of data has been inserted.

You can add more data if required.
Show the data within the database table

Select the table name. The data are listed as shown below.

Related Posts about phpMyAdmin:
Installation
Create a new user

phpMyAdmin installation

phpMyAdmin is web-based tool for MySQL administration. This tool is free and open source. It is written in PHP.

V4.5.2 released on November 23, 2015 is compatible with PHP 5.5 to 7.0 and MySQL 5.5.

1. To install phpMyAdmin, type this command:

sudo apt-get install phpmyadmin

Choose apache2.


Choose Yes.

Enter the password for the database.

Enter the password for phpMyAdmin.

Enter the password again.


2. Open the browser and enter this URL:
http://192.168.xx.xx/phpmyadmin/


3. Login. The username is root.

4. Check the versions of MySQL, Apache, and phpMyAdmin.

In this example, versions are:
MySQL: 5.5.46
Apache: 2.2.22
phpMyAdmin: 3.4.11.1


Reference:
Tutorial – Install PhpMyAdmin on your Raspberry Pi

Monday, December 14, 2015

Remote access to Raspberry Pi using SSH terminal command

The SSH (Secure Shell) server of Raspberry Pi is enabled by default. To access Raspeberry Pi using SSH, follow the steps below:

1. Type ifconfig to get the IP address of Raspberry Pi.

2. If on a Mac, turn on the Terminal:

Finder -> Applications -> Utilities -> Terminal

3. Type this command in the terminal:

ssh pi@192.168.x.x

and enter the password.


4. To terminate the SSH connection, type exit.



==================

How to disable/enable the SSH server

1. Turn on the software configuration tool with this command:

sudo raspi-config

2. Select 8 Advanced Options.


3. Select A4 SSH.


4. Select disable/enable.


5. If you selected to disable the SSH server, reboot the Raspberry Pi with this command:
sudo reboot

6. If the SSH server has been disabled, you'll see this result with the previous ssh pi@192.168.x.x terminal command:


=============
Chinese version of this post:
以SSH指令遠端存取樹莓派

Configure the date and time of Raspberry Pi

Configure the timezone

1. Type this command:
sudo raspi-config

2. Select:
4. Internationalisation Options


3. Select:
I2 Change Timezone


4. Select Geographic area.

5. Select Timezone.

6. Select Finish.

The other method to change the timezone is to use the tzselect command.

Update the current time

1. Type this command:
sudo apt-get install ntpdate



2. Connect to the clock server with this:
sudo ntpdate -u serveraddress

where serveraddress is like watch.stdtime.gov.tw or 2.pool.ntp.org.


See the list of NTP pool time servers.
Now the time is correctly adjusted.

3. Shut down the Raspberry Pi:

sudo halt

4. Remove the power cable and wait for a while.

5. Connect to the power cable.

Unfortunately, the clock after powered up is delayed.

Update the current time while booting

1. Type this command:
sudo nano /etc/rc.local

2. Add the line after if [ "$_IP ]; then:
/usr/sbin/ntpdate -u serveraddress



3. Remove the power cable and wait for a while.

4. Connect to the power cable.

Now the time and date are correct!!

Friday, December 11, 2015

Boot failure and reinstall Raspberry Pi 2 樹莓派開不了機

I haven't played with my Raspberry Pi 2 Model B for several months due to busy work with iOS Swift. Today I tried to test my Raspberry Pi. Unfortunately, it couldn't boot!

I tried to reconnect the power several times, but the booting process was always stuck with a lot of strange warnings. The warnings shown varied between different boots.

So I held the 'Shift' key to enter the recovery mode, reinstalled the Raspbian operating system and configure the keyboard to the US layout. Finally, it worked!! My SD card is still working without the need for a new one. But I have to spend a little effort to reinstall and configure the data and environment.

===========

This reinstallation has also resolved an issue. Previously, my Raspberry Pi did not always boot successfully. In about 50% of chance, the screen was constantly showing a lot of lines of warnings. In this case, the Raspberry Pi just did not complete the boot and never ask to enter the login name. Now the Raspberry Pi can always boot successfully after the reinstallation.

===========

我很久沒在玩樹莓派了,因為前陣子一直在忙著寫iOS Swift的App,結果今天把我在今年上半年買的Raspberry Pi 2 Model B拿出來,接上電源跟螢幕,就一整個悲劇!根本開不了機!

我試著重新插拔電源好幾次,但還是會卡在一行行奇怪的錯誤訊息,而且每次開機會出現的錯誤訊息都不太一樣。

於是,我只好在開機時按住Shift鍵,進入recovery mode之後,選擇重新安裝Raspbian作業系統,並設定為美式鍵盤。最後,終於成功開機了!還好記憶卡還可以用,不用換!只是要辛苦一下花點功夫重新設定及安裝資料跟環境。

===========

這次的重新安裝也解決了一個問題。在之前,我的樹莓派常常無法成功開機,大概有一半的機會,會不斷出現許多奇怪的訊息。當出現這種現象時,樹莓派不會結束開機以及顯示要求使用者登入的畫面。現在在重新安裝樹莓派後,每次都可以成功開機。

Wednesday, June 17, 2015

C Programming Hello World

1. In terminal, enter the file name and edit it with nano:

nano test.c

2. Edit the file as below:

#include <stdio.h>

void main()
{
    printf("Hello World\n");
}



Exit and save the file.

3. Compile the file with the gcc command below:

gcc test.c -o test

There should be no warning message.

4. Run the file:

./test


The result should print out "Hello World".