/********* Rui Santos Complete project details at https://randomnerdtutorials.com *********/ //Modified by Kishore Gaikwad on 01-05-2022 // Load libraries #include "BluetoothSerial.h" #include #include // Check if Bluetooth configs are enabled #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it #endif // Bluetooth Serial object BluetoothSerial SerialBT; // GPIO where LED is connected to const int RelayPin = 26; // GPIO where the DS18B20 is connected to const int oneWireBus = 12; // Setup a oneWire instance to communicate with any OneWire devices OneWire oneWire(oneWireBus); // Pass our oneWire reference to Dallas Temperature sensor DallasTemperature sensors(&oneWire); // Handle received and sent messages String message = ""; char incomingChar; String temperatureString = ""; // Timer: auxiliar variables unsigned long previousMillis = 0; // Stores last time temperature was published const long interval = 10000; // interval at which to publish sensor readings void setup() { pinMode(RelayPin, OUTPUT); Serial.begin(115200); // Bluetooth device name SerialBT.begin("ESP32"); Serial.println("The device started, now you can pair it with bluetooth!"); } void loop() { unsigned long currentMillis = millis(); // Send temperature readings if (currentMillis - previousMillis >= interval){ previousMillis = currentMillis; sensors.requestTemperatures(); temperatureString = String(sensors.getTempCByIndex(0)) + "C " + String(sensors.getTempFByIndex(0)) + "F"; SerialBT.println(temperatureString); } // Read received messages (LED control command) if (SerialBT.available()){ char incomingChar = SerialBT.read(); if (incomingChar != '\n'){ message += String(incomingChar); } else{ message = ""; } Serial.write(incomingChar); } // Check received message and control output accordingly if (message =="Relay_on"){ digitalWrite(RelayPin, LOW); } else if (message =="Relay_off"){ digitalWrite(RelayPin, HIGH); } delay(20); }