1 of 33

Arduino課程講義

2023/5/23

2 of 33

大綱

  1. Arduino及溫溼度感測器介紹 (1小時)
  2. 安裝開發環境 (1小時)
  3. 整合 Arduino 與 PowerBI (1小時)

3 of 33

電腦環境

電腦預裝

  • Win 10 / 11

課程中安裝

  • USB to Serial
  • MySQL 資料庫
  • Arduino IDE
  • Power BI Desktop
  • Python

4 of 33

Arduino 介紹

  • Arduino 是一種很酷的電子裝置,它能幫助我們製作各種有趣的東西。它的外觀就像一塊小小的電路板,上面有很多接腳,可以插入電子元件。你可以將它視為一個電子的大腦,可以控制其他元件的運作。

  • Arduino 很容易上手,因為它有一個簡單的程式語言,可以讓我們編寫指令。這種程式語言叫做 Arduino 程式,或稱為 Arduino Sketch。你可以使用 Arduino IDE(Integrated Development Environment,整合開發環境)這個軟體來編寫和上傳程式碼到 Arduino 板子上。

5 of 33

Arduino 介紹

  • Arduino可以連接各種元件,例如感測器、LED 燈、馬達等等。你可以根據自己的創意和想法,編寫程式讓這些元件做出不同的動作和反應。比如,你可以使用感測器來偵測周圍的溫度或者光線強度,然後讓 Arduino 控制 LED 燈的亮度或者馬達的轉動速度。

  • Arduino 的強大之處在於它的應用廣泛。你可以用它製作一個智慧家居系統,讓家裡的燈光和電器根據你的需要自動開關;或者你可以製作一個機器人,讓它能根據指令走來走去。只要你有創意,Arduino 可以幫助你實現它!

6 of 33

線上資源

  • https://reurl.cc/8j498g

7 of 33

Arduino 線路圖

8 of 33

Arduino 設定

9 of 33

Arduino 設定

  • Tools > Board “esp8266” install

10 of 33

Arduino 設定

  • Tools > Board “LOLIN(WEMOS) D1 R2 & mini”

11 of 33

設定連接 Port

12 of 33

Arduino 程式

  • 編寫程式碼

  • 燒錄
  • 執行 Sketch > Upload

13 of 33

程式碼:include

�#include <dht11.h>

dht11 DHT;�

14 of 33

程式碼:setup

// setup() 會先被執行且只會執行一次

void setup() {

// 設定serial傳輸率

Serial.begin(9600);

// 將 A0 設定為輸入

pinMode(A0, INPUT);

}

15 of 33

程式碼:loop

// loop() 裡面的程式會不斷重複執行

void loop() {

// 讀取 D2

DHT.read(D2);

// 讀取 D2 後的值寫到 serial port

Serial.print("temperature:");

Serial.print(DHT.temperature);

Serial.print(" humidity:");

Serial.print(DHT.humidity);

// 讀取 A0 並將值轉化為 0~99

Serial.print(" light:");

Serial.println((map(analogRead(A0), 0, 1024, 0, 99)));

// 等待 2 秒再抓取資料

delay(2000);

}

16 of 33

Dht11

17 of 33

Dht11 Library

  • Sketch > Include Library > Add Zip Library�dht11-master.zip

18 of 33

上傳

  • 在Arduino IDE選擇Sketch -> Verify/Compile
  • 使用USB Cable連接Arduino板子跟PC
  • 在Arduino IDE選擇Sketch > Upload

19 of 33

資料庫

  • 資料庫介紹
  • 下載連結
  • 安裝步驟
  • 注意事項
  • 建立資料庫
  • 建立資料表
  • 資料查詢

20 of 33

安裝 MySQL

下載連結

21 of 33

設定 SQL Server Express

  • 建立資料庫:arduino
  • 建立資料表:dht

22 of 33

Python

  • 抓取 COM port
  • 存到資料庫

23 of 33

使用 pip 安裝

  • pip install pyserial
  • pip install pymysql # MySQL, MariaDB
  • pip install pyodbc # MS SQL Server

24 of 33

pip 安裝

  • pip install serial
  • pip install pyserial # serial tools
  • pip install pymysql # 資料庫

25 of 33

Python 程式:import

import datetime

import serial

import serial.tools.list_ports

import pymysql

Import pyodbc

26 of 33

列出可用的 COM Port

ports = serial.tools.list_ports.comports()

for port in ports:

print(f"{port.device}: {port.description}")

27 of 33

讀取資料

# 讀取資料, 按下 Ctrl-C 中斷

with serial.Serial('COM5', 9600) as ser:

while True:

line = ser.readline().decode('utf-8').strip()

print(line)

28 of 33

建立SQL

def line_sql(line):

if ('temperature' in line):

list = line.split(' ')

print(list)

# 抓數字,加時間

temp = list[0].split(':')[1]

hum = list[1].split(':')[1]

light = list[2].split(':')[1]

sql = f'INSERT INTO dht (temp, hum, light) VALUES ({temp} {hum}, {light})'

insert(sql)

29 of 33

寫到資料庫

conn_str = (

'Driver={SQL Server};'

'Server=localhost\SQLEXPRESS;'

'Database=arduino;'

'Trusted_Connection=yes;'

)

def insert(sql):

with pyodbc.connect(conn_str) as conn:

cursor = conn.cursor()

cursor.execute(sql)

conn.commit()

30 of 33

讀取資料

# 按下 Ctrl-C 中斷

with serial.Serial('COM5', 9600) as ser:

while True:

line = ser.readline().decode('utf-8').strip()

line_sql(line)

31 of 33

Power BI

32 of 33

Power BI

  • 安裝 Power BI Desktop
  • 建立專案
  • 資料來源使用 Microsoft SQL Server Express
  • DirectQuery
  • 更新報表

33 of 33

(補充) Python 產生圖表

import matplotlib.pyplot as plt

plt.plot(hum_values, label="Humidity")

plt.plot(temp_values, label="Temperature")

plt.plot(light_values, label="Light")

plt.xlabel("Time")

plt.ylabel("Values")

plt.title("PowerBI Data")

plt.legend()

plt.show()