Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mqtt client won't connect to broker on same esp8266 (127.0.0.1) #47

Closed
pottendo opened this issue Dec 6, 2020 · 1 comment
Closed

Comments

@pottendo
Copy link

pottendo commented Dec 6, 2020

hi,
this may be related to
#16
but I couldn't try this yet.

My issue:
I've successfully got the broker running on a esp8266 / Arduino.
One can connect from other (Linux or Raspi) boxes flawlessly and use the broker (tried with mosquitto clients)
However connecting a client from the very same esp8266 won't connect - the client stays in stat 'TCP_CONNECTING' (state 12, see below).

I've tried to connect to '127.0.0.1' or the local IP (see below) - no difference.

Interesting is that if I let the esp8266 client connect to another broker (on a RPi) in my network it works as expected and the published messages are processed.

I wonder how the 'localhost' path can be any different - but I'm not really an expert on IP programming.
I've tried the 'fix' from the other post to add 'WiFi.mode(WIFI_STA)' ... but this didn't change anything.
Note that I'm using the 'AutoConnect' library to get the IP from the nvs, or offer the Wifi selection webserver on bootstrap + some goodies like OTA. All this works nicely. So the general IP connectivity (from outside) seems to be there.

Any hints would be very much appreciated.

thx, pottendo

Here the debug log:
[...]
Setting up Wifi...done.
Setting up local time...Sun Dec 6 20:00:10 2020
Setting hostname fcce
Starting MQTT broker
Starting MQTT server on port 1883
MQTT:InitConnection
MQTT:InitClient
connect mqtt client...
MQTT: Client task activated - state 0
TCP: Connect to ip 192.168.188.34:1883
MQTT: queue subscribe, topic"/mytopic", id: 1
MQTT: Client task activated - state 12
MQTT: queuing publish, length: 26, queue size(17/2048)
MQTT: Client task activated - state 12
MQTT: queuing publish, length: 26, queue size(45/2048)
MQTT: Client task activated - state 12
[...]

and the code (follows pretty much the example):

[...]
void setup_mqtt(void)
{
delay(100);
mqtt_client = new MQTT{my_clientID.c_str(), "192.168.188.34" /WiFi.localIP().toString().c_str()/, 1883};
// setup callbacks
mqtt_client->onConnected(myConnectedCb);
mqtt_client->onDisconnected(myDisconnectedCb);
mqtt_client->onPublished(myPublishedCb);
mqtt_client->onData(myDataCb);
log_msg("connect mqtt client...");
delay(100);
mqtt_client->connect();
delay(100);
mqtt_client->subscribe("/mytopic");
}

void mqtt_publish(String topic, String msg)
{
//log_msg("publishing: " + my_clientID + topic + msg);
mqtt_client->publish(my_clientID + topic, msg, 0, 0);
}

@pottendo
Copy link
Author

pottendo commented Dec 7, 2020

hi,
as I stumbled over the '5x connection and then connection error' issue as well, I've activated lwip <2!
Still no success (but 5+ connections possible) BUT with changing the client connecting from '127.0.0.1' (or "localhost") to the real IP address ('WiFi.localIO()') it works like a charm!
my code now:
[...]
mqtt_client = new MQTT{my_clientID.c_str(), WiFi.localIP().toString().c_str(), 1883};
[...]

Note that I'm using PlatformIO and here activating lwip 1.x one needs to add
build_flags = -DPIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH
in platformio.ini
However then build failed in my setup with some 'ipv4_addr_t' being undefined.
I 'fixed' that by adding the typedef myself to
~/.platformio/packages/framework-arduinoespressif8266/tools/sdk/include/ipv4_addr.h
[...]
///////////////////////////////////////////////
#if LWIP_VERSION_MAJOR == 1

#define ipv4_addr ip_addr
typedef struct ip_addr ipv4_addr_t;
[...]

Hope this may help others - cheers, pottendo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant