Aller au contenu


Information tutoriel

  • Ajouté le: mars 17 2016 10:19
  • Date Updated: mars 13 2017 07:55
  • Lectures: 15157
 


* * * * *
0 Notes

Communication entre raspberry et arduino UNO via USB

Posté par Path on mars 17 2016 10:19

Après la compilation et l'upload depuis le PI, toujours sans débrancher la prise USB entre le PI et le Duino, ils se parlent ! cf. http://www.robot-mak...ne-de-commande/
 

post-9452-0-72697100-1458205262.png

 
Préalable sur le raspberry
 
Il faut installer Serial pour python.

sudo apt-get install python-serial

Préalable sur arduino : rien.
 
Programme de test PI (en python)

#!/usr/bin/python
# -*- coding: utf-8 -*-


import serial
import time

ser = serial.Serial('/dev/ttyACM0', 9600)

while 1 :
    message = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
    ser.write(message)
    print(ser.readline())
    time.sleep(0.5)

Programme de test arduino :

int compteur = 0;
String message;

void setup(){
Serial.begin(9600);
}


void loop() {
message = "-";
if (Serial.available()) {
message = Serial.readString();
}
Serial.print("MSG # ");
Serial.print(compteur);
Serial.print(" read : ");
Serial.println(message);

compteur++;
delay(1000);
}

Le PI envoi la date.
Le Duino lit cette date, ajoute une donnée et retourne la texte modifié.
Le PI affiche ce que Duino lui envoi.
 
Après compilation et upload, on lance le programme de test serial :

pi@raspberrypi:~/SBR $ ./test-serial-rpi.py
Wed, 16 MSG # 0 read : -

MSG # 1 read : Wed, 16 Mar 2016 21:30:21 +0000

MSG # 2 read : Wed, 16 Mar 2016 21:30:23 +0000

MSG # 3 read : Wed, 16 Mar 2016 21:30:25 +0000

MSG # 4 read : Wed, 16 Mar 2016 21:30:27 +0000

MSG # 5 read : Wed, 16 Mar 2016 21:30:29 +0000

Tuto réalisé dans le cadre de la construction de Ash V1 : http://www.robot-mak...-robot/?p=67997