• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Conflict Between Bluetooth Serial and NTP (Wifi/time) with ESP32

Q&A Forum › Category: ESP32 › Conflict Between Bluetooth Serial and NTP (Wifi/time) with ESP32
0 Vote Up Vote Down
radiovan7 asked 1 year ago

I can compile the sketch from
ESP32 NTP Client-Server: Get Date and Time (Arduino IDE)
or the sketch from
ESP32 Bluetooth Classic with Arduino IDE – Getting Started -Serial to Serial Bluetooth
I and both compile just fine. When I try to merge the two in one sketch I get these errors:
error: expected constructor, destructor, or type conversion before ‘(‘ token configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
^ ESP32_BT_Ambient_Lamp:45:17: error: expected constructor, destructor, or type conversion before ‘;’ token printLocalTime();
Maybe these won’t work together or there’s some trick to it. Thank you in advance to everyone for ideas on what I should try.
Here’s the beginning of the sketch:
// Load libraries
#include “Arduino.h”
#include “BluetoothSerial.h”
#include <WiFi.h>
#include “time.h”
// 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;
// GPIOs for LED strip colors
const int PIN_RED = 4;
const int PIN_GREEN = 16;
const int PIN_BLUE = 17;
const char* ssid = “Guest3360”;
const char* password = “Alkitab1”;
const char* ntpServer = “north-america.pool.ntp.org”;
// for 8 hour offset from UTC for PST
const long gmtOffset_sec = -28800;
const int daylightOffset_sec = 3600;
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
…

Question Tags: Bluetooth Classic, ESP32, NTP Server, time
6 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi.
Can you show more of your code?
To share your code, you can use github gist or pastebin, for example.
Regards,
Sara

0 Vote Up Vote Down
radiovan7 answered 1 year ago

Hi Sara,
I did find this  Instructables today that talks about issues with simultaneous Wifi and BLE:
https://www.instructables.com/ESP32C3-BLE-to-WiFi-Bridge-BLE-WiFi-Running-Togeth/
Initially, I was going to write this program for WiFi but I switched to regular Bluetooth after not being able to get the Wifi to compile. Bluetooth Serial works for direct control without the Wifi.
I downloaded this code from Pastebin and copied it out of Notepad, not sure if that’s the way to do it. This is the entire program.
Thank you,
Bill
/*********
*
* BT Ambient Lamp Control Program for ESP32 with RGB strip
* BY 12-26-23
* rev A 01-08-24 added wake up routine
* Rui Santos
* Complete project details at http://randomnerdtutorials.com
* ntp client at https://RandomNerdTutorials.com/esp32-date-time-ntp-client-server-arduino/
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files.
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*********/
// Load libraries
#include “Arduino.h”
#include “BluetoothSerial.h”
#include <WiFi.h>
#include “time.h”
// 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;
// GPIOs for LED strip colors
const int PIN_RED = 4;
const int PIN_GREEN = 16;
const int PIN_BLUE = 17;
const char* ssid = “SSID”;
const char* password = “Password”;
const char* ntpServer = “north-america.pool.ntp.org”;
// for 8 hour offset from UTC for PST
const long gmtOffset_sec = -28800;
const int daylightOffset_sec = 3600;
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
// Handle received and sent messages
String message = “”;
char incomingChar;

void setup() {
pinMode(PIN_RED, OUTPUT);
pinMode(PIN_GREEN, OUTPUT);
pinMode(PIN_BLUE, OUTPUT);
Serial.begin(115200);
// Bluetooth device name
SerialBT.begin(“ESP32”);
Serial.println(“The device started, now you can pair it with bluetooth!”);
// Connect to Wi-Fi
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi 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() {
// Read received messages (LED strip control commands)
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
// Choices are red, green, blue, white, off, wake

if (message ==”red”){
// Turn on Red
setColor(255, 0, 0);
delay(100);
}
if (message ==”green”){
// Turn on green
setColor(0, 255, 0);
delay(100);
}
if (message ==”blue”){
// Turn on blue
setColor(0, 0, 255);
delay(100);
}
if (message ==”white”){
// Turn on white
setColor(255, 255, 255);
delay(100);
}

if (message ==”wake” && (hour() == 5 && minute() == 30)) {
// start 30 minute sequence
// Turn on Red
setColor(255, 0, 0);
delay(100);
}
if (message ==”wake” && (hour() == 5 && minute() == 40)) {
// Turn on Orange at 5:40
setColor(255, 70, 0);
delay(100);
}
if (message ==”wake” && (hour() == 5 && minute() == 50)) {
// Turn on Yellow at 5:50
setColor(255, 90, 0);
delay(100);
}
if (message ==”wake” && (hour() == 6 && minute() == 0)) {
// Turn on White at 6:00
setColor(255, 255, 255);
delay(100);
}
else if (message ==”off”){
setColor(0, 0, 0);
delay(100);
}
printLocalTime();
}
void setColor(uint8_t red, uint8_t green, uint8_t blue) {
analogWrite(PIN_RED, red);
analogWrite(PIN_GREEN, green);
analogWrite(PIN_BLUE, blue);
}
void printLocalTime(){
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println(“Failed to obtain time”);
return;
}
Serial.println(&timeinfo, “%A, %B %d %Y %H:%M:%S”);
Serial.print(“Day of week: “);
Serial.println(&timeinfo, “%A”);
Serial.print(“Month: “);
Serial.println(&timeinfo, “%B”);
Serial.print(“Day of Month: “);
Serial.println(&timeinfo, “%d”);
Serial.print(“Year: “);
Serial.println(&timeinfo, “%Y”);
Serial.print(“Hour: “);
Serial.println(&timeinfo, “%H”);
Serial.print(“Hour (12 hour format): “);
Serial.println(&timeinfo, “%I”);
Serial.print(“Minute: “);
Serial.println(&timeinfo, “%M”);
Serial.print(“Second: “);
Serial.println(&timeinfo, “%S”);
Serial.println(“Time variables”);
char timeHour[3];
strftime(timeHour,3, “%H”, &timeinfo);
Serial.println(timeHour);
char timeWeekDay[10];
strftime(timeWeekDay,10, “%A”, &timeinfo);
Serial.println(timeWeekDay);
Serial.println();
}

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi.
Can you try moving the following lines to the beginning of the setup() (inside the setup() function)? 
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();

Let me know if this fixes the problem.
Regards,
Sara

0 Vote Up Vote Down
radiovan7 answered 1 year ago

Hi Sara,
Yes, it worked. I also had to choose the Huge App option (3MB /  No OTA  /1 MB SPIFFS) to clear the out of memory error. Bluetooth serial control is still working.
I will find out tomorrow morning at 5:30 if my other code changes to run a red to white sequence are working. I am slowly increasing my understanding of the Unix standard structure behind ESP32 time.h core.
I’m using this with the 3d printed hinge lamp from Instructables built with a filament spool. If other people are interested, I will upload my working sketch.
Thank you and Rui for all of the great work you do!
Bill

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Thank you.
I’m glad everything is working at the moment.
If you have your project saved on Github, for example, you can share here the link so that other can benefit from it in the future,
Regards,
Sara

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.