Closed holgerkoch closed 7 years ago
Hello, The first mistake i see in your code (might not be a big one) is that you have a 5 seconds delay in loop. This delay will block the loop and you won't be able to receive anything in this time. This also might cause a reconnect. Try to use millis() for an interval to publush data.
Hello,
thank you for your answer. I removed the delay, but it doesn't help. My problem is, that the connect not work:
if (!mqtt.connect("elnagh", "
Connecting to alpha.holgerkoch.de fail -4
The error code is -4, timeout. But which? I increase both
but it doesn't help. If i increase the serial connection speed to 19200 baud. It works, but only for this example. In my very large project, i still get the timeout.
best regards Holger Koch
Hello,
i try your original example for a mqtt client:
https://github.com/vshymanskyy/TinyGSM/tree/master/examples/MqttClient
and make some tests with it. With a serial speed of
SerialAT.begin(115200);
it works nearly always. If you lower the speed, it works only irregular. With a speed of 9600, it works never.
You got a -4, timeout.
Initializing modem... Waiting for network... OK Connecting to web.vodafone.de OK Connecting to test.mosquitto.org fail
Can you tell me, which timeout this means? Is there a possibility to enlarge it?
best regards
Holger
Hello,
i make some further tests with tcpdump and it seems, the network traffic is the same. Whether it works or not.
The state timeout would be set in the "PubSubClient.cpp" line 195, if the client doesn't response in the timeout interval.
while (!_client->available()) {
unsigned long t = millis();
if (t-lastInActivity >= ((int32_t) MQTT_SOCKET_TIMEOUT*1000UL)) {
_state = MQTT_CONNECTION_TIMEOUT;
_client->stop();
return false;
}
}
Please, can you tell me where tinygsm handle the client network connection? Do you have a hint for me, why the communication between the PubSubClient and the TinyGSM library is interrupted?
best regards
Holger
I have the same question, there are any solution now?
No, at the moment there isn't a solution for this problem. I think the problem is the communcation between the pubsubclient and the Client.h In a other library, the pubsubclient is customized for this. Maybe the original can't be stabil for some timing problems. Do you have a ideal for me?
Holger
Hello Holger. Thanks for your post. I tried the higher baudrate and this solved the problem for me.
Hello,
Unfortunately this is not a definitive solution. Only the probability that it works will be better. It seems that the client.h simulation for SIM800 is not working perfectly.
Holger
I think, there is somewhere a timeout problem or a race condition. But without help, i can't find the problem.
Holger
Unfortunately I don't have much time currently to test this, maybe in few weeks...
Thank you for your response. Until you have time for this, can you give me a hint, where to start to analyse?
Best regards
Holger
9600 with SoftwareSerial has some drawbacks with interrupts and timing.
https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
AltSoftSerial can tolerate almost 1 bit time latency, so its baud rate must be at least 10 times less than the baud rate used for SoftwareSerial.
Because SoftwareSerial creates 10 bit times of latency for other libraries, it should be used for a device needing high baud rate. SoftwareSerial should NOT be used at slow baud rates, because it will interfere with the other ports.
Wow @bsiege , thanks for pointing this out! Could anyone check if it really helps? You should be able to use AltSoftSerial with TinyGSM as well.
Unfortunately, i can't test the lib, because i use a wemos. It seems, that the library is only for Arduino Uno:
"Yet another alternate software serial exists for only Arduino Uno, using timer2 and pins 3 and 4. It appears to only work with the ATMEGA328 chip on Uno."
I will try it with the hardware RX/TX, but than i haven't a console log...
Is there another alternative library for wemos?
Best regards Holger
@holgerkoch Beside from using another optimized SoftwareSerial library. The point is: Try to use as fast as possible/working with any SS, because slower speeds do longer block the system for other subsystems that use interrupts too. Compare with explanations here http://forum.arduino.cc/index.php?topic=206283.0
SoftwareSerial library turns off interrupts during a character transmit, which will severely affect the timing of timer1 interrupts used by Servo library.
And if you need a console log: For mostly sending, this is much less critical with SoftwareSerial (IMHO)
@bsiege, you are right. I try the hardware TX/RX, serial. And it works perfect. Now i make a longterm test and if it works stable, than it is a solution for me. After the program is finished, i don't need the console anymore.
Best regards
Holger
Thanks guys, I just updated the README to make life easier for others.
Hi holgerkoch, I am using a6c gsm module and i have the same timeout problem in below line: while (!_client->available()) { unsigned long t = millis(); if (t-lastInActivity >= ((int32_t) MQTT_SOCKET_TIMEOUT*1000UL)) { _state = MQTT_CONNECTION_TIMEOUT; _client->stop(); return false; } }
And another problem is that the A6c gsm module is not working in 115200 baud also.could you please help me out. Note :- I am using arduino uno hardware serial(pin:0,1). could you please share me your working code (tinygsm library and mqtt library,and the .ino file.). I need your help.please help me out if possible. my mail id:-psatya111@gmail.com
@psatya111 it was commented around here that SoftwareSerial has some trouble to deal with the necessary latency for the lowest and highest baud rates, you may try to change the modem baud rate and/or change your serial library in favor of AltSoftSerial. You may find a better explanation here: https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
Hi Anibald, Thank you very much for your reply but I am using H/W serial (not software serial) but any way I will try it with different baud rate. and the link you have given is not working(error 404).
Any other approach or help will be appreciated.
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:
and the Logfile of the mosquitto server:
I use the latest version of pubsub and TinyGSM library. The MQTT Server is
I tried to raise the timeouts for
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