Hi Again. Thank your help todate.
I’m using a RAK 7249 in this configuration:- Gateway->Built-in Server->Application/device->Built-in MQTT Bridge->software on an Arduino Mega+Ethernet sheld.
Maybe my approach and software is too simple?
I wondered if you cld have a look at the code and suggest where I may have gone wrong.
Many Thanks.
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Arduino MAC address
IPAddress ip(192, 168, 1, 177); // Arduino IP address
IPAddress server(192, 168, 1, 245); // RAK7249 IP address
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);
void setup()
{
Serial.begin(9600);
mqttClient.setServer(server, 1883); // MQTT
mqttClient.setCallback(callback);
Ethernet.begin(mac, ip); // Allow the hardware to sort itself out
delay(1500);
}
void loop()
{
if (!mqttClient.connected())
{
reconnect();
}
mqttClient.loop();
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print(“Message arrived [”);
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect()
{
while (!mqttClient.connected()) // Loop until we’re reconnected
{
Serial.print(“Attempting MQTT connection…”);
if (mqttClient.connect(“Oldfield”)) // Attempt to connect
{
Serial.println(“connected
mqttClient.subscribe(“application/sensor-node-dht22/device/01f3b8f85a1c1050/”); // subscribe
}
else
{
Serial.print(“failed, rc=”);
Serial.print(mqttClient.state());
Serial.println(” try again in 5 seconds");
delay(5000); // Wait 5 seconds before retrying
}
}
}

