ESP8266 and MQTT

Hello all,

I am early enough in my Arduino day to call myself a beginner, please be kind and assist me through a problem.

Some back ground:
Board: Arduino Uno
ESP Module: ESP-01 ESP8266
Other: 4Ch Relay

My goal is to have the Arduino connected to the ESP-01 (SoftwareSerial) and connect onto my internal WiFi network. I have a Raspberry PI with Mosquito running. The system should then subscribe to a Topic, once received the message, then trigger the relay.

Right... easy enough I thought. I have connected the ESP and I use the "WiFiEsp.h" library to link onto the network. This I have been able to achieve. The next step was then to use the "PubSubClient.h", connect to the MQTT Broker and begin communicating. This last step has proved to be the challenge.

I have connected to the broker and received messages, the problem however is that it is really intermittent, the norm is to not receive the message and the exception is to get it. I have tried to work around having the QoS set to 1, but that has not resolved the problem. Other MQTT devices (PI & PC), send and receive the messages without a problem.

I notice that the EPS Tx/Rx light (blue LED), is flashing at a serious rate.

I am lost and don’t really know where to go from here. My suspicion is that the ESP module is so busy that the packets don’t reach it, but I am unsure how to prove it.

My Sketch:
#include <WiFiEsp.h>
#include "SoftwareSerial.h"
#include <PubSubClient.h>
#define BUFFER_SIZE 100

int ledPin = 7;
//IPAddress server(192, 168, 1, 100);

char ssid[] = "MYSSID"; // your network SSID (name)
char pass[] = "MYPASS"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status

// Initialize the Ethernet client object
WiFiEspClient espClient;

PubSubClient client(espClient,"192.168.1.100"); //client(espClient,server);
long lastMsg = 0;
char msg[50];
int value = 0;

SoftwareSerial soft(10,11); // RX, TX
void setup() {
//Prep pin
pinMode(ledPin, OUTPUT);

// initialize serial for debugging
Serial.begin(9600);
// initialize serial for ESP module
soft.begin(9600);
// initialize ESP module
WiFi.init(&soft);
}

//print any message received for subscribed topic
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload*);*

  • }*
  • Serial.println();*
  • // Switch on the LED if an 1 was received as first character*
  • if ((char)payload[0] == '1') {*
  • digitalWrite(13, LOW); // Turn the LED on (Note that LOW is the voltage level*
  • // but actually the LED is on; this is because*
  • // it is acive low on the ESP-01)*
  • } else {*
  • digitalWrite(13, HIGH); // Turn the LED off by making the voltage HIGH*
  • }*
    }
    void loop() {
  • if (!client.connected()) {*
  • reconnect();*
  • }*
  • client.loop();*
  • long now = millis();*
  • if (now - lastMsg > 2000) {*
  • lastMsg = now;*
  • ++value;*
  • snprintf (msg, 75, "hello world #%ld", value);*
  • Serial.print("Publish message: ");*
  • Serial.println(msg);*
  • client.publish("outTopic", msg);*
  • }*
    }
    void reconnect() {
  • // Loop until we're reconnected*
  • while (!client.connected()) {*
  • Serial.print("Attempting MQTT connection...");*
  • // Create a random client ID*
  • String clientId = "ESP8266Client-";*
  • clientId += String(random(0xffff), HEX);*
  • // Attempt to connect*
  • if (client.connect(clientId.c_str())) {*
  • Serial.println("connected");*
  • // Once connected, publish an announcement...*
  • client.publish("outTopic", "hello world");*
  • // ... and resubscribe*
  • client.subscribe("inTopic");*
  • } else {*
  • Serial.print("failed, rc=");*
  • // Serial.print(client.state());*
  • Serial.println(" try again in 5 seconds");*
  • // Wait 5 seconds before retrying*
  • delay(5000);*
  • }*
  • }*
    }
    Wiring similar to what I Have attached.
    Really looking forward to assistance, many thanks.
    ESP8266-ESP-01-Firmware-Update_Elec-Cafe.png

Hi,
did you solved? I am facing the same issue.

mementoauderesemper:
Hi,
did you solved? I am facing the same issue.

What level of QOS are you using? Maybe using level 1 instead of the default level 0 will help.