วันศุกร์ที่ 1 พฤศจิกายน พ.ศ. 2556

Raspberry Pi as a Router


Raspberry Pis have started making there way into homes all across the world. When I got mine, the first thing I wanted to do was set it up as a router to replace my aging piece of D-link garbage.


This guide is somewhat geared towards those new to Linux so experienced users can feel free to skip around a bit and modify some of my commands.

The first step is to download and install Raspbian onto your SD card. You could just as easily use Debian, but Raspbian is preferred because it's optimized for the Pi. I'm using theDarkbasic minimal image because there's really no use for a GUI on a router and it would just hog resources but you can decide what flavor you like best.

I won't tell you how to install Raspbian because there's plenty of guides on the Raspbian site that explain it way better than I ever could.

From my experience, the Raspbian images you can download contain older versions of the Raspberry Pi firmware which is a problem because the kernel is not compiled with iptables support. Hexxeh to the rescue! The easiest way to install the new firmware is to use Hexxeh's rpi-updater which is available here with instructions as to how to install it. Remember to install ca-certificates before you wget the updater.

sudo apt-get update && sudo apt-get install ca-certificates

If I remember correctly, the script has one more dependency, but it will tell you what it is and it's available in the repositories and you can just apt-get it.

After your firmware has been updated, reboot the pi to activate it.

Here's where the fun begins, but first a bit of logic regarding my setup. As is shown in my video, the on-board NIC of the pi (eth0) is connected to the internet via my cable modem. My USB-NIC (eth1) is connected to my internal network, specifically in my case a wireless access point, but you could just as easily connect a switch if you need more wired ports.

As you can see in the video, I am using an apple branded USB-NIC because it's what I had lying around, but this is not a necessity. Debian has good support for lots of USB-NICs and in most cases they'll be recognized when you plug them in without any additional configuration.

The first step is to edit /etc/network/interfaces.

sudo nano /etc/network/interfaces

We want the internet facing NIC to get an address from our ISP via DHCP and our internal NIC to have a static address. I am using 192.168.50.0/24 as my internal subnet but you can use any subnet you like as long as it is RFC 1918 compliant. Keep in mind a /24 (255 addresses) is most always big enough for a home network.

Here is what my /etc/network/interfaces file looks like. If you decide to change the internal subnet, you'll need to edit my addresses to suit your setup.

# interfaces(5) file used by ifup(8) and ifdown(8)

auto lo
iface lo inet loopback

#Onboard NIC connecting to the Internet
auto eth0
iface eth0 inet dhcp

#USB NIC serving as internal gateway
auto eth1
iface eth1 inet static
address 192.168.50.1
netmask 255.255.255.0
network 192.168.50.0
broadcast 192.168.50.255
gateway 192.168.50.1
Save the file and restart networking (or reboot).

sudo /etc/init.d/networking restart

Next we need to install the DHCP server package on our Pi so we can allocate addresses to clients.

sudo apt-get install isc-dhcp-server

Now let's edit the DHCP server configuration file.

sudo nano /etc/dhcp/dhcpd.conf

The configuration files provides a lot of examples that are all commented out. Feel free to read them if you care to. Since this should be the only DHCP server on our network, let's make it authoritative. Uncomment out the authoratative line near the top of the file.

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;

Next, lets add a new subnet. Scroll down to the bottom of the file and add something like this:

subnet 192.168.50.0 netmask 255.255.255.0 {
range 192.168.50.10 192.168.50.250;
option broadcast-address 192.168.50.255;
option routers 192.168.50.1;
default-lease-time 600;
max-lease-time 7200;
option domain-name "local";
option domain-name-servers 8.8.8.8, 8.8.4.4;
}

Again, this is assuming a 192.168.50.0/24 subnet. You'll need to change it if you've chosen a different subnet.

You'll also notice I'm using the Google public DNS servers, 8.8.8.8 and 8.8.4.4. If you prefer to use the DNS servers provided by your ISP, you'll need to change this line to reflect their addresses.

