顯示具有 機智雲開發板 標籤的文章。 顯示所有文章
顯示具有 機智雲開發板 標籤的文章。 顯示所有文章

2024年10月28日 星期一

請ChatGPT協助除錯ESP8266機智雲的MicroPython程式

一、先給提示詞"修訂程式與並給一個初步程式碼:









 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from machine import Pin
from machine import Timer

# Define led_status as a global variable
led_status = 0

# Initialize LED pin
led = Pin(2, Pin.OUT)

# Define the LED flash function
def flash_led(timer):
    global led_status
    led.value(led_status)
    led_status = 1 - led_status

# Set up a periodic timer to toggle the LED every 500ms
tim = Timer(-1)
tim.init(period=500, mode=Timer.PERIODIC, callback=flash_led)

# No need for a while loop if the timer handles the LED toggling
# Just keep the script alive
while True:
    pass










ChatGPT很快地幫忙找出程式有三項問題,包括:全域變數、計時器的初始化、以及多餘程式碼,並給予正確的程式碼。

二、提示語告訴要產生的程式和使用接腳


 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
from machine import Pin, Timer, PWM
import time

# 定義 LED 狀態
led_status = 0

# 初始化 LED 引腳
led = Pin(2, Pin.OUT)

# 定義 PWM 的引腳,使用 GPIO 4
buzzer = PWM(Pin(4))

# 音符對應的頻率(以簡單音階為例)
tones = {
    'C': 261,
    'D': 294,
    'E': 329,
    'F': 349,
    'G': 392,
    'A': 440,
    'B': 493
}

# 播放音符的函數
def play_tone(tone, duration):
    if tone in tones:
        buzzer.freq(tones[tone])  # 設定頻率
        buzzer.duty(512)  # 設定占空比,0-1023 之間,512 表示 50% 占空比
        time.sleep(duration)  # 播放音符的持續時間
        buzzer.duty(0)  # 停止聲音

# 定義 LED 閃爍的函數
def flash_led(timer):
    global led_status
    led.value(led_status)
    led_status = 1 - led_status

# 設置一個定時器,每500毫秒切換 LED 狀態
tim = Timer(-1)
tim.init(period=500, mode=Timer.PERIODIC, callback=flash_led)

# 播放一段簡單音樂的函數
def play_melody():
    melody = ['C', 'D', 'E', 'F', 'G', 'A', 'B', 'C']  # 簡單音階
    duration = 0.5  # 每個音符的持續時間為0.5秒
    for note in melody:
        play_tone(note, duration)
        time.sleep(0.1)  # 音符之間的短暫停頓

# 播放音樂
play_melody()

# 保持程式運行
while True:
    pass



三、請ChatGPT產生機智雲板子上的光敏電阻和LED控制程式

 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
from machine import Pin, ADC, PWM
import time

# 初始化光敏電阻 (A0)
light_sensor = ADC(0)

# 初始化 RGB LED 引腳
red = PWM(Pin(15), freq=500)
green = PWM(Pin(12), freq=500)
blue = PWM(Pin(13), freq=500)

# 設置初始 LED 亮度
red.duty(0)
green.duty(0)
blue.duty(0)

# 設定光線閾值,低於這個值時視為光線暗,進行顏色變化
LIGHT_THRESHOLD = 500  # 根據實際環境調整

# 顏色變化函數
def set_color(r, g, b):
    red.duty(r)
    green.duty(g)
    blue.duty(b)

# 變化 RGB 顏色
def color_cycle():
    set_color(1023, 0, 0)    # 紅色
    time.sleep(0.5)
    set_color(0, 1023, 0)    # 綠色
    time.sleep(0.5)
    set_color(0, 0, 1023)    # 藍色
    time.sleep(0.5)
    set_color(1023, 1023, 0) # 黃色
    time.sleep(0.5)
    set_color(0, 1023, 1023) # 青色
    time.sleep(0.5)
    set_color(1023, 0, 1023) # 紫色
    time.sleep(0.5)
    set_color(1023, 1023, 1023) # 白色
    time.sleep(0.5)

# 主迴圈
while True:
    light_value = light_sensor.read()  # 讀取光敏電阻的數值
    print("Light Value:", light_value)  # 可在控制台查看光線讀數

    if light_value < LIGHT_THRESHOLD:  # 如果光線低於閾值,啟動 RGB 變化
        color_cycle()
    else:
        # 關閉 RGB LED
        set_color(0, 0, 0)

    time.sleep(1)  # 每秒檢查一次光線狀態



最後把兩支程式請ChatGPT合併後的程式:

 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
from machine import Pin, Timer, PWM, ADC
import time

# 初始化 LED 引腳
led = Pin(2, Pin.OUT)
led_status = 0

# 初始化蜂鳴器的 PWM 引腳,使用 GPIO 4
beeper = PWM(Pin(4, Pin.OUT))

# 初始化光敏電阻 (A0) 和 RGB LED (GPIO 15, 12, 13)
light_sensor = ADC(0)
red = PWM(Pin(15), freq=500)
green = PWM(Pin(12), freq=500)
blue = PWM(Pin(13), freq=500)

# 設置初始 RGB LED 亮度
red.duty(0)
green.duty(0)
blue.duty(0)

