hello everyone,
i have a problem with my arduino project. The purpose of the project is to show the data collected by a sensor connected to an arduino wifi rev 2 board on the serial monitor using the wifi connection and mqtt broker. I managed to establish the connection between broker and arduino, but the collected data is not shown on the serial monitor. I think it is a script problem ? I am new in this field and don't know how to allow the system to show them to me or what codes or scripts to use.
Could anyone help me, do you have any ideas?
Thanks in advance.
Welcome to the forum
We need much more information about your project and code. Start by posting your code, using code tags when you do. Which sensor are you using and how is it connected to the Arduino ? How is the Arduino powered ?
It is not clear to me what part MQTT has in your project. Are you saying that the data is being published to the broker but is not displayed on the local Serial monitor ?
i want to exploit the mqtt protocol to establish a wifi connection between pc and the arduino board to which the heart rate finger sensor is connected. I have already made the version without wifi and this is the script I used to allow the sensor data to be viewed on the serial monitor.
Next I installed the mqtt broker to allow the creation of the publisher - subscriber hierarchy and started working on the publisher by introducing a script that would allow arduino to connect to our wifi router and then to the broker.
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "test.mosquitto.org";
int port = 1883;
const char topic[] = "real_unique_topic";
const char topic2[] = "real_unique_topic_2";
const char topic3[] = "real_unique_topic_3";
//set interval for sending messages (milliseconds)
const long interval = 8000;
unsigned long previousMillis = 0;
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() {
// 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
int Rvalue = analogRead(A0);
int Rvalue2 = analogRead(A1);
int Rvalue3 = analogRead(A2);
Serial.print("Sending message to topic: ");
Serial.println(topic);
Serial.println(Rvalue);
Serial.print("Sending message to topic: ");
Serial.println(topic2);
Serial.println(Rvalue2);
Serial.print("Sending message to topic: ");
Serial.println(topic2);
Serial.println(Rvalue3);
// send message, the Print interface can be used to set the message contents
mqttClient.beginMessage(topic);
mqttClient.print(Rvalue);
mqttClient.endMessage();
mqttClient.beginMessage(topic2);
mqttClient.print(Rvalue2);
mqttClient.endMessage();
mqttClient.beginMessage(topic3);
mqttClient.print(Rvalue3);
mqttClient.endMessage();
Serial.println();
}
}
Did you miss the request
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "test.mosquitto.org";
int port = 1883;
const char topic[] = "real_unique_topic";
const char topic2[] = "real_unique_topic_2";
const char topic3[] = "real_unique_topic_3";
//set interval for sending messages (milliseconds)
const long interval = 8000;
unsigned long previousMillis = 0;
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() {
// 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
int Rvalue = analogRead(A0);
int Rvalue2 = analogRead(A1);
int Rvalue3 = analogRead(A2);
Serial.print("Sending message to topic: ");
Serial.println(topic);
Serial.println(Rvalue);
Serial.print("Sending message to topic: ");
Serial.println(topic2);
Serial.println(Rvalue2);
Serial.print("Sending message to topic: ");
Serial.println(topic2);
Serial.println(Rvalue3);
// send message, the Print interface can be used to set the message contents
mqttClient.beginMessage(topic);
mqttClient.print(Rvalue);
mqttClient.endMessage();
mqttClient.beginMessage(topic2);
mqttClient.print(Rvalue2);
mqttClient.endMessage();
mqttClient.beginMessage(topic3);
mqttClient.print(Rvalue3);
mqttClient.endMessage();
Serial.println();
}
}
this is the final code that i use
arduino is connected to the computer via USB cable and uses the connection of an external router. Between broker and computer I was able to establish the connection and mqtt I need to sort the data coming from the sensor and publish them directly on the serial monitor of the arduino sketch.
Answering your question, you can not see the data on the serial monitor, but only the part related to the connection and not the data sent.
Are you saying that when you analogRead() say Rvalue from pin A1 then print it to the Serial monitor then you see nothing ?
and how i can see the data from the sensor to the serial monitor?
That is what
Serial.println(Rvalue);
in your code should be doing
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
#include <Wire.h>
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
WiFiClient wifiClient; // creates WiFi client
MqttClient mqttClient(wifiClient); // connects the WiFi to the MQTT client
const char broker[] = "192.168.0.101"; //IP address of the pc
int port = 1883; // port broker
const char topic[] = "toto";
unsigned char c = 0;
//set interval for sending messages (milliseconds)
const long interval = 8000;
unsigned long previousMillis = 0;
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(WiFi.localIP());
Serial.println();
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
// mqttClient.connect(broker, port) make a connection beetwen the broker and the port
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();
}
// connessione riuscita (ok!)
void loop() {
// call poll() regularly to allow the library to send MQTT keep alive which
// avoids being disconnected by the broker
mqttClient.poll(); // keeps the connection alive, used in the loop()
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time a message was sent
previousMillis = currentMillis;
//record random value
int Rvalue = analogRead(c);
Wire.requestFrom(0xA0 >> 1, 1); // request 1 byte from sensor
while(Wire.available()) { // sensor may send less than requested
c = Wire.read(); // receive heart rate value (a byte)
Serial.println(c, DEC); // print heart rate value
}
Serial.print("Sending message to topic: ");
Serial.println(topic);
Serial.println(c);
// send message, the Print interface can be used to set the message contents
mqttClient.beginMessage(topic); // creates a new message to be published
mqttClient.print(c); // prints the content of message between ()
mqttClient.endMessage(); // publishes the message to the broker
Serial.println();
}
}
i put it in the script but it doesn't work
What do you see in the Serial monitor ?
in the Serial monitor i only see the successful connection between broker and network and I don't understand what I have to change in the script to show the data
Start by putting some debugging prints in the code so that you know which sections are being executed and how often
For instance, does this this test ever return true ?
if (currentMillis - previousMillis >= interval)
i've already entered the code, this is a part of my script:
nt Rvalue = analogRead(c);
Wire.requestFrom(0xA0 >> 1, 1); // request 1 byte from sensor
while(Wire.available()) { // sensor may send less than requested
c = Wire.read(); // receive heart rate value (a byte)
Serial.println(c, DEC); // print heart rate value
}
Serial.print("Sending message to topic: ");
Serial.println(topic);
Serial.println(c);