13_-_arduino_et_python
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| 13_-_arduino_et_python [2018/06/30 10:01] – mistert | 13_-_arduino_et_python [2020/09/26 15:15] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| === Arduino et Python === | === Arduino et Python === | ||
| - | Disons le d' | + | Disons le d' |
| **Le code Arduino** | **Le code Arduino** | ||
| Ligne 81: | Ligne 81: | ||
| Le protocole d' | Le protocole d' | ||
| - | Il y a un code pour les différents de commandes. | + | Il y a un code pour les différentes |
| #define PIN_MODE 100 | #define PIN_MODE 100 | ||
| Ligne 89: | Ligne 89: | ||
| #define ANALOG_READ 104 | #define ANALOG_READ 104 | ||
| | | ||
| - | A chaque commande un certain nombre de paramètres est requis. Pour écrire sur une sortie on enverra trois caractères: | + | A chaque commande un certain nombre de paramètres est requis. Pour écrire sur une sortie |
| - | Ce code vous permet d' | + | Ce code vous permet d' |
| - | Une classe python est crée pour permettre un interfaçage facile. | + | Une classe python est créée |
| **commandesPython.py** | **commandesPython.py** | ||
| Ligne 153: | Ligne 153: | ||
| return c1*0x100+c2 | return c1*0x100+c2 | ||
| + | **fichier test.py** | ||
| + | # -*- coding: utf-8 -*- | ||
| + | import time | ||
| + | from commandesPython import Arduino | ||
| + | | ||
| + | port = " | ||
| + | ard = Arduino(port) | ||
| + | | ||
| + | ard.pinMode(13, | ||
| + | | ||
| + | | ||
| + | | ||
| + | for i in range(10): | ||
| + | print(ard.analogRead(0)) | ||
| + | print(i) | ||
| + | ard.digitalWrite(13, | ||
| + | time.sleep(0.5) | ||
| + | ard.digitalWrite(13, | ||
| + | time.sleep(0.5) | ||
| + | | ||
| + | ard.close() | ||
| + | | ||
| + | Si la commande en ligne de commande vous semble trop austère, il est possible d' | ||
| + | |||
| + | # -*- coding: utf-8 -*- | ||
| + | import time | ||
| + | from commandesPython import Arduino | ||
| + | from tkinter import * | ||
| + | | ||
| + | class App(Arduino): | ||
| + | def __init__(self): | ||
| + | self.port = " | ||
| + | self.ard = Arduino(self.port) | ||
| + | self.W=200 | ||
| + | self.H=200 | ||
| + | self.root = Tk() | ||
| + | self.create_interface() | ||
| + | self.update_clock() | ||
| + | self.configure() | ||
| + | self.root.mainloop() | ||
| + | | ||
| + | def send_arduino(self): | ||
| + | self.ard.digitalWrite(13, | ||
| + | time.sleep(0.25) | ||
| + | self.ard.digitalWrite(13, | ||
| + | time.sleep(0.25) | ||
| + | | ||
| + | def create_interface(self): | ||
| + | can = Canvas(self.root, | ||
| + | can.pack(side=TOP, | ||
| + | btn1 = Button(self.root, | ||
| + | btn1.pack(side=LEFT) | ||
| + | self.text1=Label(self.root, | ||
| + | self.text1.pack() | ||
| + | | ||
| + | def configure(self): | ||
| + | self.ard.pinMode(13, | ||
| + | |||
| + | def update_clock(self): | ||
| + | now = time.strftime(" | ||
| + | self.text1.configure(text= self.ard.analogRead(0)) | ||
| + | self.root.after(1000, | ||
| + | | ||
| + | app=App() | ||
| + | |||
| + | |||
| + | Deuxième exemple, plus évolué: un programme python qui embarque un serveur twisted pour contrôler l' | ||
| + | Exemple de commandes: | ||
| + | |||
| + | <A href=" | ||
| + | <A href=" | ||
| + | <A href=" | ||
| + | <A href=" | ||
| + | |||
| + | """ | ||
| + | Programme de gestion d'une Arduino à partir de Python | ||
| + | A partir de tkinter, twisted et commandesPython (R) | ||
| + | MrT - sebastien.tack@ac-caen.fr | ||
| + | """ | ||
| + | from tkinter import * | ||
| + | from twisted.internet import tksupport, reactor | ||
| + | from twisted.web import server, resource | ||
| + | from twisted.internet import reactor, endpoints | ||
| + | from commandesPython import Arduino | ||
| + | import serial.tools.list_ports | ||
| + | import sys | ||
| + | | ||
| + | class Counter(resource.Resource): | ||
| + | isLeaf = True | ||
| + | numberRequests = 0 | ||
| + | | ||
| + | | ||
| + | def __init__(self, | ||
| + | """ | ||
| + | Constructeur de la classe | ||
| + | """ | ||
| + | self.root = root | ||
| + | self.port = None | ||
| + | ports = list(serial.tools.list_ports.comports()) | ||
| + | for p in ports: | ||
| + | if " | ||
| + | self.port =str(p[1][p[1].find(" | ||
| + | try: | ||
| + | self.ard = Arduino(self.port) | ||
| + | | ||
| + | except: | ||
| + | print(" | ||
| + | self.root.destroy() | ||
| + | sys.exit(1) | ||
| + | self.make_interface() | ||
| + | |||
| + | def make_interface(self): | ||
| + | """ | ||
| + | Création de l' | ||
| + | """ | ||
| + | self.text2 = Label(self.root, | ||
| + | self.text2.pack() | ||
| + | | ||
| + | |||
| + | def send_arduino(self, | ||
| + | """ | ||
| + | Envoi des ordres en sortie sur Arduino | ||
| + | """ | ||
| + | self.ard.digitalWrite(port, | ||
| + | | ||
| + | def render_GET(self, | ||
| + | """ | ||
| + | Gestion des réponses GET | ||
| + | """ | ||
| + | # | ||
| + | self.numberRequests += 1 | ||
| + | #trouver path | ||
| + | content= "" | ||
| + | req = request.uri | ||
| + | if b' | ||
| + | pass | ||
| + | | ||
| + | if req==b'/': | ||
| + | | ||
| + | content ='< | ||
| + | content | ||
| + | content += "< | ||
| + | content += u'< | ||
| + | content += u'< | ||
| + | content += u'< | ||
| + | content += u'< | ||
| + | content += "</ | ||
| + | | ||
| + | if b'/ | ||
| + | | ||
| + | ordres = req.split(b'/' | ||
| + | pin = int(ordres[3]) | ||
| + | mode = (ordres[4] == b' | ||
| + | self.ard.pinMode(pin, | ||
| + | self.send_arduino(pin, | ||
| + | content ='< | ||
| + | content = " | ||
| + | content += "</ | ||
| + | | ||
| + | if b'/ | ||
| + | | ||
| + | ordres = req.split(b'/' | ||
| + | pin = int(ordres[3]) | ||
| + | value = self.ard.analogRead(pin) | ||
| + | content ='< | ||
| + | content += "Pin " | ||
| + | content += "</ | ||
| + | | ||
| + | if b'/ | ||
| + | | ||
| + | ordres = req.split(b'/' | ||
| + | pin = int(ordres[3]) | ||
| + | mode = int(ordres[4]) | ||
| + | self.ard.pinMode(pin, | ||
| + | content ='< | ||
| + | content += u" | ||
| + | content += "</ | ||
| + | | ||
| + | request.setHeader(b" | ||
| + | return content.encode(" | ||
| + | | ||
| + | """ | ||
| + | Début du programme | ||
| + | """ | ||
| + | | ||
| + | try: | ||
| + | site = Counter(Tk()) | ||
| + | tksupport.install(site.root) | ||
| + | endpoints.serverFromString(reactor, | ||
| + | reactor.run() | ||
| + | except: | ||
| + | pass | ||
| + | | ||
| + | On peut maintenant construire des pages HTML et réaliser des requêtes Ajax en jQuery sur ces URL pour piloter une Arduino à partir de pages Web. ;) | ||
13_-_arduino_et_python.1530352909.txt.gz · Dernière modification : (modification externe)