Save the file and restart the DHCP service.
sudo /etc/init.d/isc-dhcp-server restart
You should recieve two ok messages.

[ ok ] Stopping ISC DHCP server: dhcpd.
[ ok ] Starting ISC DHCP server: dhcpd.

If you receive an error about no interfaces being in the proper address space double check your configurations and make sure your static address on eth1 is in the same subnet as your DHCP subnet. if you need to make any changes you'll need to restart the relevant services.

At this point you should be able to plug a device into the USB NIC (eth1) interface of your pi and receive an IP address via dhcp. However, you won't be able to get any further on the network than your Pi itself. To solve this, we need to enable IP forwarding.

Guides I read said I needed to do one or the other of the following things, however, I had to do both. First run the following command:

sudo echo 1 > /proc/sys/net/ipv4/ip_forward

Next edit /etc/sysctl.conf and uncomment out the line that says net.ipv4.ip_forward = 1.

sudo nano /etc/sysctl.conf

# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

Save the file.

The final step is to insert an iptables rule to allow NAT.

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Time for final testing. Plug a computer into the eth1 interface on your pi and plug the onboard NIC into your modem.

After the negotiation phase, your computer will pull an address and you should be able to access the internet! if it doesn't work, ssh to the Pi by using the address you gave eth1 (192.168.50.1 in my case) and ensure that eth0 has a public address by running ifconfig.

sudo ifconfig -a
eth0      Link encap:Ethernet  HWaddr b8:27:eb:e8:4a:fe  
          inet addr:68.229.57.30  Bcast:68.229.51.255  Mask:255.255.255.0
          inet6 addr: fe80::ba27:ebff:fee8:4afe/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1851717 errors:0 dropped:0 overruns:0 frame:0
          TX packets:680737 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1493496473 (1.3 GiB)  TX bytes:131062180 (124.9 MiB)

