Hi all,
I have this code to publish by mqtt and it worked great for month.
#include <EthernetENC.h> //più leggera rispetto UIPEthernet
#include <PubSubClient.h>
unsigned long mytime = 0;
byte mac[] = {0x80, 0x7D, 0x3A, 0x69, 0x20, 0x99 }; //physical mac address
byte mqtt_ip[] = {mqtt ip}; // ip in lan
const char* mqttUser = "usr";
const char* mqttPassword = "pass";
int updateInterval = 10000; // Interval in milliseconds
EthernetClient espClient;
PubSubClient client(espClient);
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClientSuperior",mqttUser, mqttPassword)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void sensors() { Serial.println("TEST");}
void setup()
{
Serial.begin(115200);
delay(100);
Ethernet.begin(mac,{nano ip },{dns },{gw }); //Ethernet.begin(mac, ip, dns, gateway);
client.setServer(mqtt_ip, 1883);
}
void loop()
{
if (!client.connected()) { reconnect(); }
client.loop();
if (millis()-mytime>updateInterval)
{ mytime=millis();
sensors();
}
}
The ardware is an arduino nano with enc28j60. I use this to read some sensors connected to the nano and sending the values by mqtt,
Today I resumed this code to add another arduino nano+ enc28j60 but the code remains in loop with this output:
I tried with new nano board, new enc28j60 board but doen't work.... the code is always the same, should be an hardware problem?
You know the if (client.connect("arduinoClientSuperior",mqttUser, mqttPassword)) will not happen because your while loop will have exited if this condition is true?
void connectToMQTT()
{
byte mac[5]; // create client ID from mac address
WiFi.macAddress(mac); // get mac address
String clientID = String(mac[0]) + String(mac[4]) ; // use mac address to create clientID
while ( !MQTTclient.connected() )
{
MQTTclient.connect( clientID.c_str(), mqtt_username, mqtt_password );
vTaskDelay( 250 );
}
MQTTclient.setCallback( mqttCallback );
MQTTclient.subscribe( topicOK );
MQTTclient.subscribe( topicGrpA );
MQTTclient.subscribe( topicGrpB );
MQTTclient.subscribe( topicGrpC );
MQTTclient.subscribe( topicGrpD );
MQTTclient.subscribe( topicGrpE );
MQTTclient.subscribe( topicFlash );
} //void connectToMQTT()
Here is my code to maintain a constant WiFi and MQTT connection:
void MQTTkeepalive( void *pvParameters )
{
sema_MQTT_KeepAlive = xSemaphoreCreateBinary();
xSemaphoreGive( sema_MQTT_KeepAlive ); // found keep alive can mess with a publish, stop keep alive during publish
MQTTclient.setKeepAlive( 90 ); // setting keep alive to 90 seconds makes for a very reliable connection, must be set before the 1st connection is made.
TickType_t xLastWakeTime = xTaskGetTickCount();
const TickType_t xFrequency = 250; //delay for ms
for (;;)
{
//check for a is-connected and if the WiFi 'thinks' its connected, found checking on both is more realible than just a single check
if ( (wifiClient.connected()) && (WiFi.status() == WL_CONNECTED) )
{
xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY ); // !!!!!whiles MQTTlient.loop() is running no other mqtt operations should be in process!!!!!
MQTTclient.loop();
xSemaphoreGive( sema_MQTT_KeepAlive );
}
else {
if ( !(wifiClient.connected()) || !(WiFi.status() == WL_CONNECTED) )
{
connectToWiFi();
}
connectToMQTT();
}
xLastWakeTime = xTaskGetTickCount();
vTaskDelayUntil( &xLastWakeTime, xFrequency );
}
vTaskDelete ( NULL );
}
////
Perhaps there are a few things in that code you can use to get your thing do working.
If the LAN connection has failed then the MQTT connection has failed. If the program is trying to reconnect to the MQTT Broker with a failed LAN connection then how will the Broker connection be re-established?
understood....
I'll try to convert the wifi part in ethernet....
Do you know what is the equivalent of this:
(wifiClient.connected()) && (WiFi.status() == WL_CONNECTED)?