10.Oct.2023

The following Flutter codes can be used to create a mobile application that visualizes air quality data in JSON format received over a Bluetooth device. These codes guide Flutter app developers in exchanging data via Bluetooth, parsing JSON data, and displaying this data with line charts.

The main features of these codes are:
1. Connecting a Bluetooth Device: Flutter app can use Bluetooth libraries such as bluetooth_serial or flutter_blue to connect to the Bluetooth device. These codes handle the Bluetooth connection and provide a channel for data exchange.

2. JSON Data Retrieval: Data from the Bluetooth device must be in JSON format. The data is read via Bluetooth and parsed into JSON format. These codes parse incoming JSON data to obtain air quality data such as temperature, humidity, and CO2 concentration.

3. Creating Line Charts: Flutter application converts data into line charts using a chart drawing library such as fl_chart. A separate line chart is created for each type of data (e.g. temperature, humidity, CO2 concentration). These charts show users how data changes over time.

4. Data Streaming and Update: Data is continuously received via Bluetooth and line graphs are updated. The user can observe up-to-date air quality information.

These codes provide a basic structure for air quality monitoring applications and allow users to instantly track data received from Bluetooth devices. This type of app can be useful for users who want to monitor air quality and keep track of environmental conditions.

First, don't forget to add the required package to your pubspec.yaml file:

 

dependencies:
   flutter:
     sdk:flutter
   bluetooth_serial: ^0.5.4 # adding bluetooth_serial package
   fl_chart: ^0.40.0 # adding fl_chart package

import 'package:flutter/material.dart';
import 'package:bluetooth_serial/bluetooth_serial.dart';
import 'package:fl_chart/fl_chart.dart';

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

class MyApp extends StatelessWidget {
   @override
   Widget build(BuildContext context) {
     return MaterialApp(
       home:HomeScreen(),
     );
   }
}

class HomeScreen extends StatefulWidget {
   @override
   _HomeScreenState createState()=> _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
   BluetoothConnection? connection;
   String data='';
   List<double> _temperatureData=[];
   List<double> _humidityData=[];
   List<double> _co2Data=[];
   int _dataCount=0;

   @override
   void initState() {
     super.initState();
     _connectToBluetoothDevice();
   }

   Future<void> _connectToBluetoothDevice() async {
     BluetoothDevice? device=await BluetoothDevice.connect("YOUR_DEVICE_MAC_ADDRESS");

     if (device.isConnected) {
       connection=device.connection;
       connection!.input.listen((Uint8List data) {
         String receivedData=String.fromCharCodes(data);
         setState(() {
           data=receivedData;
         });

         try {
           Map<String, dynamic> jsonData=json.decode(receivedData);
           double temperature=jsonData["Temperature"];
           double humidity=jsonData["Humidity"];
           double co2=jsonData["CO2_Density"];
           setState(() {
             _temperatureData.add(temperature);
             _humidityData.add(humidity);
             _co2Data.add(co2);
             _dataCount++;
           });
         } catch (e) {
           print("JSON parsing error: $e");
         }
       });
     }
   }

   @override
   Widget build(BuildContext context) {
     return Scaffold(
       appBar: AppBar(
         title: Text('Air Quality Measurement'),
       ),
       body: Center(
         child: Column(
           mainAxisAlignment: MainAxisAlignment.center,
           children: <Widget>[
             data.isNotEmpty
                 ? LineChart(
                     LineChartData(
                       gridData: FlGridData(show: false),
                       titlesData: FlTitlesData(show: false),
                       borderData: FlBorderData(
                         show:true,
                         border: Border(
                           bottom: BorderSide(color: Colors.black, width: 2),
                           left: BorderSide(color: Colors.black, width: 2),
                         ),
                       ),
                       minX:0,
                       maxX: _dataCount.toDouble(),
                       minY:0,
                       maxY: 100,
                       lineBarsData: [
                         _createLineChartBar(_temperatureData, Colors.red),
                         _createLineChartBar(_humidityData, Colors.green),
                         _createLineChartBar(_co2Data, Colors.blue),
                       ],
                     ),
                   )
                 : Text('Connecting to Bluetooth device...'),
           ],
         ),
       ),
     );
   }

