Closed blank-tree closed 5 years ago
I'm going to create a pull request when I've tested this thoroughly:
#include <WiFiNINA.h>
#include <MQTT.h>
const char WIFI_SSID[] = "ssid"; // WiFI ssid
const char WIFI_PASS[] = "pass"; //WiFI password
const char mqttServer[] = "broker"; // broker, with shiftr.io it's "broker.shiftr.io"
const int mqttServerPort = 1883; // broker mqtt port
const char key[] = "key"; // broker key
const char secret[] = "secret"; // broker secret
const char device[] = "arduino"; // broker device identifier
int status = WL_IDLE_STATUS;
WiFiClient net;
MQTTClient client;
unsigned long lastMillis = 0;
void connect() {
Serial.print("checking wifi...");
while ( status != WL_CONNECTED) {
status = WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected to WiFi!\n");
client.begin(mqttServer, mqttServerPort, net);
Serial.println("connecting to broker...");
while (!client.connect(device, key, secret)) {
Serial.print(".");
delay(1000);
}
Serial.println("Connected to MQTT");
client.onMessage(messageReceived);
client.subscribe("/hello");
// client.unsubscribe("/hello");
}
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
}
void setup() {
Serial.begin(115200);
connect();
}
void loop() {
client.loop();
// delay(1000); // helps eventually
if (!net.connected()) {
connect();
}
if (millis() - lastMillis > 1000) {
lastMillis = millis();
client.publish("/hello", "world");
lastMillis = millis();
}
}
I'm closing this for now. If you have some updates please reopen. :-)
Any progress on this @blank-tree? Would be great to have Arduino MKR 1010 Wifi support with Arduino-MQTT.
The code above seems to be working fine. Just made a quick test with the shiftr-server.
@timpulver It would be very cool if you could add the example to the wiki (https://github.com/256dpi/arduino-mqtt/wiki) for future reference.
@timpulver @256dpi The "ARDUINO MKR WIFI 1010" just dies after a few hours of running the code above and I wasn't able to find the problem yet and tested it with 3 of them. .. :-/
@blank-tree So far I didn’t have any problems with my Arduino MKR 1010 Wifi. I didn’t run it three hours in a row. Will report back once I tried.
@256dpi I added it to the wiki.
I do have a lot of disconnects / reconnects though.
@256dpi If you had a donation channel I would donate a half Arduino MKR 1010 Wifi to support development.
I've got the same issue. Disconnects after a time (3-4 hours).
Why do you thing that delay(1000); inside the loop would help? I have seen this on other sites too...
Any experience? Recomendations for keepAlive-time (= logInterval? ) and timeout (maybe 3000ms)?
The new "ARDUINO MKR WIFI 1010" makes use of a "ESP32" wifi-chip. The library "Wifi101" doesn't work with the "EPS32" and Arduino recommends to use the "WiFiNINA" library to use with the 1010. "WiFiNINA" doesn't provide the methods "connect()", "connected()", "available()" etc... which are used by this library. Are there plans to expand this library to make use of the "EPS32" chip and/or is there an easy workaround to this problem to make us of the "shiftr.io" platform? Links: ARDUINO MKR WIFI 1010 ARDUINO LIBRARY: WiFiNINA