#include #include #include #include "ArduinoTimer.h" #define Type DHT22 ArduinoTimer Timer1; int thsens= 4; //using D4 pin on NANO DHT HT(thsens,Type); //connect to DHT22 float hum; float tempC; float tempF; int delaytime=500; //delay time in milliseconds int dt=1000; const int ledPin = LED_BUILTIN; // pin to use for the LED long previousMillis = 0; // last time the battery level was checked, in ms BLEService SCGService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service // Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite | BLENotify); BLEByteCharacteristic humCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite | BLENotify); //************************************************************************************* void setup() { Serial.begin(9600); HT.begin(); while (Serial); //does not wait for PC to open serial port // set LED pin to output mode pinMode(thsens, INPUT); //set up pin for input or output this case //we are receiving data from sensor so input // set LED pin to output mode pinMode(ledPin, OUTPUT); // begin initialization if (!BLE.begin()) { Serial.println("starting Bluetooth® Low Energy module failed!"); while (1); } // set advertised local name and service UUID: BLE.setLocalName("SCG"); BLE.setAdvertisedService(SCGService); // add the characteristic to the service SCGService.addCharacteristic(switchCharacteristic); SCGService.addCharacteristic(humCharacteristic); // add service BLE.addService(SCGService); // set the initial value for the characeristic: this might need to be changes to read switchCharacteristic.writeValue(0); humCharacteristic.writeValue(0); // start advertising BLE.advertise(); // Serial.println("BLE SCG Peripheral"); } //**************************************************************************************** void loop() { // listen for Bluetooth® Low Energy peripherals to connect: BLEDevice central = BLE.central(); // if a central is connected to peripheral: if (central) { //Serial.print("Connected to central: "); // print the central's MAC address: //Serial.println(central.address()); // tempF=HT.readTemperature(true); // Serial.print(tempF); // Serial.println(" F"); // switchCharacteristic.writeValue(tempF); // while the central is still connected to peripheral: while (central.connected()) { // if the remote device wrote to the characteristic, // use the value to control the LED: long currentMillis = millis(); // if 200ms have passed, check measuretemp. if (currentMillis - previousMillis >= 2000) { previousMillis = currentMillis; MeasureTemperature(); } if (switchCharacteristic.written()) { if (switchCharacteristic.value()) { // any value other than 0 //Serial.println("LED on"); digitalWrite(ledPin, HIGH); // will turn the LED on } else { // a 0 value //Serial.println(F("LED off")); digitalWrite(ledPin, LOW); // will turn the LED off } } } } } //End void loop void MeasureTemperature(){ hum=HT.readHumidity(); tempF=HT.readTemperature(true); Serial.println((int)tempF); switchCharacteristic.writeValue(tempF); //Serial.print(moisture); //Serial.print("|"); Serial.println((int)hum); humCharacteristic.writeValue(hum); }