Table des matières

Allumer une led via bluetooth

Suite à la découverte du module bluetooth tel0026, voici un premier essais de communication entre deux Arduino.

Mise en place

TODO: Ajouter photos!

Programme "master"

/* Minimal code for testing bluetooth module TEL0026 with a button
 
This program must be used with "bluetooth-TEL0026-robot-avec-led.ino".
 
The circuit is based on Dreamer Nano V4.1, compatible Arduino Leonardo ( https://www.dfrobot.com/product-786.html ).
TEL0026 and button are plugged on Nano I/O Shield ( https://www.dfrobot.com/product-68.html )
 
Created 2017 by Simon Lefort.
*/
 
int buttonPin = 8; //button is on pin 8
 
void setup() {
  Serial.begin(38400);   // Serial to talk with computer via USB
  Serial1.begin(38400);  // Serial to talk to bluetooth module TEL0026
}
 
void loop() {
  int val = digitalRead(buttonPin);
  delay(50);
 
  if (val == HIGH) {
    Serial1.print("b");
    //Serial.println("push");
  }
  else
  { 
    //Serial.println("no push");
  }
}

Programme "slave"

/* Minimal code for testing bluetooth module TEL0026 with a led.
If we received "b1" by the bluetooth module, we light on the led.
 
This program must be used with "bluetooth-TEL0026-commande-avec-bouton.ino".
 
The circuit is based on Dreamer Nano V4.1, compatible Arduino Leonardo ( https://www.dfrobot.com/product-786.html ).
TEL0026 and button are plugged on Nano I/O Shield ( https://www.dfrobot.com/product-68.html )
 
Created 2017 by Simon Lefort.
*/
 
int ledPin = 14;
int incomingByte = 0;
 
void setup() {
  Serial.begin(38400);  //communicate via USB
  Serial1.begin(38400); //communicate via Bluetooth
 
  digitalWrite(ledPin, LOW);
}
 
void loop() {
  if (Serial1.available() > 0){
    incomingByte = Serial1.read();
 
    Serial.print("I received: ");
    Serial.println(incomingByte);
 
    if (incomingByte = 'b'){
      digitalWrite(ledPin, HIGH);
      delay(500);
    }
  }
  else{
    Serial.println("nothing...");
    digitalWrite(ledPin, LOW);
    delay(500);
  }
}

Remarques

Ça marche mais c'est basique! Je ne vérifie pas l'état précédent du bouton avant d'envoyer (ce qui permettrait d'envoyer uniquement les changements et serait plus intéressant!), le traitement est simple et basique.