2017年11月16日 星期四

Arduino, Python Firmata


$sudo pip install pySerial
$sudo pip install pyfirmata
$sudo pip install requests



upload firmata



>>> import pyfirmata
>>> pin = 13
>>> port = '/dev/cu.usbmodem1411'
>>> board = pyfirmata.Arduino(port)
>>> board.digital[pin].write(1)
>>> board.digital[pin].read()
1
>>> board.digital[pin].write(0)
>>> board.digital[pin].read()
0





1LED_Blink.py


#!/usr/bin/python



import pyfirmata
from time import sleep
pin = 13

port = '/dev/cu.usbmodem1411'
board = pyfirmata.Arduino(port) 
while True:
board.digital[pin].write(1) 
sleep(1)  
board.digital[pin].write(0) 
sleep(1)
board.exit()

Maxde-MacBook-Pro:downloads max$ python 1LED_Blink.py

跳出 Crtl+C
^CTraceback (most recent call last):
  File "1LED_Blink.py", line 11, in <module>
    sleep(1)  
KeyboardInterrupt
Maxde-MacBook-Pro:downloads max$

2console.py

#!/usr/bin/python

import pyfirmata
from time import sleep
pin = 13
port = '/dev/cu.usbmodem1411'
board = pyfirmata.Arduino(port) 
while True:
board.digital[pin].write(1) 
print(board.digital[pin].read()) 
sleep(1)  
board.digital[pin].write(0) 
print(board.digital[pin].read()) //Print out LED status
sleep(1)
board.exit()


^CTraceback (most recent call last):
  File "2console.py", line 14, in <module>
    sleep(1)
KeyboardInterrupt



4helloGUI.py  圖形化介面
#!/usr/bin/python
# -*- coding: UTF-8 -*-

# This code is supporting material for the book
# Python Programming for Arduino
# by Pratik Desai
# published by PACKT Publishing

import Tkinter

# Initialize main windows with title and size
top = Tkinter.Tk()
top.title("Hello GUI")
top.minsize(200, 30)

# Label widget
helloLabel = Tkinter.Label(top,
                           text="Hello World! It is a course這是一個課程")
helloLabel.pack()

# Start and open the window

top.mainloop()

=====7csvWriter.py=====

#!/usr/bin/python
# This code is supporting material for the book
# Python Programming for Arduino
# by Pratik Desai
# published by PACKT Publishing

import csv

data = [[1, 2, 3],['a','b','c'],['Python','for','Arduino']]

with open('PythonforArduino.csv', 'wb') as f:
    w = csv.writer(f)
    for row in data:

        w.writerow(row)


=====7csvReader.py=====


import csv


f = open ('PythonforArduino.csv', 'r')
ll = f.read()
print ll

Run:


Maxde-MacBook-Pro:Downloads max$ python 7csvReader.py 

1,2,3
a,b,c
Python,for,Arduino


=====9plotLive.py=====
顯示電阻Status





沒有留言:

張貼留言

check_systemv1.1

 check_systemv1.1.bat 可用於電腦資產盤點 @echo off REM 後續命令使用的是:UTF-8編碼 chcp 65001 echo ***Thanks for your cooperation*** echo ***感謝你的合作*** timeout 1...