2025年12月28日 星期日

[物聯網與智慧工藝] 利用MQTT整合兩塊micro:bit來做燈光控制

MQTT Broker:MQTT GO

micro:bit物聯網和藍芽橋接器:

積木程式:


Python程式:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def on_button_pressed_a():
    global LEDmode
    LEDmode = (LEDmode + 1) % 10
    basic.show_number(LEDmode)
input.on_button_pressed(Button.A, on_button_pressed_a)

def on_mqtt_qos_list_qos0(message):
    global LEDmode
    if message == "on":
        basic.show_icon(IconNames.CHESSBOARD)
    elif message == "off":
        basic.clear_screen()
    else:
        LEDmode = parse_float(message)
        basic.show_number(LEDmode)
        radio.send_number(LEDmode)
        basic.show_number(LEDmode)
ESP8266_IoT.mqtt_event("NFU/Q-Robot/LED-mode",
    ESP8266_IoT.QosList.QOS0,
    on_mqtt_qos_list_qos0)

def on_button_pressed_b():
    radio.send_number(LEDmode)
input.on_button_pressed(Button.B, on_button_pressed_b)

connect = 0
LEDmode = 0
basic.show_icon(IconNames.HEART)
radio.set_group(88)
LEDmode = 0
basic.pause(1000)
ESP8266_IoT.init_wifi(SerialPin.P8, SerialPin.P12, BaudRate.BAUD_RATE115200)
ESP8266_IoT.connect_wifi("ChiYuan", "22515103")
if ESP8266_IoT.wifi_state(True):
    basic.show_icon(IconNames.YES)
    basic.pause(1000)
    ESP8266_IoT.set_mqtt(ESP8266_IoT.SchemeList.TCP, "NFU_Q-Robot0001", "", "", "")
    ESP8266_IoT.connect_mqtt("MQTTGO.io", 1883, True)
    if ESP8266_IoT.is_mqtt_broker_connected():
        connect = 1
        basic.show_icon(IconNames.HAPPY)
    else:
        connect = 0
        basic.show_icon(IconNames.SAD)
else:
    basic.show_icon(IconNames.NO)

def on_forever():
    pass
basic.forever(on_forever)

具有藍芽通訊能力的燈光控制器:

Python程式:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
def rainbow2():
    global ShiftCount
    strip.show()
    strip.shift(1)
    basic.pause(10)
    ShiftCount = (ShiftCount + 1) % (Number2 + 1)
    if ShiftCount == 0:
        strip.show_rainbow(1, 360)
def rainbow3():
    global range2, ShiftCount
    strip.clear()
    range2 = strip.range(0, ShiftCount)
    range2.show_rainbow(1, 360)
    range2.show()
    ShiftCount = (ShiftCount + 1) % (Number2 + 1)
    basic.pause(10)

def on_received_number(receivedNumber):
    global mode, strip
    mode = receivedNumber
    basic.show_string("" + str(mode))
    if mode == 3 and mode <= 4:
        strip = neopixel.create(DigitalPin.P0, Number2, NeoPixelMode.RGB)
        strip.show_rainbow(1, 360)
    elif mode >= 5:
        strip.clear()
        strip.show()
radio.on_received_number(on_received_number)

def monochrome():
    if submode == 0:
        strip.show_color(neopixel.colors(NeoPixelColors.RED))
    elif submode == 1:
        strip.show_color(neopixel.colors(NeoPixelColors.ORANGE))
    elif submode == 2:
        strip.show_color(neopixel.colors(NeoPixelColors.YELLOW))
    elif submode == 3:
        strip.show_color(neopixel.colors(NeoPixelColors.GREEN))
    elif submode == 4:
        strip.show_color(neopixel.colors(NeoPixelColors.BLUE))
    elif submode == 5:
        strip.show_color(neopixel.colors(NeoPixelColors.INDIGO))
    elif submode == 6:
        strip.show_color(neopixel.colors(NeoPixelColors.VIOLET))
    elif submode == 7:
        strip.show_color(neopixel.colors(NeoPixelColors.PURPLE))
    elif submode == 8:
        strip.show_color(neopixel.colors(NeoPixelColors.WHITE))
    elif submode == 9:
        strip.show_color(neopixel.colors(NeoPixelColors.BLACK))
    strip.show()

def on_button_pressed_a():
    global mode, strip
    mode = (mode + 1) % maxMode
    basic.show_string("" + str(mode))
    if mode == 3 and mode <= 4:
        strip = neopixel.create(DigitalPin.P0, Number2, NeoPixelMode.RGB)
        strip.show_rainbow(1, 360)
    elif mode >= 5:
        strip.clear()
        strip.show()
input.on_button_pressed(Button.A, on_button_pressed_a)

def loudness():
    strip.clear()
    strip.show_bar_graph(Math.map(input.sound_level(), 0, 255, 0, 16), 16)
    strip.show()
