AnaviTechnology / anavi-thermometer-sw

Open source Arduino sketch for the smart WiFi dev board ANAVI Thermometer
GNU General Public License v3.0
27 stars 17 forks source link

MQTT esp8266 failed, rc=-1 error #43

Closed atd closed 4 years ago

atd commented 4 years ago

I have just uploaded the sketch from master branch using the instructions in the documentation, but I have to issues:

I have double checked the library versions and everything seems to be ok

The output from Arduino IDE when uploading

Executable segment sizes:
IROM   : 381628          - code in flash         (default or ICACHE_FLASH_ATTR) 
IRAM   : 28572   / 32768 - code in IRAM          (ICACHE_RAM_ATTR, ISRs...) 
DATA   : 1632  )         - initialized variables (global, static) in RAM/HEAP 
RODATA : 4988  ) / 81920 - constants             (global, static) in RAM/HEAP 
BSS    : 32960 )         - zeroed variables      (global, static) in RAM/HEAP 
El Sketch usa 416820 bytes (39%) del espacio de almacenamiento de programa. El máximo es 1044464 bytes.
Las variables Globales usan 39580 bytes (48%) de la memoria dinámica, dejando 42340 bytes para las variables locales. El máximo es 81920 bytes.
esptool.py v2.8
Serial port /dev/ttyUSB0
Connecting....
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: xx:xx:xx:xx
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 420976 bytes to 298538...

Writing at 0x00000000... (5 %)
Writing at 0x00004000... (10 %)
Writing at 0x00008000... (15 %)
Writing at 0x0000c000... (21 %)
Writing at 0x00010000... (26 %)
Writing at 0x00014000... (31 %)
Writing at 0x00018000... (36 %)
Writing at 0x0001c000... (42 %)
Writing at 0x00020000... (47 %)
Writing at 0x00024000... (52 %)
Writing at 0x00028000... (57 %)
Writing at 0x0002c000... (63 %)
Writing at 0x00030000... (68 %)
Writing at 0x00034000... (73 %)
Writing at 0x00038000... (78 %)
Writing at 0x0003c000... (84 %)
Writing at 0x00040000... (89 %)
Writing at 0x00044000... (94 %)
Writing at 0x00048000... (100 %)
Wrote 420976 bytes (298538 compressed) at 0x00000000 in 26.4 seconds (effective 127.4 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

The output from the serial monitor:

s$l⸮⸮⸮⸮#⸮o⸮⸮l⸮b|⸮⸮⸮p⸮{⸮c⸮c⸮⸮on⸮$oN⸮⸮⸮cp⸮$s$r$p⸮o⸮⸮l⸮⸮cn⸮|l⸮l⸮#⸮|⸮o⸮l⸮⸮l ⸮nod`nr⸮⸮⸮N⸮l`{⸮⸮o⸮d`⸮r⸮p⸮l⸮{⸮ ⸮⸮N⸮
MQTT: 1+N: Using single connection, multiple status topics
Mini I2C OLED Display at address 3C: OK
Press button within 2 seconds for factory reset...
mounting FS...
mounted file system
reading config file
opened config file
{"mqtt_server":"192.168.x.x","mqtt_port":"1883","workgroup":"xxx","username":"","password":"","temp_scale":"celsius","ha_name":"xxx","ota_server":""}
parsed json
*WM: Adding parameter
*WM: server
*WM: Adding parameter
*WM: port
*WM: Adding parameter
*WM: workgroup
*WM: Adding parameter
*WM: user
*WM: Adding parameter
*WM: pass
*WM: Adding parameter
*WM: temp_scale
*WM: Adding parameter
*WM: ha_name
*WM: Adding parameter
*WM: ota_server
*WM: Adding parameter
*WM: 
*WM: 
*WM: AutoConnect
*WM: Connecting as wifi client...
*WM: Status:
*WM: 6
*WM: Using last saved values, should be faster
*WM: Connection result: 
*WM: 3
*WM: IP Address:
*WM: 192.168.x.x
connected...yeey :)
local ip
192.168.x.x
MQTT Server: 192.168.x.x
MQTT Port: 1883
No MQTT username
No MQTT password
Saved temperature scale: celsius
Temperature scale: Celsius
Home Assistant sensor name: xxx
No OTA server
MQTT esp8266 failed, rc=-1

