HTTP Client and MQTT Client library conflict

Hi,

I am using Arduino Yun for development. I want to use a MQTT client and a HTTP client in the same code but only one works at a time. I think its some sort of library conflict in both of them. When I remove MQTT client library the HTTP client works and vice versa but I am unable to use both at the same time. Has anyone ever encountered such a problem ?

Has anyone ever encountered such a problem ?

All the damned time. People come blundering in here without reading the "How to post in this forum" thread that CLEARLY says "POST YOUR CODE!".

Sorry, my bad. I forgot to put my code !

// include process library
#include <Process.h>
#include <MQTTclient.h>
#include <DHT.h>
#include <Time.h>
#include <HttpClient.h>
#include <ArduinoJson.h>


#define MQTT_HOST "xxx.xxx.xx.xx" // test.mosquitto.org

Process date;

int first_run = 1;
String location;

char mac[] = "B4:21:8A:F0:07:56";

////DHT pin
#define DHTPIN 8
#define DHTTYPE DHT22
//
DHT dht(DHTPIN, DHTTYPE);

void setup() {
   
  // start serial
  Serial.begin(115200);
  
  Bridge.begin();
  // begin the client library (initialize host)
  mqtt.begin(MQTT_HOST, 1883);
  dht.begin();
 
}

void loop() {
 
  //check for incoming events
  
  if(first_run == 1){
  Serial.println(getLocation());
    first_run = 2;
  }

  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();
  int light_level = analogRead(A0);
  
  String row;
  
  if (!date.running()) {
    date.begin("date");
    date.addParameter("+%m/%d/%y %H:%M:%S");
    date.run();
  }

  String timeString;
  //if there's a result from the date process, parse it:
  while (date.available() > 0) {
    timeString = date.readString();
  }
  
  row.concat(mac);
  row.concat(",");
  row.concat ("1");
  row.concat(",");
  row.concat(timeString);
  row.concat(",");
  row.concat(temperature);
  row.concat(",");
  row.concat(humidity);
  row.concat(",");
  row.concat(light_level);
  row.concat(",");
  row.concat(location);
  Serial.println(row);
  
  mqtt.monitor();
  mqtt.subscribe("light_LED", lightLED);
 
  mqtt.publish("String_Check", row);
  mqtt.publish("String_Check", "published");
 

  mqtt.publish("Temperature", temperature);

  delay(2000);

}

String getJson() {
    HttpClient client;
    client.get("ipinfo.io");

    String str = "";
    while (client.available()) {
      char c = client.read();
      str = str + c;
    }
    Serial.flush();
    return str;
}

String getLocation() {
    
    StaticJsonBuffer<500> jsonBuffer;
    JsonObject& root = jsonBuffer.parseObject(getJson());
    
    String location;
    
    if (!root.success()) {
      Serial.println("parseObject() failed");
      String* result = new String[1];
      result[0] = "Parsing failed";
      return *result;
      
    } 
  
    const char* ip = root["ip"];
    const char* hostname = root["hostname"];
    const char* city = root["city"];
    const char* region = root["region"];
    const char* country = root["country"];
    String loc = root["loc"];
    String latitude = loc.substring(0, 7);
    String longitude = loc.substring(8);
    const char* org = root["org"];
    const char* postal = root["postal"];

    String locationInfo[9] = {ip,hostname,city,region,country,latitude,longitude,org,postal};
    
    for (int i=0; i<9;i++){
      location.concat(locationInfo[i]);
      if(i<9){
        location.concat(",");
      }
    }
    
    return location;
}
      String* result = new String[1];
      result[0] = "Parsing failed";
      return *result;

WTF?

return "Parsing failed";

You just leaked memory.

Some library links would be useful.

PaulS:

      String* result = new String[1];

result[0] = "Parsing failed";
      return *result;



WTF?



return "Parsing failed";




You just leaked memory.

Some library links would be useful.

is this the reason i am having this problem ?

is this the reason i am having this problem ?

Probably not THE reason. But, it is bad code.

Links?

PaulS:
Probably not THE reason. But, it is bad code.

Links?

what links ?

PaulS:
Probably not THE reason. But, it is bad code.

Links?

I understand its a bad code, can you please focus on the problem that I am facing ?

what links ?

The links to non-core libraries that I asked for.

I understand its a bad code, can you please focus on the problem that I am facing ?

Hey, doc, I know I bleeding all over the floor from the chainsaw cut, but can we focus on the hangnail I came in for?

Please give links to the non core libraries.

What makes you say it doesn't work? Does it compile? If not, what errors?

If it compiles but fails to do what it's supposed to, despite that both libraries work alone, that might very well be a memory problem, so you should fix the issue Paul pointed out!

DrAzzy:
Please give links to the non core libraries.

What makes you say it doesn't work? Does it compile? If not, what errors?

If it compiles but fails to do what it's supposed to, despite that both libraries work alone, that might very well be a memory problem, so you should fix the issue Paul pointed out!

I have fixed what Paul pointed but it still doesn't work. The code compiles perfect and works fine apparently but the http request just doesn't work good. It doesn't get the json correctly.

And I am quite new to Arduino programming so can you please point out the non-core libraries I am using ? Then I can give you their links.

And I am quite new to Arduino programming so can you please point out the non-core libraries I am using ?

You know which library or libraries you needed to download. The MQTTclient and ArduinoJson libraries, for instance.

HTTP Client

Time Library

ArduinoJson

MQTT Client

UmerF92:
I have fixed what Paul pointed but it still doesn't work. The code compiles perfect and works fine apparently but the http request just doesn't work good. It doesn't get the json correctly.

Does it get anything? For that matter... how long is that response you're expecting to get?

Based on how much you're using String, and how things work individually, but not when you put them together, I'd suspect you're running out of ram. String is the sort of thing you want to avoid at all costs.

The Arduino only has 2048 bytes of RAM, which is a tiny amount of data by modern general purpose computer standards...

UmerF92:
HTTP Client

Time Library

ArduinoJson

MQTT Client

Move all the class into Linux side:

...
import mosquitto
import sys
import json
import requests
import datetime
import pytz
...

leave ATmega32u4 very minimum:

#include <Process.h>
void setup() {
  Bridge.begin();   // Initialize Bridge
}
void loop() {
  int temperature = random(0, 100);
  Process p;        // Create a process and call it "p"
  p.begin("/root/mqtt.py");   // Process that launch the  command
  p.addParameter(temperature); // pass  parameter 
  p.run();      // Run the process and wait for its termination
  delay(5000);
}

http://forum.arduino.cc/index.php?topic=348030.msg2418782#msg2418782