Hello, i am currently using 2 mkr wifi 1010 to send and recieve something on a topic. The sender of those two has an mkr env shiel. My problem is that the sender won't send anything.
screenshot of the serial monitor:
My code:
#include <Arduino_MKRENV.h>
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "test.mosquitto.org";
int port = 1883;
const char topic[] = "Temperature";
const char topic2[] = "Humidity";
const char topic3[] = "Pressure";
//set interval for sending messages (milliseconds)
const long interval = 8000;
unsigned long previousMillis = 0;
float t;
float h;
float pr;
int count = 0;
void setup() {
//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 Wifi network:
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(5000);
}
Serial.println("You're connected to the network");
Serial.println();
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();
}
void loop() {
t = ENV.readTemperature();
h = ENV.readHumidity();
pr = ENV.readPressure();
Serial.println(t);
// call poll() regularly to allow the library to send MQTT keep alive which
// avoids being disconnected by the broker
mqttClient.poll();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time a message was sent
previousMillis = currentMillis;
//record random value from A0, A1 and A2
Serial.print("Sending message to topic: ");
Serial.println(topic);
Serial.println(t);
Serial.print("Sending message to topic: ");
Serial.println(topic2);
Serial.println(h);
Serial.print("Sending message to topic: ");
Serial.println(topic3);
Serial.println(pr);
// send message, the Print interface can be used to set the message contents
mqttClient.beginMessage(topic);
mqttClient.print(t);
mqttClient.endMessage();
mqttClient.beginMessage(topic2);
mqttClient.print(h);
mqttClient.endMessage();
mqttClient.beginMessage(topic3);
mqttClient.print(pr);
mqttClient.endMessage();
Serial.println();
}
}
I was using this tutorial: https://docs.arduino.cc/tutorials/mkr-wifi-1010/mqtt-device-to-device
Can someone help?