从带有HC-06蓝牙的arduino接收数据

时间:2018-09-27 16:51:18

标签: ionic-framework bluetooth arduino

在搜索失败很多之后,我无法恢复arduino发送到我的用ionic编码的android应用程序中的数据。

目标是恢复存储在arduino中的变量,以在由ionic生成的应用程序中显示它们。但是,除了像BlueTerm +这样的第三方应用程序,我无法读取此数据。

你能帮我吗?这是我的代码:

.ino功能,已通过BlueTerm +应用程序测试

在这里找到:
tutoriel module HC-06

#include <SoftwareSerial.h> SoftwareSerial BT(10, 11); //  RX,TX respectivement char BTdata;// void setup() {//***************VERIFIER CES 2 VITESSES***************   BT.begin(9600);//La liaison série soft D10 et D11!!!VERIFIER  cette vitesse!!   Serial.begin(9600);//La série standard, hardware, D0 et D1 vers PC!!VERIFIER  cette vitesse!! //**************Vers PC   Serial.println("Bonjour ");   Serial.println("Le caractere entre sur l'Android doit se retrouver ci dessous"); //***************Vers module     BT.println(" ");   BT.println(" ");   BT.println("Bonjour sur Android");   BT.println(" ");   BT.println("HC05, la led doit clignoter 2 flash, 1s Off, 2flash...");   BT.println("HC06, led On fixe");   BT.println(" ");   BT.println("Entrer un caractere ");   BT.println("S'il se retouve sur le PC, tout va bien...");


} void loop() {    if (BT.available()) //Car pret en entrée sur Serie soft?   { BTdata = BT.read(); //oui, le clavier Android BT a émis un car   BT.println(" ");//Aller à la ligne sur 

// **************Vers PC 
    Serial.write(BTdata);//ecrire le car sur le PC
    Serial.println();   }

  envoyer();

}

void envoyer() {

  if (BT.read() == 'k'){   BT.println("BT Ceci est un essai avec k");  Serial.println("Serial Ceci est un essai avec k");   BT.write("l");   }

  if (BT.read() != -1) {
    BT.println(BT.read());
    Serial.println(BT.read());   }    }

.TS:

import { Component, NgZone } from '@angular/core'; import { NavController } from 'ionic-angular'; import { BluetoothSerial } from '@ionic-native/bluetooth-serial'; import { AlertController } from 'ionic-angular';


@Component({   selector: 'page-connexion',   templateUrl: 'connexion.html' }) export class ConnexionPage {

  public bletoggle: boolean = true;   public nomModule: string = 'non connecté';   public colorLabel: string = 'danger';   public colorBalEtat: string = 'danger';   public balEtat: boolean = false;   public balInfoEtat: string = 'désactivé';   public cmdTerminal: any = '';

  //Variables arduino

  public nbreLigne: number = 1;   public nbreCol: number = 35;   public nbreLED: number = 0;   public mode: string = 'auto';   public balises: boolean = true;   public luxBal: number = 127;   public colorBal01: any = '(0, 0, 255)';   public colorBal02: any = '(255, 0, 255)';   public colorBal03: any = '(0, 255, 0)';   public senarioBal: string = 'normal';   public eclairage: boolean = true;   public luxEcl: number = 127;   public colorEcl01: any = '(0, 0, 255)';   public colorEcl02: any = '(255, 0, 255)';   public colorEcl03: any = '(0, 255, 0)';   public senarioEcl: string = 'normal';   public infoArduino: any = '';

  public message: any = 'aucune donnée';   public message2: any = 'aucune donnée';

  unpairedDevices: any;   pairedDevices: any;   gettingDevices: Boolean;   constructor(private bluetoothSerial: BluetoothSerial,
              private alertCtrl: AlertController,
              private ngZone: NgZone) {
    bluetoothSerial.enable();
    this.nomModule = 'non connecté';
    this.colorLabel = 'danger';
    this.bletoggle = false;   }

  startScanning() {
    this.bletoggle = true;

    this.pairedDevices = null;
    this.unpairedDevices = null;
    this.gettingDevices = true;
    this.bluetoothSerial.discoverUnpaired().then((success) => {
      this.unpairedDevices = success;
      this.gettingDevices = false;
      success.forEach(element => {
        // alert(element.name);
      });
    },
      (err) => {
        console.log(err);
      })

    this.bluetoothSerial.list().then((success) => {
      this.pairedDevices = success;
    },
      (err) => {

      })   }   success = (data) => alert(data);   fail = (error) => alert(error);

  selectDevice(address: any, name: any) {

    this.colorLabel = 'Default';
    this.nomModule = 'connexion à ' + name + ' en cours ...';

    let alert = this.alertCtrl.create({
      title: 'Connexion',
      message: 'Voulez vous vous connecter à ' + name,
      buttons: [
        {
          text: 'Annuler',
          role: 'cancel',
          handler: () => {
            console.log('Cancel clicked');
          }
        },
        {
          text: 'Connecter',
          handler: () => {
            this.bluetoothSerial.connect(address).subscribe(this.success, this.fail);
            this.colorLabel = 'secondary';
            this.nomModule = name;
            this.bletoggle = false;
          }
        }
      ]
    });
    alert.present();

  }

