Page 1 of 1

ESP32U with bluetooth and AWS mqtt code

Posted: Tue Mar 29, 2022 8:39 am
by spmodi
Hello there,
I am developing a project using ESP32U(16MB) . In my project, I need both Serial Bluetooth and AWS mqtt code.
But I am facing problem like only one functionality is works. When I first initialized Bluetooth, aws mqtt doesn't work or vise versa.
Here is my code.





#include <WiFiClientSecure.h>
#include "BluetoothSerial.h"
#include "PubSubClient.h"
#include "root.h"

char* ssid = "KEENTECHNO";
char* password = "keen$2019$";
const char* mqttServer = "a19u2z2shoq5px-ats.iot.us-east-1.amazonaws.com";
#define mqttPort 8883

WiFiClientSecure secureClient = WiFiClientSecure();
PubSubClient client(secureClient);

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

char msg[50];

void callback(char* topic, byte* payload, unsigned int length)
{
int i;
Serial.println("A");
for (i = 0; i < length; i++)
{
msg = (payload);
payload = 0;
}
}

void setup()
{
unsigned char k = 0, t = 0;
Serial.begin(9600);
SerialBT.begin();
WiFi.begin(ssid, password);
Serial.println(ssid);
Serial.println(password);
while (WiFi.status() != WL_CONNECTED)
{
vTaskDelay(500);
Serial.print(".");
k++;
if (k > 20)
{
k = 0;
break;
}
}
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("connected");
client.setServer(mqttServer, mqttPort);
secureClient.setCACert(rootCA);
secureClient.setCertificate(certificate);
secureClient.setPrivateKey(privateKey);
client.setCallback(callback);
while (!client.connected())
{
Serial.println("Connecting to MQTT...");
if(client.connect("espClient"))
{
Serial.println("connected");
client.publish("12312312345","Hello");
}
else
{
//Serial.print("failed with state ");
Serial.print(client.state());
//Serial.println(ESP.getFreeHeap());
delay(2000);
}
/*if (client.connect("espClient"))
{
Serial.println("connected");
client.subscribe(topic);
client.publish(topic1, "hello");
}
else
{
Serial.print("failed with state ");
Serial.print(client.state());
vTaskDelay(500);
k++;
if (k > 7)
{
break;
}
}*/
}
}
else
{
Serial.println("NOt connected");
}
}

void loop()
{
client.publish("12312312345","Hello");
delay(2000);
}