Wireless

iw dev will show the wireless device:

	[root@XXXyyy Trusty]# iw dev
	phy#0
		Interface wlo1
			ifindex 2
			wdev 0x1
			addr xx:xx:xx:xx:xx:xx
			type managed

Then use the deivce name got from last step, ip link set it up

	[root@XXXyyy Trusty]# ip link set wlo1 up
	# Check the status:
	[root@XXXyyy Trusty]# iw dev wlo1 link
	Not connected.

Scan the available wireless network:

	[root@XXXyyy Trusty]# iw dev wlo1 scan | more
	[root@XXXyyy Trusty]# iw dev wlo1 scan | grep SSID
		SSID: WLAN14
		SSID: VISITOR
		.........

Connect to the existing Wireless LAN:
Install the wpa_supplicant:

	$ pacman -S wpa_supplicant
	[root@XXXyyy Trusty]# touch /etc/wpa_supplicant.conf
	[root@XXXyyy Trusty]# wpa_supplicant -i wlo1 -c /etc/wpa_supplicant.conf
	Successfully initialized wpa_supplicant
Generate the wpa_supplicant:
	[root@XXXyyy Trusty]# wpa_passphrase OpenWrt_11 XXXXXX > /etc/wpa_supplicant/openwrt.conf
	[root@XXXyyy Trusty]# cat /etc/wpa_supplicant/openwrt.conf 
	network={
		ssid="OpenWrt_11"
		#psk="XXXXXX"
		psk=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	}
	[root@XXXyyy Trusty]# wpa_supplicant -i wlo1 -c /etc/wpa_supplicant/openwrt.conf 
	Successfully initialized wpa_supplicant
	wlo1: Trying to associate with xx:xx:xx:xx: (SSID='OpenWrt_11' freq=2462 MHz)
	ioctl[SIOCSIWFREQ]: Device or resource busy
	wlo1: Association request to the driver failed
	wlo1: Associated with xx:xx:xx:xx
	wlo1: WPA: Key negotiation completed with xx:xg:xx:xx:xx [PTK=CCMP GTK=TKIP]
	wlo1: CTRL-EVENT-CONNECTED - Connection to xx:xx:xx:xx:xx completed [id=0 id_str=]
Get the Wireless address:
	$ [root@XXXyyy Trusty]# dhcpcd wlo1
	dhcpcd[3344]: version 6.0.5 starting
	dhcpcd[3344]: wlo1: soliciting an IPv6 router
	dhcpcd[3344]: wlo1: soliciting a DHCP lease
	dhcpcd[3344]: wlo1: offered 10.0.0.232 from 10.0.0.1
	dhcpcd[3344]: wlo1: leased 10.0.0.232 for 7200 seconds
	dhcpcd[3344]: wlo1: adding host route to 10.0.0.232 via 127.0.0.1
	dhcpcd[3344]: wlo1: adding route to 10.0.0.0/24
	dhcpcd[3344]: wlo1: adding default route via 10.0.0.1
	dhcpcd[3344]: forked to background, child pid 3434

Now we can access the internet via Wireless!

OpenWRT on Mercury MW151RM3G

1. Change Mercury MW151RM3G into TP-Link 703N. You have to download the firmware named “MW151rm3G_to_wr703nv1” from google searched result. After flashing this onto your Mercury MW151RM3G, In fact you have got a TP-LINK 703N.

2. Download the latest firmware of TP-LINK 703N from openwrt.org. The Download URL is located at http://wiki.openwrt.org/toh/tp-link/tl-wr703n. You can get two files, named “squashfs-factory.bin” and “squashfs-sysupgrade.bin”, the factory.bin is for flashing firstly, after flashed this image, flash another named sysupgrade.bin. This will lead your MW151RM3G into a OpenWRT based system.

3. Now you have to change the status of your MW151RM3G’s ethernet port from static ip address into dhcp based address. Then plug it into the Wireless Router, you will found the OpenWRT’s IP address from you router’s connected equipments. Log on it, open the openssh server, log into the router.

4. Now we can boot the system using flash disk, because the inner image is too small for run applications.

5. Prepare the flash disk, format it into ext3 based partition.

6. Install the necessary package on OpenWRT:

	$ opkg update
	$ opkg install block-mount
	$ opkg install kmod-usb-storage 
	$ opkg install fdisk
	$ opkg install kmod-fs-ext4

After the installation, you can mount the flash disk using mount, and your system shall support ext3 based partitions.

7. Edit the fstab file, /etc/config/fstab

	config mount
	        option target        /overlay
	        option device        /dev/sda1
	        option fstype        ext3
	        option options       rw,sync
	        option enabled       1
	        option enabled_fsck  0

