Jump to content









Photo

Error connecting Carriots with ESP8622 using MQTT

MQTT NodeMCU Arduino IDE

  • Please log in to reply
1 reply to this topic

#1 JPablo42

JPablo42

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 09 July 2017 - 02:33 AM

Hi guys, 

 

I'm using Carriots to send data from diferent devices, but I got an issue connecting my NodeMCU using MQTT, my example is based in the post "How to use a MQTT library for Arduino to send streams to Carriots", I'm using the recommended libraries, but I have no connection with the server.

 

This is my code.
 
#include <SPI.h>
#include <PubSubClient.h> // You must include the library here in order to use it in the sketch
#include "ESP8266WiFi.h"
 
////////////////////////////////////////
// CARRIOTS Connectivity definitions. //
////////////////////////////////////////
#define CARRIOTS_DEVICE       "My device"
#define CARRIOTS_USERNAME     "JPablo42"
#define CARRIOTS_APIKEY       "My apy key"
 
#define CARRIOTS_MQTT_PORT    1883
#define CARRIOTS_MQTT_HOST    "mqtt.carriots.com"
#define CARRIOTS_MQTT_TOPPIC  "/streams"
 
const String DEVICE_ID = CARRIOTS_DEVICE "@" CARRIOTS_USERNAME "." CARRIOTS_USERNAME;
////////////////////////////////////////
 
const char *ssid = "ssid";
const char *password = "pass";
 
WiFiClient client;
// Instantiate an instance of the MQTT client.
PubSubClient mqtt_client(CARRIOTS_MQTT_HOST, CARRIOTS_MQTT_PORT, client);
 
void mqtt_connect() {
  // Loop until we're connected to MQTT.
  while (!mqtt_client.connected()) {
    Serial.print("Attempting MQTT connection... ");
    // Attempt to connect.
    if (mqtt_client.connect(CARRIOTS_USERNAME, CARRIOTS_APIKEY, NULL)) {
      Serial.println("Success");
    // Failed connection.
    } else {
      Serial.print("Failed connection: rc=");
      Serial.print(mqtt_client.state());      
      Serial.println(". Retry on 1 second.");
      // Wait 1 second before retrying
      delay(1000);
    }
  }
}
 
void mqtt_publish(char* frame) {
  // Note - the default maximum packet size is 128 bytes.
  // You may need to increase the value of MQTT_MAX_PACKET_SIZE in
  //   Arduino/libraries/pubsubclient/src/PubSubClient.h
  //   (Increased to 256 on this example)
  Serial.print("Publishing at " CARRIOTS_MQTT_TOPPIC ": ");
  Serial.println(frame);
  // Publish data frame. 
   if (mqtt_client.publish(CARRIOTS_APIKEY CARRIOTS_MQTT_TOPPIC, frame)) { // No comma between APIKEY and TOPPIC.
  // if (mqtt_client.publish("fd5623da0d7852e341df61e3eed44d5037d86851d365811bb140ee85f01c4307/streams", frame)) { // No comma between APIKEY and TOPPIC.
    Serial.println("Published.");
  } else {
    Serial.println("Failed publishing.");
    //Serial.println(frame);
  }
  Serial.println();
}
 
char* get_frame() {
  // Build Data JSON.
  String data_json  = "{\"Sample_Value_1\":10,\"Sample_Value_2\":\"Sample 2\"}";
  // String data_json  = "{\"Sample_Value_2\":\"Sample 2\"}";
  // Build Frame JSON.
  String frame_json = "{\"protocol\":\"v2\",\"device\":\""+DEVICE_ID+"\",\"at\":1488447613,\"data\":"+data_json+"}";
  // Dump to "char array" and return.
  //   (mqtt.publish method does not works with "String" type, so it must be "casted" to "char array")
  int frame_length = frame_json.length() + data_json.length() + 1;
  char frame_buffer[frame_length];
  frame_json.toCharArray(frame_buffer, frame_length);
  return frame_buffer;
}
 
void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println();
  Serial.print("Conecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("Wifi conected");
  Serial.println("Ip address: ");
  Serial.println(WiFi.localIP());
}
 
void loop() {
  // Connect to MQTT.
  if (!mqtt_client.connected()) {
    mqtt_connect();
  }
  else {
    Serial.println("MQTT Conectado");
  }  
  // Send frame.
  mqtt_publish(get_frame());
  delay(5000);
}
 
 
And this is the response from the server:
 
 
Wifi conected
Ip address: 
-----------------
Attempting MQTT connection... Success
Publishing at /streams: {"protocol":"v2","device":"device@JPablo42.JPablo42","at":1488447613,"data":{"Sample_Value_1":10,"Sample_Value_2":"Sample 2"}}
Failed publishing.
 

Pablo.

Regards.



#2 jpastor

jpastor

    Development leader

  • Administrators
  • 158 posts

Posted 11 July 2017 - 10:27 AM

Hi Pablo.

 

- The library "pubsubclient" in the last versions has this feature (specified in the documentation): -> The maximum message size, including header, is 128 bytes by default.

 

This is configurable via MQTT_MAX_PACKET_SIZE in PubSubClient.h.

- Try to modify this size to 256, in case it was a problem with the size of the packages

 

 

Hope it helps!







Also tagged with one or more of these keywords: MQTT, NodeMCU, Arduino IDE

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users