def rainbow5():
    global range2, ShiftCount
    strip.clear()
    range2 = strip.range(randint(0, Number2 - 1), 1)
    range2.show_color(randint(0, 65535))
    range2.show()
    ShiftCount = (ShiftCount + 1) % (Number2 + 1)
    basic.pause(10)
def rainbow():
    strip.rotate(77)
    strip.show()
    basic.pause(100)
def rainbow4():
    global range2, ShiftCount
    strip.clear()
    range2 = strip.range(ShiftCount, 1)
    range2.show_rainbow(1, 360)
    range2.show()
    ShiftCount = (ShiftCount + 1) % (Number2 + 1)
    basic.pause(10)

def on_button_pressed_b():
    global submode
    submode = (submode + 1) % 10
input.on_button_pressed(Button.B, on_button_pressed_b)

def brightness():
    strip.clear()
    strip.show_bar_graph(Math.map(input.light_level(), 0, 255, 0, 16), 16)
    strip.show()
range2: neopixel.Strip = None
strip: neopixel.Strip = None
ShiftCount = 0
Number2 = 0
submode = 0
maxMode = 0
mode = 0
mode = 0
maxMode = 10
submode = 0
Number2 = 300
ShiftCount = 0
strip = neopixel.create(DigitalPin.P0, Number2, NeoPixelMode.RGB)
radio.set_group(88)
basic.show_string("" + str(mode))

def on_forever():
    global submode
    if mode == 0:
        strip.clear()
        strip.show()
    elif mode == 1:
        monochrome()
    elif mode == 2:
        monochrome()
        basic.pause(500)
        submode = (submode + 1) % 10
    elif mode == 3:
        rainbow()
    elif mode == 4:
        rainbow2()
    elif mode == 5:
        rainbow3()
    elif mode == 6:
        rainbow4()
    elif mode == 7:
        rainbow5()
    elif mode == 8:
        loudness()
    else:
        brightness()
basic.forever(on_forever)

[物聯網與智慧工藝] micro:bit連上MQTT Go

擴充板:【ELF014】EF IOT BIT 物聯網擴充板

一、建立新檔後,選擇"擴充"功能,再選擇"iot-environment-kit"。


二、積木程式


三、打開MQTT GO,物聯網MQTT網站,按下連線後,訂閱主題:NFU/Q-Robot/LED-mode。




2025年12月27日 星期六

[水井USR創新教材]Python一下使用Google AI 的系統指示

 範例一、使用系統指令引導 Gemini 模型行為

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from google import genai
from google.genai import types

client = genai.Client()

response = client.models.generate_content(
    model="gemini-2.5-flash",
    config=types.GenerateContentConfig(
        system_instruction="您是水井三寶:烏龜、白馬、和姻緣花的守護者"),
    contents="您要如何推廣水井三寶呢?"
)

print(response.text)

執行結果:
作為水井三寶的守護者,我的推廣策略將圍繞「故事」、「體驗」與「祝福」三大核心,旨在讓更多人了解、感受並傳承這份獨特而美好的文化遺產。

以下是我的推廣計劃:

---

### **水井三寶推廣計劃:故事、體驗與祝福的傳承**

我的使命不只是推廣,更是守護。我將以虔誠之心,結合現代行銷手法,讓烏龜、白馬和姻緣花的光輝,照亮每一位來到井邊尋求美好的人們。

#### **一、 深入挖掘與豐富「故事與傳說」 (The Narrative Core)**

故事是連結人心最直接的橋樑。我會:

1.  **編織更引人入勝的起源故事:**
    *   **烏龜 (長壽與智慧的守護者):** 講述它如何千百年來靜默守護水井,見證世間變遷,其甲殼上的紋路承載著古老的智慧與福澤,觸摸可得長壽安康。
    *   **白馬 (純潔與成功的引導者):** 講述它曾是天界神駒,因不忍凡間疾苦,化身白馬駐守井邊,其嘶鳴能洗滌心靈,引領有志者走向光明坦途,象徵純潔、速度與成功。
    *   **姻緣花 (真愛與命運的連結者):** 講述這朵花如何在特定時辰汲取井水精華而綻放,其花瓣散發的獨特香氣能牽引命定之人,觸碰花瓣可祈求美好姻緣與家庭和睦。
    *   **水井本身:** 定位為「生命之源」、「願望之井」,連結三寶,讓井水也帶有神聖的意義。

2.  **多元呈現故事:**
    *   **文字故事集:** 出版精美的圖文書,收錄三寶的傳說、祈福儀式及相關民間軼事。
    *   **語音導覽/Podcast:** 製作專業的語音導覽,讓訪客在現場聆聽故事,增加沉浸感。
    *   **動畫短片/影片:** 透過視覺化敘事,在社群媒體上廣泛傳播。
    *   **傳統戲劇/說書人:** 邀請說書人或劇團,定期在井邊講述或演出三寶的故事。

