Commit c90bdaba authored by a-j.towse's avatar a-j.towse

Add new file

parents
#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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment