knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.78k stars 1.46k forks source link

performce / timeout problem with a GSM Connection / SIM808 at 9600 baud #266

Open holgerkoch opened 7 years ago

holgerkoch commented 7 years ago

Hello, i tried really hard and read all issues. But i can't found a solution for my problem. So maybe you can give me a hint.

I try your example code for a mqtt client. If i set the connection speed for the modem to a higher value than 9600, it's working. If a set it to 9600, i get the error:

Initializing modem... Waiting for network... OK Connecting to web.vodafone.de OK Connecting to alpha.holgerkoch.de fail -4 Connecting to alpha.holgerkoch.de fail -4

and the Logfile of the mosquitto server:

1491121475: New connection from 109.43.1.92 on port 1883. 1491121475: New client connected from 109.43.1.92 as elnagh (c1, k15, u'wohnmobil'). 1491121475: Sending CONNACK to elnagh (0, 0) 1491121491: Socket error on client elnagh, disconnecting.

I use the latest version of pubsub and TinyGSM library. The MQTT Server is

[root@alpha mosquitto]# rpm -qa|grep mosquitto libmosquitto1-1.4.10-3.1.x86_64 libmosquitto-devel-1.4.10-3.1.x86_64 mosquitto-1.4.10-3.1.x86_64 mosquitto-clients-1.4.10-3.1.x86_64

I tried to raise the timeouts for

define MQTT_KEEPALIVE 60

define MQTT_SOCKET_TIMEOUT 60

but it doesn't help. From time to time, maybe once the hour, i got a connection and then it works like a dream.

Can you give me a hint?

Best regards

Holger

/** *

// Select your modem:

define TINY_GSM_MODEM_SIM800

//#define TINY_GSM_MODEM_SIM900 //#define TINY_GSM_MODEM_A6 //#define TINY_GSM_MODEM_M590

include

include

// Your GPRS credentials // Leave empty, if missing user or pass const char apn[] = "web.vodafone.de"; const char user[] = ""; const char pass[] = "";

// Use Hardware Serial on Mega, Leonardo, Micro //#define SerialAT Serial1

// or Software Serial on Uno, Nano

include

SoftwareSerial SerialAT(5, 4); // RX, TX

TinyGsm modem(SerialAT); TinyGsmClient client(modem); PubSubClient mqtt(client);

long lastReconnectAttempt = 0;

void setup() { // Set console baud rate Serial.begin(9600); delay(10);

// Set GSM module baud rate SerialAT.begin(9600); delay(3000);

// Restart takes quite some time // To skip it, call init() instead of restart() Serial.println("Initializing modem..."); modem.restart();

Serial.print("Waiting for network..."); if (!modem.waitForNetwork()) { Serial.println(" fail"); while (true); } Serial.println(" OK");

Serial.print("Connecting to "); Serial.print(apn); if (!modem.gprsConnect(apn, user, pass)) { Serial.println(" fail"); while (true); } Serial.println(" OK");

// MQTT Broker setup mqtt.setServer("alpha.holgerkoch.de", 1883); }

boolean mqttConnect() { Serial.print("Connecting to "); Serial.print("alpha.holgerkoch.de"); if (!mqtt.connect("elnagh", "", "")) { Serial.println(" fail"); Serial.println(mqtt.state()); return false; } Serial.println(" OK"); mqtt.publish("owntracks/wohnmobil/elnagh", "GsmClientTest started"); return mqtt.connected(); }

void loop() {

if (mqtt.connected()) { mqtt.loop(); } else { // Reconnect every 10 seconds unsigned long t = millis(); if (t - lastReconnectAttempt > 10000L) { lastReconnectAttempt = t; if (mqttConnect()) { lastReconnectAttempt = 0; } } } delay(5000); mqtt.publish("owntracks/wohnmobil/elnagh", "Bin drin");

}

zhoufusong commented 7 years ago

I have the same question. there are any solution now?

pengchifei commented 6 years ago

I think you should use millis() instead of delay(5000), the mqtt.loop() will not execute in time.