6 years, 3 months ago.

Incompatibility Between W5500 MQTT lib and mbed SD Card lib

Hi all,

I have been using this lib since few days ago, and it works great for MQTT. Especially with https://developer.mbed.org/teams/EthernetInterfaceW5500-makers/code/IBMIoTClientEthernetExample_W5500/ tutorial.

Today, I want to create a simple data logger app. Read sensor data, save to SD card, then publish data to MQTT broker. I use both this lib and SD Card lib https://developer.mbed.org/teams/mbed/code/SDFileSystem/. However, I found out that this lib seems incompatible with the official SD Card lib.

Below are the code to write to and read from SD card with MQTTEthernet initialization (no accessing MQTT broker yet).

#include "mbed.h"
#include "SDFileSystem.h"
#include "MQTTClient.h"
#include "MQTTEthernet.h"
#include <string>

#define MQTT_MAX_PACKET_SIZE    250
#define MQTT_HOST               "q.thingfabric.com"
#define MQTT_PORT               1883
#define SERVER_PORT             8888

//init sd card
SDFileSystem sd(PA_7, PA_6, PA_5, PC_12, "sd"); //mosi, miso, sck, cs

//init debug serial
Serial DBG(PA_9, PA_10);

//init eth
SPI spi(PB_15, PB_14, PB_13);               //mosi, miso, sck
MQTTEthernet ipstack(&spi, PB_12, PC_6);    //spi, cs, reset
MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);

int main() {
    DBG.baud(19200);
    DBG.printf("\r\nETH and SD Card\r\n");
    
    //param for sd card
    signed char c;
    char rcv[1024];
    int i=0;
    
    mkdir("/sd/testdir",0777);
    //write data to file
    FILE *fp = fopen("/sd/testdir/sdtest.txt","a"); //use "a" to append or "w" to write
    if(fp == NULL) {
        DBG.printf("error write\r\n");    
    }
    fprintf(fp,"mbed is cool!\tcarry on\t\r\n");
    fclose(fp);
    
    //read data from file
    fp = fopen("/sd/testdir/sdtest.txt","r");
    if(fp == NULL) {
        DBG.printf("error read\r\n");    
    }
    while(1) {
        c = fgetc(fp);
        if(c == EOF){
            break;
        }
        //pc.putc(c);
        rcv[i] = c;
        i++;    
    }
    DBG.printf("%s",rcv);
    fclose(fp);
}

Below is the result when I run those code. It stopped on

mkdir("/sd/testdir",0777);

/media/uploads/arsenalist/error1.png

However, when I commented out the below part, it runs ok.

//MQTTEthernet ipstack(&spi, PB_12, PC_6);    //spi, cs, reset
//MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);

Below is the result when I run without initialize MQTTEthernet /media/uploads/arsenalist/ok.png

My questions is:

1. Does anybody ever encountered this issue and have a solution or workaround? I just don't want to reinvent the wheel if somebody already have solution for this.

2. Or, do you have alternative lib (similar to these ones) that compatible to each other?

Thanks for your time and willingness to answer my question.

EDIT: sorry for the inconvenience. Actually the problem is because I use SD type card. If I change to SDHC one, it works well.

TC

Question relating to:

An API for using MQTT over multiple transports MQTT, MQTTClient
Be the first to answer this question.