Now reboot your MW151RM3G, you will got an 8G based flash disk OpenWRT.

Using SNMP for administ PogoPlug

###Preparation For using SNMP for administrating PogoPlug, we have to install following packages first:

	# apt-get install snmpd smartmontools super

snmpd is the daemon process for snmp, smartmontools is the monitor tools for harddisk, super let you run daemon process in super priviledge.
###Configuration Configure snmp firstly.

###Client Configuration Install the webserver using the following command:

	$ pacman -S apache php php-apache mariadb

After install the packages, if you want to change the configuration file, directly edit the /etc/httpd/conf/httpd.conf or extra/httpd-default.conf, then using systemd to restart the httpd daemon.
Change priviledge of the public_html directory:

	$ chmod o+x ~
	$ chmod o_x ~/public_html
	# A more safe way for controlling the safety
	# usermod -aG groupname http

###SSL Configuration Generate a new self-signed certificate(key size and the number of days of validity will be customized):

	# openssl genrsa -out server.key 2048
	Generating RSA private key, 2048 bit long modulus
	..........................+++
	...........................................................................................................................................................+++
	e is 65537 (0x10001)
	[root@XXXyyy conf]# ls
	extra  httpd.conf  magic  mime.types  server.key
	[root@XXXyyy conf]# chmod 600 server.key
	[root@XXXyyy conf]# openssl req -new -key server.key -out server.csr
	You are about to be asked to enter information that will be incorporated
	into your certificate request.
	What you are about to enter is what is called a Distinguished Name or a DN.
	There are quite a few fields but you can leave some blank
	For some fields there will be a default value,
	If you enter '.', the field will be left blank.
	-----
	Country Name (2 letter code) [AU]:
	State or Province Name (full name) [Some-State]:
	Locality Name (eg, city) []:
	Organization Name (eg, company) [Internet Widgits Pty Ltd]:
	Organizational Unit Name (eg, section) []:
	Common Name (e.g. server FQDN or YOUR name) []:
	Email Address []:
	
	Please enter the following 'extra' attributes
	to be sent with your certificate request
	A challenge password []:
	An optional company name []:
	[root@XXXyyy conf]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
	Signature ok
	subject=/C=AU/ST=Some-State/O=Internet Widgits Pty Ltd
	Getting Private key
	[root@XXXyyy conf]# ls
	extra  httpd.conf  magic  mime.types  server.crt  server.csr  server.key

Then, uncomment the line containing following lines in /etc/httpd/conf/httpd.conf:

	Include conf/extra/httpd-ssl.conf
	Restart httpd to see the result. 

###Enable php Place Following line after LoadModule dir_module modules/mod_dir.so in /etc/httpd/conf/httpd.conf:

	LoadModule php5_module modules/libphp5.so
Place following line at the end of the Include list:
	Include conf/extra/php5_module.conf
Make sure you uncommented following line:
	TypesConfig conf/mime.types
	# Optional uncomment:
	MIMEMagicFile conf/magic
Add this line in /etc/httpd/conf/mime.types:
	application/x-httpd-php       php    php5
Make sure libphp5.so is under /etc/httpd/modules, if not, you may forgot to install php-apache.  
Test the php via following line under test.php:
	<?php phpinfo(); ?>

###Configure MariaDB Uncomment at least one line under /etc/php/php.ini:

	extension=pdo_mysql.so
	extension=mysqli.so
	extension=mysql.so

###Mysql Configuration

	[root@XXXyyy http]# mysqld_safe --skip-grant-tables &
	[1] 26315
	[root@XXXyyy http]# 131014 16:55:07 mysqld_safe Logging to '/var/lib/mysql/XXXyyy.err'.
	131014 16:55:07 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
	
	[root@XXXyyy http]# mysql -u root mysql
	Welcome to the MariaDB monitor.  Commands end with ; or \g.
	Your MariaDB connection id is 6
	Server version: 5.5.33a-MariaDB-log Source distribution
	
	Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
	
	Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
	
	MariaDB [mysql]> UPDATE mysql.user SET Password=PASSWORD('******') WHERE User='root';
	Query OK, 4 rows affected (0.00 sec)
	Rows matched: 4  Changed: 4  Warnings: 0
	
	MariaDB [mysql]> FLUSH PRIVILEGES;
	Query OK, 0 rows affected (0.00 sec)
	
	MariaDB [mysql]> exit
	Bye
	[root@XXXyyy http]# systemctl start mysqld

###CACTI configuration Install cacti via:

	$ pacman -S cacti
	$ pacman -S php-snmp

uncomment the following lines in /ec/php/php.ini:

	;extension=mysql.so
	;extension=sockets.so
	;extension=snmp.so
	open_basedir=...
	;date.timezone =

