Hi everyone! I am new to the arduino world, I am trying to control a light through an app and the Pub/sub model through a public Mqtt broker.
I am using an arduino UNO and an Arduino 2 Ethernet shield. I found a sketch- attached below - which runs perfectly and allows me to control the light through the developed app which publish on the broker. The light is powerd by an external battery.
After a few hours, however, it stops working and I have to restart arduino, to make it work again.
It seems that arduino after some time “stops checking” any new messages published on the broker by the app.
I am not sure if the issue might be the broker that i am using (test.mosquitto.org) or something in the loop or something else.
[code]
#include <SPI.h>
#include <Ethernet2.h>
#include <PubSubClient.h>
byte mac[] = { 0xD4, 0x23, 0x76, 0x87, 0x90, 0x87 };
IPAddress staticIP( 192, 168, 1, 123);
EthernetClient client;
void connectToInternet()
{
// Attempt to connect to Ethernet with DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.print("[ERROR] Failed to Configure Ethernet using DHCP");
// DHCP failed, attempt to connect to Ethernet with static IP
Ethernet.begin(mac, staticIP);
}
// Delay to let Ethernet shield initialize
delay(1000);
// Connection successful
Serial.println("[INFO] Connection Successful");
Serial.print("");
printConnectionInformation();
Serial.println("-----------------------------------------------");
Serial.println("");
}
void printConnectionInformation()
{
// Print Connection Information
Serial.print("[INFO] IP Address: ");
Serial.println(Ethernet.localIP());
Serial.print("[INFO] Subnet Mask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("[INFO] Gateway: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("[INFO] DNS: ");
Serial.println(Ethernet.dnsServerIP());
}
// IP address of the MQTT broker
char server[] = { "test.mosquitto.org"};
int port = 1883;
char topic[] = { "codifythings/lightcontrol"};
PubSubClient pubSubClient(server, 1883, callback, client);
void callback(char* topic, byte* payload, unsigned int length)
{
// Print payload
String payloadContent = String((char *)payload);
Serial.println("[INFO] Payload: " + payloadContent);
// Turn lights on/off
turnLightsOnOff();
}
void turnLightsOnOff()
{
// Check if lights are currently on or off
if (digitalRead(9) == LOW)
{
//Turn lights on
Serial.println("[INFO] Turning lights on");
digitalWrite(9, HIGH);
}
else
{
// Turn lights off
Serial.println("[INFO] Turning lights off");
digitalWrite(9, LOW);
}
}
void setup()
{
// Initialize serial port
Serial.begin(9600);
// Connect Arduino to internet
connectToInternet();
//Connect MQTT Broker
Serial.println("[INFO] Connecting to MQTT Broker");
if (pubSubClient.connect("arduinoClient"))
{
Serial.println("[INFO] Connection to MQTT Broker Successful");
pubSubClient.subscribe(topic);
Serial.println("[INFO] Successfully Subscribed to MQTT Topic ");
// Set LED pin mode
pinMode(9, OUTPUT);
}
else
{
Serial.println("[INFO] Connection to MQTT Broker Failed");
}
}
void loop()
{
// Wait for messages from MQTT broker
pubSubClient.loop();
}
[/code]
Thank you for any help!