2019年9月6日 星期五

MicroPython物聯網科技應用設計班

2019/8/23
Micropython環境布置
(1)安裝CP2012Driver
  1.接好ESP8266  usbt8插電腦
  2.控制台->系統->裝置管理員->連接埠(COM3)
(2)使用ESP8266Flasher燒錄Micropython韌體
確定有bin檔
1.執行ESP8266Flasher
operation  COM3
2.config 確定bin檔位置  不能有中文目錄最好放在c
3.advance確定值
4.回operation 按下flasher
5.log 成功否
   拔下usb再插上













(3)使用putty測試韌體是否燒錄成功
1.serial2.COM3 1152003.mp8266 ->save->load->open

(4)安裝Micropython編輯器
upycraft-master3放在c
(5)撰寫Micropython程式測試

8/30

9/6

from machine import Pin
import time

p0=Pin(0,Pin.OUT)
p2=Pin(2,Pin.OUT)
p4=Pin(4,Pin.OUT)
p5=Pin(5,Pin.OUT)
p12=Pin(12,Pin.OUT)
p0.value(0)
p2.value(0)
p4.value(0)
p5.value(0)
p12.value(0)

while True:
  p0.value(1)
  time.sleep(0.5)
  p0.value(0)

  p2.value(1)
  time.sleep(0.5)
  p2.value(0)

  p4.value(1)
  time.sleep(0.5)
  p4.value(0)

  p5.value(1)
  time.sleep(0.5)
  p5.value(0)

  p12.value(1)
  time.sleep(0.5)
  p12.value(0)
====================================
from machine import Pin
import timefrom machine import Pin
import time

myled=[0,2,4,5,12]
len1=len(myled)
for i in range(0,len1):
  pled=Pin(myled[i],Pin.OUT)
  pled.value(0)

  while True:
    for j in range(0,len1):
      for k in range(0,len1):
        pled=Pin(myled[k],Pin.OUT)
        pled.value(0)
      pled2=Pin(myled[j],Pin.OUT)
      pled2.value(1)
      time.sleep(0.5)
============================
左右來回一棟跑馬燈
from machine import Pin
import time

myled=[0,2,4,5,12]
len1=len(myled)
for i in range(0,len1):
  pled=Pin(myled[i],Pin.OUT)
  pled.value(0)

while True:
    for j in range(0,len1):
      for k in range(0,len1):
        pled=Pin(myled[k],Pin.OUT)
        pled.value(0)
      pled2=Pin(myled[j],Pin.OUT)
      pled2.value(1)
      time.sleep(0.2)
      if j==(len1-1):
        for l in range(len1-1,0,-1):
          for m in range(0,len1):
            pled=Pin(myled[m],Pin.OUT)
            pled.value(0)
          pled3=Pin(myled[l],Pin.OUT)
          pled3.value(1)
          time.sleep(0.2)
=======================
按鈕控制LED燈
from machine import Pin
import time

pled=Pin(4,Pin.OUT)
pbt1=Pin(5,Pin.IN)
pled.value(0)

while True:
  btv=pbt1.value()
  if btv==1:
    pled.value(1)
  else:
    pled.value(0)
  time.sleep(0.5)
=================

from machine import Pin
import time

pled=Pin(4,Pin.OUT)
pbt1=Pin(5,Pin.IN)
pled.value(0)
flag=False

while True:
  btv=pbt1.value()

  if btv==1:
    flag=not flag

  if flag==True:
    pled.value(1)
  else:
    pled.value(0)
  time.sleep(0.5)
+++++++++++++++++++++++++
9/20
1
from machine import Pin,PWM

pled1=PWM(Pin(5),freq=250,duty=50)
======================
from machine import Pin,PWM
import time

v=0

while True:
  v=v+5
  pled1=PWM(Pin(5))
  pled1.freq(500)
  pled1.duty(v)
  time.sleep(1)
  if v>=250:
    v=0
==================
from machine import Pin,PWM
import time
k=5
v=0
while True:
  v=v+k
  pled1=PWM(Pin(5),freq=500,duty=v)
  time.sleep(0.1)
  if v>=255:
    k=-5
  if v<0:
     k=5
=========================
==================

from machine import ADC
import time

chr1=ADC(0)
while True:
  v=chr1.read()
  print(v)
  time.sleep(0.5)
===================

from machine import Pin,PWM,ADC
import time

chr=ADC(0)
while True:
  cv=chr.read()
  total=int(cv/4)
  pled1=PWM(Pin(5))
  pled1.freq(500)
  pled1.duty(total)
  time.sleep(0.5)
++++++++++++++++++++++++++++
10/03
叫號器
from machine import Pin
import time