# 光線強度閾值,低於此值視為光線暗
LIGHT_THRESHOLD = 500

# 定義 LED 閃爍的函數
def flash_led(timer):
    global led_status
    led.value(led_status)
    led_status = 1 - led_status

# 定義 PWM 控制 RGB 顏色
def set_color(r, g, b):
    red.duty(r)
    green.duty(g)
    blue.duty(b)

# 顏色變化函數
def color_cycle():
    set_color(1023, 0, 0)    # 紅色
    time.sleep(0.5)
    set_color(0, 1023, 0)    # 綠色
    time.sleep(0.5)
    set_color(0, 0, 1023)    # 藍色
    time.sleep(0.5)
    set_color(1023, 1023, 0) # 黃色
    time.sleep(0.5)
    set_color(0, 1023, 1023) # 青色
    time.sleep(0.5)
    set_color(1023, 0, 1023) # 紫色
    time.sleep(0.5)
    set_color(1023, 1023, 1023) # 白色
    time.sleep(0.5)

# 音樂音符和頻率
notes = [
    392, 330, 330, 0, 
    349, 294, 294, 0, 
    262, 294, 330, 349, 392, 392, 392, 0
]

# 音樂播放函數
def play_melody():
    for note in notes:
        if note == 0:
            beeper.duty(0)  # 休止符
        else:
            beeper.duty(512)  # 設置音量
            beeper.freq(note) # 設置頻率
        time.sleep(0.2)  # 音符持續 0.2 秒
        beeper.duty(0)
        time.sleep(0.1)  # 休止 0.1 秒

# 設置一個定時器,每 500 毫秒切換 LED 狀態
tim_led = Timer(-1)
tim_led.init(period=500, mode=Timer.PERIODIC, callback=flash_led)

# 主迴圈
while True:
    # 讀取光敏電阻的數值
    light_value = light_sensor.read()
    print("Light Value:", light_value)

    if light_value < LIGHT_THRESHOLD:  # 如果光線低於閾值,顯示 RGB 顏色變化
        color_cycle()
    else:
        set_color(0, 0, 0)  # 光線充足時,關閉 RGB LED

    play_melody()  # 播放音樂
    time.sleep(2)  # 每隔 2 秒播放一次音樂並檢查光線狀況


2024年6月18日 星期二

機智雲開發板的網路輸出控制



硬體:機智雲開發板

參考程式:MicroPython – Getting Started with MQTT on ESP32/ESP8266

燒錄暨編寫工具:uPyCraft

boot.py:

 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
# Complete project details at https://RandomNerdTutorials.com

try:
  import usocket as socket
except:
  import socket

from machine import Pin, PWM
import network

import esp
esp.osdebug(None)

import gc
gc.collect()

ssid = 'cmlin2'
password = '0928931103'

station = network.WLAN(network.STA_IF)

station.active(True)
station.connect(ssid, password)

while station.isconnected() == False:
  pass

print('Connection successful')
print(station.ifconfig())

led = Pin(2, Pin.OUT)

main.py:

 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
import time
# Complete project details at https://RandomNerdTutorials.com

def web_page():
  if led.value() == 1:
    gpio_state="ON"
  else:
    gpio_state="OFF"
  
  html = """<html><head> <title>ESP Web Server</title> <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" href="data:,"> <style>html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
  h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}.button{display: inline-block; background-color: #e7bd3b; border: none; 
  border-radius: 4px; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}
  .button2{background-color: #4286f4;}</style></head><body> <h1>ESP Web Server</h1> 
  <p>GPIO state: <strong>""" + gpio_state + """</strong></p><p><a href="/?led=on"><button class="button">ON</button></a></p>
  <p><a href="/?led=off"><button class="button button2">OFF</button></a></p></body></html>"""
  return html

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)

while True:
  conn, addr = s.accept()
  print('Got a connection from %s' % str(addr))
  request = conn.recv(1024)
  request = str(request)
  print('Content = %s' % request)
  led_on = request.find('/?led=on')
  led_off = request.find('/?led=off')
  if led_on == 6:
    print('LED ON')
    led.value(0)
    RedLED = PWM(Pin(15))
    GreedLED = PWM(Pin(12))
    BlueLED=  PWM(Pin(13))
    RedLED.duty(0)
    GreedLED.duty(0)
    BlueLED.duty(0)
    for i in range(0, 1024, 10):
        RedLED.duty(i)   
        time.sleep(0.01)
    RedLED.duty(0)
    for i in range(0, 1024, 10):
        GreedLED.duty(i)   
        time.sleep(0.01)
    GreedLED.duty(0)
    for i in range(0, 1024, 10):
        BlueLED.duty(i)   
        time.sleep(0.01)
    BlueLED.duty(0)
  if led_off == 6:
    print('LED OFF')
    led.value(1)
    RedLED = PWM(Pin(15))
    GreedLED = PWM(Pin(12))
    BlueLED=  PWM(Pin(13))
    RedLED.duty(0)
    GreedLED.duty(0)
    BlueLED.duty(0)

  response = web_page()
  conn.send('HTTP/1.1 200 OK\n')
  conn.send('Content-Type: text/html\n')
  conn.send('Connection: close\n\n')
  conn.sendall(response)
  conn.close()