2014年7月24日 星期四

Using LiveCode on Raspberry Pi – Demo 1 : PIR sonsor & LED (Traditional Chinese)

      (You can see this article in english here : http://hedgehao.blogspot.tw/2014/07/raspberry-pi-has-26-gpio-pins-on-it-it_24.html)
      Raspberry Pi擁有26GPIO腳位(新版的Raspberry Pi Model +新增至40個腳位)GPIO的腳位可以用來控制LED燈泡各種感應器馬達等等,這篇文章中我將要分享PIR這個動作感應器配合LED燈泡來做回應,當偵測到動作時燈泡會亮起,當動作結束後燈泡會熄滅。


      可以參考這個Demo影片:http://youtu.be/ZLLmuAAM7Cc
      在LiveCode的程式碼中,我設定了2秒的等待時間(這個等待時間是參考一些網路上的教學文章),所以在影片當中可以看到揮動手的時候燈泡並沒有馬上的反應,而會有一段延遲時間,這個程式碼是公開發佈的,大家可以自行修改這個等待時間,或許能夠找出最適合的延遲時間。
      以下是這個範例的電路圖:
      這個電路圖並不完全和影片和照片中的相同,為了呈現的方便我將他簡化了。首先將PIR sesnor接上RPi,將PIR的電源(紅色線)接上RPi 5V腳位,接地(黑色線)接到RPi 的接地腳位,訊號線(黃色線)接到RPiGPIO18腳位,訊號線必須接到18腳位,因位程式中設定的是控制18這個腳位。
      再來是LED的部分,在LED負極接上一個電阻,電阻的另一端接到RPiGND腳位,LED的正極接到GPIO4腳位,一樣是程式中設定控制的腳位。

      這次Demo的範例程式提供兩個LiveCode External,可以在這裡下載:

      下載完後將檔案解壓縮,執行”haoRPiDemo1.rev”
      如果你試圖移動三個檔案的位置,必須依照下列步驟從新設定external位置:把”haorpiled.so””haorpipir.so”兩個external放到適合的資料夾中,例如“/home/pi/haopir”。接著打開壓縮檔中的範例程式” haoRPiDemo1.rev”,將StackExternal屬性設定為兩個External存放的路徑位置,或是透過屬性面板的External Reference來設定。

存檔之後重新開啟LiveCode(確保External有被載入到Stack)
執行時先按下按鈕”setup”來開啟GPIO的控制連線,如果沒有出錯的話Message Box應該顯示出”OK”的字樣,接著就可以按下按鈕”Start Detect”開始偵測,如果PIR偵測到動作的話LED就會亮起,若動作停止了,LED燈就會熄滅。
再來談談程式碼,程式碼的部分非常簡單,以LED為例,首先利用” haoRPiLEDOpen()”來做基本的設定工作,接著可以使用”haoRPiLEDLight()”“haoRPiLEDDark()”來控制LED燈泡的亮、暗。以PIR來說可以透過”haoRPiPIRReadMotion()”來讀取PIR的資料,讀取的資料中1代表針測到動作0代表動作停止,當LiveCode讀到資料後會送出” PIRSensorDidReadValue”這個訊息到目前顯示的卡(current card)上。最後當程式結束前,記得將GPIO的控制連線關閉,使用“haoRPiPIRClose()” “haoRPiLEDClose()”
這邊提到的所有函式(Function)LiveCode都定義為Function而不是Command,如果執行的過程中沒有出錯,每個函式應該會回傳“OK”的字樣。

以上就是這次的Demo,感謝收看,如果發現有任何Bug請通知我 J

Using LiveCode on Raspberry Pi – Demo 1 : PIR sonsor & LED (English)

    Raspberry Pi has 26 GPIO pins on it (it will be up to 40 pins on new RPi Model B+). It can controls the LEDs, sensors, motors and many others. In this article I’m going to demo a program writing in LiveCode sciprts to detect motions using PIR motion sensor. When sensor detect motions light up the LED. When that motion is gone LED went dark.

    Here’s the demo vidieo : http://youtu.be/ZLLmuAAM7Cc
 
    In my script, I set the program to wait 2 seconds for each detection(Which some tutorial on the web suggests). That’s why you could possibly feel the delays of the sensor’ detection in the video. My LiveCode script in this demo are opened. Maybe you could try yourself to modify the waiting time. To find the best time setting.

    Here’s the electrical circuits:
    There circuits here are not exactly the same us the circuits you can see in the photo or video. I make it as simple as it can. 
    First, connect PIR to our RPi. Connect PIR-VCC (red) to 5V pin and PIR-GND(black) to any GND pin on RPi. Connect PIR-OUT (yello) to GPIO18 pin. You HAVE TO connect to GPIO 18 cause the program is written to control GPIO 18 pin. 
    Second, plug LED cathode to resistor on breadboard. The other pin of resistor connect to GND pin. Remember to connect LED anode the GPIO 4 which the program sets.

    In this demo I offer two LiveCode externals. You can download them here :

    Unzip the file.Run “haoRPiDemo1.rev”.
    
    If you move the three files to other place. You should reset the external as following steps. Put “haorpiled.so” and “haorpipir.so” into the proper direction let’s say “/home/pi/haopir”.Open the demo program “haoRPiDemo1.rev” .Set the external of this stack to the both file path. You can use the Inspector to set the External References, either.
    Save stack , restart LiveCode IDE just to be sure the library load into our stack file.

    When the stack is open. First press button “setup” to open the GPIO connection. If everything goes well the message box will display message “OK”. Then press button “Start Detect” the PIR sensor will start the detection.When PIR dectect motion LED will light up.
    Scripts are simple. Take LED for example. First use “haoRPiLEDOpen()” to setup. Function “haoRPiLEDLight()” to light up LED,”haoRPiLEDDark()” to shut down LED.For PIR sensor, after setup you can use “haoRPiPIRReadMotion()” to read values. 1 means motion detect, 0 means no motion detect. When LiveCode did read a value, it will send a message “PIRSensorDidReadValue” with the read value as first parameter.At the end of the program you should close the GPIO connections.Use function “haoRPiPIRClose” and “haoRPiLEDClose” to accomplish that.
        Remember all the function you can use is defined as function in LiveCode(instead of command), It will return “OK” if everything goes well.

That’s all ! Thanks for watching.
If you find any bugs.That me know. J

中文版連結:中文版

2014年7月17日 星期四

Android Socket for LiveCode

So many people using LiveCode are looking forward to have a socket on Android.
You can use socket on Windows, Linux, Mac OS and there is also an external for iOS.
Now, I wrote a socket external for Android.
Take a look at what I've done.

First, I use my notebook as a server which IP is 192.168.0.103
My Android mobile phone as a client which IP is 192.168.0.102

Here's my demo stack.I'm going to show you the function on TCP socket.
As you can see, the connection state on the top is "Closed" when I open the stack.
Laptop(left)  Android Phone (right)

when I hit "accept" the laptop start to act like a server to wait for connections in.
The connection state then change to "Waiting".
When the server is set up. I hit the "open" button on the Android phone. You can see the message tells that my phone is connected. The connection state change to "Connected" on the phone.
Next, when I hit button "write" on the laptop. There server will write a message which is type on the "Send Message" area. On my mobile phone I can hit button "read". The message that the server just send will be received on the phone.
On the other hand, when I hit button "write" on the phone. Server should have received the message. This time I change the message to "From Android Phone"

Finally, when I close the connection on either side. Both the laptop and the phone will call an close message then answer the close message. (I have not change the file descriptor to real IP on Android so you can see the 52 on the screen)
That's all for today! Hope you enjoy it!

2014年7月8日 星期二

LiveCode + Raspberry Pi - Light up a LED

Recently I bought a Raspberry Pi.
And I noticed that LiveCode has a RPi version installer on the download page.
so, I install LiveCode on my Rasberry Pi.
Write an external on LiveCode with C to control the GPIO.

Finally I can use a simple LiveCode Script to light the LED.
And event blinking an LED.
Here's what I wrote in LiveCode script


    haogpiosetup()
    repeat with x = 1 to 10
        put haogpiowrite(4,true)
        wait 1 sec
        put haogpiowrite(4,false)
        wait 1 sec
    end repeat

I use function instead of command just in case that I can be sure whether my external code are wrong.
If everything go well, my function will return "OK" 


I'll post my work here constantly.
If you're interesting in what I am doing. Please leave a message and let me know. :)