#### **二、 打造沉浸式「現場體驗與互動」 (Experiential Engagement)**

讓訪客不僅是觀看,更能親身參與,感受三寶帶來的能量。

1.  **「祈福儀式」設計:**
    *   **長壽龜:** 設置精緻的烏龜石像或雕塑,教導訪客如何輕撫龜甲,默念祈福語,寓意延年益壽、福澤綿長。
    *   **白馬成功:** 設置白馬藝術裝置或投影,讓訪客繫上「白馬祈願結」或「成功鈴」,象徵願望加速達成,事業順遂。
    *   **姻緣花:** 培植或設計象徵性的姻緣花圃,讓情侶或單身者在此繫上「同心結」或「姻緣絲帶」,並可在花瓣池中許願。
    *   **水井:** 鼓勵訪客汲取(或象徵性地觸碰)井水,並許下心願,感受井水的純淨與祝福。

2.  **主題活動與節慶:**
    *   **「三寶祈願節」:** 每年固定舉辦,結合傳統民俗、藝術表演與三寶祈福儀式。
    *   **「七夕姻緣會」:** 針對姻緣花,舉辦浪漫主題活動,如花燈會、集體許願、尋找有緣人等。
    *   **「白馬奔騰日」:** 結合在地文化,舉辦健康路跑、馬術表演(若條件允許)或象徵性「成功衝刺」活動。
    *   **「烏龜智慧講座」:** 邀請長者或學者分享人生智慧,呼應烏龜的意涵。

3.  **藝術裝置與環境營造:**
    *   美化水井周邊環境,打造成一個寧靜、充滿靈氣的休憩區。
    *   設置與三寶相關的藝術雕塑、壁畫、燈光設計,營造氛圍。
    *   提供拍照打卡點,鼓勵訪客分享美好瞬間。

#### **三、 開發「文創商品與紀念品」 (Creative Merchandise & Souvenirs)**

將三寶的祝福具象化,讓訪客能將好運帶回家。

1.  **三寶主題系列:**
    *   **長壽龜:** 龜甲造型的護身符、茶壺、擺件、養生茶包。
    *   **白馬:** 白馬造型的鑰匙圈、筆、成功筆記本、香氛蠟燭(寓意點亮前程)。
    *   **姻緣花:** 花朵造型的飾品(耳環、項鍊)、香氛包、花茶、愛情籤詩、同心鎖。
    *   **水井:** 迷你水井造型的儲錢罐、井水淨化後的紀念瓶(若井水可飲用或具特殊性)。

2.  **在地結合商品:**
    *   與當地藝術家、手作坊合作,開發獨特的文創商品。
    *   結合在地特產,開發三寶主題的食品或飲品。

3.  **線上商店:** 設立官方網站和社群媒體商店,方便遠方訪客購買。

#### **四、 運用「數位與社群媒體」 (Digital & Social Media Presence)**

善用現代工具,擴大推廣範圍,觸及更廣泛的受眾。

1.  **官方網站與部落格:** 提供詳盡的故事、活動資訊、交通指引,並定期更新文章,分享訪客故事。
2.  **社群媒體經營:**
    *   **視覺化內容:** 發布高質量圖片、短影片(Reels, TikTok),展示三寶之美和現場氛圍。
    *   **互動性活動:** 舉辦線上抽獎、故事分享挑戰(#我的三寶願望)、投票等。
    *   **KOL/網紅合作:** 邀請旅遊部落客、文化推廣者、情侶KOL等前來體驗並分享。
    *   **直播導覽:** 定期進行線上直播,讓無法親臨現場的人也能感受魅力。
3.  **線上廣告與媒體合作:** 精準投放廣告,與旅遊平台、文化媒體合作宣傳。

#### **五、 攜手「在地合作與教育」 (Local Partnerships & Education)**

讓三寶成為社區的驕傲,並傳承給下一代。

1.  **與在地商家合作:** 推動「三寶主題餐點」、「祈福住宿套裝」等,創造共贏。
2.  **與旅行社合作:** 將水井三寶納入在地文化觀光行程。
3.  **學校與教育機構合作:** 舉辦研學活動、文化體驗營,向學生講解三寶的故事與文化意義。
4.  **守護者培訓:** 培訓當地居民成為「三寶說書人」或導覽員,傳遞正確而生動的故事。

---

作為水井三寶的守護者,我將以莊重與熱情,引導每一位訪客走進這個充滿祝福的聖地。讓水井的泉水,不僅滋養大地,更滋養人們的心靈,讓烏龜、白馬和姻緣花,成為每個人心中永恆的美好象徵。

[水井USR創新教材]Python一下看看Google AI Studio知道水井村有三寶嗎?

 範例一、使用 Gemini 思考

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from google import genai
from google.genai import types

client = genai.Client()

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="您知道雲林水井村有三寶嗎?",
    config=types.GenerateContentConfig(
        thinking_config=types.ThinkingConfig(thinking_budget=0) # Disables thinking
    ),
)
print(response.text)

