Bluetooth Input Reading and Setting Time over Bluetooth

Hey Everyone,
So I have a project in which I want to be able to set the time over Bluetooth. I am using an esp32 module and I am using the NTP protocol in order to retrieve the time and I am using a Bluetooth Serial Monitor in order to send the time to the Arduino. If the NTP time equals the time that I input from the Bluetooth serial monitor, then the relay should turn on. So far I haven't had any success with the relay actually turning on but its not an issue with the relay in terms of hardware so I thought it was something in the code. I will link the entire code below for my project but the part where the problem is located is in "void turnOnLight". I am a beginner to Arduino and I know mainly just basic commands and not too complicated ones so if any descriptions that you guys give could be sorta tuned to make sense to a beginner that would be nice.

Project Code.ino (4.1 KB)

It would help us if You post a system drawing giving a large overview of the system.
Links to data sheet of special components are also appreciated.

I have a relay connected to digital pin 25 and a 0.96" OLED screen connected to the digital 21 and 22. I have a Wide Angle PIR Sensor connected to digital pin 34 on my esp32 board (the wide angle PIR sensor functions the same as a normal one). The concept of my project is to be able to set the time over bluetooth for my relay to turn on. When it does the PIR sensor will detect if there is motion and it will turn the light on or off depending on its state. The OLED Screen just displays the time and the state of the light.

Please read the advice in "How to use the Forum". Post the code here. Then a lot more helpers can read it.
Post a link to the data sheet of that relay. They often cause problem.
Post a wiring diagram. Trouble with poor power is too common.....

Im using straight wall power for the relay and a basic blink command works with the relay. I also linked the code in a file in the first comment, I have linked it again underneath this comment.

Project Code.ino (4.1 KB)

rushildasari:
I also linked the code in a file in the first comment, I have linked it again underneath this comment.

That is not the way told in topics like "How to use the Forum". Helpers using smart phones or tablets have no IDE, can't download Your code. You are cutting out a lot of helpers.
Your choice....

I have copy pasted the entire code into this comment
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "time.h"
#include <WiFi.h>
#include "BluetoothSerial.h"
#define SCREEN_WIDTH 128 // OLED display width
#define SCREEN_HEIGHT 64 // OLED display height in pixels
#define OLED_RESET -1 // OLED Reset Pin
#define SCREEN_ADDRESS 0x3C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

BluetoothSerial SerialBT;

// 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

const char* ssid = "dasari-5g-guest";
const char* password = "dasar1h0m3";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = -18000;
const int daylightOffset_sec = 3600;

String message = "";
char incomingChar;

int onhour;
int onminute;
int offhour = onhour;
int offminute = onminute + 1;
byte HOUR;
byte MIN;

int pir = digitalRead(34);

void setup() {
pinMode(34, INPUT_PULLUP); //PIR Sensor
pinMode(25, OUTPUT); //Relay
Serial.begin(115200);

// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}

display.clearDisplay(); //Clears the Splash Screen

SerialBT.begin("ESP32");
Serial.println("The device started, now you can pair it with bluetooth!");

//connect to WiFi
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("CONNECTED");

//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();

//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}

void loop() {

printLocalTime();
turnOnLight();
}

void turnOnLight() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}

if (SerialBT.available()) {
char incomingChar = SerialBT.read();
if (incomingChar != '\n') {
message += String(incomingChar);
}
else {
message = "";
}

if (message.length() == 5) {
int onminute = message.substring(3, 5).toInt();
Serial.println(onminute);
int onhour = message.substring(0, 2).toInt();
Serial.println(onhour);
}
}

HOUR = timeinfo.tm_hour;
MIN = timeinfo.tm_min;

if (HOUR == onhour && MIN == onminute) {
Serial.println("SANITIZING");
digitalWrite(25, HIGH);
if (pir == LOW) {
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 30);
display.print("SANITIZING");
Serial.println("SANITIZING");
delay(300);
}

else {
digitalWrite(25, LOW);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 30);
display.print("MOTION DETECTED!");
Serial.println("MOTION DETECTED!");
delay(300);
}
display.display();
display.clearDisplay();
}
else if (HOUR == offhour && MIN == offminute) {
Serial.println("LIGHT OFF");
digitalWrite(25, LOW);
}

}

void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}

Serial.println(&timeinfo, "%A, %B %d %H:%M:%S");

display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print(&timeinfo, "%A, %B %d");

display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.print(&timeinfo, "%H:%M:%S");

if (HOUR != onhour && HOUR != offhour && MIN != offminute && MIN != onminute) {
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 30);
display.print("IDLE");
Serial.println("IDLE");
}

display.display();
display.clearDisplay();

}

How hard can it be to read instructions? "How to use the Forum". Search for CODE TAGS!
Then the code is much more handy to read, and easy to copy into IDE without wasting disc space.

