Arduino, ESP8266 and MQTT-Raspberry Pi communication


I’m very new to Arduino programming and I’d like to ask for your help. I’ve managed to set up my Arduino Nano with an ESP01 module and connect to my Wifi network through AT commands. I have an IP, all good there.

I have set up an MQTT server on my Raspberry pi, which seem to be working fine, I get the messages on my mobile phone with MyMQTT on the subscriptions.

I’m stuck here though. I would like to learn how to proceed towards my final goal of transmitting the data of a DHT11 sensor to my MQTT server, and I’d like to ask if anyone could tell me what command should I use which sends the data (any data, even just a string for me to see how it works) to the MQTT.
I’d also appreciate some explanation or if you pointed me in the right direction of sources. Unfortunately after excessive google-ing I could only find examples which come up with errors upon compilation or require an ethernet shield instead of the wifi connection via ESP01.

See attached the arduino script I use now.

I’d appreciate any help. Thank you very much!

The attachment…

#include <SoftwareSerial.h>
SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
                             // This means that you need to connect the TX line from the esp to the Arduino's pin 2
                             // and the RX line from the esp to the Arduino's pin 3
void setup()
  esp8266.begin(115200); // your esp's baud rate might be different
void loop()
  if(esp8266.available()) // check if the esp is sending a message 
      // The esp has data so display its output to the serial window 
      char c =; // read the next character.
    // the following delay is required because otherwise the arduino will read the first letter of the command but not the rest
    // In other words without the delay if you use AT+RST, for example, the Arduino will read the letter A send it, then read the rest and send it
    // but we want to send everything at the same time.
    String command="";
    while(Serial.available()) // read the command character by character
        // read one character
    esp8266.println(command); // send the read character to the esp8266