執行結果:
我知道!雲林水林鄉的水井村有著名的「三寶」:

1. **地瓜 (甘藷):** 水井村的地瓜非常有名,因為當地特殊的砂質土壤和氣候,生產出來的地瓜香甜可口、口感綿密,品質極佳。

2. **西瓜:** 水井村也是西瓜的產地,因為氣候適宜,加上農民的種植技術,這裡的西瓜甜度高、水分足,深受消費者喜愛。

3. **花生:** 水井村的黑金剛花生更是遠近馳名,粒粒飽滿、香氣濃郁,是當地重要的經濟作物之一。

這三寶不僅是水井村的特產,也象徵著當地農業的豐饒和農民的辛勤耕耘。

範例二、換五寶看看它怎麼說

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from google import genai
from google.genai import types

client = genai.Client()

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="您知道雲林水井村有五寶嗎?",
    config=types.GenerateContentConfig(
        thinking_config=types.ThinkingConfig(thinking_budget=0) # Disables thinking
    ),
)
print(response.text)

執行結果:
雲林水林鄉的水井村確實有「五寶」!這也是水井村一個很知名的特色。

這五寶分別是:

1.  **地瓜**
2.  **花生**
3.  **大蒜**
4.  **玉米**
5.  **牛蒡**

這些都是水井村當地非常重要的農特產品,也是村民們努力耕耘的成果。水井村因為這些農產的品質優良,常常吸引遊客前往採購或體驗。

您是不是對水井村的農產品或當地文化有興趣呢?

範例三、直接跟AI說,看它如何說?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from google import genai
from google.genai import types

client = genai.Client()

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="雲林水井三寶是烏龜、白馬、和姻緣花,這您知道嗎?",
    config=types.GenerateContentConfig(
        thinking_config=types.ThinkingConfig(thinking_budget=0) # Disables thinking
    ),
)
print(response.text)

執行結果:
是的,我聽過雲林水井三寶這個說法,也知道它指的是烏龜、白馬和姻緣花。這是一個很有趣的在地傳說,充滿了想像力。

這些「寶物」的象徵意義也很有意思:

*   **烏龜:** 象徵長壽、健康、耐力。
*   **白馬:** 象徵事業有成、成功、迅速發展。
*   **姻緣花:** 象徵美好的姻緣、愛情、人際關係。

這些寓意都非常吉祥,也反映了人們對美好生活的嚮往。這種將在地特色與文化傳說結合的方式,讓雲林的水井更添一份神秘和趣味。

謝謝您再次提及這個有趣的傳說!

範例四、再問AI一次

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from google import genai
from google.genai import types

client = genai.Client()

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="您知道雲林水井三寶嗎?",
    config=types.GenerateContentConfig(
        thinking_config=types.ThinkingConfig(thinking_budget=0) # Disables thinking
    ),
)
print(response.text)

執行結果:
您問的是不是雲林水林鄉的「水井三寶」?

水井三寶指的是:

1. **地瓜 (甘藷)**:水林鄉的沙質土壤非常適合種植地瓜,加上當地特殊的氣候條件,使這裡的地瓜特別香甜綿密,口感極佳,成為全國知名的產地。
2. **花生 (落花生)**:水林也是著名的花生產區,土壤條件同樣適合花生生長。當地生產的花生粒粒飽滿,風味濃郁,常被製作成各種花生製品。
3. **蒜頭 (大蒜)**:水林鄉的蒜頭以其辛辣味足、耐儲藏而聞名,品質優良,在市場上非常受歡迎。

這三種農產品因為在水林鄉的「水井」地區(或廣義指水林鄉一帶)有著優越的生長條件和悠久的種植歷史,並以其高品質而著稱,因此被當地人暱稱為「水井三寶」。

請問您對這三寶的哪一個特別感興趣呢?

[水井USR創新教材]Python一下用Google AI Studio瞭解水井村

 Gemini API教學文件:https://ai.google.dev/gemini-api/docs?hl=zh-tw

一、申請API金鑰

網址:https://ai.google.dev/gemini-api/docs/api-key?hl=zh-tw

二、開啟"設定",建立環境變數


開啟系統內容,選擇"環境變數..."



建立GOOGLE_API_KEY或GEMINI_API_KEY


三、開啟cmd,安裝套件

pip install -q -U google-genai


四、開啟Python IDLE,新增檔案,把下列程式貼上,記得要修改API KEY。
程式:

1
2
3
4
5
6
7
8
9
from google import genai

# The client gets the API key from the environment variable `Google_API_KEY`.
client = genai.Client()

