eclipse / mosquitto

Eclipse Mosquitto - An open source MQTT broker
https://mosquitto.org
Other
8.99k stars 2.39k forks source link

Arduino MKR1500 Mosquitto- did not receive any data from Mosquitto #2268

Open dkanday opened 3 years ago

dkanday commented 3 years ago

Hi,

I am new to Mosquitto and I am having an issue where I am not receiving any data at the serial monitor. I am using Arduino MKR1500 to send data to the server and then receive any change/ button press down from HiveMQ-> Mosquitto-> arduino to change the ADC value based on the button press on the server, this is my ultimate goal. But in the first step I am using the example code from: https://github.com/HologramEducation/hologram-arduino-mkr-1500/blob/master/examples/MKR-1500/MQTT/MKRNB-MQTT.ino and I have modified it to get data from test.mosquitto.org and I am not receiving any data from it. My code is as folllows: `/* Adapted from ArduinoMqttClient - WiFi Simple Receive Callback This example connects to a MQTT broker and subscribes to a single topic. When a message is received it prints the message to the serial monitor, it uses the callback functionality of the library. This example also publishes a sensor value to a separate topic The circuit:

include

include

// Please enter your sensitive data in the Secret tab or arduino_secrets.h // PIN Number const char PINNUMBER[] = "";

// MQTT Broker and Port const char broker[] = "test.mosquitto.org"; // int port = 1883;

// Pub/Sub Topics - Make it something unique since we are using a public broker const char subscribeTopic[] = "hologram-test/to"; const char publishTopic[] = "hologram-test1/from";

// To connect with SSL/TLS: // 1) Change NBClient to NBSSLClient. // 2) Change port value from 1883 to 8883. // 3) Change broker value to a server with a known SSL/TLS root certificate // flashed in the WiFi module.

NBClient client; GPRS gprs; NB nbAccess; MqttClient mqttClient(client);

int LED = 6; // Onboard LED

// Publish interval long previousMillis = 0; long interval = 10000;

void setup() {

pinMode(LED, OUTPUT); // Set built in LED pinMode

//Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only }

// attempt to connect to GSM and GPRS: Serial.print("Attempting to connect to GSM and GPRS"); // connection state bool connected = false;

// After starting the modem with GSM.begin() // attach the shield to the GPRS network with the APN, login and password while (!connected) { if ((nbAccess.begin(PINNUMBER) == NB_READY) && (gprs.attachGPRS() == GPRS_READY)) { connected = true; } else { Serial.println("Not connected"); delay(1000); } }

Serial.println("You're connected to the network"); Serial.println();

// You can provide a unique client ID, if not set the library uses Arduino-millis()

// Each client can have a unique client ID // mqttClient.setId("clientId");

// You can provide a username and password for authentication // mqttClient.setUsernamePassword("username", "password");

Serial.print("Attempting to connect to the MQTT broker: "); Serial.println(broker);

if (!mqttClient.connect(broker, port)) { Serial.print("MQTT connection failed! Error code = "); Serial.println(mqttClient.connectError());

while (1);

}

Serial.println("You're connected to the MQTT broker!"); Serial.println();

// set the message receive callback mqttClient.onMessage(onMqttMessage);

Serial.print("Subscribing to topic: "); Serial.println(subscribeTopic); Serial.println();

// subscribe to a topic mqttClient.subscribe(subscribeTopic);

// topics can be unsubscribed using: // mqttClient.unsubscribe(topic);

Serial.print("Waiting for messages on topic: "); Serial.println(subscribeTopic); Serial.println(); }

void loop() { unsigned long currentMillis = millis();

// call poll() regularly to allow the library to receive MQTT messages and // send MQTT keep alives which avoids being disconnected by the broker mqttClient.poll();

// Enforce Interval if(currentMillis - previousMillis > interval) { previousMillis = currentMillis;

    // Call a function that handles publishing message
    publishSensorVal();

} }

void publishSensorVal() { // read the first Analog pin int sensorVal = analogRead(0);
String speak = "The sensor val is " + String(sensorVal);

// Publish our sensor value to the publish topic
mqttClient.beginMessage(publishTopic);
mqttClient.print(speak);
mqttClient.endMessage();

}

