Report
2 years, 10 months ago.

No messages arrive DISCO-F746NG.

Hi, im using the same example of your HelloMQTT, but messages never arrive. Here's the code im using:

#include "mbed.h"
#include "rtos.h"
#include "TS_DISCO_F746NG.h"
#include "LCD_DISCO_F746NG.h"
#include <iostream>
#include <string>
#include <sstream>
#include "MQTTEthernet.h"
#include "MQTTClient.h"

LCD_DISCO_F746NG lcd;
TS_DISCO_F746NG ts;
//EthernetInterface eth;

MQTTEthernet ipstack = MQTTEthernet();
MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
MQTT::Message message;
int message_count = 0;
string buff;
stringstream convert;
int rc;

#define TOKEN "HQ0dPZ1MGCbhE6gGtZMk7w8s80Dtjf0VtiWKghbTYSw"

string topic;

void addValue(string key, int value)
{
    if (message_count > 0) buff.append(",");
    buff.append("{\"key\":\"");
    buff.append(key);
    buff.append("\",\"value\":\"");
    convert.str("");
    convert << value;
    buff.append(convert.str());
    buff.append("\"}");
    message_count+= 1;
}

void send()
{
    buff.append("]}");
    //convert << "]}";
    message.qos = MQTT::QOS0; // QOS0 or 1 available
    message.payload = (void*)buff.c_str();
    message.retained = false;
    message.dup = false;
    message.payloadlen = strlen(buff.c_str());
    rc = client.publish(topic.c_str(), message);
    buff = "{\"values\":[";
    //convert.str("{\"values\":[");
    //lcd.DisplayStringAt(0, LINE(5), (uint8_t *)convert.str().c_str(), CENTER_MODE);
    message_count = 0;
}


/*
void show_gauge(int value) {
    int filtered = value%100;
    //calcular a on va la recta
    lcd.SetTextColor(LCD_COLOR_BLACK);
    lcd.DrawCircle(240, 135, 135);
    lcd.FillCircle(240, 135, 135);
    lcd.SetTextColor(LCD_COLOR_WHITE);
    lcd.DrawCircle(240, 135,130);
    lcd.FillCircle(240, 135,130);
    lcd.SetTextColor(LCD_COLOR_YELLOW);
    pinta_cercle(240, 135, 135);
    lcd.SetTextColor(LCD_COLOR_BLACK);
    lcd.DrawCircle(240, 135,5);
    lcd.FillCircle(240, 135,5);
    lcd.DrawLine(240, 135, 240, 271);
}*/

void paint_block()
{
    lcd.SetTextColor(LCD_COLOR_WHITE);
    lcd.DrawRect(100, 50, 280, 30);
}

void paint_block(int valor)
{
    int filtered = valor%100;
    lcd.SetTextColor(LCD_COLOR_WHITE);
    lcd.FillRect(100, 50, 280, 30);
    if (valor <= 33) {
        lcd.SetTextColor(LCD_COLOR_GREEN);
    } else if(valor <= 66) lcd.SetTextColor(LCD_COLOR_YELLOW);
    else lcd.SetTextColor(LCD_COLOR_RED);

    float perc = 280.0f*((float)valor/100.0f);
    lcd.FillRect(100,50,(int) perc ,30);
}

void messageArrived(MQTT::MessageData& md)
{
    MQTT::Message &message = md.message;
    printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
    printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
    string missatge((char*)message.payload);
    string valor;
    valor = missatge.substr(missatge.find("\"value\":\"") + 9, missatge.find("\"}]"));
    lcd.SetTextColor(LCD_COLOR_WHITE);
    lcd.DisplayStringAt(50, 150, (uint8_t *) valor.c_str(), CENTER_MODE);
    /*if (missatge.find("\"key\":\"valor\"") >= 0) {

    }*/
}

int main(int argc, char* argv[])
{
    TS_StateTypeDef TS_State;
    uint16_t x, y, x_ant, y_ant;
    /***** TS *****/

    printf("hola\n");
    topic.append("v2/things/");
    topic.append(string(TOKEN));
    /***** MQTT *****/

    char* hostname = "mqtt.thethings.io";
    int port = 1883;
    rc = ipstack.connect(hostname, port);
    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    data.MQTTVersion = 3;
    data.clientID.cstring = "disco-f746";
    rc = client.connect(data);
    printf(topic.c_str());
    rc = client.subscribe(topic.c_str(), MQTT::QOS1, messageArrived);
    wait(5);
    printf("rc from MQTT subscribe is %d\n", rc);
    buff = "{\"values\":[";
    addValue("demo", 0);
    send();
    lcd.Clear(LCD_COLOR_BLACK);
    /*
    lcd.SetTextColor(LCD_COLOR_RED);
    lcd.DrawCircle(160, 136, 50);
    wait(0.1);
    lcd.SetTextColor(LCD_COLOR_RED);
    lcd.FillCircle(160, 136, 50);
    wait(1);
    lcd.SetTextColor(LCD_COLOR_GREEN);
    lcd.DrawCircle(320, 136, 50);
    wait(0.1);
    lcd.SetTextColor(LCD_COLOR_GREEN);
    lcd.FillCircle(320, 136, 50);
    wait(1);
    ts.Init(lcd.GetXSize(), lcd.GetYSize());
    wait(3);
    */
    paint_block(0);
    ts.Init(lcd.GetXSize(), lcd.GetYSize());
    while (1) {
        rc = client.connect(data);
        ts.GetState(&TS_State);
        if (TS_State.touchDetected) {
            x = TS_State.touchX[0];
            y = TS_State.touchY[0];
            if ((x_ant != x) && (y_ant != y)) {
                if ((x >= 110 && x <= 210)&&(y >= 86 && y <= 186)) {
                    addValue("demo", 1);
                    send();
                } else if ((x >= 270 && x <= 370)&&(y >= 86 && y <= 186)) {
                    addValue("demo", 2);
                    send();
                }
            }
            x_ant = x;
            y_ant = y;
        }
    }
}

Question relating to:

Working on MQTT, a machine-to-machine (M2M) "Internet of Things" connectivity protocol.
Comment on this question
Be the first to answer this question.

You need to log in to post a question