วันพฤหัสบดีที่ 28 มกราคม พ.ศ. 2559

เล่นกะ GPIO

 
ทดสอบที่ GPIO 2 


Solution 1 (wiring PI)
sudo apt-get install git-core
git clone git://git.drogon.net/wiringPi 
cd wiringPi
git pull origin
./build

gpio -v
gpio -g mode 2 out
gpio -g write 2 1
gpio -g write 2 0 
 
 Link>>https://www.unzeen.com/article/2179/
 
Solution 2 (Shell script)
 
echo 2 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio2/direction
echo 1 > /sys/class/gpio/gpio2/value
echo 0 > /sys/class/gpio/gpio2/value
echo 2 > /sys/class/gpio/unexport
 
 
Solution 3 (python script)
 
  $  sudo apt-get install python-dev
  $  sudo apt-get install python-pip
  $  sudo apt-get install python-rpi.gpio 
 
nano gpio-on.py  

#!/usr/bin/env python
# Turn on GPIO 2
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(2,GPIO.OUT)
GPIO.output(2,GPIO.HIGH)
nano gpio-off.py  
#!/usr/bin/env python
# Turn on GPIO 2
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(2,GPIO.OUT)
GPIO.output(2,GPIO.LOW)
 
Link>>>http://www.arduitronics.com/article/raspberry-pi-connect-to-the-real-world-gpio-part-2 
Link>>>http://www.sathittham.com/raspberry-pi/rpi-ep-4/
Link>>>http://www.ezenow.com/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%AA%E0%B8%B1%E0%B9%88%E0%B8%87%E0%B9%80%E0%B8%9B%E0%B8%B4%E0%B8%94-%E0%B8%9B%E0%B8%B4%E0%B8%94-led-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-python-gpio/ 
 
แถม Blink ที่ GPIO 2
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(2, GPIO.OUT)
while 1:
    GPIO.output(2, True)
    time.sleep(0.2)
    GPIO.output(2, False)
    time.sleep(0.2)

GPIO.cleanup()
 
  



แก้ AttributeError: 'module' object has no attribute 'setmode' ใน GPIO

sudo easy_install -U RPi.GPIO
or

Discovered the problem:
Some guides online instruct you do download version 0.1.0 of the GPIO library which does not have the setup function. You must use a more recent version of the library.
RPi.GPIO-0.1.0.tar.gz
I have downloaded version 0.5.x and it works correctly.
RPi.GPIO-0.5.3a.tar.gz
Download from here https://pypi.python.org/pypi/RPi.GPIO
or Download from here http://code.google.com/p/raspberry-gpio-python/downloads/list

Installation Instructions
Extract the script
tar -zxvf RPi.GPIO-0.5.3a.tar.gz
cd RPi.GPIO-0.5.3a
sudo python setup.py install
 
 

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

Detect someone online bash script (email alert)

#!/bin/bash
SUBJ="!!!Host Online!!!"
EMAIL="xxx@gmail.com"

intertube=0
echo "begin ping"
while [ $intertube -ne 1 ]; do
        ping -c 1 192.168.1.8>/dev/null
        if [ $? -eq  0 ]; then
                echo "$(date) Host Online" | mail -s "SUBJ$ at $(date)" $EMAIL;
                intertube=1;
        else
                echo "still offine"
        fi
done
echo "fin script"

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