2020年5月28日 星期四

用MicroPython設計遠距情境燈以機智雲為例

遠距情境燈是指可以在遠方使用資通訊設備,例如:電腦和手機,來進行控制LED的顏色。我們採用物聯網通訊協定MQTT,以主題R,G, B來分別控制紅色、綠色、藍色的亮度,進而經由RGB三種顏色來調變RGB LED顏色。

機智雲:http://wiki.ai-thinker.com/esp8266/boards/gizwits
MicroPython的MQTT參考程式如下:
https://randomnerdtutorials.com/micropython-mqtt-esp32-esp8266/

把該網頁的程式改成遠距情境燈。
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
32
33
34
35
36
37
38
# Complete project details at https://RandomNerdTutorials.com

import time
from umqttsimple import MQTTClient
import ubinascii
import machine
import micropython
import network
import esp
esp.osdebug(None)
import gc
gc.collect()

ssid = '您的熱點'
password = '您的密碼'
mqtt_server = 'broker.hivemq.com'
#EXAMPLE IP ADDRESS
#mqtt_server = '192.168.1.144'
client_id = ubinascii.hexlify(machine.unique_id())
topic_sub = b'R'
topic_sub2 = b'G'
topic_sub3 = b'B'
topic_pub = b'hello'

last_message = 0
message_interval = 5
counter = 0

station = network.WLAN(network.STA_IF)

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

while station.isconnected() == False:
  pass

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

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
68
69
# Complete project details at https://RandomNerdTutorials.com
R=0
G=0
B=0
def sub_cb(topic, msg):
  global R, G, B
  print((topic, msg))
  if topic == b'R':
    print('R=', msg)
    try:
      R=int(msg,10)
    except:
      R=0
  if topic == b'G':
    print('G=', msg)
    try:
      G=int(msg,10)
    except:
      G=0
  if topic == b'B':
    print('G=', msg)
    try:
      B=int(msg,10)
    except:
      B=0
  RedLED.duty(R)
  GreenLED.duty(G)
  BlueLED.duty(B)

def connect_and_subscribe():
  global client_id, mqtt_server, topic_sub, topic_sub2, topic_sub3
  client = MQTTClient(client_id, mqtt_server)
  client.set_callback(sub_cb)
  client.connect()
  client.subscribe(topic_sub)
  client.subscribe(topic_sub2)
  client.subscribe(topic_sub3)
  print('Connected to %s MQTT broker, subscribed to %s, %s, %s topic' % (mqtt_server, topic_sub, topic_sub2, topic_sub3))
  return client

def restart_and_reconnect():
  print('Failed to connect to MQTT broker. Reconnecting...')
  time.sleep(10)
  machine.reset()

from machine import Pin, PWM
import time
RedLED = PWM(Pin(15))
GreenLED = PWM(Pin(12))
BlueLED=  PWM(Pin(13))
RedLED.duty(0)
GreenLED.duty(0)
BlueLED.duty(0) 

try:
  client = connect_and_subscribe()
except OSError as e:
  restart_and_reconnect()

while True:
  try:
    client.check_msg()
    if (time.time() - last_message) > message_interval:
      msg = b'Hello #%d' % counter
      client.publish(topic_pub, msg)
      last_message = time.time()
      counter += 1
  except OSError as e:
    restart_and_reconnect()

2020年4月30日 星期四

利用MicroPython線上模擬器來學習控制伺服馬達


模擬器內建範例:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Using the Servo
# Make sure you have the Servo checkbox marked!

import machine
import pyb

# The pyboard has four simple servo connections
servo = pyb.Servo(1)

servo.angle(90, 100)

用ADC來控制伺服馬達的角度:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# Using the Servo
# Make sure you have the Servo checkbox marked!

import machine
import pyb
import time


# The pyboard has four simple servo connections
servo = pyb.Servo(1)
y4 = machine.Pin('Y4')
adc = pyb.ADC(y4)

while True:
  value = adc.read() / 255 * 180-90

  servo.angle(int(value), 5000)
  
  time.sleep_ms(100)


利用MicroPython線上模擬器實作小夜燈


模擬器提供的ADC範例程式:
# Using the ADC (Analogue to Digital Converter)
# Make sure you have the ADC checkbox marked!

import machine
import pyb

# The slider is connected to pin Y4, try adjusting it
y4 = machine.Pin('Y4')

adc = pyb.ADC(y4)

print(adc.read())

模擬器提供的LED範例程式:

# four LEDS numbered 1 to 4

import time
import pyb

for i in range(1000):
    pyb.LED((i%4) + 1).toggle()
    time.sleep_ms(100)

組合後的程式:
# Using the ADC (Analogue to Digital Converter)
# Make sure you have the ADC checkbox marked!

import machine
import pyb
import time

# The slider is connected to pin Y4, try adjusting it
y4 = machine.Pin('Y4')

led = pyb.LED(4)

