This commit is contained in:
2021-06-06 19:51:18 +02:00
commit d19f1a84e3
6 changed files with 309 additions and 0 deletions

39
include/README Normal file
View File

@@ -0,0 +1,39 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

46
lib/README Normal file
View File

@@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

15
platformio.ini Normal file
View File

@@ -0,0 +1,15 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
upload_port = COM[6]

27
src/.clang-format Normal file
View File

@@ -0,0 +1,27 @@
---
# We'll use defaults from the LLVM style, but with 4 columns indentation.
BasedOnStyle: LLVM
IndentWidth: 4
---
Language: Cpp
# Force pointers to the type for C++.
DerivePointerAlignment: false
PointerAlignment: Middle
AlignTrailingComments: true
BreakBeforeBraces: Custom
BraceWrapping:
BeforeElse: true
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: InlineOnly
AccessModifierOffset: -4
ColumnLimit: 100
---
Language: JavaScript
# Use 100 columns for JS.
ColumnLimit: 100
---
Language: Proto
# Don't format .proto files.
DisableFormat: true
...

171
src/main.cpp Normal file
View File

@@ -0,0 +1,171 @@
#include <Arduino.h>
/*Program to use GATT service on ESP32 to send Battery Level
* ESP32 works as server - Mobile as client
* Program by: B.Aswinth Raj
* Dated on: 13-10-2018
* Website: www.circuitdigest.com
*/
#include <BLE2902.h>
#include <BLEDevice.h>
#include <BLEServer.h> //Library to use BLE as server
#include <BLEUtils.h>
//#define TESTMODE 1
bool _BLEClientConnected = false;
#define AlarmService BLEUUID((uint16_t)0x1811) // ialert notification 0x1802
BLECharacteristic AlarmCharacteristic(BLEUUID((uint16_t)0x2A46), BLECharacteristic::PROPERTY_WRITE);
BLEDescriptor AlarmLevelDescriptor(BLEUUID((uint16_t)0x2901));
BLEServer * pServer = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer * pServer) {
deviceConnected = true;
_BLEClientConnected = true;
};
void onDisconnect(BLEServer * pServer) {
_BLEClientConnected = false;
deviceConnected = false;
}
};
// uint8_t toggle = 0;
uint8_t alarmFired = 0;
class MyAlarmCallback : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic * pCharacteristic) {
alarmFired = 1;
// if (toggle) {
// digitalWrite(32, HIGH);
// toggle = 0;
// }
// else {
// digitalWrite(32, LOW);
// toggle = 1;
// }
}
};
void InitBLE() {
BLEDevice::init("OilCheck");
// Create the BLE Server
BLEServer * pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create BLE Service
BLEService * pAlarm = pServer->createService(AlarmService);
pAlarm->addCharacteristic(&AlarmCharacteristic);
// AlarmLevelDescriptor.setValue("on/off");
// AlarmCharacteristic.addDescriptor(&AlarmLevelDescriptor);
AlarmCharacteristic.setCallbacks(new MyAlarmCallback);
AlarmCharacteristic.addDescriptor(new BLE2902());
pServer->getAdvertising()->addServiceUUID(AlarmService);
// pAlarm->
pAlarm->start();
// Start advertising
BLEAdvertising * pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(AlarmService);
pAdvertising->setScanResponse(true);
BLEDevice::startAdvertising();
}
volatile int ctr;
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR onTimer() {
portENTER_CRITICAL_ISR(&timerMux);
ctr++;
portEXIT_CRITICAL_ISR(&timerMux);
}
void setup() {
// upcounting timer 1MHz
timer = timerBegin(0, 40, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 100000, true);
timerAlarmEnable(timer);
// blink led
pinMode(32, OUTPUT);
// status led
pinMode(33, OUTPUT);
Serial.begin(115200);
Serial.println("Start BLE");
InitBLE();
}
std::string x;
int state = 0;
void loop() {
// BatteryLevelCharacteristic.setValue(&level, 1);
// BatteryLevelCharacteristic.notify();
// x = AlarmCharacteristic.getValue();
// occures every 100ms
if (ctr > 0) {
portENTER_CRITICAL(&timerMux);
ctr--;
portEXIT_CRITICAL(&timerMux);
#if defined TESTMODE
if (state % 2 == 0) {
digitalWrite(32, HIGH);
}
else {
digitalWrite(32, LOW);
}
state++;
#else
if (alarmFired) {
if (state % 2 == 0) {
digitalWrite(32, HIGH);
}
else {
digitalWrite(32, LOW);
}
state++;
}
#endif
if (state == 22) {
state = 0;
#if defined TESTMODE
ctr = -20;
#endif
alarmFired = 0;
}
}
if (deviceConnected) {
digitalWrite(33, HIGH);
// //pCharacteristic->setValue((uint8_t*)&value, 4);
// pCharacteristic->notify();
// value++;
delay(10); // bluetooth stack will go into congestion, if too many packets are sent, in 6
// hours test i was able to go as low as 3ms
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
digitalWrite(33, LOW);
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);
}

11
test/README Normal file
View File

@@ -0,0 +1,11 @@
This directory is intended for PlatformIO Unit Testing and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/page/plus/unit-testing.html