void handleCommand(String cmd) { if (cmd == "on"){ digitalWrite(LED, HIGH); }else if(cmd == "off"){ digitalWrite(LED, LOW); } }

void onMqttMessage(int messageSize) { // we received a message, print out the topic and contents Serial.println("Received a message with topic '"); Serial.print(mqttClient.messageTopic()); Serial.print("', length "); Serial.print(messageSize); Serial.println(" bytes:");

// We want to read each character into a useable String String content = ""; char character;

// use the Stream interface to print the contents while (mqttClient.available()) { character = (char)mqttClient.read(); content.concat(character); }

handleCommand(content); // This function does something with our incoming string

Serial.println(content); Serial.println(); Serial.println(); // double spacing }`

and the chirper-flow.json doc is as below: [ { "id": "5199e666.cafc58", "type": "mqtt-broker", "z": "91xN7kD2zjI", "server": "wss://test.mosquitto.org:443/ws", "clientId": "test1", "username": "test1", "password": "Test1234" }, { "id": "SDgXjXm8EXM", "type": "mqtt in", "z": "91xN7kD2zjI", "name": "", "topic": "hologram-test1/from", "broker": "5199e666.cafc58", "x": 330, "y": 253, "wires": [ [ "7oz7rvrDqtw", "y3MxuNrcNhU" ] ] }, { "id": "mib-Z2IUCWQ", "type": "inject", "z": "91xN7kD2zjI", "name": "", "topic": "", "payload": "on", "payloadType": "str", "repeat": "", "crontab": "", "once": false, "allowDebugInput": false, "x": 302, "y": 66, "wires": [ [ "TCHX2DIYKn4" ] ] }, { "id": "TCHX2DIYKn4", "type": "mqtt out", "z": "91xN7kD2zjI", "name": "", "topic": "hologram-test/to", "broker": "5199e666.cafc58", "x": 533, "y": 115, "wires": [] }, { "id": "7oz7rvrDqtw", "type": "espeak", "z": "91xN7kD2zjI", "name": "", "variant": "Alex", "active": false, "x": 531, "y": 219, "wires": [] }, { "id": "y3MxuNrcNhU", "type": "debug", "z": "91xN7kD2zjI", "name": "", "active": true, "console": "false", "complete": "false", "x": 553, "y": 293, "wires": [] }, { "id": "hknJ5yqYo-E", "type": "inject", "z": "91xN7kD2zjI", "name": "", "topic": "", "payload": "off", "payloadType": "str", "repeat": "", "crontab": "", "once": false, "allowDebugInput": false, "x": 301, "y": 161, "wires": [ [ "TCHX2DIYKn4" ] ] }, { "id": "K1RAF7aNi1g", "type": "voice rec", "z": "91xN7kD2zjI", "name": "", "x": 279, "y": 114, "wires": [ [ "TCHX2DIYKn4" ] ] } ] The output of the serial monitor is 15:47:10.385 -> Attempting to connect to GSM and GPRS You're connected to the network 15:47:21.013 -> 15:47:21.013 -> Attempting to connect to the MQTT broker: test.mosquitto.org 15:47:23.178 -> You're connected to the MQTT broker! 15:47:23.178 -> 15:47:23.178 -> Subscribing to topic: hologram-test/to 15:47:23.178 -> 15:47:23.701 -> Waiting for messages on topic: hologram-test/to 15:47:23.701 -> and it is stuck there. Is there some thing that I am missing? Also if you have any more guidance or e=any example that I could use to complete my final goal of sending data from HiveMQ to mosquitto would be of a great help. Thanks a ton in advance!

ralight commented 3 years ago

I'm sorry I'm not sufficiently familiar with the code you're using to comment on how best to use it. I'd suggest that you ask that project directly.

If you do think this is a mosquitto problem, please try to reproduce it using a known good client, like mosquitto_sub so you can be sure that the rest of your setup is working correctly.

Good luck!