I want to send data from sensors that are connected to my Arduino UNO R4 WiFi [https://amzn.eu/d/8ZAUdRp](https://Arduino UNO R4 Wifi) to node-red via my Wifi. I have found many sketches, but they all had some kind of flaw so they did not even compile. I have already tested that my Arduino can connect to the internet and I have set up an MQTT broker in my node-red. Now I just need a proper code to get started. Can anybody help me, please?
#include <WiFi.h>
#include <PubSubClient.h>
// WiFi
const char *ssid = "xxxxxxxx"; // Enter your WiFi name
const char *password = "xxxxxxxx"; // Enter WiFi password
// MQTT Broker
const char *mqtt_broker = "broker.emqx.io";
const char *topic = "emqx/esp32";
const char *mqtt_username = "emqx";
const char *mqtt_password = "public";
const int mqtt_port = 1883;
// Set software serial baud to 115200;
Serial.begin(115200);
// Connecting to a Wi-Fi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {
String client_id = "esp32-client-";
client_id += String(WiFi.macAddress());
Serial.printf("The client %s connects to the public MQTT broker\n", client_id.c_str());
if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
Serial.println("Public EMQX MQTT broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
For example this code does not compile, because it shows : Compilation error: 'WiFi' does not name a type
I have looked up this problem and many people encountered the same issue, however there does not seem to be a proper solution... I have already downloaded many libaries...
I have tried the following Example: WiFiSimpleReceive
*
ArduinoMqttClient - WiFi Simple Receive
This example connects to a MQTT broker and subscribes to a single topic.
When a message is received it prints the message to the Serial Monitor.
The circuit:
- Arduino MKR 1000, MKR 1010 or Uno WiFi Rev2 board
This example code is in the public domain.
*/
#include <ArduinoMqttClient.h>
#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2)
#include <WiFiNINA.h>
#elif defined(ARDUINO_SAMD_MKR1000)
#include <WiFi101.h>
#elif defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_GIGA) || defined(ARDUINO_OPTA)
#include <WiFi.h>
#elif defined(ARDUINO_PORTENTA_C33)
#include <WiFiC3.h>
#elif defined(ARDUINO_UNOR4_WIFI)
#include <WiFiS3.h>
#endif
//#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "xxxxxxxx"; // your network SSID (name)
char pass[] = "xxxxxxxxx"; // your network password (use for WPA, or use as key for WEP)
// To connect with SSL/TLS:
// 1) Change WiFiClient to WiFiSSLClient.
// 2) Change port value from 1883 to 8883.
// 3) Change broker value to a server with a known SSL/TLS root certificate
// flashed in the WiFi module.
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "TestBroker";
int port = 1883;
const char topic[] = "arduino/simple";
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();
// You can provide a unique client ID, if not set the library uses Arduino-millis()
// Each client must have a unique client ID
// mqttClient.setId("clientId");
// You can provide a username and password for authentication
// mqttClient.setUsernamePassword("username", "password");
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();
Serial.print("Subscribing to topic: ");
Serial.println(topic);
Serial.println();
// subscribe to a topic
mqttClient.subscribe(topic);
// topics can be unsubscribed using:
// mqttClient.unsubscribe(topic);
Serial.print("Waiting for messages on topic: ");
Serial.println(topic);
Serial.println();
}
void loop() {
int messageSize = mqttClient.parseMessage();
if (messageSize) {
// we received a message, print out the topic and contents
Serial.print("Received a message with topic '");
Serial.print(mqttClient.messageTopic());
Serial.print("', length ");
Serial.print(messageSize);
Serial.println(" bytes:");
// use the Stream interface to print the contents
while (mqttClient.available()) {
Serial.print((char)mqttClient.read());
}
Serial.println();
Serial.println();
}
}
and it compiled and was able to connect to my Wifi, however I still can´t connect to the MQTT broker as I get the following message:
Attempting to connect to WPA SSID: xxxxxxxxxxx
You're connected to the network
Attempting to connect to the MQTT broker: TestBroker
MQTT connection failed! Error code = -2
The port of my MQTT Broker in node-red is:
I guess for const char broker[] = "TestBroker"; I have to write something different than just the name...
Your help is much appreciated! I am sorry that I came across as impolite...
I am just frustrated that I couldn't make it work as I imagined it being more straight forward😒.
I will try a few things and come back here, when I made progress.
with the help of my friend Fabian, I have managed to find a solution:
Open the Example: WiFi UDP Send and Receive String for Arduino UNO R4 Wifi.
Add your network name and password under "arduino_secrets.h".
Copy the number of the local port
unsigned int localPort = 2394;
Start Node-Red and connect an inject with Udp out. In Udp out add the number of the local port, the ID from IPAddress remoteIp(XXXXXXXX); and send a test message. You can also choose to attach to local port, which you can see on the serial monitor (IDE) after a message is received.
If it works, add a Udp in and connect it to a debug node. In Udp in add the number of the local port (in this case 2394) and that´s it