Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arduino使用入門 建國科技大學 資管系 饒瑞佶.

Similar presentations


Presentation on theme: "Arduino使用入門 建國科技大學 資管系 饒瑞佶."— Presentation transcript:

1 Arduino使用入門 建國科技大學 資管系 饒瑞佶

2 何謂Arduino? 義大利團隊設計開發 2005.10.1第一批
快速開發prototype: artists、designers、 hobbyists OPEN SOURCE 軟體+硬體+社群 Arduino HOME

3 Arduino能做什麼? 簡單易入門的控制器 可以連接感測器,取得外界的訊號,例如溫度與濕度 可以進行簡易的運算與邏輯判斷
可以對外輸出訊號,進行控制

4 Arduino架構_軟體_OPEN魅力 開放IDE軟體原始碼,可針對底層做修改。 相同軟體平台,透過OPEN,快速累積應用範例。
玩家、廠商提供多元函式庫

5 Arduino

6 Open的結果 出現一系列Arduino相關類似產品 Appsduino Arduino Yun…

7 Arduino架構_硬體 數位I/O腳 類比輸入腳 供電系統 電源輸出 數位 I/O腳位:D0~D13 雙供電系統 10Bit解析度
類比訊號輸入 A0~A5 三種電源輸出供應 Vin、5V、3.3V

8 數位/類比/輸入/輸出 數位輸出 DO 數位輸入 DI 類比輸入 AI 類比輸出 A0 多數是電壓訊號 再轉成人可以懂的訊號

9 監控 監視 AI或DI 控制 AO或DO

10 Arduino架構_硬體_OPEN魅力 Arduino不生產周邊硬體不綁死周邊裝置價格平實 周邊裝置搭配arudino腳位開發
硬體電路開放,可自行製作與開發 這些週邊通常被稱為Shield

11 擴充Shield MP3播放板 SD卡模組 WIFI模組 GPRS模組 AV端子顯示模組 COLOR LCD模組 CAN BUS模組
RFID模組 MIDI 訊號模組

12 我們用的是Arduino UNO 直接從USB供電

13 感測器與線材 杜邦線 麵包板

14 下載IDE http://arduino.cc/en/Main/Software/
目前1.x版 (Windows installer-包含usb driver與IDE)

15 確認驅動程式安裝完成 裝置管理員

16 Integradted Development Environment 整合開發環境
Arduino IDE Integradted Development Environment 整合開發環境

17 啟動Arduino IDE

18 載入範例程式Blink Blink程式會控制Arduino板上的LED產生明滅的效果

19 Blink專案開啟畫面 Arduino專案是.ino或.pde檔案

20 Arduino IDE 功能選項區 程式編輯區 編譯訊息區 錯誤訊息區

21 檔案偏好設定

22 主要快速功能選項 編譯確認程式是否正確? 上傳程式到Arduino 開新專案視窗 監看程式執行情形

23 請先確定Arduino UNO硬體已經正確透過USB連接到電腦上
開始前 請先確定Arduino UNO硬體已經正確透過USB連接到電腦上

24 開始使用IDE 步驟1、選擇對應的硬體設備型號

25 開始使用IDE 步驟2、選擇連結的通訊埠(port)位置

26 結果

27 驗證Verify

28 執行Blink Upload

29 Arduino上的黃燈持續閃爍 腳位13

30 程式碼解說 程式說明

31 Arduino專案程式架構 Setup() 啟動設定區 Loop() 循環執行區

32 基本語法 與C語法類似 註解: // 或 /* 多行註解 */ 程式結束需要加上; 程式沒有自動提示的功能

33 只會執行1次 啟動或reset時 設定腳位腳色為OUTPUT 定義使用腳位

34 重複執行 開LED 關LED

35 練習1 透過Arduino控制LED明滅 使用2顆LED 分別接在PIN11(D11)與PIN12(D12)
使用程式控制兩顆輪流間隔2秒做明滅 - +

36 紅綠燈? 黃燈先閃3次 接著亮紅燈 最後亮綠燈

37 Serial Monitor 監看執行狀態

38 使用Serial Monitor監看程式執行狀態 需要設定傳輸速率Baudrate 使用以下指令輸出到監視視窗
Serial.print:輸出後不換行 Serial.println:輸出後換行 Serial就相當於把實際硬體轉成虛擬軟體可以處理的物件

39 範例 從Serial Monitor輸入文字 Arduino收到後轉成ASCII顯示出來

