forked from 0015/ThatProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ESP32 | FLUTTER | BLE - Dust Sensor, A simple Air Monitor App (with G…
…raph)
- Loading branch information
Eric
committed
Sep 28, 2019
1 parent
3d03daa
commit 419e44d
Showing
64 changed files
with
2,706 additions
and
0 deletions.
There are no files selected for viewing
196 changes: 196 additions & 0 deletions
196
Esp32_dust_sensor_ble_FLUTTER/Esp32_dust_sensor_test/Esp32_dust_sensor_test.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
/********************************************************************************************************* | ||
* | ||
* File : DustSensor | ||
* Hardware Environment: | ||
* Build Environment : Arduino | ||
* Version : V1.0.5-r2 | ||
* By : WaveShare | ||
* | ||
* (c) Copyright 2005-2011, WaveShare | ||
* http://www.waveshare.net | ||
* http://www.waveshare.com | ||
* All Rights Reserved | ||
* | ||
*********************************************************************************************************/ | ||
#define COV_RATIO 0.2 //ug/mmm / mv | ||
#define NO_DUST_VOLTAGE 400 //mv | ||
#define SYS_VOLTAGE 5000 | ||
|
||
#include <BLEDevice.h> | ||
#include <BLEServer.h> | ||
#include <BLEUtils.h> | ||
#include <BLE2902.h> | ||
|
||
BLEServer* pServer = NULL; | ||
BLECharacteristic* pCharacteristic = NULL; | ||
bool deviceConnected = false; | ||
bool oldDeviceConnected = false; | ||
|
||
|
||
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" | ||
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" | ||
|
||
|
||
class MyServerCallbacks: public BLEServerCallbacks { | ||
void onConnect(BLEServer* pServer) { | ||
deviceConnected = true; | ||
BLEDevice::startAdvertising(); | ||
}; | ||
|
||
void onDisconnect(BLEServer* pServer) { | ||
deviceConnected = false; | ||
} | ||
}; | ||
|
||
|
||
/* | ||
I/O define | ||
*/ | ||
const int iled = 12; //drive the led of sensor | ||
const int vout = 36; //analog input | ||
|
||
/* | ||
variable | ||
*/ | ||
float density, voltage; | ||
int adcvalue; | ||
|
||
/* | ||
private function | ||
*/ | ||
int Filter(int m) | ||
{ | ||
static int flag_first = 0, _buff[10], sum; | ||
const int _buff_max = 10; | ||
int i; | ||
|
||
if(flag_first == 0) | ||
{ | ||
flag_first = 1; | ||
|
||
for(i = 0, sum = 0; i < _buff_max; i++) | ||
{ | ||
_buff[i] = m; | ||
sum += _buff[i]; | ||
} | ||
return m; | ||
} | ||
else | ||
{ | ||
sum -= _buff[0]; | ||
for(i = 0; i < (_buff_max - 1); i++) | ||
{ | ||
_buff[i] = _buff[i + 1]; | ||
} | ||
_buff[9] = m; | ||
sum += _buff[9]; | ||
|
||
i = sum / 10.0; | ||
return i; | ||
} | ||
} | ||
|
||
|
||
void setup(void) | ||
{ | ||
pinMode(iled, OUTPUT); | ||
digitalWrite(iled, LOW); //iled default closed | ||
|
||
Serial.begin(115200); //send and receive at 115200 baud | ||
Serial.print("*********************************** WaveShare ***********************************\n"); | ||
|
||
// Create the BLE Device | ||
BLEDevice::init("ESP32 THAT PROJECT"); | ||
|
||
// Create the BLE Server | ||
pServer = BLEDevice::createServer(); | ||
pServer->setCallbacks(new MyServerCallbacks()); | ||
|
||
// Create the BLE Service | ||
BLEService *pService = pServer->createService(SERVICE_UUID); | ||
|
||
// Create a BLE Characteristic | ||
pCharacteristic = pService->createCharacteristic( | ||
CHARACTERISTIC_UUID, | ||
BLECharacteristic::PROPERTY_READ | | ||
BLECharacteristic::PROPERTY_WRITE | | ||
BLECharacteristic::PROPERTY_NOTIFY | | ||
BLECharacteristic::PROPERTY_INDICATE | ||
); | ||
|
||
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml | ||
// Create a BLE Descriptor | ||
pCharacteristic->addDescriptor(new BLE2902()); | ||
|
||
// Start the service | ||
pService->start(); | ||
|
||
// Start advertising | ||
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); | ||
pAdvertising->addServiceUUID(SERVICE_UUID); | ||
pAdvertising->setScanResponse(false); | ||
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter | ||
BLEDevice::startAdvertising(); | ||
Serial.println("Waiting a client connection to notify..."); | ||
} | ||
|
||
void loop(void) | ||
{ | ||
/* | ||
get adcvalue | ||
*/ | ||
digitalWrite(iled, HIGH); | ||
delayMicroseconds(280); | ||
adcvalue = analogRead(vout); | ||
digitalWrite(iled, LOW); | ||
|
||
adcvalue = Filter(adcvalue); | ||
|
||
/* | ||
covert voltage (mv) | ||
*/ | ||
voltage = (SYS_VOLTAGE / 1024.0) * adcvalue * 11; | ||
|
||
/* | ||
voltage to density | ||
*/ | ||
if(voltage >= NO_DUST_VOLTAGE) | ||
{ | ||
voltage -= NO_DUST_VOLTAGE; | ||
|
||
density = voltage * COV_RATIO; | ||
} | ||
else | ||
density = 0; | ||
|
||
/* | ||
display the result | ||
*/ | ||
Serial.print("The current dust concentration is: "); | ||
Serial.print(density); | ||
Serial.print(" ug/m3\n"); | ||
|
||
|
||
// notify changed value | ||
if (deviceConnected) { | ||
String str = ""; | ||
str += density; | ||
|
||
pCharacteristic->setValue((char*)str.c_str()); | ||
pCharacteristic->notify(); | ||
} | ||
// disconnecting | ||
if (!deviceConnected && oldDeviceConnected) { | ||
delay(500); // give the bluetooth stack the chance to get things ready | ||
pServer->startAdvertising(); // restart advertising | ||
Serial.println("start advertising"); | ||
oldDeviceConnected = deviceConnected; | ||
} | ||
// connecting | ||
if (deviceConnected && !oldDeviceConnected) { | ||
// do stuff here on connecting | ||
oldDeviceConnected = deviceConnected; | ||
} | ||
|
||
delay(500); | ||
} |
36 changes: 36 additions & 0 deletions
36
Esp32_dust_sensor_ble_FLUTTER/flutter_app_esp32_dust_sensor/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Miscellaneous | ||
*.class | ||
*.log | ||
*.pyc | ||
*.swp | ||
.DS_Store | ||
.atom/ | ||
.buildlog/ | ||
.history | ||
.svn/ | ||
|
||
# IntelliJ related | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea/ | ||
|
||
# The .vscode folder contains launch configuration and tasks you configure in | ||
# VS Code which you may wish to be included in version control, so this line | ||
# is commented out by default. | ||
#.vscode/ | ||
|
||
# Flutter/Dart/Pub related | ||
**/doc/api/ | ||
.dart_tool/ | ||
.flutter-plugins | ||
.packages | ||
.pub-cache/ | ||
.pub/ | ||
/build/ | ||
|
||
# Web related | ||
lib/generated_plugin_registrant.dart | ||
|
||
# Exceptions to above rules. | ||
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages |
10 changes: 10 additions & 0 deletions
10
Esp32_dust_sensor_ble_FLUTTER/flutter_app_esp32_dust_sensor/.metadata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This file tracks properties of this Flutter project. | ||
# Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
# | ||
# This file should be version controlled and should not be manually edited. | ||
|
||
version: | ||
revision: cc3ca9a916cb1da851a1f36432154a534787da99 | ||
channel: dev | ||
|
||
project_type: app |
1 change: 1 addition & 0 deletions
1
Esp32_dust_sensor_ble_FLUTTER/flutter_app_esp32_dust_sensor/Pods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ios/Pods |
16 changes: 16 additions & 0 deletions
16
Esp32_dust_sensor_ble_FLUTTER/flutter_app_esp32_dust_sensor/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# flutter_app_esp32_dust_sensor | ||
|
||
A new Flutter application. | ||
|
||
## Getting Started | ||
|
||
This project is a starting point for a Flutter application. | ||
|
||
A few resources to get you started if this is your first Flutter project: | ||
|
||
- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab) | ||
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook) | ||
|
||
For help getting started with Flutter, view our | ||
[online documentation](https://flutter.dev/docs), which offers tutorials, | ||
samples, guidance on mobile development, and a full API reference. |
7 changes: 7 additions & 0 deletions
7
Esp32_dust_sensor_ble_FLUTTER/flutter_app_esp32_dust_sensor/android/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
gradle-wrapper.jar | ||
/.gradle | ||
/captures/ | ||
/gradlew | ||
/gradlew.bat | ||
/local.properties | ||
GeneratedPluginRegistrant.java |
67 changes: 67 additions & 0 deletions
67
Esp32_dust_sensor_ble_FLUTTER/flutter_app_esp32_dust_sensor/android/app/build.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
def localProperties = new Properties() | ||
def localPropertiesFile = rootProject.file('local.properties') | ||
if (localPropertiesFile.exists()) { | ||
localPropertiesFile.withReader('UTF-8') { reader -> | ||
localProperties.load(reader) | ||
} | ||
} | ||
|
||
def flutterRoot = localProperties.getProperty('flutter.sdk') | ||
if (flutterRoot == null) { | ||
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") | ||
} | ||
|
||
def flutterVersionCode = localProperties.getProperty('flutter.versionCode') | ||
if (flutterVersionCode == null) { | ||
flutterVersionCode = '1' | ||
} | ||
|
||
def flutterVersionName = localProperties.getProperty('flutter.versionName') | ||
if (flutterVersionName == null) { | ||
flutterVersionName = '1.0' | ||
} | ||
|
||
apply plugin: 'com.android.application' | ||
apply plugin: 'kotlin-android' | ||
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" | ||
|
||
android { | ||
compileSdkVersion 28 | ||
|
||
sourceSets { | ||
main.java.srcDirs += 'src/main/kotlin' | ||
} | ||
|
||
lintOptions { | ||
disable 'InvalidPackage' | ||
} | ||
|
||
defaultConfig { | ||
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). | ||
applicationId "com.thatproject.flutter_app_esp32_dust_sensor" | ||
minSdkVersion 19 | ||
targetSdkVersion 28 | ||
versionCode flutterVersionCode.toInteger() | ||
versionName flutterVersionName | ||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
// TODO: Add your own signing config for the release build. | ||
// Signing with the debug keys for now, so `flutter run --release` works. | ||
signingConfig signingConfigs.debug | ||
} | ||
} | ||
} | ||
|
||
flutter { | ||
source '../..' | ||
} | ||
|
||
dependencies { | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" | ||
testImplementation 'junit:junit:4.12' | ||
androidTestImplementation 'androidx.test:runner:1.1.1' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' | ||
} |
7 changes: 7 additions & 0 deletions
7
...ensor_ble_FLUTTER/flutter_app_esp32_dust_sensor/android/app/src/debug/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.thatproject.flutter_app_esp32_dust_sensor"> | ||
<!-- Flutter needs it to communicate with the running application | ||
to allow setting breakpoints, to provide hot reload, etc. | ||
--> | ||
<uses-permission android:name="android.permission.INTERNET"/> | ||
</manifest> |
33 changes: 33 additions & 0 deletions
33
...sensor_ble_FLUTTER/flutter_app_esp32_dust_sensor/android/app/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.thatproject.flutter_app_esp32_dust_sensor"> | ||
|
||
<!-- io.flutter.app.FlutterApplication is an android.app.Application that | ||
calls FlutterMain.startInitialization(this); in its onCreate method. | ||
In most cases you can leave this as-is, but you if you want to provide | ||
additional functionality it is fine to subclass or reimplement | ||
FlutterApplication and put your custom class here. --> | ||
<application | ||
android:name="io.flutter.app.FlutterApplication" | ||
android:label="flutter_app_esp32_dust_sensor" | ||
android:icon="@mipmap/ic_launcher"> | ||
<activity | ||
android:name=".MainActivity" | ||
android:launchMode="singleTop" | ||
android:theme="@style/LaunchTheme" | ||
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" | ||
android:hardwareAccelerated="true" | ||
android:windowSoftInputMode="adjustResize"> | ||
<!-- This keeps the window background of the activity showing | ||
until Flutter renders its first frame. It can be removed if | ||
there is no splash screen (such as the default splash screen | ||
defined in @style/LaunchTheme). --> | ||
<meta-data | ||
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame" | ||
android:value="true" /> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN"/> | ||
<category android:name="android.intent.category.LAUNCHER"/> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
</manifest> |
13 changes: 13 additions & 0 deletions
13
...android/app/src/main/kotlin/com/thatproject/flutter_app_esp32_dust_sensor/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.thatproject.flutter_app_esp32_dust_sensor | ||
|
||
import android.os.Bundle | ||
|
||
import io.flutter.app.FlutterActivity | ||
import io.flutter.plugins.GeneratedPluginRegistrant | ||
|
||
class MainActivity: FlutterActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
GeneratedPluginRegistrant.registerWith(this) | ||
} | ||
} |
Oops, something went wrong.