Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2022-018
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2022-018
2022-018
Commits
51fec958
Commit
51fec958
authored
Nov 13, 2022
by
Peellapitiya D I IT19111902
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
node mcu 32 and dht 11
parent
30966d30
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
125 additions
and
0 deletions
+125
-0
sketch_nov12a_esp_32.ino
sketch_nov12a_esp_32.ino
+125
-0
No files found.
sketch_nov12a_esp_32.ino
0 → 100644
View file @
51fec958
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT11
DHT
dht
(
DHTPIN
,
DHTTYPE
);
BLEServer
*
pServer
=
NULL
;
BLECharacteristic
*
pCharacteristic
=
NULL
;
bool
deviceConnected
=
false
;
bool
oldDeviceConnected
=
false
;
uint32_t
value
=
0
;
float
prev_temp
;
float
prev_humidity
;
#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
;
}
};
void
updateTemp
(
float
temp
){
if
(
prev_temp
!=
temp
){
String
tempString
=
""
;
tempString
+=
(
int
)
temp
;
tempString
+=
"C"
;
prev_temp
=
temp
;
}
}
void
updateHumidity
(
float
humidity
){
if
(
prev_humidity
!=
humidity
){
String
humidityString
=
""
;
humidityString
+=
(
int
)
humidity
;
humidityString
+=
"%"
;
prev_humidity
=
humidity
;
}
}
void
setup
()
{
Serial
.
begin
(
115200
);
dht
.
begin
();
// Create the BLE Device
BLEDevice
::
init
(
"ESP32 Test"
);
// 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
);
// 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
()
{
updateTemp
(
dht
.
readTemperature
());
updateHumidity
(
dht
.
readHumidity
());
// notify changed value
if
(
deviceConnected
)
{
String
str
=
""
;
str
+=
prev_temp
;
str
+=
","
;
str
+=
prev_humidity
;
pCharacteristic
->
setValue
((
char
*
)
str
.
c_str
());
pCharacteristic
->
notify
();
Serial
.
println
(
prev_humidity
);
Serial
.
println
(
F
(
"% Temperature: "
));
Serial
.
println
(
prev_temp
);
}
// disconnecting Serial.println("start advertising");
oldDeviceConnected
=
deviceConnected
;
if
(
!
deviceConnected
&&
oldDeviceConnected
)
{
delay
(
500
);
// give the bluetooth stack the chance to get things ready
pServer
->
startAdvertising
();
// restart advertising
}
// connecting
if
(
deviceConnected
&&
!
oldDeviceConnected
)
{
// do stuff here on connecting
oldDeviceConnected
=
deviceConnected
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment