{"id":754,"date":"2018-12-24T10:26:30","date_gmt":"2018-12-24T09:26:30","guid":{"rendered":"http:\/\/www.boichat.ch\/wpjrsp\/?p=754"},"modified":"2019-10-12T16:39:27","modified_gmt":"2019-10-12T14:39:27","slug":"communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2","status":"publish","type":"post","link":"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/","title":{"rendered":"Communiquer entre un Arduino et un Raspberry Pi via USB (Python, Java) (2\/2)"},"content":{"rendered":"<p>Un article sur ce sujet a \u00e9t\u00e9 soumis \u00e0\u00a0<a href=\"https:\/\/www.developpez.com\/\" target=\"_blank\" rel=\"noopener\">developpez.com<\/a>\u00a0et est maintenant disponible sous le titre\u00a0<a href=\"https:\/\/boichat.developpez.com\/tutoriel\/communication-arduino-raspberry-pi\/\" target=\"_blank\" rel=\"noopener\">Cr\u00e9er une communication entre Arduino et Raspberry Pi 3 via USB<\/a>.<\/p>\n<p><a href=\"https:\/\/www.boichat.ch\/wpjrsp\/2018\/11\/16\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb\/\" target=\"_blank\" rel=\"noopener\">En fin d&rsquo;article pr\u00e9c\u00e9dent<\/a> nous avions parl\u00e9 de d\u00e9velopper une application plus compl\u00e8te avec:<\/p>\n<ul>\n<li>Ajouter sur un Arduino Duemilanove un capteur analogique LM35 (pas possible sur un Raspberry Pi sans interface analogique);<\/li>\n<li>D\u00e9velopper sur le PC un sketch Arduino pour:<br \/>\n&#8211; enclencher le relais (petit chauffage) si la temp\u00e9rature est trop basse<br \/>\n&#8211; recevoir la temp\u00e9rature dans la console de l&rsquo;IDE<br \/>\n&#8211; d\u00e9cider si le Pi prend le contr\u00f4le pour enclencher ou d\u00e9clencher le relais;<\/li>\n<li>Envoyer une nouvelle limite de temp\u00e9rature;<\/li>\n<li>D\u00e9velopper sous Eclipse une classe Java d\u00e9di\u00e9 au Raspberry Pi pour faire et v\u00e9rifier le travail!<\/li>\n<\/ul>\n<p>Nous allons d\u00e9velopper compl\u00e8tement cette application dans cette article.<\/p>\n<h3>Pr\u00e9paration sur l\u2019Arduino<\/h3>\n<p>Nous commencerons \u00e9videmment par le sch\u00e9ma Fritzing:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-853\" src=\"https:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/relaislm35_bb_r-300x264.png\" alt=\"\" width=\"300\" height=\"264\" srcset=\"https:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/relaislm35_bb_r-300x264.png 300w, https:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/relaislm35_bb_r-768x676.png 768w, https:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/relaislm35_bb_r-1024x901.png 1024w, https:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/relaislm35_bb_r.png 1107w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>et le sketch Arduino qui est pr\u00e9par\u00e9 dans l&rsquo;IDE de l&rsquo;Arduino avec le PC connect\u00e9 avec le c\u00e2ble USB. Ce dernier sera d&rsquo;abord connect\u00e9 au PC pour v\u00e9rifier le sketch avant de passer au Raspberry Pi. Dans les deux cas, le c\u00e2ble USB jouera le r\u00f4le d&rsquo;alimentation et de moyen de transfert avec une communication s\u00e9rie.<\/p>\n<h3>Notre sketch sur l\u2019Arduino<\/h3>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-857\" src=\"https:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/IdeEdiLm35-1-205x300.png\" alt=\"\" width=\"453\" height=\"663\" srcset=\"https:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/IdeEdiLm35-1-205x300.png 205w, https:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/IdeEdiLm35-1-768x1123.png 768w, https:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/IdeEdiLm35-1-700x1024.png 700w, https:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/IdeEdiLm35-1.png 956w\" sizes=\"(max-width: 453px) 100vw, 453px\" \/><\/p>\n<p>Et le code complet:<\/p>\n<pre><span style=\"background-color: #ccffcc;\">int tempPin = 0;<\/span>\n<span style=\"background-color: #ccffcc;\">int relaisPin = 7;<\/span>\n<span style=\"background-color: #ccffcc;\">double tempC;<\/span>\n<span style=\"background-color: #ccffcc;\">double tempLimit = 20.5;<\/span>\n<span style=\"background-color: #ccffcc;\">bool isOn = false;<\/span>\n\n<span style=\"background-color: #ccffcc;\">int modet = 1; \/\/mode standard<\/span>\n\n<span style=\"background-color: #ccffcc;\">String incoming = \"\"; \/\/ for incoming serial string data<\/span>\n\n<span style=\"background-color: #ccffcc;\">void setup()<\/span>\n<span style=\"background-color: #ccffcc;\">{<\/span>\n<span style=\"background-color: #ccffcc;\">  Serial.begin(9600);<\/span>\n<span style=\"background-color: #ccffcc;\">  Serial.setTimeout(100);<\/span>\n<span style=\"background-color: #ccffcc;\">  pinMode(relaisPin, OUTPUT);<\/span>\n<span style=\"background-color: #ccffcc;\">  digitalWrite(relaisPin, LOW);<\/span>\n<span style=\"background-color: #ccffcc;\">}<\/span>\n\n<span style=\"background-color: #ccffcc;\">void loop()<\/span>\n<span style=\"background-color: #ccffcc;\">{<\/span>\n<span style=\"background-color: #ccffcc;\">  if (Serial.available() &gt; 0)<\/span>\n<span style=\"background-color: #ccffcc;\">  {<\/span>\n<span style=\"background-color: #ccffcc;\">    \/\/ read the incoming:<\/span>\n<span style=\"background-color: #ccffcc;\">    incoming = Serial.readString();<\/span>\n<span style=\"background-color: #ccffcc;\">    Serial.flush();<\/span>\n\n<span style=\"background-color: #ccffcc;\">    if (incoming.startsWith(\"on\"))<\/span>\n<span style=\"background-color: #ccffcc;\">    {<\/span>\n<span style=\"background-color: #ccffcc;\">      modet = 0;<\/span>\n<span style=\"background-color: #ccffcc;\">      digitalWrite(relaisPin, HIGH);<\/span>\n<span style=\"background-color: #ccffcc;\">      delay(2500);<\/span>\n<span style=\"background-color: #ccffcc;\">      isOn = true;<\/span>\n<span style=\"background-color: #ccffcc;\">    }<\/span>\n<span style=\"background-color: #ccffcc;\">    else if (incoming.startsWith(\"off\"))  <\/span>\n<span style=\"background-color: #ccffcc;\">    {<\/span>\n<span style=\"background-color: #ccffcc;\">      modet = 0;<\/span>\n<span style=\"background-color: #ccffcc;\">      digitalWrite(relaisPin, LOW);<\/span>\n<span style=\"background-color: #ccffcc;\">      delay(2500);<\/span>\n<span style=\"background-color: #ccffcc;\">      isOn = false;<\/span>\n<span style=\"background-color: #ccffcc;\">    }<\/span>\n<span style=\"background-color: #ccffcc;\">    else<\/span>\n<span style=\"background-color: #ccffcc;\">    {<\/span>\n<span style=\"background-color: #ccffcc;\">      double tempLimitC = incoming.toDouble();<\/span>\n<span style=\"background-color: #ccffcc;\">      if (tempLimitC != 0.0)<\/span>\n<span style=\"background-color: #ccffcc;\">      {<\/span>\n<span style=\"background-color: #ccffcc;\">        tempLimit = tempLimitC;<\/span>\n<span style=\"background-color: #ccffcc;\">        modet = 1;<\/span>\n<span style=\"background-color: #ccffcc;\">      }<\/span>\n<span style=\"background-color: #ccffcc;\">    }<\/span>\n<span style=\"background-color: #ccffcc;\">  }<\/span>\n \n<span style=\"background-color: #ccffcc;\">  tempC = analogRead(tempPin);<\/span>\n<span style=\"background-color: #ccffcc;\">  tempC = tempC * 0.48828125;<\/span>\n \n<span style=\"background-color: #ccffcc;\">  if (isOn) {<\/span>\n<span style=\"background-color: #ccffcc;\">    tempC = tempC - 1.97; \/\/calibration<\/span>\n<span style=\"background-color: #ccffcc;\">  }<\/span>\n\n<span style=\"background-color: #ccffcc;\">  Serial.print(\"Temperature (\");<\/span>\n<span style=\"background-color: #ccffcc;\">  Serial.print(tempLimit);<\/span>\n<span style=\"background-color: #ccffcc;\">  Serial.print(\") (\");<\/span>\n<span style=\"background-color: #ccffcc;\">  Serial.print(isOn);<\/span>\n<span style=\"background-color: #ccffcc;\">  Serial.print(\") (\");<\/span>\n<span style=\"background-color: #ccffcc;\">  Serial.print(modet);<\/span>\n<span style=\"background-color: #ccffcc;\">  Serial.print(\") = \");<\/span>\n<span style=\"background-color: #ccffcc;\">  Serial.print(tempC);<\/span>\n<span style=\"background-color: #ccffcc;\">  Serial.print(\"*C\");<\/span>\n<span style=\"background-color: #ccffcc;\">  Serial.println();<\/span>\n\n<span style=\"background-color: #ccffcc;\">  if (modet == 1)<\/span>\n<span style=\"background-color: #ccffcc;\">  {<\/span>\n<span style=\"background-color: #ccffcc;\">    if (tempLimit &lt; tempC)<\/span>\n<span style=\"background-color: #ccffcc;\">    {<\/span>\n<span style=\"background-color: #ccffcc;\">      digitalWrite(relaisPin, LOW);<\/span>\n<span style=\"background-color: #ccffcc;\">      isOn = false;<\/span>\n<span style=\"background-color: #ccffcc;\">    }<\/span>\n<span style=\"background-color: #ccffcc;\">    else<\/span>\n<span style=\"background-color: #ccffcc;\">    {<\/span>\n<span style=\"background-color: #ccffcc;\">      digitalWrite(relaisPin, HIGH);<\/span>\n<span style=\"background-color: #ccffcc;\">      isOn = true;<\/span>\n<span style=\"background-color: #ccffcc;\">    }<\/span>\n<span style=\"background-color: #ccffcc;\">  }<\/span>\n<span style=\"background-color: #ccffcc;\">  delay(2000);<\/span>\n<span style=\"background-color: #ccffcc;\">}<\/span><\/pre>\n<p><a href=\"http:\/\/www.boichat.ch\/codesource\/lm35.ino\" target=\"_blank\" rel=\"noopener\">Le fichier Arduino lm35.ino est t\u00e9l\u00e9chargeable ici<\/a>. Il doit \u00eatre install\u00e9 dans un sous-r\u00e9pertoire <strong>lm35<\/strong> que nous pourrons alors charger dans l&rsquo;<a href=\"https:\/\/www.arduino.cc\/en\/Guide\/HomePage\" target=\"_blank\" rel=\"noopener\">IDE d&rsquo;Arduino<\/a>.<\/p>\n<p>Apr\u00e8s avoir mont\u00e9 notre circuit Arduino d\u00e9crit dans le sch\u00e9ma Frizing ci-dessus, branch\u00e9 le c\u00e2ble USB entre l&rsquo;Arduino et le PC, d\u00e9termin\u00e9 quel COM est utilis\u00e9, nous pourrons l&rsquo;ex\u00e9cuter:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-858\" src=\"https:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/IdeLm35-1-300x157.png\" alt=\"\" width=\"447\" height=\"234\" srcset=\"https:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/IdeLm35-1-300x157.png 300w, https:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/IdeLm35-1-768x402.png 768w, https:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/IdeLm35-1.png 910w\" sizes=\"(max-width: 447px) 100vw, 447px\" \/><\/p>\n<p>A pr\u00e9sent cela n\u00e9cessite quelques explications.<\/p>\n<p>La s\u00e9rie de <strong>print<\/strong> qui suit\u00a0<span style=\"background-color: #ccffcc;\">Serial.print(\u00ab\u00a0Temperature (\u00ab\u00a0);<\/span>\u00a0va nous retourner la temp\u00e9rature mesur\u00e9e par le <strong>LM35<\/strong> accompagn\u00e9e d&rsquo;une s\u00e9rie d&rsquo;information, typiquement:<\/p>\n<pre><strong>Temperature (20.50) (0) (1) = 21.48*C<\/strong><\/pre>\n<p>dans notre fen\u00eatre Moniteur s\u00e9rie (menu <strong>Outils<\/strong> de l&rsquo;IDE). Cette ligne de texte pourra \u00eatre r\u00e9cup\u00e9r\u00e9e plus tard par un script Python ou une classe Java par un Raspberry Pi qui utilisera ce m\u00eame c\u00e2ble USB.<\/p>\n<p>La premi\u00e8re valeur 20.50 va d\u00e9terminer la temp\u00e9rature limite avec laquelle le relais sera enclench\u00e9, avec l&rsquo;instruction\u00a0<span style=\"background-color: #ccffcc;\">digitalWrite(relaisPin, HIGH);<\/span>, pour allumer par exemple un chauffage d&rsquo;appui. Ici, avec une temp\u00e9rature de <strong>21.48<\/strong>, le <strong>(0)<\/strong> nous sera retourn\u00e9, c&rsquo;est \u00e0 dire la variable <strong>isOn<\/strong>, indiquant que le relais est d\u00e9clench\u00e9.<\/p>\n<p>Le second <strong>(1)<\/strong> est une indication du mode de travail (<span style=\"background-color: #ccffcc;\">modet<\/span>). La valeur 1 est le mode standard, c&rsquo;est \u00e0 dire avec la v\u00e9rification de la temp\u00e9rature et de sa limite, pour ensuite activer ou d\u00e9sactiver le relais.<\/p>\n<p>Le calcul avec la valeur\u00a0<span style=\"background-color: #ccffcc;\">0.48828125<\/span>\u00a0nous permet le calcul analogique de la temp\u00e9rature, et j&rsquo;ai d\u00fb calibrer le r\u00e9sultat si le relais \u00e9tait enclench\u00e9 (sans doute un probl\u00e8me de tension). <strong>La partie\u00a0<span style=\"background-color: #ccffcc;\">tempC = tempC &#8211; 1.97;<\/span>\u00a0devra sans doute \u00eatre adapt\u00e9e.\u00a0<\/strong><\/p>\n<p>Il est possible d&rsquo;envoyer une nouvelle limite de temp\u00e9rature (<span style=\"background-color: #ccffcc;\">tempLimit<\/span>). Si la conversion de la valeur <strong>double<\/strong> donne une erreur\u00a0 (<span style=\"background-color: #ccffcc;\">incoming.toDouble()<\/span>), elle est ignor\u00e9e. Par contre nous pouvons envoyer un <strong>on<\/strong> ou un\u00a0<strong>off.<\/strong><\/p>\n<p>Si un de ces deux strings est re\u00e7u, le relais est enclench\u00e9 (<strong>on<\/strong>) ou d\u00e9clench\u00e9 (<strong>off<\/strong>).\u00a0 \u00a0Dans ces deux cas nous passons en\u00a0<strong><span style=\"background-color: #ccffcc;\">modet = 0;<\/span><\/strong> et le relais restera inchang\u00e9 suivant le dernier on ou off. La temp\u00e9rature continuera d&rsquo;\u00eatre envoy\u00e9e et si un programme Python ou Java est actif, il pourrait lire la temp\u00e9rature, activer le relais avec une commande voire encore faire sa propre logique d\u00e9cisionnel. Pour repasser en mode normal, il faudra renvoyer une nouvelle limite de temp\u00e9rature.<\/p>\n<p>Toute ces variantes peuvent \u00eatre v\u00e9rifi\u00e9e avec l&rsquo;envoi de commandes depuis le Moniteur s\u00e9rie.\u00a0 Si nous avons ceci:<\/p>\n<pre><strong>Temperature (22.30) (1) (1) = 21.97*C<\/strong><\/pre>\n<p>dans notre fen\u00eatre de moniteur s\u00e9rie, cela veut dire qu&rsquo;une commande 22.2 a \u00e9t\u00e9 envoy\u00e9e, que le relais est enclench\u00e9, et que si la temo\u00e9rature passe \u00e0 22.31, le relais sera d\u00e9clench\u00e9.<\/p>\n<h3>Notre application Java sous Eclipse<\/h3>\n<p>Nous garderons notre c\u00e2ble connect\u00e9 \u00e0 l&rsquo;Arduino et stopperons son IDE afin de lib\u00e9rer le port COM. Comme pr\u00e9sent\u00e9 \u00e0 plusieurs occasions dans le livre, nous allons cr\u00e9er ou r\u00e9utiliser un projet Eclipse, par exemple <strong>USBport<\/strong>, et y d\u00e9poser cette classe Java, c&rsquo;est \u00e0 dire <strong>TemperatureRelaisArduino.java<\/strong>:<\/p>\n<table>\n<tbody>\n<tr>\n<td bgcolor=\"#ffe6cc\">\n<pre>import java.io.IOException;\nimport java.io.RandomAccessFile;\n\npublic class TemperatureRelaisArduino implements Runnable {\n   private Thread monThread;\n   private RandomAccessFile rAF;\n   private String portName;\n   \n   private double  temperature = 0.0;\n   private String  cmd         = null;\n   private boolean stopThread  = false;\n   \n   private boolean isTarget = true;  \/\/Raspberry Pi\n\n   public TemperatureRelaisArduino(String portName) {\n      this.portName = portName;\n      monThread = new Thread(this);\n      \n      if (System.getProperty(\"os.name\").startsWith(\"Windows\")) {\n         isTarget = false;\n      }\n   }\n\n   public void start() {\n      try {\n         rAF = new java.io.RandomAccessFile(portName, \"rwd\");\n         rAF.writeBytes(\"\\r\\n\");\n      } \n      catch (Exception e) {\n         System.out.println(\"Le port \" + portName + \" n'existe pas\");\n         System.exit(0);\n      }\n      monThread.start();\n   }\n\n   public void run() {\n      System.out.println(\"Lecture du port \" + portName);\n      for (;;) {\n         if (stopThread) {\n            System.exit(0);\n         }\n         \n         String response = null;\n         try {\n            response = rAF.readLine();\n         } \n         catch (IOException e) {\n            System.out.println(e.getMessage());\n         }\n         System.out.println(response);\n         \n         int posTemp = response.indexOf(\" = \");\n         \n         \/\/Temperature message:\n         \/\/Temperature (20.50) (1) = 22.42*C\n         \/\/\n         if (posTemp &gt; 0) {  \/\/temperature received \n            temperature = Double.parseDouble(\n                     response.substring(posTemp + 3, posTemp + 8));\n         }\n         \n         if (cmd != null) {\n            try {\n               rAF.writeBytes(cmd);\n               if (isTarget) {\n                 rAF.writeBytes(\"\\r\\n\"); \n               }\n               cmd = null;\n            } catch (IOException e) {\n            }\n         }\n      }\n   }\n   \n   public double getTemperature() {\n      return temperature;\n   }\n   \n   public void setCmdToSent(String cmd) {\n      this.cmd = cmd;\n   }\n   \n   public void setStopThread() {\n      stopThread = true;\n   }\n   \n   public void pause(int ms) {\n      try {\n      Thread.sleep(ms);\n      } \n      catch (InterruptedException e) {\n      }\n   }\n\n   public static void main(String[] args) {\n      String portDevice = \"COM3\";\n      if (args.length &gt; 0) {\n         portDevice = args[0];\n      }\n      \n      TemperatureRelaisArduino tempRelArd = new\n                               TemperatureRelaisArduino(portDevice);\n      tempRelArd.start();\n      \n      for (int i = 0; i &lt; 3; i++) {\n         tempRelArd.pause(2200);\n      }\n      \n      tempRelArd.setCmdToSent(\"on\");\n      tempRelArd.pause(2200);      \n      tempRelArd.setCmdToSent(\"off\");\n      tempRelArd.pause(2200);\n      tempRelArd.setCmdToSent(\"on\");\n      tempRelArd.pause(2200);      \n      tempRelArd.setCmdToSent(\"off\");\n      tempRelArd.pause(2200);\n      \n      tempRelArd.setCmdToSent(\"31.3\");\n      tempRelArd.pause(2200);\n      \n      for (int i = 0; i &lt; 3; i++) {\n         tempRelArd.pause(1500);\n      }\n      \n      tempRelArd.pause(2200);\n      tempRelArd.setStopThread();\n   }\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>En ex\u00e9cutant cette classe sous Eclipse, nous aurons le m\u00eame r\u00e9sultat qu&rsquo;avec l&rsquo;IDE d&rsquo;Eclipse:<\/p>\n<pre><strong>Lecture du port COM3<\/strong>\n<strong>Temperature (20.50) (0) (1) = 21.97*C<\/strong>\n<strong>Temperature (20.50) (0) (1) = 21.97*C<\/strong>\n<strong>Temperature (20.50) (0) (1) = 21.97*C<\/strong>\n<strong>........<\/strong><\/pre>\n<p>Il faudra attendre un peu pour voir la seconde partie fonctionner avec l&rsquo;envoi de commandes on, off et 31.3 avec une valeur limite plut\u00f4t \u00e9lev\u00e9e! Pour activer l&rsquo;air conditionn\u00e9, il faudrait invers\u00e9 la logique.<\/p>\n<p>La commande DOS <a href=\"https:\/\/www.boichat.ch\/wpjrsp\/2018\/11\/16\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb\/\" target=\"_blank\" rel=\"noopener\">MODE est expliqu\u00e9e dans l&rsquo;article pr\u00e9c\u00e9dent<\/a>.<\/p>\n<p>La classe\u00a0<strong>TemperatureRelaisArduino<\/strong> est suffisamment claire et ne n\u00e9cessite pas vraiment d&rsquo;explications. Dans mon livre un certain nombre de composants et d&rsquo;applications utilisent des Thread afin de pouvoir traiter d\u2019autres fonctions ou composants en parall\u00e8le.\u00a0 La variable\u00a0<strong>isTarget<\/strong> a \u00e9t\u00e9 n\u00e9cessaire pour envoyer un\u00a0\u00ab\u00a0\\r\\n\u00a0\u00bb qui n&rsquo;\u00e9tait pas n\u00e9cessaire sur la version Windows avec un COM. La commande <strong>stty<\/strong> ci-dessous a r\u00e9solu les autres probl\u00e8mes.<\/p>\n<h3><strong>Ex\u00e9cuter TemperatureRelaisArduino sur le Raspberry Pi<\/strong><\/h3>\n<p>Comme d\u00e9crit dans mon livre \u00e0 plusieurs occasions, nous transf\u00e9rerons le fichier <strong>TemperatureRelaisArduino.class<\/strong> depuis le Workspace d&rsquo;Eclipse (r\u00e9pertoire <strong>bin<\/strong>) sur le Raspberry Pi dans le r\u00e9pertoire habituel <strong>\/home\/pi\/java<\/strong>. Il faudra alors faire passer le c\u00e2ble USB du PC sur le Raspberry Pi, \u00e9videmment!<\/p>\n<p>Sur le Raspberry Pi il faudra configurer correctement le port (<a href=\"https:\/\/www.boichat.ch\/wpjrsp\/2018\/11\/16\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb\/\" target=\"_blank\" rel=\"noopener\">comme d\u00e9termin\u00e9 dans l&rsquo;article pr\u00e9c\u00e9dent<\/a>) avec la commande:<\/p>\n<pre><strong>stty -F \/dev\/ttyACM0 cs8 9600 ignbrk -brkint -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts<\/strong><\/pre>\n<p>Nous pourrons la d\u00e9poser dans un fichier\u00a0<strong>setStty.sh s<\/strong>ans oublier un <strong>chmod +x\u00a0setStty.sh<\/strong> afin de l\u2019ex\u00e9cuter avant l&rsquo;application Java.<\/p>\n<p>Ensuite la commande java sera ici:<\/p>\n<pre><strong>java TemperatureRelaisArduino \/dev\/ttyUSB0<\/strong><\/pre>\n<p>Et cela fonctionne!<\/p>\n<h3><strong>Exercices<\/strong><\/h3>\n<ul>\n<li>Adapter la classe Java\u00a0<strong>TemperatureRelaisArduino\u00a0<\/strong>pour qu&rsquo;elle soit responsable de v\u00e9rifier la limite de temp\u00e9rature &#8230; et non plus l&rsquo;Arduino.<\/li>\n<li>Ajouter un senseur de lumi\u00e8re sur l&rsquo;Arduino et faire une application similaire. Le mat\u00e9riel du chapitre 16 peut \u00eatre utile!<\/li>\n<li>Cr\u00e9er une classe Java sur le Raspberry Pi pour recueillir la temp\u00e9rature mesur\u00e9e par le capteur LM35 de l&rsquo;Arduino, sans utilisation du relais, et faire une repr\u00e9sentation journali\u00e8re des variations de temp\u00e9rature. D\u00e9finir par exemple des p\u00e9riodes de 5 minutes o\u00f9 les variations excessives seraient ignor\u00e9es. Un rapport journalier pourrait \u00eatre envoy\u00e9 par courriel (chapitre 24 de mon livre).<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>Que ce soit l&rsquo;Arduino, l&rsquo;IDE de l&rsquo;Arduino, la cr\u00e9ation de sketchs et de sch\u00e9mas Fritzing, l&rsquo;utilisation d&rsquo;Eclipse pour d\u00e9velopper des classes Java, la ma\u00eetrise de Raspbian (le Linux du Raspberry Pi) ou encore du langage Python, tous ces outils, ces composants \u00e9lectroniques et ces langages vont permettre aux bricoleurs de s&rsquo;initier dans le monde des\u00a0 technologies modernes au travers, \u00e9videmment, et c&rsquo;est incontournable de la programmation et de son apprentissage.<\/p>\nDate de la derni\u00e8re modification: 12 octobre 2019\n","protected":false},"excerpt":{"rendered":"<p>Un article sur ce sujet a \u00e9t\u00e9 soumis \u00e0\u00a0developpez.com\u00a0et est maintenant disponible sous le titre\u00a0Cr\u00e9er une communication entre Arduino et Raspberry Pi 3 via USB.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v17.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Communiquer entre un Arduino et un Raspberry Pi via USB (Python, Java) (2\/2) - Un livre sur Java, Python, Eclipse et le Raspberry Pi 3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/\" \/>\n<meta property=\"og:locale\" content=\"fr_FR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Communiquer entre un Arduino et un Raspberry Pi via USB (Python, Java) (2\/2) - Un livre sur Java, Python, Eclipse et le Raspberry Pi 3\" \/>\n<meta property=\"og:description\" content=\"Un article sur ce sujet a \u00e9t\u00e9 soumis \u00e0\u00a0developpez.com\u00a0et est maintenant disponible sous le titre\u00a0Cr\u00e9er une communication entre Arduino et Raspberry Pi 3 via USB.&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Un livre sur Java, Python, Eclipse et le Raspberry Pi 3\" \/>\n<meta property=\"article:published_time\" content=\"2018-12-24T09:26:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-10-12T14:39:27+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/relaislm35_bb_r-300x264.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u00c9crit par\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jean-Bernard Boichat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Dur\u00e9e de lecture estim\u00e9e\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.boichat.ch\/wpjrsp\/#website\",\"url\":\"https:\/\/www.boichat.ch\/wpjrsp\/\",\"name\":\"Un livre sur Java, Python, Eclipse et le Raspberry Pi 3\",\"description\":\"Jean-Bernard Boichat - Auteur chez Eyrolles\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.boichat.ch\/wpjrsp\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"fr-FR\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/#primaryimage\",\"inLanguage\":\"fr-FR\",\"url\":\"http:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/relaislm35_bb_r-300x264.png\",\"contentUrl\":\"http:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/relaislm35_bb_r-300x264.png\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/#webpage\",\"url\":\"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/\",\"name\":\"Communiquer entre un Arduino et un Raspberry Pi via USB (Python, Java) (2\/2) - Un livre sur Java, Python, Eclipse et le Raspberry Pi 3\",\"isPartOf\":{\"@id\":\"https:\/\/www.boichat.ch\/wpjrsp\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/#primaryimage\"},\"datePublished\":\"2018-12-24T09:26:30+00:00\",\"dateModified\":\"2019-10-12T14:39:27+00:00\",\"author\":{\"@id\":\"https:\/\/www.boichat.ch\/wpjrsp\/#\/schema\/person\/fe6328cc5b74b8a6e8d841285a928751\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/#breadcrumb\"},\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.boichat.ch\/wpjrsp\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Communiquer entre un Arduino et un Raspberry Pi via USB (Python, Java) (2\/2)\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.boichat.ch\/wpjrsp\/#\/schema\/person\/fe6328cc5b74b8a6e8d841285a928751\",\"name\":\"Jean-Bernard Boichat\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.boichat.ch\/wpjrsp\/#personlogo\",\"inLanguage\":\"fr-FR\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/862886ce35a52b9d13595e18ac3c3ea5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/862886ce35a52b9d13595e18ac3c3ea5?s=96&d=mm&r=g\",\"caption\":\"Jean-Bernard Boichat\"},\"sameAs\":[\"http:\/\/www.boichat.ch\"],\"url\":\"https:\/\/www.boichat.ch\/wpjrsp\/author\/bjb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Communiquer entre un Arduino et un Raspberry Pi via USB (Python, Java) (2\/2) - Un livre sur Java, Python, Eclipse et le Raspberry Pi 3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/","og_locale":"fr_FR","og_type":"article","og_title":"Communiquer entre un Arduino et un Raspberry Pi via USB (Python, Java) (2\/2) - Un livre sur Java, Python, Eclipse et le Raspberry Pi 3","og_description":"Un article sur ce sujet a \u00e9t\u00e9 soumis \u00e0\u00a0developpez.com\u00a0et est maintenant disponible sous le titre\u00a0Cr\u00e9er une communication entre Arduino et Raspberry Pi 3 via USB.&hellip;","og_url":"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/","og_site_name":"Un livre sur Java, Python, Eclipse et le Raspberry Pi 3","article_published_time":"2018-12-24T09:26:30+00:00","article_modified_time":"2019-10-12T14:39:27+00:00","og_image":[{"url":"http:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/relaislm35_bb_r-300x264.png"}],"twitter_card":"summary_large_image","twitter_misc":{"\u00c9crit par":"Jean-Bernard Boichat","Dur\u00e9e de lecture estim\u00e9e":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/www.boichat.ch\/wpjrsp\/#website","url":"https:\/\/www.boichat.ch\/wpjrsp\/","name":"Un livre sur Java, Python, Eclipse et le Raspberry Pi 3","description":"Jean-Bernard Boichat - Auteur chez Eyrolles","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.boichat.ch\/wpjrsp\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"fr-FR"},{"@type":"ImageObject","@id":"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/#primaryimage","inLanguage":"fr-FR","url":"http:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/relaislm35_bb_r-300x264.png","contentUrl":"http:\/\/www.boichat.ch\/wpjrsp\/wp-content\/uploads\/2018\/12\/relaislm35_bb_r-300x264.png"},{"@type":"WebPage","@id":"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/#webpage","url":"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/","name":"Communiquer entre un Arduino et un Raspberry Pi via USB (Python, Java) (2\/2) - Un livre sur Java, Python, Eclipse et le Raspberry Pi 3","isPartOf":{"@id":"https:\/\/www.boichat.ch\/wpjrsp\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/#primaryimage"},"datePublished":"2018-12-24T09:26:30+00:00","dateModified":"2019-10-12T14:39:27+00:00","author":{"@id":"https:\/\/www.boichat.ch\/wpjrsp\/#\/schema\/person\/fe6328cc5b74b8a6e8d841285a928751"},"breadcrumb":{"@id":"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/#breadcrumb"},"inLanguage":"fr-FR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.boichat.ch\/wpjrsp\/2018\/12\/24\/communiquer-entre-un-arduino-et-un-raspberry-pi-via-usb-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.boichat.ch\/wpjrsp\/"},{"@type":"ListItem","position":2,"name":"Communiquer entre un Arduino et un Raspberry Pi via USB (Python, Java) (2\/2)"}]},{"@type":"Person","@id":"https:\/\/www.boichat.ch\/wpjrsp\/#\/schema\/person\/fe6328cc5b74b8a6e8d841285a928751","name":"Jean-Bernard Boichat","image":{"@type":"ImageObject","@id":"https:\/\/www.boichat.ch\/wpjrsp\/#personlogo","inLanguage":"fr-FR","url":"https:\/\/secure.gravatar.com\/avatar\/862886ce35a52b9d13595e18ac3c3ea5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/862886ce35a52b9d13595e18ac3c3ea5?s=96&d=mm&r=g","caption":"Jean-Bernard Boichat"},"sameAs":["http:\/\/www.boichat.ch"],"url":"https:\/\/www.boichat.ch\/wpjrsp\/author\/bjb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.boichat.ch\/wpjrsp\/wp-json\/wp\/v2\/posts\/754"}],"collection":[{"href":"https:\/\/www.boichat.ch\/wpjrsp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.boichat.ch\/wpjrsp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.boichat.ch\/wpjrsp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.boichat.ch\/wpjrsp\/wp-json\/wp\/v2\/comments?post=754"}],"version-history":[{"count":0,"href":"https:\/\/www.boichat.ch\/wpjrsp\/wp-json\/wp\/v2\/posts\/754\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.boichat.ch\/wpjrsp\/wp-json\/wp\/v2\/media?parent=754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.boichat.ch\/wpjrsp\/wp-json\/wp\/v2\/categories?post=754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.boichat.ch\/wpjrsp\/wp-json\/wp\/v2\/tags?post=754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}