Android Telefonla Direnç Ölçmek
Android Telefonla Direnç Ölçmek

Android Telefonla Direnç Ölçmek

Bu uygulamada direnç bilgisini ölçüp bluetooth modül aracılığıyla Android telefona göndermeyi inceleyeceğiz.

Multimetrelerdeki direnç ölçme işlemi aslında bir gerilim ölçme işlemidir. Arduino’ya bağlı dirençli bağlantı şemasında da görüleceği gibi, değeri bilinmeyen direnç devreye bağladığında üzerinde bir gerilim oluşur. Direncin değeri ne kadar büyükse üzerindeki gerilim de o derece büyüktür. Direnç değeri ise şu kodlarla hesaplanır:

float volt_x = analogRead(A0);// Vin INPUT

   float volt_in=5*(volt_x/1023);

  

   float RX=(volt_in*100000)/(5-volt_in);  //------------>>>>>>  resistance formula

   float RX_divided=RX/1000;    // ohm ----> K OHM

  

AnalogRead komutuyla okunan değer formülde yerine konursa direnç değeri bulunur.

Bu değeri de öncelikle string değişken tipine, sonra ise dizi-char tipine dönüştürüyoruz. Ve aşağıdaki komut satırıyla bluetooth modüle gönderiyoruz.

resistor_string=String(RX_divided);

       resistor_string=  resistor_string+" K ohm";

     resistor_string.toCharArray(Buf, 50);

     mySerial.write(Buf);

Android programla için Flutter-Dart kodları kullanıldı. Flutter-Dart ve Arduino kodları aşağıda.

Flutter-Dart Kodları:

import 'dart:async';

import 'dart:convert';

//import 'dart:convert';

 

import 'package:flutter/foundation.dart';

import 'package:flutter/material.dart';

import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';

 

void main() => runApp(const MyApp());

 

class MyApp extends StatefulWidget {

  const MyApp({super.key});

 

  @override

  // ignore: library_private_types_in_public_api

  _MyAppState createState() => _MyAppState();

}

 

class _MyAppState extends State<MyApp> {

  List<BluetoothDevice> _devices = [];

  late BluetoothConnection connection;

  String adr = "00:21:07:00:50:69"; // my bluetooth device MAC Adres

  String recData = "0 ohm";

 

  late Timer _timer;

  String _timeString = "";

 

  @override

  void initState() {

    // _startTimer();  automatic

    super.initState();

    _loadDevices();

  }

 

 

  Future<void> _loadDevices() async {

    List<BluetoothDevice> devices = await FlutterBluetoothSerial.instance

        .getBondedDevices();

 

    setState(() {

      _devices = devices;

    });

  }

 

 

  //----------------------------

  Future<void> sendData(String data) async {

    data = data.trim();

    try {

      List<int> list = data.codeUnits;

      Uint8List bytes = Uint8List.fromList(list);

      connection.output.add(bytes);

      await connection.output.allSent;

      if (kDebugMode) {

        // print('Data sent successfully');

      }

    } catch (e) {

      //print(e.toString());

    }

  }

 

  // data RECEIVED --------------

 

  Future<void> receiveData() async {

    connection.input!.listen((Uint8List data) {

      //Data entry point

      setState(() {

        recData=ascii.decode(data);

        //var n1 = int.parse('-42');

      });

    });

  }

  //--------------------------------------

 

// TIMER START-----------

  void _startTimer() {

    _timer = Timer.periodic(Duration(milliseconds: 500), (timer) {

      receiveData();

 

    });

  }

 

  // TIMER STOP--------------------------------------

  Future<void> _stopTimer() async {

    setState(() {

    });

    _timer.cancel();

  }

 

//---------------------------------------------

  @override

  void dispose() {

    _timer.cancel();

    super.dispose();

  }

 

 

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

        home: Scaffold(

          appBar: AppBar(

            title: const Text("OHMMeter with BlueTooth"),

          ),

          body: Center(

            child: Column(

              mainAxisAlignment: MainAxisAlignment.center,

              children: [

                const Text("MAC Adress: 00:21:07:00:50:69"),

 

                ElevatedButton(child: Text("Connect"), onPressed: () {

                  connect(adr);

                },),

 

                const SizedBox(height: 30.0,),

 

                const Text("Receive Data: ",style: TextStyle(fontSize: 55.0),),

                Text(recData,style: TextStyle(fontSize: 45.0),),

 

                const SizedBox(height: 10.0,),

               // Text(_timeString),

                const SizedBox(height: 10.0,),

 

                ElevatedButton(child: Text("Stop timer"), onPressed: () {

                  _stopTimer();

                },),

                const SizedBox(height: 10.0,),

 

                ElevatedButton(child: Text("Start timer"), onPressed: () {

                  _startTimer();

                },),

 

              ],

            ),

          ),

 

        )

 

    );

  }

 

  Future connect(String address) async {

    try {

      connection = await BluetoothConnection.toAddress(address);

     // sendData('111');

      //durum="Connected to the device";

 

    } catch (exception) {

      // durum="Cannot connect, exception occured";

    }

  }

 

  @override

  void debugFillProperties(DiagnosticPropertiesBuilder properties) {

    super.debugFillProperties(properties);

  }

 

// --------------**************data gonder

  //Future send(Uint8List data) async {

    //connection.output.add(data);

   // await connection.output.allSent;

 // }

 

}

//------------*********** data gonder end

 

 

 

 

Arduino codes:

#include <SoftwareSerial.h>

 

SoftwareSerial mySerial(10, 11); // RX, TX

 char Buf[50]; //  to bluetooth device<------

String resistor_string="";

 

 

void setup() { 

 // hc-05 bluetooth module resistor-----------------------------

 

  // Open serial communications and wait for port to open:

  Serial.begin(9600);

  mySerial.begin(9600);

 }

 

void loop() {

   float volt_x = analogRead(A0);// Vin INPUT

   float volt_in=5*(volt_x/1023);

  

   float RX=(volt_in*100000)/(5-volt_in);  //------------>>>>>>  resistance formula

   float RX_divided=RX/1000;    // ohm ----> K OHM

     

     resistor_string=String(RX_divided);

       resistor_string=  resistor_string+" K ohm";

     resistor_string.toCharArray(Buf, 50);

     mySerial.write(Buf);

     delay(200);

     }