LED Connectée

Introduction

Ce tutoriel montre comment réaliser une application Android qui se connecte à une carte Arduino grâce à un module Bluetooth. Cette application commande l'allumage et l'extinction d'une LED connectée à la carte Arduino.

Prérequis nécessaires : Avoir fait les tutoriels d'initiation à Arduino et à App inventor2.

DESIGNER

L'écran comporte :

• Le ListPicker_BT permet d'établir la connexion avec le module BT

• Le Label Label_Connexion pour afficher l'état de la connexion

• deux Boutons : Bouton_ON et Bouton_OFF pour la commander la LED

•  Un bouton btn_Deconnect pour la déconnexion du module BT

• et le composant invisible Client_Blutooth

  • Créer un nouveau projet nommé LED_CONNECTEE sous App inventor 2.

  • Réaliser l'interface suivante :

Au centre-droit de l'écran, il y a la liste des composants que vous devez glisser et poser de la palette vers le viewer.

À l'extrême droite de l'écran apparaissent les propriétés des composants que vous devez personnaliser.

Cliquer sur Blocks (en haut à droite) pour passer à l'écriture du « code ».

BLOCKS

Réaliser le « code » suivant :

Côté Arduino

Matériel nécessaire :

  • Une carte Arduino UNO

  • Un shield GROVE

  • Un module grove Bluetooth

  • Un module grove LED

Connexions :

La LED sur D5 et le module bluetooth sur D6.

Dans le logiciel Arduino vous devez modifier le programme suivant pour obtenir

  1. Un nom original de l'interface Bluetooth

  2. Un code plus sécurisé.

1
/*
2
 * Programme permettant l'allumage et l'extinction d une 
3
 * LED en fonction de la valeur d'un caractere recu via 
4
 * le module bluetooth connecte aux broches 6(Rx) et 7 (Tx) 
5
 * de la carte arduino UNO. La LED est reliee à la broche 5.
6
 * Si le caractere recu vaut '1' la led s'allume
7
 * Si il vaut 0 la LED s'eteind.  
8
 */
9
10
#include <SoftwareSerial.h>   
11
const int RxD    = 6;  // Broche de reception de donnees 
12
const int TxD    = 7;  // Broche de transmission de donnees
13
const int PINLED = 5;  // Broche de la LED
14
/* 
15
 * PARAMETRES BLUETOOTH :  
16
 */
17
String Nom_Du_Module = "MaSuperLED";
18
String PIN_Du_Module = "1234";
19
    
20
SoftwareSerial blueToothSerial(RxD,TxD); // Liaison serie du module Bluetooth
21
void setup()
22
{
23
    Serial.begin(9600);
24
    Serial.println("Bluetooth module");
25
    
26
    pinMode(PINLED, OUTPUT);
27
    
28
29
    setupBlueToothConnection(Nom_Du_Module, PIN_Du_Module);
30
}
31
32
void loop()
33
{
34
    char recvChar;
35
    
36
    while(1)
37
    {
38
        if(blueToothSerial.available())       // SI DONNEES RECUES
39
        {
40
            recvChar = blueToothSerial.read();//    Lecture des donnees
41
            Serial.print(recvChar);           //    Affichage au moniteur serie
42
            
43
            if(recvChar == 1)             //      Si le caractere recu est un "1" :
44
            {
45
                digitalWrite(PINLED, HIGH); //          On allume la LED
46
            }
47
            else if(recvChar == 0)        //      Si c'est un 0
48
            { 
49
                digitalWrite(PINLED, LOW);  //         On l'eteint  
50
            }
51
        }
52
    }
53
}
54
55
56
57
/***************************************************************************
58
 * La fonction setupBlueToothConnection() permet d'initialiser le module
59
 * Bluetooth. La confiuration est 
60
***************************************************************************/
61
void setupBlueToothConnection(String nom_du_module, String pin)
62
{  
63
  blueToothSerial.begin(9600);                     // Vitesse de transmission : 9600 bauds
64
  blueToothSerial.print("AT");                     // Reponse du module BT
65
  delay(1000); 
66
  while(blueToothSerial.available())
67
  {
68
    char recvChar = blueToothSerial.read();        // Reponse du module BT
69
    Serial.print(recvChar);
70
  }
71
  Serial.println("");
72
73
  blueToothSerial.print("AT+DEFAULT");             // Config usine (par defaut)
74
  delay(2000); 
75
  while(blueToothSerial.available())
76
  {
77
    char recvChar = blueToothSerial.read();        // Reponse du module BT
78
    Serial.print(recvChar);
79
  }
80
  Serial.println("");
81
82
  String at_command("AT+NAME"+nom_du_module);
83
  char *at_str = at_command.c_str();
84
  Serial.println(at_str);
85
  delay(5000);
86
  blueToothSerial.print(at_str);                    // Nom du peripherique BT  
87
                                                    // Longueure max 12 caracteres
88
  delay(5000);
89
  while(blueToothSerial.available())
90
  {
91
    char recvChar = blueToothSerial.read();        // Reponse du module BT
92
    Serial.print(recvChar);
93
  }
94
  Serial.println("");
95
  
96
97
  at_command="AT+PIN"+pin;
98
  at_str = at_command.c_str();
99
  blueToothSerial.print(at_str);             // Configuration du code PIN
100
  delay(2000);
101
  while(blueToothSerial.available())
102
  {
103
    char recvChar = blueToothSerial.read();       // Reponse du module BT
104
    Serial.print(recvChar);
105
  }
106
  Serial.println("");
107
  
108
  blueToothSerial.print("AT+AUTH1");             // 
109
  delay(2000);    
110
  while(blueToothSerial.available())
111
  {
112
    char recvChar = blueToothSerial.read();      // Reponse du module BT
113
    Serial.print(recvChar);
114
  }
115
  Serial.println("");
116
    blueToothSerial.flush();
117
118
}