We make temperature and humidity detection system with Android device
We make temperature and humidity detection system with Android device
We make temperature and humidity detection system with Android device

We make temperature and humidity detection system with Android device

We will make an application with the DHT-11 temperature and humidity sensing module. We will make this system with the DHT-11 module connected to the microcontroller system and one HC-05 bluetooth module. Also, we will make an Android application using the Flutter-Dart programming language.

You can watch the details of the application on our YouTube channel:

In the microcontroller system, we read the data from the DHT-11 module from digital pin 2. And we convert this numeric value to string type. We convert this new string type data into an array-char type value. This value is sent to the Bluetooth module with the myserial.write command.


On the Android application side, we get the value of this char type with the receiveData function. This function is included in the timer function.
At certain time intervals, this value is read and written to the text object.

 

Flutter-Dart codes are given below:

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 = "....";

  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("----Temperature and Humidity Meter 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("Temperature-Humidity: ",style: TextStyle(fontSize: 55.0),),
                Text(recData,style: TextStyle(fontSize: 40.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

 

Microcontroller codes-Arduino:


#include <SoftwareSerial.h>
#include <dht11.h> 

SoftwareSerial mySerial(10, 11); // RX, TX
#define DHT11PIN 2 //  

char Buf[100]; //  to bluetooth device<------
String values_string="--";

dht11 DHT11;

void setup()
{
  Serial.begin(9600); // 
  mySerial.begin(9600);
  
}

void loop()
{
  
               int chk = DHT11.read(DHT11PIN);
    
  float humd=(float)DHT11.humidity;
  float temp=(float)DHT11.temperature;
  int fahr=DHT11.fahrenheit();
  int kelv=DHT11.kelvin();
    
  
  values_string="Humd:%"+String(humd)+"  Temp:" + String(temp)+"C  Fahr:" + String(fahr)+"F  Kelv:"+ String(kelv)+"K"; 
     
     values_string.toCharArray(Buf, 100);
     mySerial.write(Buf); 

     Serial.println(values_string);
  delay(1000);
  
}