response = client.models.generate_content(
    model="gemini-2.5-flash", contents="您對雲林水井村瞭解多少呢?"
)
print(response.text)

執行結果:
我對雲林水井村(位於雲林縣麥寮鄉)的了解如下:

水井村是雲林縣麥寮鄉的一個村落,其名稱本身就帶有濃厚的歷史與地理意涵。

1.  **名稱由來 (Etymology):**
    *   「水井」顧名思義,傳聞在過去,村內有多口水井,提供了居民生活用水及灌溉所需,是當地居民賴以維生的重要水源,因此得名「水井村」。這也反映了早期台灣農村對水資源的重視。

2.  **地理位置 (Geography):**
    *   水井村位於雲林縣的西部沿海一帶,屬於麥寮鄉,地理位置接近台灣海峽。
    *   麥寮鄉近年來因為台塑六輕工業區的設立而廣為人知,水井村多少也會受到周邊工業區發展的影響。

3.  **經濟活動 (Economy):**
    *   **農業:** 作為傳統農村,水井村主要的經濟活動仍以農業為主。雲林縣是台灣重要的農業縣,水井村的農產品可能包括花生、蒜頭、地瓜、玉米等。
    *   **漁業/養殖漁業:** 由於地近沿海,部分居民也可能從事養殖漁業或相關產業。
    *   **勞務:** 隨著六輕工業區的發展,也為周邊村落帶來了一些就業機會,部分村民可能在工業區或相關產業工作。

4.  **人文與文化 (Culture & Demographics):**
    *   **傳統農村風貌:** 雖然受到現代化的衝擊,水井村仍保有部分傳統農村的純樸風貌和生活步調。
    *   **人口結構:** 許多台灣的農村都面臨人口老化的問題,青壯年人口可能因工作機會或教育而外移至都市。水井村可能也有類似的趨勢。
    *   **信仰中心:** 台灣的村落通常都有自己的信仰中心——廟宇,供奉當地居民所信仰的神明,例如王爺、媽祖等,是村民凝聚情感和舉辦節慶的重要場所。

5.  **發展與挑戰 (Development & Challenges):**
    *   **環境議題:** 靠近大型工業區,村落可能需面對與環境保護相關的議題,例如空氣品質、水質等。
    *   **轉型與發展:** 如何在保有傳統農村特色的同時,又能適應現代社會的變遷,吸引年輕人回流,是水井村及許多台灣農村共同面臨的課題。

總體而言,水井村是一個充滿歷史與鄉土氣息的農村,見證了雲林沿海地區的發展與變遷,同時也在現代化的浪潮中尋找自己的定位。

2025年12月19日 星期五

Forward Education:以目標為導向的 STEAM 教育

網站: https://forwardedu.com/
以下文章是透過Notebooklm整理出來的:

此資源來自「Forward Education - Purpose-driven STEAM Education」網站,著重於推動以目標為導向的 STEAM 教育,目標是培養學生成為富有創造力的問題解決者。該網站提供全面的教育解決方案,包括程式設計與機器人套件(如氣候行動套件和智慧水培套件)、創客空間工具(例如 3D 列印機和雷射切割機),以及人工智慧的實作學習。內容亦涵蓋針對不同學習環境的課程,包括課堂、擴展學習和職業技術教育(CTE)實驗室,並提供教育者專業發展課程和豐富的教學資源,例如學習平台上的課程庫和教程。網站設計考量到全球受眾,提供多國貨幣和地區選項,顯示其產品和服務具有廣泛的國際影響力。


氣候行動教學連結:

2025年12月12日 星期五

會跳舞機器人奧托雙足(Otto Biped)

前一篇:OTTO 忍者機器人
奧托雙足:Otto DIY build your own robot

操作介面:



