telefonicaid / iotagent-json

IoT Agent for a JSON based protocol (with HTTP, MQTT and AMQP transports)
https://fiware-iotagent-json.rtfd.io/
GNU Affero General Public License v3.0
51 stars 88 forks source link

mosquitto_pub does not work #519

Closed rezendel closed 3 years ago

rezendel commented 3 years ago

Hello, I hope this message finds you well.

I just started to use fiware for a academic project, so apologies if I ask something trivial.

I'am trying to use the following command line structure to publish on the to the iot-json agent, but I could not get it to work from terminal. (try to send gps data)

$ mosquitto_pub -t /json/ABCDEF/id_sen1/attrs -m '{"h": 70, "t": 15}' -h -p -u

please see my code.

mosquitto_pub -t /json/4jggokgpepnvsb2uv4s40d59ov/gps002/attrs -m {"l": [10.2424, 45.5642]}' \ -h localhost -p 1883

when I try this command it works fine:

sudo docker run -it --rm --name mqtt-publisher --network \ fiware_default efrecon/mqtt-client pub -h mosquitto -m '{"l": [65.2424, 45.5642]}' \ -t "/4jggokgpepnvsb2uv4s40d59ov/gps002/attrs"

I am using a architecture with orion context broker + IoT Agent Json + MQTT Broker

I am also trying to send coordinate data from a ESP32 using arduino library, I could not get the data to topic as well. I can get a MQTT connection but it looks like that the data doesn't reach the mqtt topic.

After the code serialize using Json the message format looks like this. {"l":"[23.6666, 24.6574]"}, I'm not sure if this would be a issue.

I don't know if my code on this could help, but here it is:

// Author Leandro Rezende // 06/12/2020 19h

include // SPI bus

include //

include

include

include

include

include

// para testes rede local

include

//Dados para conexão ao WiFi

const char SSID = "Rolima"; const char PWD = "38362945";

//const char SSID = "JULIANA"; //const char PWD = "lela7910";

// função para conexão ao wifi

void connectToWiFi(){

  Serial.print("Connecting to");
  Serial.println(SSID);
  WiFi.begin(SSID,PWD);
    while(WiFi.status()!=WL_CONNECTED){
            Serial.print(".");
            delay(500);

      }

      Serial.println ("Connected - ");

} // fim da função wifi

/////////////////////// MQTT

const char mqtt_server = "192.168.15.134"; const char Device_Fiware ="urn:ngsi-ld:Gps:002"; //const char* Device_Fiware ="gps002"; WiFiClient espClient; PubSubClient mqttClient(espClient);

String value; /// conectando no topico do dojo

void connectToFiware(){ mqttClient.setServer(mqtt_server,1883);

  mqttClient.connect(Device_Fiware);

  Serial.println("Conectando no MQTT Fiware");
   while (!mqttClient.connected()) {

      Serial.println("Connecting to MQTT......Retry");
      mqttClient.connect(Device_Fiware);
      delay(5000);
   }

  Serial.println("Conectado ao Fiware");

} // fim código conexão Fiware

//publica no Fiware

void PublicaFiware(){

  StaticJsonDocument<256> jsonDoc;
  JsonObject eventDoc = jsonDoc.createNestedObject("event");

  eventDoc["l"] = value;

  char jsonBuffer[256];
  serializeJson(eventDoc,jsonBuffer);

  mqttClient.publish(" /json/4jggokgpepnvsb2uv4s0d59ov/gps002/attrs",jsonBuffer);

  Serial.println("enviado para Fiware");
  Serial.println(jsonBuffer);
  //Serial.println(eventDoc);

}

void setup() { // put your setup code here, to run once:

Serial.begin(9600);

 // conectando ao wifi

 connectToWiFi();

 // conectando ao mqtt

connectToFiware();

}

void loop() { // put your main code here, to run repeatedly:

 value="[23.6666, 24.6574]";

Serial.print ("Coordenada ");
Serial.println (value);

PublicaFiware();

delay(5000);

}

Thank you

AlvaroVega commented 3 years ago

Maybe you need provide user and password for your broker in mosquito_pub command ? mosquitto_pub -t /json/ABCDEF/id_sen1/attrs -m '{"h": 70, "t": 15}' -h <mosquitto_broker> -p <mosquitto_port> -u <user> -P '<passwrod>'

rezendel commented 3 years ago

Thank you Alvaro, I was missing the -P and '' on the message sorry :(

But now it worked on the terminal, I just need to work on device now.

Thank you very much.

rezendel commented 3 years ago

I solved the issue for the device as well. Thank you very much!