while True:
  adc = pyb.ADC(y4)

  print(adc.read())

  if adc.read() > 128:
    led.on()
  else:
    led.off()
  time.sleep_ms(100)


改成PIN LED

程式如下:(要特別注意PIN宣告的地方)

import machine
import pyb
import time

# The slider is connected to pin Y4, try adjusting it
y4 = machine.Pin('Y4')

led = machine.Pin('Y12')

while True:
  adc = pyb.ADC(y4)

  print(adc.read())

  if adc.read() > 128:
    led.on()
  else:
    led.off()
  time.sleep_ms(100)

2020年4月5日 星期日

使用ESP32/8266晶片的MicroPython範例

一、uPyCraft工具安裝
(1) Install uPyCraft IDE – Windows PC Instructions
(2) Install uPyCraft IDE – Mac OS X Instructions
(3) Install uPyCraft IDE – Linux Ubuntu Instructions

二、工具
(1) Flash/Upload MicroPython Firmware to ESP32 and ESP8266
三、基本篇


四、感知篇

五、通訊篇

六、應用篇





2020年4月3日 星期五

[MicroPython模擬器] Unicorn使用方法簡介

沒有硬體但想學MicroPython怎麼辦?就用MicroPython on Unicorn!
模擬器的網址:http://micropython.org/unicorn/。
主畫面如下圖,可分成四大區域:
(1) 命令列(左上):提供一個可以互動可輸入命令的介面。
(2) 程式區(右上):可以用來編輯程式,提供一些程式範例。
(3) 硬體選項區(左下):提供可以選擇硬體種類、重置以及週邊的選項。
(4) 模擬硬體板子(右下):可以展示硬體設備的功能。


如上圖的左上方有個命令列,一開使用就說明unicorn with Cortex-M3,Cortex-M3是ARM公司推出的微控器的CPU,其內核主要是應用於低成本和低功耗的場合,因具有極高的運算能力和極強的中斷響應能力,目前應用非常廣泛。在第二行有提示要我們鍵入help(),其執行後的畫面如下:


由於Unicorn採用REPL (read-eval-print-loop)操作模式,中文為 讀取﹣求值﹣輸出循環,是一個簡單的,交互式的編程環境。其控制命令有5種,分別為
(1) CTRL-A:進入raw REPL模式。
(2) CTRL-B:進入正常REPL模式。
(3) CTRL-C:中斷執行中的程式。
(4) CTRL-D:軟體重置。
(5) CTRL-E:進入張貼模式。
預設值是正常REPL模式,有看到>>>符號,raw REPL則只有一個>符號,在這個模式下不會有回應值。

相關模式說明可以參考:The MicroPython Interactive Interpreter Mode (aka REPL)
在正常REPL模式下輸入:
for i in range(30):
      print(i)
就能印出0到29的整數。
for i in range(30):
      if i > 3:
            break
      print(i)
就僅能印出0到3的整數。

dir()命令可以看到目前使用到的變數值

2020年3月31日 星期二

使用EduBlocks用樍木式圖形化介面學習Python程式設計

許多人反應學習Python能用樍木式圖形化介面來學習嗎?答案是OK,網址為:https://app.edublocks.org/。
1.啟動EduBlocks網站,選擇下方Start Creating按鈕。
2.選擇Python程式按鈕。

3.選擇右手邊的功能表選單。

 4.選擇Smaple功能表單

5.選擇Graph範例
 6.查看樍木式程式結構
7.執行結果
8.查看程式

2020年3月29日 星期日

圖形化編輯器學習Python, MicroPython, micro:bit

在2020年2月2日我們發表"沒有硬體但想學MicroPython怎麼辦?就用MicroPython on Unicorn!",今天我們來看看對於兒童學習程式設計,利用圖形化編輯器來培養兒童的邏輯思考能力。
(1)Micropython圖形化編輯器就是Python編輯器
在Python有一個分支,MicroPython或CirduitPython,可以把程式放到晶片中,透過軟硬體控制,可以更容易瞭解微控制器的運作。對於TPYBoard系列開發板而言,第一款Micropython圖形化編輯器就是Python編輯器,網址:http://www.tpyboard.com/pythoneditor/#。
(2)微軟的MakeCode
微軟的MakeCode也提供許多工具,包括:模擬器、積木編輯器、以及Java編輯器。在模擬器方面有micro:bit、Circuit Playground Express等,網址:https://www.microsoft.com/zh-tw/makecode。

以下是micro:bit可以用積木來控制micro:bit的模擬器,挺好玩的。
(3) EduBlocks
很容易地轉換積木到Python,網址:https://edublocks.org/。
上圖的右下角有一個Start Creating按鈕,就可以看到它支援4種積木式程式設計,分別是:
(1) Python 3
(2) Raspberry Pi
(3) Micro:bit
(4) CircuitPython

https://microbit.org/code/ 網頁最下方還有許多工具喔!