• 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

MQTT connection state 5

Q&A Forum › MQTT connection state 5
0 Vote Up Vote Down
GARLAND asked 6 years ago
3 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

Hello,

  • can you describe what are you doing and which project are you following?
  • What’s the full error message?

Thanks for your patience. Regards,
Rui

0 Vote Up Vote Down
GARLAND answered 6 years ago

Rui,

Thanks for your work. I have learned to at least think about how to run a project using aduino’s and raspberry pi’s.

My project is to make a device that would alert someone that a person is at a particular point physically. Using a PIR to send a signal to turn on a light and//or a bell/buzzer.
This of course is easy if the two units are connected together with a wire, but in this instance the person to be notified is far enough away that a wire is impractical.

At any rate, my problem is that I do not seem to be able to get both the esp8266 and the esp32 (this will be two esp32’s in the near future) connect to cloudmqtt.com at the same time. I have tried using one instance with two users and two instances with one user on each. The two sketches is the first one above.

Quite frankly, I am way above my pay scale here. Do you have any plans to write a tutorial using cloudmqtt?

Thanks for any direction you might give me,

Garland Sparks

/*
   This sketch will receive a signal from the esp32 using mqtt.
   For now I am using a switch to simulate a PIR signal on the esp32.
*/

#include
#include

// Connect to the local router
const char* ssid = "wrt";
const char* password = "password";

/*/ Connect to cloudmqtt.com (mqttserver)
const char *mqttServer = "m12.cloudmqtt.com";
const int mqttPort = 10542;
const char *mqttUser = "82input";
const char *mqttPassword = "";

WiFiClient Client1;
PubSubClient client(Client1);
*/
// Define which pin turns on light when switch on
// esp32 is turned on
int Pin2 = 4;

void reconnect() {

while (!client.connected()) {
Serial.println("Connecting to MQTT...");

if (client.connect("ESP8266Client", mqttUser, mqttPassword ))
{
Serial.println("connected");
//Once connected subscribe to topic
client.subscribe("esp/test");
} else {
Serial.print("failed with RC = ");
Serial.println(client.state());
delay(5000);
}

client.subscribe("esp/test");
}
}

// This functions is executed when ESP32 some device publishes a
// message to a topic that your ESP8266 is subscribed to.

void callback(String topic, String payload, unsigned int length) {
Serial.println("Call back");
Serial.print("Message arrived on topic: ");
Serial.println(topic);
Serial.print("Message:");
String messageTemp;

for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
messageTemp += (char)payload[i];
}

Serial.println();
Serial.println("-----------------------");

// If a message is received on the topic esp/test,
// you check if the message is either on or off.
// Turns the led GPIO according to the message
if (topic == "esp/test") {
Serial.print("Changing led to ");
if (messageTemp == "1") {
digitalWrite(Pin2, HIGH);
Serial.print("On");
}
else if (messageTemp == "0") {
digitalWrite(Pin2, LOW);
Serial.print("Off" );
}
}
Serial.println();
}

void setup() {

Serial.println("Start WiFi setup");
Serial.begin(115200);
pinMode(Pin2, OUTPUT);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");

client.setServer(mqttServer, mqttPort);
// client.setCallback(callback());
}

void loop() {
client.loop();

reconnect();
// callback(("esp/test") topic), String payload, unsigned int length);
//action();
}

Another sketch:

/*
This sketch will send a signal to an esp32 using mqtt.
For now I am using a switch to simulate a PIR signal.
*/
#include
#include

// Connect to the local router
char* ssid = "dd-wrt";
char* password = "password";

// Connect to cloudmqtt.com (mqttserver)
const char *mqttServer = "m12.cloudmqtt.com";
const int mqttPort = 10542;
const char *mqttUser = "32output"; //
const char *mqttPassword = ""; //

// Setup WiFi and MQTT
WiFiClient Client1;
PubSubClient client(Client1);

// Define switch pin and turn on a light when switch
// is on
#define Pin32 17 // switch
#define Pin36 36 // light

void setup() {

Serial.begin(115200);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500); // needed to connect to router
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");

client.setServer(mqttServer, mqttPort);

while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}

pinMode(Pin32, OUTPUT);
pinMode(Pin36, INPUT);
}

void loop() {
char* messageTemp = "2";

// read the value of Pin2 Output
int PinVal = digitalRead(Pin36);

Serial.print("Pin 'In' Value = ");
Serial.println(PinVal);

// Logic to tell remote light to turn on/off
if (Pin36, HIGH) {
messageTemp = "1";
client.publish("esp/Test", "PinVal");
Serial.print("Measage to Send ");
Serial.println(PinVal);
digitalWrite(Pin32, HIGH);
}

if (Pin36, LOW) {
//Serial.print("Pin 'In' Value = ");
//Serial.println(PinVal);
messageTemp = "0";
client.publish("esp/Test", "PinVal");
Serial.print("Measage = ");
Serial.println(PinVal);
digitalWrite(Pin32, LOW);
}
delay(3000);
}
0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

Unfortunately due to time constraints I can’t debug custom projects, I can only help with specific errors during our courses or problems that I’ve encountered myself (and know how to fix them).

I have a quick post on how to use CloudMQTT with ESP: https://rntlab.com/question/how-to-use-cloud-mqtt-broker-with-esp32-using-async-mqtt-client-library/

It’s quite easy, you just need to define the Host name, Port and set the MQTT credentials provided. Can you connect to Cloud MQTT? According to what you said and example, it looks like it should be establishing a connection.

If you are connected to the same broker, then you need subscribe to the same MQTT topic (as you are already doing esp/test) , so it should work.

You are thinking correctly and according to your code, it looks like it should be working… I’m not sure what’s missing.

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.