40 程式 轉換後的ASCII CODE 設定傳輸速率 檢查監視通訊通道是否開啟成功 讀取輸入值 轉換成ASCII CODE輸出

41 結果

42 DHT11溫溼度

43 DHT11 Digital in

44 code #include <dht.h> #define dht_dpin 2 //定義訊號要從Pin D2 進來
dht DHT; // 從dht.h建立一個名稱為DHT的軟體物件來代表實際感測器 void setup(){ Serial.begin(9600); delay(300); //延遲3秒 Serial.println("Humidity and temperature\n\n"); delay(700); } void loop(){ DHT.read11(dht_dpin); //使用DHT物件的read11方法讀取還境的溫溼度 Serial.print("Humidity = "); Serial.print(DHT.humidity); Serial.print("% "); Serial.print("temperature = "); Serial.print(DHT.temperature); Serial.println("C "); delay(1000); //每1s更新一次

45 dht.h函式庫 https://github.com/adafruit/DHT-sensor-library
我們只需要透過引入include方式來使用就可以

46 練習2 透過溫度控制LED明滅 使用1顆LED與DHT11溫濕度感測器 LED接在PIN11(D11) 使用程式控制
- +

47 Arduino LCD HD44780

48 LCD HD44780 2x16 LCD 可以顯示兩行訊息,每行 16 個字元
背面已經焊接好一片 LCM1602 IIC V1 控制板,可以使用 IIC(I2C)來控制 LCD 顯示器 不支援中文字

49 接線 LCM1602 IIC V1 控制板有 I2C 的四個針腳,分別為 GND、VCC、SDA 與 SCL,用杜邦線全部接出來

50 接線 VCC 接到 5V,GND 接到 GND,SDA 接到 A4,SCL 接到 A5

51 開始前 安裝 LiquidCrystal 函式庫,下載之後解壓縮到 Arduino 的 libraries 目錄中即可

52 CODE 宣告 #include <Wire.h> // Arduino IDE 內建
// LCD I2C Library,從這裡可以下載: // #include <LiquidCrystal_I2C.h> // Set the pins on the I2C chip used for LCD connections: // addr, en,rw,rs,d4,d5,d6,d7,bl,blpol LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // 設定 LCD I2C 位址 有些會用0X3F

