Hi,
I am new to node-red and IoT databases. I am in frustration as i cannot figure out why i cannot connect to an MQTT broker using my arduino nano rp2040. I can successfully connect on node-red using the aedes MQTT broker. I'll attach both my Arduino code and node-red code below
Any help would be much provided in how to access this.
#include <PubSubClient.h>
#include <SPI.h>
#include <WiFiNINA.h>
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID
char pass[] = SECRET_PASS; // your network password
int status = WL_IDLE_STATUS; // the WiFi radio's status
const char* mqtt_server = "192.168.1.12";
WiFiClient rp2040Client;
PubSubClient client(rp2040Client);
unsigned long lastMillis = 0;
void wificonnect() {
// Connecting to a WiFi network
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
Serial.println("In reconnect...");
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("hello")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
*void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
if (topic == "/hello") {
if (payload == "open") {
Serial.println("open");
digitalWrite(LED_BUILTIN, HIGH);
} else if (payload == "closed") {
Serial.println("closed");
digitalWrite(LED_BUILTIN, LOW);
}
}
}*/
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
// wificonnect();
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
client.setServer(mqtt_server, 1883);
}
void loop() {
if (!client.connected()) {
reconnect();
}
// publish a message roughly every second.
if (millis() - lastMillis > 1000) {
lastMillis = millis();
client.publish("/hello", "world"); //PUBLISH TO TOPIC /hello MSG world
}
}
[
{
"id": "6a69d8ab81cc5798",
"type": "tab",
"label": "Flow 2",
"disabled": false,
"info": ""
},
{
"id": "3379850d.902a8a",
"type": "mqtt in",
"z": "6a69d8ab81cc5798",
"name": "",
"topic": "#",
"qos": "0",
"datatype": "auto",
"broker": "cce024b53e0d96d6",
"nl": false,
"rap": false,
"x": 115,
"y": 260,
"wires": [
[
"9b59d15d.6d1d28"
]
],
"l": false
},
{
"id": "9b59d15d.6d1d28",
"type": "debug",
"z": "6a69d8ab81cc5798",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 450,
"y": 300,
"wires": []
},
{
"id": "6ff9ea89.aac6e4",
"type": "inject",
"z": "6a69d8ab81cc5798",
"name": "inject \"open\" command",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "/hello",
"payload": "open",
"payloadType": "str",
"x": 200,
"y": 400,
"wires": [
[
"97f6e1c5.ab547"
]
]
},
{
"id": "97f6e1c5.ab547",
"type": "mqtt out",
"z": "6a69d8ab81cc5798",
"name": "",
"topic": "",
"qos": "",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "cce024b53e0d96d6",
"x": 500,
"y": 420,
"wires": []
},
{
"id": "c8e9cb97.9ddfb",
"type": "inject",
"z": "6a69d8ab81cc5798",
"name": "inject \"closed\" command",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "/hello",
"payload": "closed",
"payloadType": "str",
"x": 210,
"y": 480,
"wires": [
[
"97f6e1c5.ab547"
]
]
},
{
"id": "bff256acbf91a40a",
"type": "aedes broker",
"z": "6a69d8ab81cc5798",
"name": "",
"mqtt_port": 1883,
"mqtt_ws_bind": "port",
"mqtt_ws_port": null,
"mqtt_ws_path": "",
"cert": "",
"key": "",
"certname": "",
"keyname": "",
"dburl": "",
"usetls": false,
"x": 300,
"y": 160,
"wires": [
[]
]
},
{
"id": "cce024b53e0d96d6",
"type": "mqtt-broker",
"name": "Aedes",
"broker": "localhost",
"port": "1883",
"clientid": "",
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"sessionExpiry": ""
}
]