eth1      Link encap:Ethernet  HWaddr 40:3c:fc:00:74:b0  
          inet addr:192.168.50.1  Bcast:192.168.50.255  Mask:255.255.255.0
          inet6 addr: fe80::423c:fcff:fe00:74b0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:675292 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1052136 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:116080201 (110.7 MiB)  TX bytes:1474222354 (1.3 GiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:2 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:152 (152.0 B)  TX bytes:152 (152.0 B)
If your eth0 still shows a private address it probably didn't renew when you moved it to your modem. Fix this by running:

sudo ifdown eth0 && sudo ifup eth0

Check your IP address by running ifconfig again and see if you can reach the internet. You may need to reboot your modem, however do not reboot your Pi for the reasons that are to follow.

If it's still not working, stop here and go back and make sure you've not missed any steps.

You should not reboot your Pi at this point because the iptables rules for nat we inserted earlier are not persistent and if you reboot they will be overridden by the default configuration (nothing). We can fix this by saving the rules and creating a little script to restore them as the network interfaces come up during boot.

First, save your iptables rules to a file.

sudo iptables-save > /etc/iptables.up.rules

You don't have to save them to /etc/iptables.up.rules, that's just where I save mine.

Next create a script in /etc/network/if-pre-up.d/ with the following contents:

sudo nano /etc/network/if-pre-up.d/iptables
#!/bin/sh
#This script restores iptables upon reboot

iptables-restore < /etc/iptables.up.rules

exit 0

Change ownership and permissions of the script so it will run at boot.

sudo chown root:root /etc/network/if-pre-up.d/iptables && sudo chmod +x /etc/network/if-pre-up.d/iptables && sudo chmod 755 /etc/network/if-pre-up.d/iptables

Voila. You can now reboot and your iptables rules will stay persistent.

For some additional security, lets add some more iptables rules:

sudo iptables -A INPUT -s 192.168.0.0/24 -i eth0 -j DROP
sudo iptables -A INPUT -s 10.0.0.0/8 -i eth0 -j DROP
sudo iptables -A INPUT -s 172.16.0.0/12 -i eth0 -j DROP
sudo iptables -A INPUT -s 224.0.0.0/4 -i eth0 -j DROP
sudo iptables -A INPUT -s 240.0.0.0/5 -i eth0 -j DROP
sudo iptables -A INPUT -s 127.0.0.0/8 -i eth0 -j DROP
sudo iptables -A INPUT -i eth0 -p tcp -m tcp --dport 22 -j DROP
sudo iptables -A INPUT -i eth0 -p icmp -m icmp --icmp-type 8 -j DROP

This blocks access from RFC 1918 subnets on your internet (eth0) interface as well as ICMP (ping) packets and ssh connections.

Remember to save whenever you make changes!
sudo iptables-save > /etc/iptables.up.rules

If you want to see how many packets your firewall is blocking, run this command:

iptables -L -n -v

Chain INPUT (policy ACCEPT 215 packets, 23539 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  eth0   *       192.168.0.0/24       0.0.0.0/0           
  126 34570 DROP       all  --  eth0   *       10.0.0.0/8           0.0.0.0/0           
    0     0 DROP       all  --  eth0   *       172.16.0.0/12        0.0.0.0/0           
    0     0 DROP       all  --  eth0   *       224.0.0.0/4          0.0.0.0/0           
    0     0 DROP       all  --  eth0   *       240.0.0.0/5          0.0.0.0/0           
    0     0 DROP       all  --  eth0   *       127.0.0.0/8          0.0.0.0/0           
    0     0 DROP       tcp  --  eth0   *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
    0     0 DROP       icmp --  eth0   *       0.0.0.0/0            0.0.0.0/0            icmptype 8
    0     0 DROP       all  --  eth0   *       192.168.0.0/24       0.0.0.0/0           

Chain FORWARD (policy ACCEPT 15696 packets, 14M bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 191 packets, 25875 bytes)
 pkts bytes target     prot opt in     out     source               destination  

At this point, you should have a fully functional router that has extremely low power usage and some basic security.

In the future, I plan to add a switch to my network and add some 802.1q trunks to my USB Nic so I can have multiple LANs, but that's a different article.

วันจันทร์ที่ 28 ตุลาคม พ.ศ. 2556

8 Resources To Find Raspberry Pi Project Ideas

ref: http://www.efytimes.com/e1/fullnews.asp?edid=119611
8 Resources To Find Raspberry Pi Project Ideas  
 
Read on to find Raspberry Pi inspirations and ideas from 8 different resources.  
Rate this news:   (2 Votes)
Saturday, October 26, 2013:  Raspberry Pi is indeed the hot favorite amongst all the single-board computers and DIY electronics. It runs free and open source software and has huge support of hardware add-ons and peripherals, all thanks to the incredibly large and active user community. And there is indeed no end to the creativity that the creators have been using on it. Ad if you are also hunting for a project for your Pi, here we bring to you 8 resources to hunt down a Raspberry Pi project idea.
Open Source, Linux, Raspberry Pi, Raspberry pi project, raspberry pi inventions, Open source hardware, open source board, raspberry pi ideas




Adafruit Learning System: Adafruit Learning System is partially an online store, partially a knowledge base and partially a social community. Limor "Ladyada" Fried's Adafruit Industries is a hardware hacker's paradise. And while there's much more to Adafruit than Raspberry Pi-related products and information, the project ideas you'll find in the Learning System are particularly powerful because they're based around lessons that teach you the skills you need for more complex tasks down the road.

Embedded Linux Wiki: It's really a cliche to say that sometimes quality is more important than quantity, but, often, that really is the case. The RPi Projects section of the Embedded Linux Wiki may not be the largest list around, but that doesn't mean these aren't some of the more interesting ideas you'll run into. Plus, the tabular format with information about each project's difficulty makes finding a project suited to your skill level much easier.

The Magpi: What started as an idea being kicked around an online forum by some hardcore Raspberry Pi enthusiasts has grown into a print magazine, fan community, and a branded Raspberry Pi case. Issues of this magazine include much more than project ideas, so it's a great way to stay up on current Raspberry Pi news while looking for your next project idea.

MAKE: MAKE Magazine is now a well-established in the world of DIY electronics hobbies. The brand now includes the print magazine, Maker Faire, and tons of free online content. And, it's in that online content that you'll find some great project ideas for your Raspberry Pi board.

Raspberry Pi Foundation: With so many fans and user communities popping up all over, it's easy to forget that the official Raspberry Pi forums right on the Raspberry Pi website have a whole section dedicated to project ideas. It's an active group that's well worth checking out.

Raspberry Pi on Google+: Open source is alive and well on social networks. And, Raspberry Pi is certainly no stranger to things that people love to talk about on Google+. With a fan community boasting over 25,000 members, this is a great resource for connecting directly with some of Raspberry Pi's most vocal and active users while also picking up tips and ideas for your next project.

Raspberry PI Community Projects: The large, well-known media outlets generally provide consistent and informative data about Raspberry Pi, but overlook the small independent bloggers, and you might just miss out. This blog is updated regularly and provides posts not only about project ideas but also general interest information but also curates with all the passion and care that can make blogs so much fun to read.

Raspberry Pi Spy: This blog has purely been created for the love of Raspberry Pi. Spy delivers content on hardware of interest, tutorials, and project ideas related to Raspberry Pi boards. The posts are more inclined towards the tutorials than general project ideas but you will definitely find some inspiration here.

Courtesy: about.com 

วันพฤหัสบดีที่ 3 ตุลาคม พ.ศ. 2556

Easy code readSerial Port กันลืม

void loop
{
char c=Serail.read();

if (c=='1') digitalWrite(13,HIGH);
if (c=='a') digitalWrite(13,LOW);
delay(1000);
}

วันจันทร์ที่ 30 กันยายน พ.ศ. 2556

Arduino - Ethernet Shield

Arduino - Ethernet Shield


How to build an arduino energy monitor - measuring mains current only. :ref http://openenergymonitor.org/emon/buildingblocks/how-to-build-an-arduino-energy-monitor-measuring-current-only?page=1


How to build an arduino energy monitor - measuring mains current only.

This little guide details how to build a simple electricity energy monitor on a breadboard that you could use to measure how much electrical energy you use in your home. It measures current and takes a set value for voltage (if your in the uk 230V) to calculate apparent power all the calculations are done on an arduino, although not as accurate as when a voltage measurement is taken, it is a method commonly used in whole house energy monitors for reasons of simplicity and lower cost. Here's how to do it:

Step One – Gather Components

You will need:
1x Arduino
Current sensing electronics
1x CT sensor yhdc SCT-013-000
1x Burden resistor 18 Ohms if supply voltage is 3.3V or 33 Ohms if supply voltage is 5V.
2x 10kOhm resistors (or any equall valued resistor pair upto 470kOhm)
1x 10uF capacitor
Other
1x A breadboard and some single core wire.
Oomlout do a good arduino + breadboard bundle here £29

Step Two – Assemble the electronics

The electronics consist of the current sensor (which produce signals proportional to the current in the mains) and the sensor electronics that convert these signals into a form the Arduino is happy with.
For a circuit diagram and detailed discussion of sensors and electronics see:
Assemble the components as in the diagram above.

Step Three – Upload the Arduino Sketch

The Arduino sketch is the piece of software that runs on the Arduino. The Arduino converts the raw data from its analog input into a nice useful values and then outputs them to serial.
a) Download EmonLib from github and place in your arduino libraries folder.
Download: EmonLib
b) Upload the voltage and current example:
#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1;                   // Create an instance