  disconnect() {
    let alert = this.alertCtrl.create({
      title: 'Déconnexion?',
      message: 'Voulez-vous vous déconnecter?',
      buttons: [
        {
          text: 'Annuler',
          role: 'cancel',
          handler: () => {
            console.log('Cancel clicked');
          }
        },
        {
          text: 'Déconnecter',
          handler: () => {
            this.bluetoothSerial.disconnect();
            this.colorLabel = 'danger';
            this.bletoggle = true;
          }
        }
      ]
    });
    alert.present();
       }

  envoyerK() {
    this.bluetoothSerial.write('k');   }

  envoyerCmd(){
    this.bluetoothSerial.write(this.cmdTerminal);
    this.infoArduino = 'reception données';

    this.infoArduino = JSON.stringify(this.bluetoothSerial.readUntil("\n"));

    // this.bluetoothSerial.read()
    // .then((data: any) => {
    //   if (data[0] == "D" && data[16] == "F") {
    //     this.infoArduino = parseFloat(data[1]+data[2]+data[3]+data[4]+data[5]);
    //   }
    // });

  }

read() {   this.bluetoothSerial.readUntil('\n').then()

}

read1() {   this.bluetoothSerial.read().then(
    buffer => {        
      var data = new Uint8Array(buffer);
      this.ngZone.run(() => {
        this.message = JSON.stringify(data);
      });
    }   ); }

}

HTML IONIC

<ion-header>   <ion-navbar>
    <ion-title>
      Connexion
    </ion-title>   </ion-navbar> </ion-header>       

<ion-content>


<ion-card>

  <ion-card-header>
      <ion-list>
          <ion-item>
            <ion-label>Bluetooth: <ion-badge color="{{ colorLabel }}" item-end>{{ nomModule }}</ion-badge></ion-label>
            <ion-toggle [(ngModel)]="bletoggle"></ion-toggle>
          </ion-item>
          <ion-item>

            <button ion-button outline item-end icon-start (click)="startScanning()">
              <ion-icon name="bluetooth"></ion-icon>
              Se connecter
            </button>
            <button ion-button outline item-end icon-start (click)="disconnect()">Déconnecter</button>
          </ion-item>
        </ion-list>   </ion-card-header>
     <ion-card-content *ngIf="bletoggle">
    <ion-list>

      <ion-list-header>
        Dispositif(s) Apairé(s)
      </ion-list-header>
      <ion-item *ngFor="let device of pairedDevices">
        <span (click)="selectDevice(device.address, device.name)">
          {{device.name}}
        </span>
      </ion-item>

    <ion-list-header>
      Dispositif(s) visible(s)
    </ion-list-header>
    <ion-item *ngFor='let device of unpairedDevices'>
      <span (click)="selectDevice(device.address, device.name)">
        {{device.name}}
      </span>
    </ion-item>
    <ion-spinner name="crescent" *ngIf="gettingDevices"></ion-spinner>



    </ion-list>

  </ion-card-content>

</ion-card>

<ion-card>
    <ion-card-header>
        <ion-list>
            <ion-item>
              <ion-label>INFO SYSTEME</ion-label>
              <ion-toggle [(ngModel)]="parametrestoggle"></ion-toggle>
            </ion-item>
          </ion-list>
    </ion-card-header>
    <ion-card-content *ngIf="parametrestoggle">
      <ion-card>
        <ion-card-header>
          Admin
        </ion-card-header>
        <ion-card-content>
            <ion-list>

                <ion-item>
                  <ion-label stacked>Terminal</ion-label>
                  <ion-input [(ngModel)]="cmdTerminal" type="text"></ion-input>
                </ion-item>

              </ion-list>
          <button ion-button (click)="envoyerCmd(cmd)">Envoyer</button>
          {{ infoArduino }}
        </ion-card-content>
      </ion-card>
      <ion-card>
          <ion-list>
              <ion-item>
                <ion-label>Balisage: <ion-badge color="{{ colorBalEtat }}" item-end>{{ balInfoEtat }}</ion-badge></ion-label>
                <ion-toggle [(ngModel)]="baltoggle"></ion-toggle>
              </ion-item>
            </ion-list>
      </ion-card>
    </ion-card-content> </ion-card>

<ion-card>   <ion-card-header>
    Commandes   </ion-card-header>

  <ion-card-content>
    <ion-item>
      <button ion-button outline (click)="envoyerK()">
        Envoyer K
      </button>
      <button ion-button outline (click)="read()">
          Read
        </button>
        <button ion-button outline (click)="read1()">
            Read1
          </button>
    </ion-item>

    INFO 01: {{ message }} <br>
    INFO 02: {{ message2 }} <br>

  </ion-card-content>

</ion-card>

</ion-content>

<ion-footer>   <ion-toolbar>
    <p>{{ status }}</p>   </ion-toolbar> </ion-footer>

我在这个问题上停留了一段时间,但没有找到任何功能上的选择。如果您可以帮助我或给我正确的方向。

0 个答案:

没有答案