list1=[[1,1,1,1,1,1,0],
       [0,1,1,0,0,0,0],
       [1,1,0,1,1,0,1],
       [1,1,1,1,0,0,1],
       [0,1,1,0,0,1,1],
       [1,0,1,1,0,1,1],
       [1,0,1,1,1,1,1],
       [1,1,1,0,0,1,0],
       [1,1,1,1,1,1,1],
       [1,1,1,1,0,1,1]]


pin0=Pin(0,Pin.OUT)
pin1=Pin(2,Pin.OUT)
pin2=Pin(5,Pin.OUT)
pin3=Pin(4,Pin.OUT)
pin4=Pin(12,Pin.OUT)
pin5=Pin(13,Pin.OUT)
pin6=Pin(14,Pin.OUT)

k=0
pin0.value(list1[k][0])
pin1.value(list1[k][1])
pin2.value(list1[k][2])
pin3.value(list1[k][3])
pin4.value(list1[k][4])
pin5.value(list1[k][5])
pin6.value(list1[k][6])

pin7=Pin(15,Pin.IN)
pin8=Pin(16,Pin.OUT)
pin8.value(0)

while True:
  btv=pin7.value()
  if btv==1:
    k=k+1
    pin0.value(list1[k][0])
    pin1.value(list1[k][1])
    pin2.value(list1[k][2])
    pin3.value(list1[k][3])
    pin4.value(list1[k][4])
    pin5.value(list1[k][5])
    pin6.value(list1[k][6])
    pin8.value(1)
    time.sleep(0.1)
    pin8.value(0)
 
    if k>=9:
      k=0
   
  time.sleep(0.5)
   

中斷服務

from machine import Pin
def fun1(v):
  print("Hello IRQ")

bt1=Pin(0,Pin.IN)
bt1.irq(trigger=Pin.IRQ_RISING,handler=fun1)

while True:
  pass

10/17
watersensor
from machine import ADC,Pin
import time

chr=ADC(0)
led1=Pin(4,Pin.OUT)
led1.value(0)
led2=Pin(5,Pin.OUT)
led2.value(0)
led3=Pin(12,Pin.OUT)
led3.value(0)
bee=Pin(13,Pin.OUT)
bee.value(0)

while True:
  led1.value(0)
  led2.value(0)
  led3.value(0)

  cv=chr.read()
  print(cv)
  if cv>=400:
    led3.value(1)
    bee.value(1)
    time.sleep(1)
    bee.value(0)
  elif cv>=200:
    led2.value(1)
  else:
    led3.value(0)
  time.sleep(1)
================================
火焰感測器

from machine import Pin
import time

f1=Pin(5,Pin.IN)
f1.value(0)

while True:
  fv=f1.value()
  print(fv)
  time.sleep(1)
===============
 功能
有火時led亮bee響1秒
無火時led 暗bee不響
from machine import Pin
import time

f1=Pin(5,Pin.IN)
f1.value(0)
bee=Pin(13,Pin.OUT)
bee.value(0)
led1=Pin(12,Pin.OUT)
led1.value(0)

while True:
  fv=f1.value()
  print(fv)
  if fv==0:
    bee.value(1)
    time.sleep(1)
    led1.value(1)
    bee.value(0)
  else:
     bee.value(0)
     led1.value(0)
 
  time.sleep(1)
=
from machine import Pin
import time

re=Pin(12,Pin.OUT)
re.value(0)

while True:
  re.value(1)
  time.sleep(1)
  re.value(0)
  time.sleep(1)
++++++++++++++
10/25
網路通訊
1.函數庫
import network

2.呼叫network模組
network,WLAN()函數
(a)network.STA_IF 混合型
(b)network.AP_IF
sta=network.WLAN(network.STA_IF)
ap=network.WLAN(network.AP_IF)
MCU--wifi(STA_IF)~~internet~~web

3.呼叫介面物件
sta.active() 檢查是否啟動
sta.active(boolean)  true為啟動 false關閉

4.掃描附近基地台
sta.scan()

5.連線
sta.connect("帳號","密碼")

6.顯示基本組態
sta.ifconfig()


測試通否
import network

sta_if.active(True)
sta_if=network.WLAN(network.STA_IF)
print(sta_if.active())

True
====================

import network

sta_if=network.WLAN(network.STA_IF)
print(sta_if.active())
print(sta_if.ifconfig())
-------------------------
False
('0.0.0.0', '0.0.0.0', '0.0.0.0', '192.168.0.1')
-----------------------------------
ap_if同樣測試
-----------------------------------
import network

sta_if.active(True)
sta_if=network.WLAN(network.STA_IF)

sta_if.connect('bsnet-1','062230542')
print(sta_if.ifconfig())
---------------------------
exec(open('wifi5.py').read(),globals())
('192.168.0.118', '255.255.255.0', '192.168.0.1', '192.168.0.1')
確定能連
-----------------------------------
建立socket
1.引入socket函數庫
inport socket
   標準socket API