void setup()
{  
  Serial.begin(9600);
  
  emon1.current(1, 111.1);             // Current: input pin, calibration.
}

void loop()
{
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only
  
  Serial.print(Irms*230.0);        // Apparent power
  Serial.print(" ");
  Serial.println(Irms);         // Irms
}

c) Open the arduino serial window
You should now see two columns of values. Apparent power on the left and rms current on the right.
Regeneractiv's picture

Re: How to build an arduino energy monitor - measuring current only

Guten Morning!
I translated this tutorial into German. It´s not perfect but I hope it could help someone.-->Link.
Ich hab´ den Tutorial übersetzt, ist sehr schlecht geschrieben, da Deutsch nicht meine Muttersprache ist, aber verständlich sollte es sein... für fragen:
http://electronics.stackexchange.com/questions/23591/ac-current-sensor
http://forum.arduino.cc/index.php/topic,12921.0.html
http://openenergymonitor.org/emon/buildingblocks/ct-sensors-interface
http://openenergymonitor.org/emon/buildingblocks/how-to-build-an-arduino-energy-monitor-measuring-current-only?page=1

วันพฤหัสบดีที่ 19 กันยายน พ.ศ. 2556

Auto reboot rPI hang By Watchdog นำมาจาก https://www.facebook.com/RaspberryPi66

