Ethernet MQTT using MKRZero and MKR ETH Shield

Hello,

I’m currently working on a project to send data to a MQTT using Ethernet with an MKRZero board and a MKR ETH Shield.
I am trying to make a first communication using the example from the Arduino IDE. Here is the code used to make first tests :

// This example uses an Arduino Uno together with
// an Ethernet Shield to connect to shiftr.io.
//
// You can check on your device after a successful
// connection here: https://www.shiftr.io/try.
//
// by Joël Gähwiler
// https://github.com/256dpi/arduino-mqtt

#include <Ethernet.h>
#include <MQTT.h>

byte mac = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip = {192, 168, 137, 177};

EthernetClient net;
MQTTClient client;

unsigned long lastMillis = 0;

void connect() {
Serial.print(“connecting…”);
while (!client.connect(“arduino”, “public”, “public”)) {
Serial.print(".");
delay(1000);
}

Serial.println("\nconnected!");

client.subscribe("/hello");
// client.unsubscribe("/hello");
}

void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);

// Note: Do not use the client in the callback to publish, subscribe or
// unsubscribe as it may cause deadlocks when other things arrive while
// sending and receiving acknowledgments. Instead, change a global variable,
// or push to a queue and handle it in the loop after calling client.loop().
}

void setup() {
Serial.begin(115200);
Ethernet.begin(mac, ip);

// Note: Local domain names (e.g. “Computer.local” on OSX) are not supported
// by Arduino. You need to set the IP address directly.
client.begin(“public.cloud.shiftr.io”, net);
client.onMessage(messageReceived);

connect();
}

void loop() {
client.loop();

if (!client.connected()) {
connect();
}

// publish a message roughly every second.
if (millis() - lastMillis > 1000) {
lastMillis = millis();
client.publish("/hello", “world”);
}
}

I have downloaded all the libraries and previous tests shows that the ethernet communication is working. But when it comes to connecting to the client, it’s not working and printing “.” constently on the Serial monitor (as intended in the code in case of no connection)
For information in the Network and Sharing Center, my Ethernet port IPV4 is set to 192.168.137.1.
The code is trying to connect to the MQTT “public.cloud.shiftr.io” but it is not working. I tried with several other free online MQTT but none seems to be working.
I tried looking for answers on the forum but most posts concern WIFI and not Ethernet, and trying to adapt possible solutions didn’t work.

That’s why I’m posting this to see if someone can see a problem on the code or has an idea on how to correct my problems.
I’m also wondering if you have a specific MQTT to recommend. I tried several but I don’t know which one is the best to test. Maybe my problems come from this.

The code is trying to connect to the MQTT "public.cloud.shiftr.io" but it is not working. I tried with several other free online MQTT but none seems to be working.

Does your Arduino have access to the public Internet? If you connected it to your PC's Ethernet Port, it does not have such an access! So it cannot connect to public.cloud.shiftr.io, it probably even fails to resolve that hostname.

In the Network and Sharing Center of my computer, I shared my WIFI with the Ethernet port.
I thought it was enough.
If not do you know of a way to have access to public internet from the Arduino ?

After some tests, I managed to send data to a locally hosted MQTT, but when it is a public one, I tried few things but didn't manage to find a solution.

I thought it was enough.

I have no Windows machine, so I'm no expert what it does. Such emergency solutions often have gaps in the details. I can imagine that for example the DNS is not correctly forwarded or only web traffic is forwarded (ports 80 and 443).

If not do you know of a way to have access to public internet from the Arduino ?

Anyway, accessing the data sink by a network forwarding on a PC is no solution for a reliable project. You should connect your Arduino to the Internet router directly.

After some tests, I managed to send data to a locally hosted MQTT, but when it is a public one, I tried few things but didn't manage to find a solution.

That means the Arduino part is working as expected, the problems with your Windows machine cannot be solved here.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.