Hey all, Thanks to this forum I've been able to get this code working.
I'm sure this is an easy thing to do but what would be the correct wait to tell the Arduino to move past connecting the Mqtt broker if it can't after 30 second and make the code in the loop work after?
Thanks!
Here is the code I'm working with.
/*
Arduino MQTT Prototype
*/
#include <ezButton.h>
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h> // for MKR1000 change to: #include <WiFi101.h>
//#include <WiFi101.h>
#include <Ethernet.h>
#include <SPI.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)
// 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.
String msg1 ;
const char broker[] = "192.168.1.123"; // Address of the MQTT server
int port = 1883;
const char topic[] = "mqtt/main";
const char subtopic[] = "mqtt/one";
const long interval = 1000;
unsigned long previousMillis = 0;
int Enet = 0; //0 = Ethernet connection is not available
int status = false;
int led = 1;
int led2 = 2;
WiFiClient wifiClient;
EthernetClient ethernetClient;
MqttClient *mqttClient;
ezButton button(A1); // create ezButton object that attach to pin A1;
String subMessage = "";
void setup() {
//Initialize serial
Serial.begin(9600);
button.setDebounceTime(50); // set debounce time to 50 milliseconds
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
digitalWrite(led2, LOW);
if (Ethernet.linkStatus() == LinkON) {
Serial.print("Ethernet cable is connected.");
Enet == 1; }
else { Serial.print("Ethernet cable is not connected.");
Enet == 0; }
// attempt to connect to Wifi network:
if (Enet == 0) {
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();
//If Ethernet is not availabe
if (Enet == 1) {
mqttClient = new MqttClient(ethernetClient);
} else {
mqttClient = new MqttClient(wifiClient);
}
// 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("one");
// You can provide a username and password for authentication
// mqttClient.setUsernamePassword("user", "password");
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
while (!mqttClient->connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient->connectError());
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
// set the message receive callback
mqttClient->onMessage(onMqttMessage);
// subscribe to a topic
mqttClient->subscribe(subtopic);
}
void loop() {
button.loop(); // MUST call the loop() function first
// call poll() regularly to allow the library to send MQTT keep alives which
// avoids being disconnected by the broker
mqttClient->poll();
mqttClient->loop();
if(button.isPressed()) {
Serial.println("The button is pressed");
// toggle state of LED
status = !status;
// control LED arccoding to the toggleed sate
digitalWrite(led, status);
if (status==true){
ON();
}
if (status==false){
OFF();
}
}
}
void onMqttMessage(int messageSize) {
// we received a message, print out the topic and contents
Serial.println("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()) {
msg1 +=(char)mqttClient->read();
Serial.println(msg1);
}
if (msg1 == "ON"){
digitalWrite(led, true);
}
if (msg1 == "OFF"){
digitalWrite(led, false);
}
msg1="";
}
void ON(){
mqttClient->beginMessage(topic);
mqttClient->print("on1");
mqttClient->endMessage();
Serial.println("Sent MQTT message.");
}
void OFF(){
mqttClient->beginMessage(topic);
mqttClient->print("off1");
mqttClient->endMessage();
Serial.println("Sent MQTT message.");
}