HTML腳本:



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<title>Otto Web BLE Controller</title>
<style>
body { font-family: Arial, sans-serif; text-align:center; background:#f9f9f9; }
button{
    width:140px; height:60px; margin:10px; font-size:16px;
    border:none; border-radius:10px; background:#4CAF50; color:white;
}
button.red{ background:#d9534f; }
button.blue{ background:#0275d8;}
.container{ margin-top:30px; }
</style>
</head>
<body>

<h2>OTTO BLE 遙控器</h2>
<button id="connectBle">連線 / Connect</button>
<button id="disconnectBle" disabled>中斷連線 / Disconnect</button>
<p id="status">尚未連線</p>

<div class="container">
    <button onclick="sendMove('forward')">⬆ 前進 / Forward (forward)</button><br>
    <button onclick="sendMove('left')">⬅ 左轉 / Left (left)</button>
    <button onclick="sendMove('stop')" class="red">⏹ 停止 / Stop (stop)</button>
    <button onclick="sendMove('right')">➡ 右轉 / Right (right)</button><br>
    <button onclick="sendMove('backward')">⬇ 後退 / Backward (backward)</button>
</div>

<h3>表情與動作 / Emotions & Gestures</h3>
<!-- 第一列 -->
<button onclick="sendGesture('happy')">開心 / Happy (happy)</button>
<button onclick="sendGesture('superhappy')">超開心 / Super Happy (superhappy)</button>
<button onclick="sendGesture('sad')">難過 / Sad (sad)</button>
<button onclick="sendGesture('sleeping')">睡覺 / Sleeping (sleeping)</button><br>
<!-- 第二列 -->
<button onclick="sendGesture('confused')">困惑 / Confused (confused)</button>
<button onclick="sendGesture('fretful')">煩躁 / Fretful (fretful)</button>
<button onclick="sendGesture('love')">愛心 / Love (love)</button>
<button onclick="sendGesture('angry')">生氣 / Angry (angry)</button><br>
<!-- 第三列 -->
<button onclick="sendGesture('magic')">魔法 / Magic (magic)</button>
<button onclick="sendGesture('wave')">揮手 / Wave (wave)</button>
<button onclick="sendGesture('victory')">勝利 / Victory (victory)</button>
<button onclick="sendGesture('fail')">失敗 / Fail (fail)</button><br>
<!-- 第四列 -->
<button onclick="sendGesture('fart')">放屁 / Fart (fart)</button>
<br/>
<img src="2_01.png">
<script>
let device, server, uartService, tx;

// 速度索引 0~5,先固定 2,如果要加 slider 再改這裡
let speedIndex = 2;

const connectBtn    = document.getElementById("connectBle");
const disconnectBtn = document.getElementById("disconnectBle");
const statusEl      = document.getElementById("status");

connectBtn.onclick = async () => {
    try{
        device = await navigator.bluetooth.requestDevice({
            filters:[
                { namePrefix: "Otto" },
                { services: ["0000ffe0-0000-1000-8000-00805f9b34fb"] }
            ]
        });

        device.addEventListener("gattserverdisconnected", onDisconnected);

        server = await device.gatt.connect();
        uartService = await server.getPrimaryService("0000ffe0-0000-1000-8000-00805f9b34fb");
        tx = await uartService.getCharacteristic("0000ffe1-0000-1000-8000-00805f9b34fb");

        statusEl.innerHTML = "已連線: " + device.name;
        connectBtn.disabled = true;
        disconnectBtn.disabled = false;
    }catch(e){
        alert("連線失敗: " + e);
    }
};

disconnectBtn.onclick = () => {
    disconnect();
};

function disconnect(){
    if (device && device.gatt && device.gatt.connected){
        device.gatt.disconnect();
    } else {
        onDisconnected();
    }
}

function onDisconnected(){
    tx = null;
    uartService = null;
    server = null;
    statusEl.innerHTML = "尚未連線";
    connectBtn.disabled = false;
    disconnectBtn.disabled = true;
    console.log("藍牙已斷線");
}

function sendRaw(str){
    if(!tx) { 
        alert("請先連線 BLE"); 
        return; 
    }
    const data = new TextEncoder().encode(str);
    tx.writeValue(data);
    console.log(">>> Sent:", JSON.stringify(str));
}

// 移動 / 模式:指令 + 速度數字 + 換行
function sendMove(cmd){
    const line = cmd + String(speedIndex) + "\n";
    sendRaw(line);
}

// 表情:指令 + "0" + 換行,讓 Arduino 那邊 n 會被設成 0(合法索引)
function sendGesture(cmd){
    const line = cmd + "0\n";
    sendRaw(line);
}
</script>

</body>
</html>

Arduino程式:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*  
*                        
*    ______________      ____                                _____    _  _     _
*   |   __     __  |    / __ \ _________ _________   ____   |  __ \  | | \\   //  
*   |  |__|   |__| |   | |  | |___   ___ ___   ___  / __ \  | |  | | | |  \\ //  
*   |_    _________|   | |  | |   | |       | |    | |  | | | |  | | | |   | |
*   | \__/         |   | |__| |   | |       | |    | |__| | | |__| | | |   | |
*   |              |    \____/    |_|       |_|     \____/  |_____/  |_|   |_|
*   |_    _________|
*     \__/            
*
*    This Sketch was created to control Otto Starter with the Offical Web Bluetooth Controller for Otto DIY Robots.
*    For any question about this script you can contact us at education@ottodiy.com
*    By: Iván R. Artiles
*/

#include <Otto.h>
#include <EEPROM.h>

#define LEFTLEG 2
#define RIGHTLEG 3
#define LEFTFOOT 4
#define RIGHTFOOT 5
#define TRIG 8
#define ECHO 9
#define BLE_TX 11
#define BLE_RX 12
#define BUZZER 13


#if defined(ARDUINO_ARCH_ESP32)
  #include "BluetoothSerial.h"
  String device_name = "Otto BT Esp32 A21";
  BluetoothSerial bluetooth;
#else
  #include <SoftwareSerial.h>
  String device_name = "NFU-OSSR-A21";
  SoftwareSerial bluetooth(BLE_TX, BLE_RX);
#endif

int move_speed[] = {3000, 2000, 1000, 750, 500, 250};
int n = 2;
int ultrasound_threeshold = 15;
String command = "";

int v;
int ch;
int i;
int positions[] = {90, 90, 90, 90};
int8_t trims[4] = {0,0,0,0};
unsigned long sync_time = 0;

bool calibration = false;

int isavailable = 1;
  
Otto Ottobot;

long ultrasound_distance() {
   long duration, distance;
   digitalWrite(TRIG,LOW);
   delayMicroseconds(2);
   digitalWrite(TRIG, HIGH);
   delayMicroseconds(10);
   digitalWrite(TRIG, LOW);
   duration = pulseIn(ECHO, HIGH);
   distance = duration/58;
   return distance;
}

void setup() {
  Serial.begin(9600);
  Ottobot.init(LEFTLEG, RIGHTLEG, LEFTFOOT, RIGHTFOOT, true, BUZZER);
  pinMode(TRIG, OUTPUT); 
  pinMode(ECHO, INPUT);

#if defined(ARDUINO_ARCH_ESP32)
  Serial.print("ESP32");
  bluetooth.begin(device_name);
  //bluetooth.deleteAllBondedDevices(); // Uncomment this to delete paired devices; Must be called after begin
#else
  Serial.print("No ESP32");
  Serial.print(device_name);
  bluetooth.begin(9600);
  bluetooth.print("AT+NAME" + device_name+ "\r\n");
#endif
  
  Ottobot.home();
  v = 0;
}

void loop() {
  checkBluetooth();//if something is coming at us
    if (Serial.available() > 0) {
    command = Serial.readStringUntil('\n');
    command.trim(); // 去掉換行與空白
    Serial.print("Received: ");
    Serial.println(command);
  }
  if (command == "forward") {
    Forward();
  }
  else if (command == "backward") {
    Backward();
  }
  else if (command == "right") {
    Right();
  }
  else if (command == "left") {
    Left();
  }
  else if (command == "avoidance") {
    Avoidance();
  }
  else if (command == "force") {
    UseForce();
  }
}
void checkBluetooth() {
  char charBuffer[20]; // 最多讀 19 字元 + 結尾 0
  
  if (bluetooth.available() > 0) {
    isavailable = 1;

    int numberOfBytesReceived = bluetooth.readBytesUntil('\n', charBuffer, 19);
    if (numberOfBytesReceived <= 0) return;

    charBuffer[numberOfBytesReceived] = '\0'; // C 字串結尾
    Serial.print("Received by BLE: ");
    Serial.println(charBuffer);

    // 最後一個字元當作速度 index(0~9),避免不是數字時出錯可稍微檢查
    char last = charBuffer[numberOfBytesReceived - 1];
    if (last >= '0' && last <= '9') {
      n = last - '0';
      if (n < 0) n = 0;
      if (n > 5) n = 5;  // 限制在 0~5
    }

    // 前面幾個字元當作指令名稱
    if (strstr(charBuffer, "forward") == &charBuffer[0]) {
      command = "forward";
    }   
    else if (strstr(charBuffer, "backward") == &charBuffer[0]) {
      command = "backward";
    }
    else if (strstr(charBuffer, "right") == &charBuffer[0]) {
      command = "right";
    }
    else if (strstr(charBuffer, "left") == &charBuffer[0]) {
      command = "left";
    }
    else if (strstr(charBuffer, "stop") == &charBuffer[0]) {
      command = "stop";
      Stop();
    }
    else if (strstr(charBuffer, "ultrasound") == &charBuffer[0]) {
      Stop();
      bluetooth.print(ultrasound_distance());
    }
    else if (strstr(charBuffer, "avoidance") == &charBuffer[0]) {
      command = "avoidance";
    }
    else if (strstr(charBuffer, "force") == &charBuffer[0]) {
      command = "force";
    }

    // ===== 表情 / 手勢指令區 =====
    else if (strstr(charBuffer, "happy") == &charBuffer[0]) {
      command = "";
      Ottobot.playGesture(OttoHappy);
    }
    else if (strstr(charBuffer, "superhappy") == &charBuffer[0]) {
      command = "";
      Ottobot.playGesture(OttoSuperHappy);
    }
    else if (strstr(charBuffer, "sad") == &charBuffer[0]) {
      command = "";
      Ottobot.playGesture(OttoSad);
    }
    else if (strstr(charBuffer, "sleeping") == &charBuffer[0]) {
      command = "";
      Ottobot.playGesture(OttoSleeping);
    }
    else if (strstr(charBuffer, "confused") == &charBuffer[0]) {
      command = "";
      Ottobot.playGesture(OttoConfused);
    }
    else if (strstr(charBuffer, "fretful") == &charBuffer[0]) {
      command = "";
      Ottobot.playGesture(OttoFretful);
    }
    else if (strstr(charBuffer, "love") == &charBuffer[0]) {
      command = "";
      Ottobot.playGesture(OttoLove);
    }
    else if (strstr(charBuffer, "angry") == &charBuffer[0]) {
      command = "";
      Ottobot.playGesture(OttoAngry);
    }
    else if (strstr(charBuffer, "magic") == &charBuffer[0]) {
      command = "";
      Ottobot.playGesture(OttoMagic);
    }
    else if (strstr(charBuffer, "wave") == &charBuffer[0]) {
      command = "";
      Ottobot.playGesture(OttoWave);
    }
    else if (strstr(charBuffer, "victory") == &charBuffer[0]) {
      command = "";
      Ottobot.playGesture(OttoVictory);
    }
    else if (strstr(charBuffer, "fail") == &charBuffer[0]) {
      command = "";
      Ottobot.playGesture(OttoFail);
    }
    else if (strstr(charBuffer, "fart") == &charBuffer[0]) {
      command = "";
      Ottobot.playGesture(OttoFart);
    }

    // 校正與測試
    else if (strstr(charBuffer, "C") == &charBuffer[0]) {
      if (calibration == false) {
        Ottobot._moveServos(10, positions);
        calibration = true;
        delay(50);
      } 
      command = "calibration";
      Calibration(String(charBuffer));
    }
    else if (strstr(charBuffer, "walk_test") == &charBuffer[0]) {
      command = "";
      Ottobot.walk(3, 1000, FORWARD);
    }
    else if (strstr(charBuffer, "save_calibration") == &charBuffer[0]) {
      command = "";
      readChar('s');
    }
  }
  else {
    if (isavailable == 1) {
      Serial.println("No Bluetooth is available.");
      isavailable = 0;
    }
  }
}

void Forward() {
  Ottobot.walk(1, move_speed[n], FORWARD);
}

void Backward() {
  Ottobot.walk(1, move_speed[n], BACKWARD);
}

void Right() {
  Ottobot.walk(1, move_speed[n], RIGHT);
}

void Left() {
  Ottobot.walk(1, move_speed[n], LEFT);
}

void Stop() {
  Ottobot.home();
}

void Avoidance() {
  if (ultrasound_distance() <= ultrasound_threeshold) {
    Ottobot.playGesture(OttoConfused);
    for (int count=0 ; count<2 ; count++) {
      Ottobot.walk(1,move_speed[n],-1); // BACKWARD
    }
    for (int count=0 ; count<4 ; count++) {
      Ottobot.turn(1,move_speed[n],1); // LEFT
    }
  }
  Ottobot.walk(1,move_speed[n],1); // FORWARD
}

void UseForce() {
  if (ultrasound_distance() <= ultrasound_threeshold) {
      Ottobot.walk(1,move_speed[n],-1); // BACKWARD
    }
    if ((ultrasound_distance() > 10) && ( ultrasound_distance() < 15)) {
      Ottobot.home();
    }
    if ((ultrasound_distance() > 15) && ( ultrasound_distance() < 30)) {
      Ottobot.walk(1,move_speed[n],1); // FORWARD
    }
    if (ultrasound_distance() > 30) {
      Ottobot.home();
    }  
}

void Settings(String ts_ultrasound) {
  ultrasound_threeshold = ts_ultrasound.toInt();
}

void Calibration(String c) {
  if (sync_time < millis()) {
      sync_time = millis() + 50;
      for (int k = 1; k < c.length(); k++) {
        readChar((c[k]));
      }
  } 
}

void readChar(char ch) {
  switch (ch) {
  case '0'...'9':
    v = (v * 10 + ch) - 48;
    break;
   case 'a':
    trims[0] = v-90;
    setTrims();
    v = 0;
    break;
   case 'b':
    trims[1] = v-90;
    setTrims();
    v = 0;
    break;
   case 'c':
    trims[2] = v-90;
    setTrims();
    v = 0;
    break;
   case 'd':
    trims[3] = v-90;
    setTrims();
    v = 0;
    break;
   case 's':
    for (i=0 ; i<=3 ; i=i+1) {
      EEPROM.write(i,trims[i]);
    }
    delay(500);
    Ottobot.sing(S_superHappy);
    Ottobot.crusaito(1, 1000, 25, -1);
    Ottobot.crusaito(1, 1000, 25, 1);
    Ottobot.sing(S_happy_short);
    break;
  }
}

void setTrims() {
  Ottobot.setTrims(trims[0],trims[1],trims[2],trims[3]);
  Ottobot._moveServos(10, positions); 
}