Auto reboot Raspberry Pi เมื่อมัน hang ไปซะเฉยๆ ด้วย Watchdog

16 กุมภาพันธ์ 2013 เวลา 6:48 น.

บางครั้งเราเปิด rPi ไว้บ้านเพื่อนทำ Home automation หรือ load bit ก็ว่ากันไปแล้วมันหยุดทำงาน ตอนเราไม่อยู่บ้าน แหมเหมือนใจมันจะขาดรอนๆ จะสั่งใครให้ reboot ให้ก็ไม่ได้

วันนี้เรามาดูวิธีติดตั้ง watchdog เพื่อให้มันเฝ้าดูการทำงานของเครืองให้เรากันครับ โดย watchdog timer นัั้นจะเป็นการทำงานด้านฝั่ง hardware เลย โดยจะนับถอยหลังจากค่าที่เรากำหนดไว้จนได้ค่าเป็น 0 มันจะสั่งให้ rPi reboot ทันที ซึ่งโดยปกติถ้าระบบทำงานปกติค่าตัวแปรจะถูกอัพเดตให้มีค่าสูงไว้ตลอด ถ้านับได้ 0 แปลว่ามัน หยุดทำงานไปแล้วนั่นเอง

ซึ่งโชคดีมากที่ Broadcom BCM2835 SoC ของ Raspberry Pi เรามาพร้อมกับ hardware-based watchdog timer ทำให้เราสามารถใช้งานได้ตามนิยามในย่อหน้าแรก

Load the bcm2708_wdog kernel module

ทำได้โดยเปิดไฟล์ /etc/modules แล้วเพิ่มบรรทัดนี้ลงไป

bcm2708_wdog
จากนั้นสั่ง load module เพื่อให้ทำงานได้ทันที ไม่ต้อง reboot

sudo modprobe bcm2708_wdog


Install the software watchdog daemon

ติดตั้ง package watchdog ด้วยคำสั่ง

sudo apt-get install watchdog

และสั่งให้ทำงานทุกครั้งเมื่อเริ่มระบบ

sudo update-rc.d watchdog defaults

Configure the watchdog daemon
เปิดไฟล์ /etc/watchdog.conf ขึ้นมาแก้ไข โดยเอาเครื่องหมาย # ออกจากหน้าบรรทัดเหล่านี้เพื่อให้โปรแกรมอ่านค่า parameter เหล่านี้

#watchdog-device
.
.
.
#max-load-1 = 24

หลังจากนั้นสั่งให้ service watchdog ทำงานด้วยคำสั่ง

sudo /etc/init.d/watchdog start
ลองหางานหนักๆให้มันทำงานแล้วดูว่าระบบมัน auto reboot ให้รึเปล่า
เท่านี้ก็ได้ผู้ช่วยคนใหม่มาคอย reboot ให้แล้วครับ

Driving Raspberry Pi GPIOs from Scratch



Thank you and REF :http://www.youtube.com/watch?v=uPvHY-KvelI

