Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
IOT Final Project
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
a-j.towse
IOT Final Project
Commits
c90bdaba
Commit
c90bdaba
authored
Jan 10, 2024
by
a-j.towse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parents
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
105 additions
and
0 deletions
+105
-0
Temp_sensor
Temp_sensor
+105
-0
No files found.
Temp_sensor
0 → 100644
View file @
c90bdaba
#include <WiFiNINA.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ArduinoBLE.h>
#include "arduino_secrets.h"
#include "ThingSpeak.h"
//define globals from secrets tab
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
char Uuid[] = "19b10000-e8f2-537e-4f6c-d104768a1214";
WiFiClient client;
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
//start sensor and bluetooth proccess
sensors.begin();
BLE.begin();
//Scan for target ble
BLE.scanForUuid(Uuid);
//Initialize ThingSpeak
ThingSpeak.begin(client);
}
void loop() {
// Send the command to get temperatures
sensors.requestTemperatures();
//Get temp from request assign to float
float tempC = sensors.getTempCByIndex(0);
//assign peripheral variable to connected ble device
BLEDevice peripheral = BLE.available();
//check temperature reading is not error
if (tempC != DEVICE_DISCONNECTED_C) {
// Connect or reconnect to WiFi
if (WiFi.status() != WL_CONNECTED) {
Serial.println(SECRET_SSID);
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
delay(6000);
}
}
//write temperature to thingspeak field 1
int x = ThingSpeak.writeField(2394292, 1, tempC, "4AX6F1QBBZK9XIK7");
}
//check peripheral connection exists
if (peripheral) {
//check peripheral name matches LED
if (peripheral.localName() != "LED") {
return;
}
//Stop looking for ble device
BLE.stopScan();
//call controllLed function
controlLed(peripheral, tempC);
//scan for ble device with Uuid
BLE.scanForUuid(Uuid);
}
delay(15000);
}
//function for writing temp to ble device
void controlLed(BLEDevice peripheral, float tempC) {
// connect to the peripheral
if (peripheral.connect()) {
} else {
Serial.println("Failed to connect!");
return;
}
// discover peripheral attributes
if (peripheral.discoverAttributes()) {
} else {
peripheral.disconnect();
return;
}
// retrieve the LED characteristic
BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");
//if led characteristic not exist or not write not exist disconnect from ble device
if (!ledCharacteristic) {
peripheral.disconnect();
return;
} else if (!ledCharacteristic.canWrite()) {
peripheral.disconnect();
return;
}
//if ble device connected write rounded int of temp
if (peripheral.connected()) {
//ledCharacteristic.writeValue((byte)0x01);
int tempCInt = round(tempC);
ledCharacteristic.writeValue(tempCInt);
}
}
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