Jump to content









Photo

Error connecting Carriots with ESP8622 using MQTT

MQTT NodeMCU Arduino IDE

  • Please log in to reply
2 replies 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
  • 178 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!



#3 sreejith.nair

sreejith.nair

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 02 March 2018 - 06:42 AM

Hi ,

I tested this with WifiESP library and it's working

 

#include <WiFiEspClient.h>
#include <WiFiEsp.h>
#include <PubSubClient.h>
#include "SoftwareSerial.h"
 
#define WIFI_AP "Access point name"
#define WIFI_PASSWORD "password"
 
int status = WL_IDLE_STATUS;
unsigned long lastSend;
 
// CARRIOTS Connectivity definitions. 
#define CARRIOTS_DEVICE          "device name" //Device Username + device name :(Check the details in CarriotsCPanel/Project/Service/Group/Device/Update)
#define CARRIOTS_DEVICE_PASSWORD "70a2ad" // From carriots->device->mqtt properties
#define CARRIOTS_CLIENT_ID        "Myxxxconnection" // You can use uniquie name here, it's client id
#define CARRIOTS_APIKEY          "be49b6dfgdrghfgjdtjh8" // Use your carriots apikey
 
#define CARRIOTS_MQTT_PORT    1883
#define CARRIOTS_MQTT_HOST    "mqtt.carriots.com"
#define CARRIOTS_MQTT_TOPPIC  "/streams" // Or /status
 
const String DEVICE_ID = CARRIOTS_DEVICE "@" CARRIOTS_USERNAME "." CARRIOTS_USERNAME;
 
 
// Initialize the WIFI client.
//WiFiEspClient espClient;
WiFiEspClient ESPclient;
PubSubClient mqtt_client(CARRIOTS_MQTT_HOST, CARRIOTS_MQTT_PORT, ESPclient);
SoftwareSerial soft(2, 3); // RX, TX
 
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_CLIENT_ID, CARRIOTS_APIKEY, CARRIOTS_DEVICE_PASSWORD)) {
      Serial.println("Success");
    // Failed connection.
    } else {
      Serial.print("Failed connection: rc=");
      Serial.print(mqtt_client.state());      
      Serial.println(". Retry in 5 seconds");
      // Wait 1 second before retrying
      delay(5000);
    }
  }
}
 
void mqtt_publish() {
 
  // Note - the default maximum packet size is 128 bytes.
  // You may need to increase the value of MQTT_MAX_PACKET_SIZE in
  //   C:\Users\Yourname\Documents\Arduino\libraries\pubsubclient\src\PubSubClient.h
  //   (Increased to 256 on this example)
  float t=26.5;
  Serial.print("Publishing at " CARRIOTS_MQTT_TOPPIC ": ");
  String temperature = String(t);
  String data_json  = "{\"Room_Temperature\":\""+temperature+"\"}";
  
  String data = "{\"protocol\":\"v2\",\"device\":\""+DEVICE_ID+"\",\"at\":\"now\",\"data\":"+data_json+"}}";
  char frame[data.length()];
  data.toCharArray(frame, data.length());
  Serial.println(frame);
  
  // Publish data frame. 
  if (mqtt_client.publish(CARRIOTS_APIKEY CARRIOTS_MQTT_TOPPIC, frame)) { // No comma between APIKEY and TOPPIC. //example("be49b4c0405ad387617dc8/streams", frame)
    Serial.println("Published.");
  } else {
    Serial.println("Failed publishing.");
  }
  Serial.println();
}
 
void setup() {
  // initialize serial for debugging  
  Serial.begin(9600);
  // initialize serial for ESP module
  soft.begin(9600);
  // initialize ESP module
  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);
  }
 
  Serial.println("Connecting to AP ...");
  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(WIFI_AP);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(WIFI_AP, WIFI_PASSWORD);
    delay(5000);
 
  }
  Serial.println("Connected to AP");
mqtt_client.setServer( CARRIOTS_MQTT_HOST, 1883 );
//lastSend = 0;
}
 
void loop() {
  // Connect to MQTT.
  if (!mqtt_client.connected()) 
  {
    mqtt_connect();
  }
  mqtt_publish();
  delay(5000); //publish every 5 sec
}
 
let me know, if anybody need help.






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