-----
Machine ID: xxx
-----

*WM: freeing allocated params!
Home Assistant discovery topic: homeassistant/sensor/xxx/temp/config
beginPublish failed!

Home Assistant discovery topic: homeassistant/sensor/xxx/humidity/config
beginPublish failed!

DHT Heat Index: 24.0°C
Air 24.5°C
Humidity 40%
WiFi -59 dBm
....

Mosquitto server logs:

1584219564: New connection from 192.168.x.x on port 1883.
1584219564: Socket error on client <unknown>, disconnecting.
1584219614: New connection from 192.168.x.x on port 1883.
1584219614: Socket error on client <unknown>, disconnecting.
1584219674: New connection from 192.168.x.x on port 1883.
1584219674: Socket error on client <unknown>, disconnecting.
1584219734: New connection from 192.168.x.x on port 1883.
1584219734: Socket error on client <unknown>, disconnecting.
1584219794: New connection from 192.168.x.x on port 1883.
1584219794: Socket error on client <unknown>, disconnecting.
1584219854: New connection from 192.168.x.x on port 1883.
1584219854: Socket error on client <unknown>, disconnecting.
...
atd commented 4 years ago

I manage to figure out both issues:

  • MQTT connection does not work

It does when using last release https://github.com/AnaviTechnology/anavi-thermometer-sw/releases/tag/1.0.1

I made a git bisect and first bad commit is 4b83874d31a2074beb862d15db8bfde9df58fe1a

So maybe we need to update some library? cc @cederlys

  • OLED Display is only on when USB to UART is disconnected. It remains blank and the red D1 led remains on

I was booting ANAVI Thermometer with the USB to UART device attached. Removing it before booting fixes this one

cederlys commented 4 years ago

I can see that you don't have any MQTT username and password set. The handling of that changed in 0b88565a4aa7d59a6b. Are you sure you have configured your MQTT broker to accept anonymous MQTT connections?

But I find it strange that you claim that 4b83874 is the commit that introduces your issue. That commit adds a will, but shouldn't change how the username and password is handled. This code works fo me, but I am using a username and password.

atd commented 4 years ago

Are you sure you have configured your MQTT broker to accept anonymous MQTT connections?

Previous commit 7c486c3ae0366c799289d86ca6253a1d8fd2bd38 works fine. So I guess it does :smile:

Besides, I can connect with MQTT Explorer without any problem (no user and password)

leon-anavi commented 4 years ago

Hi @atd, @cederlys,

Sorry for stepping in the discussion later. I've just tested. I confirm with the latest revision I am also unable to connect to a MQTT broker anonymously (without username and password). Otherwise the MQTT broker accepts anonymous connections. I am try to debug it further right now.

Thanks, Leon

leon-anavi commented 4 years ago

Hi @atd, @cederlys,

According to MQTT protocol version 5.0, the MQTT broker must allow 23 UTF-8 encoded bytes for ClientID. Some brokers MAY allow a longer ClientID but there are no guarantees: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901059

I was testing with the default configurations of mosquitto version 1.5.7 with anonymous access (no username/password for clients) and I had issues in the cases when the ClientID was long.

I have fixed the issue in e900f4fe948eca5cf7af685204126c92f30f945c by reducing length of the ClientId below 23 bytes. As a safety net during development, I have also set a default non-empty clientID, just in case that during feature debugging MQTTConnection::set_spec is never called. Some MQTT brokers might not accept connections from clients with empty ID.

@atd, I added your credentials in a "Reported-by" trailer in the commit. Thank you for noticing and reporting this very specific issues. Could you please test and confirm that it works for your case?

@cederlys thank you again for your huge contributions to the project and for the major changes related to MQTT connections and MQTT last will and testament.

Thanks, Leon

atd commented 4 years ago

I can confirm that e900f4f works ok

Thank you Leon