2.元件
s=socket.socket()
3.建立socket位址和埠端
getaddinfo("0.0.0.0",8080)
                  或IPV6位址
ai[0][-1]---取得IP位址

4.設定套件選項的值
setsocketopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)

5.將套件函數綁定在位址上
bind(addr)

6.開放監聽TCP傳入連接
listen(5)
一次開五個監聽

接收傳送資料
1.等候連線,接收網路封包資料
res=socket.accept()  傳回串列
client_s=res[0]   客戶端物件[封包內容]
client_addr=res[1] 位址

2.接收TCP資料
req=client_s.recv(4096)  放緩衝器的容量

3.發送web HMI內容
client_s.send(data)       webDMI

4.關閉連線
client_s.close()

HMI人機介面格式
1.html=b"""\--------- """
2.html=b"""\HTTP/1.0 200 OK ---------- """
3.html=b"""\HTTP/1.0 200 OK Hello %s from MicroPython! """


-----------------------------------------------------------
import network
import socket

sta_if.active(True)
sta_if = network.WLAN(network.STA_IF)

sta_if.connect('bsnet-1','062230542')
print(sta_if.ifconfig())


CONTENT = b"""\
HTTP/1.0 200 OK

Hello #%d from MicroPython!
"""

def main():
    s = socket.socket()
    ai = socket.getaddrinfo("0.0.0.0", 8080)
    addr = ai[0][-1]

    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    s.bind(addr)
    s.listen(5)
    print("Listening, connect your browser to http://<this_host>:8080/")
    
    
    counter = 0
    while True:
        res = s.accept()
        client_s = res[0]
        client_addr = res[1]
        req = client_s.recv(4096)
        print("Request:")
        print(req)
        client_s.send(CONTENT % counter)
        client_s.close()
        counter += 1
        print()


main()
-------------------------
import network
import socket

sta_if.active(True)
sta_if = network.WLAN(network.STA_IF)

sta_if.connect('bsnet-1','062230542')
print(sta_if.ifconfig())


CONTENT = b"""\
HTTP/1.0 200 OK

<body><font size="+6">Hello #%s from MicroPython!</font></body>
"""

def main():
  s = socket.socket()
  ai = socket.getaddrinfo("0.0.0.0", 8080)
  addr = ai[0][-1]

  s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

  s.bind(addr)
  s.listen(5)
  print("Listening, connect your browser to http://<this_host>:8080/")
    
    
    #counter = 0
  while True:
    res=s.accept()
    client_s = res[0]
    client_addr = res[1]
    req = client_s.recv(4096)
    print("Request:")
    print(req)
    name="hope"
    client_s.send(CONTENT %name)
    client_s.close()
      #counter += 1
    print()
      
main()
---------------------------------------------------
11/1
尋找http://192.168.0.137:8080/>?name=1
while True:
    res=s.accept()
    client_s = res[0]
    #client_addr = res[1]
    req = str(client_s.recv(4096))
    chin=req.find('/?name=1')
    print("Result:",chin)
    #print(req)
    name="hope"
    client_s.send(CONTENT %name)
    client_s.close()
      #counter += 1
    print()
   
main()
=======================
Result: 6

Result: 300

Result: -1

Result: -1
----------------------------------
封包分割::split()
http://192.168.0.173:8080/?name=5
chin=req.split("/?name=")
kdata=chin[]
result=int(data[0])
-----------------------------
while True:
    res=s.accept()
    client_s = res[0]
    #client_addr = res[1]
    req = str(client_s.recv(4096))
    #chin=req.find('/?name=1')
    chin=req.split("/?name=")
    kdata=chin[1]
    result=int(kdata[0])
    if result==5:
      print("success")
    #print("Result:",chin)
    #print(req)
    name="hope"
    client_s.send(CONTENT %name)
    client_s.close()
      #counter += 1
    print()
------------------------------------------------------------------
import network
import socket

#sta_if.active(True)
sta_if = network.WLAN(network.STA_IF)

sta_if.connect('bsnet-1','062230542')
print(sta_if.ifconfig())


CONTENT = b"""\
HTTP/1.0 200 OK

<body><font size="+6">Hello #%s from MicroPython!</font></body>
"""

