-
Notifications
You must be signed in to change notification settings - Fork 238
Arduino MKR 1010 Wifi
Tim Pulver edited this page Apr 5, 2019
·
1 revision
The following code by blank-tree basically works with the Arduino MKR 1010 Wifi, but might stop working after a few hours (as the author reports).
#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();
}
}
Feel free to add your findings about using the MQTT library with the MKR 1010 Wifi in this issue: compatibility: ARDUINO MKR WIFI 1010 and WiFiNINA library.