vshymanskyy / TinyGSM

A small Arduino library for GSM modules, that just works
GNU Lesser General Public License v3.0
1.91k stars 713 forks source link

Example for a stable MQTT client #35

Closed holgerkoch closed 6 years ago

holgerkoch commented 7 years ago

Hi, it seems, that the example MQTT client is not stable. Sometimes it works some hour, sometimes only few minutes.

Did somebody wrote a really stable MQTT client and can get me the code?

Here is my testcode, very close to the example code:

/** *

// Select your modem:

define TINY_GSM_MODEM_SIM800

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

define TINY_GSM_RX_BUFFER 512

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 Serial

// 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;

unsigned long lastSend = 0;

void setup() { // Set GSM module baud rate SerialAT.begin(115200); 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("servername", 1883); }

boolean mqttConnect() { if (!mqtt.connect("GsmClientTest", "username", "password")) { //Serial.println(" fail"); //Serial.println(mqtt.state()); return false; } //Serial.println(" OK"); delay(10000); mqtt.publish("owntracks/wohnmobil/elnagh", "GsmClientTest started"); return mqtt.connected(); }

void loop() { unsigned long time;

char msgBuffer[20];

if (mqtt.connected()) { mqtt.loop(); } else { // Reconnect every 10 seconds unsigned long t = millis(); if (t - lastReconnectAttempt > 10000L) { lastReconnectAttempt = t; if (mqttConnect()) { lastReconnectAttempt = 0; } } } if (millis()-lastSend>10000) { time = millis(); mqtt.publish("owntracks/wohnmobil/elnagh", dtostrf(time, 6, 0, msgBuffer)); lastSend=millis(); } }

best regards

Holger

himanshu-hearthacker commented 6 years ago

i am having the same problem. it works for about 15 minutes, then stop sending data. Do you find any solution yet??

holgerkoch commented 6 years ago

No! I tried much, but doesn't found a solution for this.

Holger

cimba007 commented 6 years ago

I am running the MQTT-Example now for ~1hour 40minutes (mqttspy toggles a variable every 10seconds). Can you tell me how you test your sketch? What are other clients publishing to the tinygsm-node and is the tinygsm-node publushing anything at all?

Edit: After ~3hours still working, 1% packet-loss due to 2 lost connections to broker.

mcr-ksh commented 6 years ago

have a look at issue #57. that might be that mqtt closes a socket and cant reopen it. enable Serial Debug via #define TINY_GSM_DEBUG Serial

cimba007 commented 6 years ago

Is there a way to use TINY_GSM_DEBUG with softserial?

I am running fine with this setup

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX

// Set serial for debug console (to the Serial Monitor, speed 115200)
#define SerialMon mySerial

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

#define TINY_GSM_DEBUG mySerial

But I get no real "verbose" messages

Funny thing, "Diagnostics" example is putting out the debug stuff

EDIT: FOUnd the culprit

StreamDebugger debugger(SerialAT, SerialMon); TinyGsm modem(debugger);

cimba007 commented 6 years ago

After reading https://github.com/vshymanskyy/TinyGSM/issues/57 I thought that modifying stop() to do the "AT+CIPCLOSE" if "AT+CIPCLOSE=MUX" fails with an error might be a workaround?

Not use to test this as I never got the error on mqtt on the wild.

mcr-ksh commented 6 years ago

This is basically what I do manually:

client.stop();
SerialAT.println("AT+CIPCLOSE");
cimba007 commented 6 years ago

Where in the code do you call thus?

Am 24. Juli 2017 19:22:16 MESZ schrieb mcr-ksh notifications@github.com:

This is basically what I do manually:

client.stop();
SerialAT.println("AT+CIPCLOSE");

-- You are receiving this because you commented. Reply to this email directly or view it on GitHub: https://github.com/vshymanskyy/TinyGSM/issues/35#issuecomment-317493422

-- Diese Nachricht wurde von meinem Android-Gerät mit K-9 Mail gesendet.

vshymanskyy commented 6 years ago

Please try the latest release of the library. SIM800 was improved significantly

vshymanskyy commented 6 years ago

Closing this for now, if experiencing issues with SIM800 - please reopen