def main():
  s = socket.socket()
  ai = socket.getaddrinfo("0.0.0.0", 8080)
  addr = ai[0][-1]

  s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

  s.bind(addr)
  s.listen(5)
  print("Listening, connect your browser to http://<this_host>:8080/")
 
 
    #counter = 0
  while True:
    res=s.accept()
    client_s = res[0]
    req = str(client_s.recv(4096))
    chin=""
    kdata=""
    chin=req.split("/?name=")
    kdata=chin[1]
    result=int(kdata[0])
    if result==1:
      print("LED_1")
    if result==2:
      print("LED_2")
    if result==3:
      print("LED_3")
    if result==4:
      print("ALL ON")
    if result==5:
      print("All OFF")
    name="hope"
    client_s.send(CONTENT %name)
    client_s.close()
    print()
   
main()
----------------------------------
實作控制
from machine import Pin
import time
import network
import socket

p1=Pin(4,Pin.OUT)
p2=Pin(5,Pin.OUT)
p3=Pin(12,Pin.OUT)
p1.value(0)
p2.value(0)
p3.value(0)
#sta_if.active(True)
sta_if = network.WLAN(network.STA_IF)

sta_if.connect('bsnet-1','062230542')
print(sta_if.ifconfig())


CONTENT = b"""\
HTTP/1.0 200 OK

<body><font size="+6">Hello #%s from MicroPython!</font></body>
"""

def main():
  s = socket.socket()
  ai = socket.getaddrinfo("0.0.0.0", 8080)
  addr = ai[0][-1]

  s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

  s.bind(addr)
  s.listen(5)
  print("Listening, connect your browser to http://<this_host>:8080/")
 
 
    #counter = 0
  while True:
    res=s.accept()
    client_s = res[0]
    req = str(client_s.recv(4096))
    chin=""
    kdata=""
    chin=req.split("/?name=")
    kdata=chin[1]
    result=int(kdata[0])
    if result==1:
      print("LED_1")
      p1.value(1)
      p2.value(0)
      p3.value(0)
   
    if result==2:
      p1.value(0)
      p2.value(1)
      p3.value(0)
      print("LED_2")
    if result==3:
      p1.value(0)
      p2.value(0)
      p3.value(1)
      print("LED_3")
    if result==4:
      p1.value(1)
      p2.value(1)
      p3.value(1)
      print("ALL ON")
    if result==5:
      p1.value(0)
      p2.value(0)
      p3.value(0)
      print("All OFF")
   
    name="hope"
    client_s.send(CONTENT %name)
    client_s.close()
    print()
    if result==2:
      p2.value(1)
      time.sleep(1)
      p2.value(0)
    if result==3:
      p3.value(1)
      time.sleep(1)
      p3.value(0)
    if result==4:
      p1.value(1)
      p2.value(1)
      p3.value(1)
      time.sleep(1)
    if result==5:
      p1.value(0)
      p2.value(0)
      p3.value(0)
    name="hope"
    client_s.send(CONTENT %name)
    client_s.close()
    print()
   
main()
____________________________________
from machine import Pin,PWM
import time
import network
import socket

p1=PWM(Pin(2))

sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)

sta_if.connect('bsnet-1','062230542')
print(sta_if.ifconfig())


CONTENT = b"""\
HTTP/1.0 200 OK

<body><font size="+6">Hello #%s from MicroPython!</font></body>
"""

def main():
  s = socket.socket()
  ai = socket.getaddrinfo("0.0.0.0", 8080)
  addr = ai[0][-1]

  s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

  s.bind(addr)
  s.listen(5)
  print("Listening, connect your browser to http://<this_host>:8080/")
  i=0
     
  while True:
    res=s.accept()
    client_s = res[0]
    req = str(client_s.recv(4096))
    chin=""
    kdata=""
    chin=req.split("/?name=")
    kdata=chin[1].split("\\r")
    result=0
    if i>0:
      len1=len(kdata[0])
   
      if len1<5:
        result=int(kdata[0])
        print(result)
      else:
        result=0
   
 
    p1.freq(500)
    p1.duty(result)
    i=i+1
 
    name="hope"
    client_s.send(CONTENT %name)
    client_s.close()
    print()
   
main()
________________________________
from machine import ADC
import time
import network
import socket

sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)

sta_if.connect('bsnet-1','062230542')
print(sta_if.ifconfig())
lightr=ADC(0)

CONTENT = b"""\
HTTP/1.0 200 OK

<body><font size="+6">Hello #%d from MicroPython!</font></body>
"""

def main():
  s = socket.socket()
  ai = socket.getaddrinfo("0.0.0.0", 8080)
  addr = ai[0][-1]

  s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

  s.bind(addr)
  s.listen(5)
  print("Listening, connect your browser to http://<this_host>:8080/")

     
  while True:
    res=s.accept()
    client_s = res[0]
    req = str(client_s.recv(4096))
 
    for i in range(1,4):
      lightv=lightr.read()
      print(lightv)
 
    name=lightv
    client_s.send(CONTENT %lightv)
    client_s.close()
    print()
   
main()
________________________________

沒有留言:

張貼留言