   LineChartBarData _createLineChartBar(List<double> data, Color color) {
     return LineChartBarData(
       spots: data.asMap().entries.map((entry) {
         return FlSpot(entry.key.toDouble(), entry.value);
       }).toList(),
       isCurved: true,
       colors: [color],
       dotData: FlDotData(show: false),
       belowBarData: BarAreaData(show: false),
     );
   }

   @override
   void dispose() {
     connection?.finish();
     super.dispose();
   }
}

İlgili Haberler

Mobile programming Fundamentals for Control Applications

Fundamentals of mobile application development for control of electronic systems

05.04.2023

With the Flutter-Dart language, can we write an artificial intelligence program that takes a picture and lists the objects in it?

Flutter-Dart language, artificial intelligence program

06.04.2023

Writing Applications that control Electronic Devices with Flutter-Dart Programming Language

Controlling Electronic Devices remotely

06.04.2023

Controlling Wifi Devices with Flutter-Dart

Control of remote devices with Android apps, Wifi-based control applications

07.04.2023

Developing an Application to Send Data to a Bluetooth-enabled Thermal Printer with Flutter

Mobile app developed with Flutter explains the step-by-step process of sending text to a Bluetooth-enabled thermal printer. Contains information about printer commands and Bluetooth communications

11.01.2024

Application Development Example with Flutter and Arduino

Learn to connect mobile devices with embedded systems! In this article, learn step by step how to develop an app using Flutter and Arduino

01.03.2024

Bluetooth Speaker Project with Flutter - Audio data transfer

In this project we will try to understand how the Bluetooth speaker system is designed and how to develop it. We will examine the basics of Android programming, MCU programming-embedded system design.

24.07.2023

Extracting Data from Database and Creating Graphs with Flutter and PHP

Learn how to pull data from a database and create a line chart using Flutter and PHP

27.08.2023

IoT System Design 1 – Temperature and Humidity Monitoring System

IoT system design with ESP 12f. Monitoring of temperature, humidity in web & mobile. Arduino, DHT11 sensor.

30.08.2023

IoT System Design 2- Sending Temperature and Humidity Data to Web Server with Arduino

Learn the steps to send temperature and humidity data from DHT11 sensor with Arduino to web server via ESP 12f

30.08.2023

IoT System Design 3- Data Processing on the Web Server Side

Learn to transmit data from DHT11 sensor with Arduino to web server via ESP8266 and save it to database with PHP.

30.08.2023

IoT System Design 4- Creating a Web Interface

Learn how data is pulled from the IoT system and used graphically.

30.08.2023

IoT System Design 5- Mobile Application Visualizing IoT Data with Flutter

Code descriptions of an application that pulls, graphs, and lists IoT data with Flutter.

30.08.2023

Mobile Application Development for Smart Homes

In this article, you can find the steps and examples of mobile application development using WiFi communication

01.09.2023

Developing Mobile Applications with Artificial Intelligence – Voltmeter Interface Application

The mobile application developed with artificial intelligence visualizes the microcontroller volt measurement with numerical data.

12.09.2023

Mobile Application Interface Development Study for Smart Homes

Ways to develop mobile applications with Flutter for smart home and workplace systems

16.09.2023

Designing an Air Quality Measurement System 1 – Basic definitions of Air Quality

Air Quality Measurement System design and air quality parameters. PM2.5, CO, NO2, O3, SO2 measurement

02.10.2023

Designing an Air Quality Measurement System 2- MQ-135 Gas Sensor Review

MQ-135 Gas Sensor: A powerful sensor used to monitor air quality and detect gases. Offers precise measurement

02.10.2023

Designing an Air Quality Measurement System 3 - Measurement with MQ-135 and DHT-11

Designing an Air Quality Measurement System - Measurement with MQ-135 and DHT-11.

10.10.2023