Im very sorry, As i said i am a beginner to both using the forum and Arduino so im very sorry if i do something wrong.
heres the code

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "time.h"
#include <WiFi.h>
#include "BluetoothSerial.h"
#define SCREEN_WIDTH 128 // OLED display width 
#define SCREEN_HEIGHT 64 // OLED display height in pixels
#define OLED_RESET -1 // OLED Reset Pin 
#define SCREEN_ADDRESS 0x3C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

BluetoothSerial SerialBT;

// 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

const char* ssid       = "dasari-5g-guest";
const char* password   = "dasar1h0m3";
const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = -18000;
const int   daylightOffset_sec = 3600;

String message = "";
char incomingChar;

int onhour;
int onminute;
int offhour = onhour;
int offminute = onminute + 1;
byte HOUR;
byte MIN;

int pir = digitalRead(34);

void setup() {
  pinMode(34, INPUT_PULLUP); //PIR Sensor
  pinMode(25, OUTPUT); //Relay
  Serial.begin(115200);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  display.clearDisplay(); //Clears the Splash Screen

  SerialBT.begin("ESP32");
  Serial.println("The device started, now you can pair it with bluetooth!");

  //connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("CONNECTED");

  //init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  printLocalTime();

  //disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
}

void loop() {


  printLocalTime();
  turnOnLight();
}

void turnOnLight() {
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return;
  }

  if (SerialBT.available()) {
    char incomingChar = SerialBT.read();
    if (incomingChar != '\n') {
      message += String(incomingChar);
    }
    else {
      message = "";
    }

    if (message.length() == 5) {
      int onminute = message.substring(3, 5).toInt();
      Serial.println(onminute);
      int onhour = message.substring(0, 2).toInt();
      Serial.println(onhour);
    }
  }

  HOUR = timeinfo.tm_hour;
  MIN = timeinfo.tm_min;
  
  if (HOUR == onhour && MIN == onminute) {
    Serial.println("SANITIZING");
    digitalWrite(25, HIGH);
    if (pir == LOW) {
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(0, 30);
      display.print("SANITIZING");
      Serial.println("SANITIZING");
      delay(300);
    }

    else {
      digitalWrite(25, LOW);
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(0, 30);
      display.print("MOTION DETECTED!");
      Serial.println("MOTION DETECTED!");
      delay(300);
    }
    display.display();
    display.clearDisplay();
  }
  else if (HOUR == offhour && MIN == offminute) {
    Serial.println("LIGHT OFF");
    digitalWrite(25, LOW);
  }

}

void printLocalTime() {
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return;
  }

  Serial.println(&timeinfo, "%A, %B %d %H:%M:%S");

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.print(&timeinfo, "%A, %B %d");

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  display.print(&timeinfo, "%H:%M:%S");

  if (HOUR != onhour && HOUR != offhour && MIN != offminute && MIN != onminute) {
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 30);
    display.print("IDLE");
    Serial.println("IDLE");
  }

  display.display();
  display.clearDisplay();

}

Bingo! Thanks!
The wiring is way too often needed to find errors. soo it is my standard request, wiring, Not any Fritzing.
Poor powering is way to usual.

Split Your project into smaller parts. I see Bluetooth, relay, protocols.....
I would work on the BT and make sure that works well, and is understood. Then applying "protocool" etc.

How You use transmitted information lays way down the road.

Which parts of the project works?

so the parts that work are the ability to set time within the code and have the light turn on. The time display also works. For the wiring photo, can i just send you a photo from my phone of the actual breadboard and give it to you ??

I would prefer a simple drawing of the wiring. How is the relay (link please) wired?
Data for Your power unit, please.
Photos are mostly useless. A wiring tells what pins, their designation, use... are connected.

I've been reading this topic "up and down". You manage to transmit/receive "time". I assume You print it out on Serial.
I'll try to find that "time received" in the code.

Your code is quite advanced. I suggest that You use the combination of Serial Monitor and Serial.print in strategic places printing important values.

Dry debugging feels like a lot more than I will take on.

I attached a picture below in jpg form which is a drawing of the circuit. I hope it will suffice.

No pic visible here. Give reply #12 some time.

yeah its being annoying and wont let me upload the image and yeah ill give it time that fine

Here's the image (this was a pain to upload cause it wouldn't allow me to attach it)

In the drawing I can't see how, from where, the controller is powered.
The other devices are connected to Vin. That is worse than strange.
Please clarify the powering. One more, but small, wiring if possible.

The board just gets power from a micro b 5v usb cable. The vin pin supplies power.

5 volt from micro USB is fine. Tapping power out of Vin... I never heard of. Tap from controller 5 volt pin is my suggestion.
Is it a Mega You're using?