Start mysqld:

	$ systemctl enable mysqld.service
	$ systemctl start mysqld.service

Start snmpd:

	$ systemctl enable snmpd
	$ systemctl start snmpd

Change the configuration file /etc/httpd/conf/httpd.conf, inside <Directory “/srv/http"> block, ensure you have:

	AllowOverride All

Create the mysql based db:

	$ mysqladmin -u root -p create cacti
	Enter password: 

Generate the sql table using existing template:

	[root@XXXyyy http]# mysql -u root -p cacti < /usr/share/webapps/cacti/cacti.sql
	Enter password: 

Update the configuration of the database, you can replace the password from “some_password” to your own password.

	[root@XXXyyy http]# mysql -u root -p
	Enter password: 
	Welcome to the MariaDB monitor.  Commands end with ; or \g.
	Your MariaDB connection id is 10
	Server version: 5.5.33a-MariaDB-log Source distribution
	
	Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
	
	Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
	
	MariaDB [(none)]> GRANT ALL ON cacti.* TO cacti@localhost IDENTIFIED BY 'some_password';
	Query OK, 0 rows affected (0.00 sec)
	
	MariaDB [(none)]> FLUSH PRIVILEGES;
	Query OK, 0 rows affected (0.00 sec)
	
	MariaDB [(none)]> exit
	Bye

Change the username and password of the config.php of cacti:

	$ vim  /usr/share/webapps/cacti/include/config.php
	$ database_username = "cacti";
	$ database_password = "some_password";

Clean up the cacti directory:

	chown -R http:http /usr/share/webapps/cacti/{rra,log}
	rm /usr/share/webapps/cacti/.htaccess
	chmod +x /usr/share/webapps/cacti/{cmd,poller}.php /usr/share/webapps/cacti/lib/ping.php

Install caccti-spine, a fast poller for cacti, from AUR. Change the db name and db password to:

	$ vim /etc/spine.conf
	DB_User cacti
	DB_Pass some_password

Restart crontab:

	$ systemctl restart cronie.service
	$ systemctl restart httpd

How python interact with RaspberryPI GPIO

Yesterday I use RaspberryPI to control the LED using its GPIO port. with Python code we can directly “talk” to RaspberryPI, and set LED on and off. But what is the principle in it? How to realize our own library(for example, python/perl/lua based library?), here I will dive into the python library code and find what’s really inside the python code.

###Prepare the environment Install the RPi.GPIO via pip, $ pip install RPi,GPIO .
After installed the GPIO library, we can insert help(‘modules’) in python shell/prompt window to view all of the installed modules, we could found RPi in the list of the packages.

We can found the corresponding packages under the installation directory of python, so first you have to find the location of your python executable file, use “which python” to find the destination of the python.
For example, my RPi installed location is under: /home/Trusty/.virtualenvs/venv2/lib/python2.7/site-packages, not in the standard directory, because the python runs in the virtual environment.

###How to control the GPIO in UserSpace. Controlling GPIO in shell :

	root@rasp:~# echo 18 > /sys/class/gpio/export 
	root@rasp:~# ls /sys/class/gpio/
	export  gpio18  gpiochip0  unexport
	root@rasp:~# echo "out" > /sys/class/gpio/gpio18/direction 
	root@rasp:~# echo 1 > /sys/class/gpio/gpio18/value 
	root@rasp:~# 
	root@rasp:~# echo 23 > /sys/class/gpio/export 
	root@rasp:~# ls /sys/class/gpio/
	export  gpio18  gpio23  gpiochip0  unexport
	root@rasp:~# echo "out" >/sys/class/gpio/gpio23/direction 
	root@rasp:~# echo 1>/sys/class/gpio/gpio23/value 
	-bash: echo: write error: Invalid argument
	root@rasp:~# echo 1 > /sys/class/gpio/gpio23/value 
	root@rasp:~# echo 0 > /sys/class/gpio/gpio23/value 
	root@rasp:~# echo 0 > /sys/class/gpio/gpio18/value 

So we can see, the standard procedure is: write exported gpio number to export file–> write direction to gpioXX’s direction–> write value to the gpioxx’s value. The python script wrapped the upper procedures, so we directly call them in python written scripts.

xively入门(1)

###What is Xively?
Xively Cloud Services™
The world’s first IoT Public Cloud
###How to get start? Read tutorial and generate an API.
Then you can visit your own APP under: WEB TOOLS-> DEVELOP -> something.
Create a new Development Devices, named “Raspberry Pi”, xively will automatically generate the API Keys and the feed id for this developement device.

The key issue for using APIs is “talk” to the APIs and upload the data to the website. images/xively.jpg