pubsubclient: Failing to publish to MQTT from Arduino

Hello Folks,

I can’t get Arduino to publish messages to MQTT via PubSubClient.

Here is the step-by-step of what I tested (this is just my mental algorithm, not the actual code):

  • Initialize ESP8266… [OK]
  • Connect to wireless network… [OK]
  • Ping MQTT server from Arduino… [OK]
  • Ping Arduino from laptop… [OK]
  • Connect to MQTT from Arduino… [OK]??
  • Publish message to MQTT… [FAIL] (no error message)

Here is the environment for the sketch I am showing below:

Structure: Arduino >> (WiFiEsp + PubSubClient) >> MQTT Arduino: 10.0.0.17 MQTT Server: 10.0.0.234 (Mosquitto, on a local Linux) Serial port for debugging: 9600 (Software Serial) Serial port for ESP8266: 38400 (Hardware Serial, Arduino Pro Mini, 5V) Power supply for the ESP8366: 3.3V, regulated through LM117

Arduino Sketch:

#include "SoftwareSerial.h"
#include "WiFiEsp.h"
#include "PubSubClient.h"

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

char ssid[] = "WINNI";
char pwd[] = "password";
int status;

char server[] = "10.0.0.234";

WiFiEspClient client;
PubSubClient mqtt(client);

void setup() {
  mySerial.begin(9600);  // Software serial for Debugging

  Serial.begin(38400);   // Serial for the ESP8266
  WiFi.init(&Serial);

  mySerial.println("Connecting to wireless...");
  while ( status != WL_CONNECTED) {
    status = WiFi.begin(ssid, pwd);
  }

  delay(5000);  // Delay to allow interface to autoconfigure

  mySerial.println(WiFi.status());  // printed multiple times just to make it obvious in the output (for debugging)
  mySerial.println(WiFi.status());
  mySerial.println(WiFi.status());
  mySerial.println(WiFi.status());

  mySerial.println(WiFi.localIP());  // printed multiple times just to make it obvious in the output (for debugging)
  mySerial.println(WiFi.localIP());
  mySerial.println(WiFi.localIP());
  mySerial.println(WiFi.localIP());

  mySerial.println(WiFi.ping("10.0.0.234"));  // ping MQTT server

  client.connect("10.0.0.234", 1883);
  delay(2000);

  mySerial.println("");
  mySerial.println(mqtt.connect("arduino123a"));
  mySerial.println(mqtt.publish("mm/p", "Message to MQTT"));

}

void loop() {

}

Output of SoftwareSerial (baud rate: 9600)

Connecting to wireless...
1
1
1
1
10.0.0.17
10.0.0.17
10.0.0.17
10.0.0.17
1

1
1

Output of Hardware Serial (baud rate: 38400)

AT
AT+RST
ATE0
AT+CWMODE=1
AT+CIPMUX=1
AT+CIPDINFO=1
AT+CWAUTOCONN=0
AT+CWDHCP=1,1
AT+GMR
[WiFiEsp] Initilization successful - 2.0.0
AT+CWJAP_CUR="WINNI","password"
[WiFiEsp] Connected to WINNI
AT+CIPSTATUS
AT+CIPSTATUS
AT+CIPSTATUS
AT+CIPSTATUS
AT+CIFSR
AT+CIFSR
AT+CIFSR
AT+CIFSR
AT+PING="10.0.0.234"
[WiFiEsp] Connecting to 10.0.0.234
AT+CIPSTART=3,"TCP","10.0.0.234",1883
AT+CIPSTATUS
AT+CIPSTATUS
AT+CIPSEND=3,19
0�mm/pMessage to MQTT

On the MQTT server (Linux) I set tcpdump to listen to all conversation with 10.0.0.17 (Arduino):

[marcelo@mqtt:~$ sudo tcpdump -n host 10.0.0.17
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp0s3, link-type EN10MB (Ethernet), capture size 262144 bytes
07:46:38.188029 ARP, Request who-has 10.0.0.17 tell 0.0.0.0, length 46
07:46:38.494361 ARP, Request who-has 10.0.0.17 tell 0.0.0.0, length 46
07:46:39.006259 ARP, Request who-has 10.0.0.17 tell 10.0.0.17, length 46
07.46.39.006527 IP 10.0.0.17 > 224.0.0.1: igmp v2 report 224.0.0.1
07:46:39.415817 ARP, Request who-has 10.0.0.234 tell 10.0.0.17, length 46
07:46:45.047714 ARP, Reply 10.0.0.234 is-at 08:00:27:b3:4d:10, length 28
07:46:45.050201 IP 10.0.0.17 > 10.0.0.234: ICMP echo request, id 44975, seq 1, length 40
07:46:45.050222 IP 10.0.0.234 > 10.0.0.17: ICMP echo reply, id 44975, seq 1, length 40
07:46:46.014821 IP 10.0.0.17.41612 > 10.0.0.234.1883: Flags [S], seq 6511, win 2920, options [mss 1460], length 0
07:46:46.014850 IP 10.0.0.234.1883 > 10.0.0.17.41612: Flags [S.], seq 338692666, ack 6512, win 29200, options [mss 1460], length 0
07:46:46.122299 IP 10.0.0.17.41612 > 10.0.0.234.1883: Flags [.], ack 1, win 2920, length 0
07.46.49.073384 IP 10.0.0.17.41612 > 10.0.0.234.1883: Flags [P.], seq 1:20, ack 1, win 2920, length 19
07:46:49.073407 IP 10.0.0.234.1883 > 10.0.0.17.41612: Flags [.], ack 20, win 29200, length 0
07:46:49.073734 IP 10.0.0.234.1883 > 10.0.0.17.41612: Flags [R.], seq 1, ack 20, win 29200, length 0

This is the before-after log entries from /var/log/mosquitto/mosquitto.log from the MQTT server, after one run of the sketch described above:

marcelo@mqtt:~$ cp /var/log/mosquitto/mosquitto.log /tmp/mosquitto.log RUN Arduino sketch once, then…

marcelo@mqtt:~$ diff /var/log/mosquitto/mosquitto.log /tmp/mosquitto.log
6847,6849d6846
< 1488458806: New connections from 10.0.0.17 on port 1883.
< 1488458809: Socket error on client <unknown>, disconnecting.

Lastly, I have a terminal in my laptop subscribed to the same topic on the MQTT server:

[marcelo@mqtt]:~$ mosquitto_sub -h 10.0.0.234 -t mm/p

But nothing ever get published. If I publish from other devices to that topic, then everything shows, but nothing from the Arduino gets published.

What am I missing?

Thanks, Marcelo

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 17 (3 by maintainers)

Most upvoted comments

@marcelops what I told you was to leave behind the AT config scenario and use ESP-01 (or whichever you use) as standalone WiFi communication capable device flashed with Arduino IDE

I googled it for you: https://create.arduino.cc/projecthub/ROBINTHOMAS/programming-esp8266-esp-01-with-arduino-011389 … these were my two cents

For me the setup with ESP-01 + Arduino nano/pro (ESP-01 as WiFi serial port via AT commands) didn’t work well from stability perspective and got disconnected every several days (sometimes even hours) and I spent too much time inventing recovery scenarios. Therefore the recommendation but the final choice is yours. We all do it for fun 😉

@slavino Your examples use NodeMCU. I am not using that.

I am interested in debugging the code that is described in this issue. Could we please stay on topic?