Go Down

Topic: MQTT not working properly (Read 336 times) previous topic - next topic

Tricker

Hello,
till now I was just a reader and learner of new things in Arduino and I just finished couple of small projects on Arduino platform.

Currently I'm dealing with an issue with receiving messages from MQTT. Connection -> Arduino Mega, ESP8826-01, (hardware connection RX1, TX1), baudrate 115200.

I'm able to connect to network, sent all the messages that are generated within Arduino (couple of sensors), but I can't receive the messages from MQTT server.
I was trying to run locally mosquitto, also tried free MQTT instance on cloudmqtt, however I'm receiving messages only when the message was sent from ESP and arrived another message from subscribed topic.
When the message arrives during the "idle" period, this message is not captured with ESP and therefor not received by arduino.

I was trying to use Knollery PubSubClient, which behaves unstable, so I tried also Imroy version of PubSubClient. This behaviour of receiving messages was not changed at all.

No devices are connected to the MQTT instance with the same name as the one from Arduino.
Code:
Code: [Select]
#include <WiFiEsp.h>
#include <WiFiEspClient.h>

#include <PubSubClient.h>

#define soft Serial1

#define in_topic "test"

int testi = 0;  //cycle break

//MQTT IP Address
IPAddress server(54, 75, 8, 165);



char ssid[] = "SSIS";           // your network SSID (name)
char pass[] = "pwd";           // your network password
int status = WL_IDLE_STATUS;   // the Wifi radio's status

// Initialize the ESP
WiFiEspClient espClient;

PubSubClient client(espClient);

void callback(const MQTT::Publish& pub) {
  Serial.print(pub.topic());
  Serial.print(" => ");
  if (pub.has_stream()) {
    uint8_t buf[100];
    int read;
    while (read = pub.payload_stream()->read(buf, 100)) {
      Serial.write(buf, read);
    }
    pub.payload_stream()->stop();
    Serial.println("");
  } else
    Serial.println(pub.payload_string());
   soft.flush();
}

void setup() {

  // initialize serial for debugging
  Serial.begin(115200);
  // initialize serial for ESP module
  soft.begin(115200);

  WiFi.init(&soft);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  //connect to MQTT server
  client.set_server(server, 1883);       //Server and Port
  client.set_callback(callback);
 
  reconnect();
 

}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
 client.loop();

  testi++;

  if (testi == 2000) { 
 
  SentMQTTF("topic",3);
  testi = 0;
  }
}


//Adjust format for MQTT broker (float values)
void SentMQTTF (String path, float value) {
  char sentout[50];
  char temppath[100];
  String SentMQTTString = String(value);
  SentMQTTString.toCharArray(sentout, SentMQTTString.length() + 1);
  path.toCharArray(temppath, path.length() + 1);
  client.publish(temppath, sentout);
  Serial.print("SENDING: ");
  Serial.print(path);
  Serial.print(" VALUE:  ");
  Serial.println(value);

  }


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect, just a name to identify the client
   
   
     client.connect(MQTT::Connect("ARDUINO Client")
                .set_clean_session()
                .set_will("status", "down")
                .set_auth("USER", "PWD")
                .set_keepalive(70)
               );
     client.subscribe("test/test");
     if (client.subscribe("test/test")) {Serial.println("Subscribed test"); } else {Serial.println("Failed to subscribe test");}; 
 
  }
}


Can somebody help? I'm stocked more then week with solving this issue. Maybe it's just me and I wrote wrong code. :o

pylon

Quote
I'm able to connect to network, sent all the messages that are generated within Arduino (couple of sensors), but I can't receive the messages from MQTT server.
Your code subscribes to the topic "test/test". Does some other instance publish messages to this topic? You will not get any message from the MQTT server if there is no publication of a message for the subscribed topic.

Go Up