53 CODE void setup() { Serial.begin(9600); // 用於手動輸入文字
lcd.begin(16, 2); // 初始化 LCD,一行 16 的字元,共 2 行,預設開啟背光 // 閃爍三次 for(int i = 0; i < 3; i++) { lcd.backlight(); // 開啟背光 delay(250); lcd.noBacklight(); // 關閉背光 } lcd.backlight(); // 輸出初始化文字 lcd.setCursor(0, 0); // 設定游標位置在第一行行首 lcd.print("Hello, world!"); delay(1000); lcd.setCursor(0, 1); // 設定游標位置在第二行行首 lcd.print("CTUIM"); delay(3000);

54 CODE void loop() { // 當使用者手動輸入訊息 if (Serial.available()) {
// 等待一小段時間,確認資料都接收下來了 delay(100); // 清除舊訊息 lcd.clear(); // 讀取新訊息 while (Serial.available() > 0) { // 將訊息顯示在 LCD 上 lcd.print(Serial.read()); }

55 整合DHT11溫濕度感測器、藍芽程式與LCD
練習4 整合DHT11溫濕度感測器、藍芽程式與LCD 平常將溫溼度值顯示到LCD上 只要藍芽有接收到資料,就顯示到LCD上 需要使用麵包板

56 Arduino Ethernet

57 Ethernet Shield

58 記得 上傳完程式都要 重新啟動Ethernet Shield

59 Arduino as client #include <SPI.h> #include <Ethernet.h>
// Enter a MAC address for your controller below. // Newer Ethernet shields have a MAC address printed on a sticker on the shield byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // if you don't want to use DNS (and reduce your sketch size) // use the numeric IP instead of the name for the server: //IPAddress server(120,109,32,19); // numeric IP for Google (no DNS) char server[] = " // 要連結的網站domain name (using DNS) // 設定IP,這裡使用DHCP的IP IPAddress ip(192, 168, 1, 14); // Initialize the Ethernet client library // with the IP address and port of the server // that you want to connect to (port 80 is default for HTTP): EthernetClient client; // client物件,負責連結server

60 void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // start the Ethernet connection: Ethernet.begin(mac, ip); //Ethernet.begin(mac); //直接透過DHCP取得IP //Serial.println(Ethernet.localIP()); //取得分配的ip // give the Ethernet shield a second to initialize: delay(1000); Serial.println("connecting..."); // if you get a connection, report back via serial: if (client.connect(server, 80)) { Serial.println("connected"); // Make a HTTP request: client.println("GET /search?q=arduino HTTP/1.1"); client.println("Host: client.println("Connection: close"); client.println(); } else { // if you didn't get a connection to the server: Serial.println("connection failed");

61 void loop() { // if there are incoming bytes available // from the server, read them and print them: if (client.available()) { char c = client.read(); Serial.print(c); } // if the server's disconnected, stop the client: if (!client.connected()) { Serial.println(); Serial.println("disconnecting."); client.stop(); // do nothing forevermore: while (true);

62 Arduino as Server 瀏覽器打入此IP,會顯示ananlog A0 ~ A5 的數值
#include <SPI.h> #include <Ethernet.h> // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192,168,1,14); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80);

63 void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP());

64 // listen for incoming clients
void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("new client"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply 判斷是否有client連著Arduino 讀取client送到Arduino的http要求

65 if (c == '\n' && currentLineIsBlank) {
// send a standard http response header client.println("HTTP/ OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); // the connection will be closed after completion of the response client.println("Refresh: 5"); // refresh the page automatically every 5 sec client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html>"); // output the value of each analog input pin for (int analogChannel = 0; analogChannel < 6; analogChannel++) { int sensorReading = analogRead(analogChannel); client.print("analog input "); client.print(analogChannel); client.print(" is "); client.print(sensorReading); client.println("<br />"); } client.println("</html>"); break; 判斷是否http要求已經結束 如果http request已經傳送完成 Arduino就將資料回傳到client的瀏覽器

66 // you're starting a new line currentLineIsBlank = true; }
if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; // give the web browser time to receive the data delay(1); // close the connection: client.stop(); Serial.println("client disonnected"); 看到\n表示http request完成 http request尚未完成

67 Arduino Socket

68 Arduino Socket 可以與其他支援Socket的設備/軟體溝通,例如另一台Arduino 需要設定IP與port

69 Arduino as Socket Server
#include <SPI.h> #include <Ethernet.h> // network configuration. gateway and subnet are optional. // the media access control (ethernet hardware) address for the shield: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //the IP address for the shield: byte ip[] = { 192, 168, 1, 12 }; // the router's gateway address: byte gateway[] = { 10, 0, 0, 1 }; // the subnet: byte subnet[] = { 255, 255, 0, 0 }; // telnet defaults to port 1001 EthernetServer server = EthernetServer(1001);

70 void setup() { Serial.begin(9600); // initialize the ethernet device //Ethernet.begin(mac, ip, gateway, subnet); Ethernet.begin(mac, ip); // start listening for clients server.begin(); } void loop() // if an incoming client connects, there will be bytes available to read: EthernetClient client = server.available(); server.write("i am server"); // 輸出 if (client.available()) { char c = client.read(); // 接收來自client的資料 Serial.print(c);

71 測試連線

72 使用ASP.NET實作

73 傳送 private void SocketSend() {
const string ipAddressString = " ";// replace with correct IP address IPAddress ipAddress = IPAddress.Parse(ipAddressString); const int portNum = 1001;// replace with correct port IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, portNum); Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.Connect(remoteEndPoint); string sendString = TextBox1.Text; byte[] bytes = Encoding.UTF8.GetBytes(sendString.ToString()); client.Send(bytes); client.Shutdown(SocketShutdown.Both); client.Close(); }

74 接收 可以加入Timer定時接收資料 private void ReceiveData() {
const string ipAddressString = " ";// replace with correct IP address IPAddress ipAddress = IPAddress.Parse(ipAddressString); const int portNum = 1001;// replace with correct port IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, portNum); Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.Connect(remoteEndPoint); byte[] receiveBuffer = new byte[3000]; client.Receive(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None); string bufferString = Encoding.GetEncoding("utf-8").GetString(receiveBuffer); LabelData.Text = "GET:" + bufferString; client.Shutdown(SocketShutdown.Both); client.Close(); } 可以加入Timer定時接收資料

75 練習 PIN9接上一顆LED 透過ASP.NET+Socket來控制Arduino的LED明滅 傳送的命令為1時,點亮LED
同時將Arduino狀態回傳ASP.NET


Download ppt "Arduino使用入門 建國科技大學 資管系 饒瑞佶."

Similar presentations


Ads by Google