BASIC PROGRAMMING PI and Hardware


Within these instructions, we shall only cover some very basic programming, more detailed examples and tutorials will be made available from the “Meltwater Raspberry Pi Hardware” site (http://pihw.wordpress.com/) in the learning section.

วันจันทร์ที่ 5 สิงหาคม พ.ศ. 2556

Git: มารู้จักกับโปรแกรมบริหารซอร์สกันเถอะ

Git: มารู้จักกับโปรแกรมบริหารซอร์สกันเถอะ

1969 Linus Torvalds (คนเดียวกับผู้สร้าง Linux) เกิด
1972 กำเนิดโปรแกรม SCCS โปรแกรมจัดการซอร์สโปรแกรมแรกๆ
1990 กำเนิดโปรแกรม CVS
2000 กำเนิดโปรแกรม SVN
2005 Linus ให้กำเนิด Git



Git เป็นโปรแกรมบริหารจัดการซอร์สตัวหนึ่ง เนื่องจากในการพัฒนาโปรแกรมนั้น เราอาจมีการลองผิดลองถูกอยู่หลายทางว่าได้ผลลัพท์เป็นที่น่าพอใจหรือไม่ หรือบางครั้งฟีเจอร์ใหม่ๆ ที่เพิ่มเข้าไปอาจจะทำให้ระบบล่ม ต้องถอยกลับไปก่อนหน้าที่จะเกิดข้อผิดพลาดนั้น การใช้โปรแกรมบริหารซอร์สจะช่วยให้เราแก้ไขปัญหาต่างๆ ได้อย่างมีประสิทธิภาพมากขึ้น

โดย Git นั้นเป็นโปรแกรมบริหารซอร์สแบบกระจายศูนย์ ที่จะแจกซอร์สทั้งหมดให้ผู้ใช้ทุกคนเหมือนกัน ไม่แบ่งแยกชนชั้นครับ (ถ้าใครเป็น CVS/SVN มาก่อน ก็อาจถือได้ว่าต้องเรียนรู้ใหม่เกือบหมดเลยทีเดียว)

ผู้ใช้ Windows สามารถใช้ msysGit แทนได้ (โปรแกรมจะไปรันบน MinGW อีกที)

ส่วนผู้ใช้ Linux ก็เช่นเคย apt-get install git กันได้เลย

หลังจากติดตั้งโปรแกรมเรียบร้อยแล้ว ก็ต้องตั้งค่าเริ่มต้นที่จำเป็นกันหน่อย โดยสั่ง
?
1
2
$ git config --global user.name 'Name Surname'
$ git config --global user.email 'name@example.com'



ลองเริ่มใช้โปรแกรมเลยดีกว่า สมมติจะสร้างโปรเจค hello ก็สั่งดังนี้
?
1
2
3
4
$ mkdir hello
$ cd hello
$ git init
Initialized empty Git repository in ~/hello/.git/

Git จะสร้างไดเรกทอรี .git/ พร้อมไฟล์ระบบต่างๆ ของโปรเจคนี้ให้ครับ

คราวนี้มาเขียนโปรแกรมหลักของโปรเจคนี้กัน
?
1
2
3
$ echo 'print("hello world!")' > hello.py
$ python hello.py
hello world!

ทดสอบแล้วรันได้ไม่มีปัญหา ก็ได้เวลาสั่งให้ Git จำ code เข้าระบบโดย
?
1
2
3
4
5
$ git add hello.py
$ git commit -m 'init program'
[master (root-commit) 2dd5f8a] init program
 1 files changed, 1 insertions(+)
 create mode 100644 hello.py

เท่านี้ก็เรียบร้อยครับ

ปล. โปรแกรมบริหารซอร์สแบบนี้ ใช้คนเดียวยังไงก็ไม่สนุก อย่างลืมชวนเพื่อนๆ มา code เล่นด้วยกันนะครับ

นำมากจาก  http://tutor0x.blogspot.com/2012/07/git_24.html