Arduino Mega send data to ESP8266-01 then ESP forward to RPI (MQTT)

Did you work through this tutorial?
http://forum.arduino.cc/index.php?topic=396450.0

I did read it before and I just read it again. I don't understand how one code can make the Esp read DHT data from the Mega and send it to a specified address such as a RPI. I get the connections but I would imagine that the ESP would have its own code separate from the code on the Mega.

oibdrew:
I did read it before and I just read it again. I don't understand how one code can make the Esp read DHT data from the Mega and send it to a specified address such as a RPI. I get the connections but I would imagine that the ESP would have its own code separate from the code on the Mega.

Of course. You write one code for the Mega that reads the sensors then formats and send the data over serial to the ESP. You write another code for the ESP that receives the data and sends it wherever it needs to go. You can write the code for both processors using the Arduino IDE.

Since you seem to already have code for the sensors and MQTT, why don’t just work on a simple set of codes for the Mega and ESP that does nothing but pass data back and forth over a serial interface? Once you get that working, you can worry about integrating it back into your other application code.

That's what I've been trying to get help with. How to write a code that will pass the info? What command tell the Mega to send the data to the ESP and what tells the ESP to send the data that it received?

oibdrew:
That's what I've been trying to get help with. How to write a code that will pass the info? What command tell the Mega to send the data to the ESP and what tells the ESP to send the data that it received?

The appropriate sequence of AT commands, which are documented and downloadable from the Espressif site. You can see them in action in a multitude of example and working sketches.

aarg:
The appropriate sequence of AT commands, which are documented and downloadable from the Espressif site. You can see them in action in a multitude of example and working sketches.

No, if you look back at the first post, you'll see that OP's ESP is flashed for Arduino programming. AT commands aren't what he/she needs.

oibdrew:
That's what I've been trying to get help with. How to write a code that will pass the info? What command tell the Mega to send the data to the ESP and what tells the ESP to send the data that it received?

There aren't any magic "commands", you have to write programs (one for Mega, one for ESP) in the C / C++ language using the Arduino IDE. The tutorial that I linked has lot about receiving serial data, but not much about how to send it. Take a look here:
http://robotic-controls.com/learn/arduino/arduino-arduino-serial-communication
It's very basic code, but should get you started. See what you can put together and then post it back here for help.

gfvalvo:
There aren't any magic "commands", you have to write programs (one for Mega, one for ESP) in the C / C++ language using the Arduino IDE.

Let me rephrase that. You'll certainly need to use functions like:
Serial.write()
Serial.read().
Serial.available()
Serial.parseInt()
for the task. See:

These functions handle the job of getting serial data in and out through the UART. However, just as important, you need to think about how you want to format your data before sending it from the TX and how you'll parse it after it's received on the other side. The link in my last post shows one very simple method.

So it looks like to me that all I have to do is write a code for the esp to read the incoming data and then send it to an IP address and that I can leave my code alone that is on the Mega.

Somebody please help me. I feel like I'm losing my mind trying to figure this out. I make a little step forward and it seems that I go in circles after that. I've tried Example #3 here Serial Input Basics - updated - Introductory Tutorials - Arduino Forum and it doesn't seem to help. I've done so many examples that I'm even more confused than before. I now know that I want to use serial1 to send data to the ESP-01 via Tx connected to Rx, but I can't seem to get the line written correctly. Ive even tried playing with the code from https://www.arduino.cc/en/Tutorial/MultiSerialMega. I want to be able to send a topic from the Mega to the Esp via tx/rx, then have the Esp take the Topic and change it to outTopic.

I have a new code for my Mega that allows me to send collected DHT22 data to my RPI3. Instead of writing a program for the ESP and a program for the Mega, I have an ALL-IN -ONE just for the Mega and leave the ESP blank. I have the DHT22 connect to the Mega's 5V and gnd with the DHT's data pin connected to pin 2 on the Mega. I have the Mega's TX connected to a Logic Level on a breadboard then to the ESP's RX and vice versa for the RX-TX. The ESP is powered separately by a breadboard power supply. The data is sent through my wifi router to my RPI 3 with the data showing on a terminal screen.

My problem now is that it looks sloppy (mostly talking about the display in my terminal) and my data from my DHT comes in the form of a LETTER and not NUMBERS. Can I get help with those two issues and possibly some better viewing options (I'm kind of experimenting with Node Red but not sure if that will work with what I am trying to achieve with 24 sensors and such.

#include <WiFiEsp.h>
#include <WiFiEspClient.h>
#include <WiFiEspUdp.h>
#include <PubSubClient.h>

#include <Adafruit_Sensor.h>  //DHT22 sensor     
#include <DHT.h>  //DHT22     
#define DHTTYPE2 DHT22   // DHT 22  (AM2302)     
#define DHTPIN2 2     // what pin we're connected to     
DHT dht2(DHTPIN2, DHTTYPE2); // 11 works fine for ESP8266
int reading2;
int chk2;
char hum2;  //Stores humidity value
char temp2; //Stores temperature value

char ssid[] = "*******";           // your network SSID (name)
char pass[] = "**********";           // your network password
char* topic = "garden";
char* server = "192.***.*.**";
#define mqtt_user "******"
#define mqtt_password "*********"

int status = WL_IDLE_STATUS;   // the Wifi radio's status

// Initialize the Ethernet client object
WiFiEspClient espClient;
PubSubClient client(espClient);

void setup() {
  dht2.begin();           // initialize temperature sensor
  Serial.begin(9600);  // initialize serial for debugging
  Serial1.begin(115200);  // initialize serial for ESP module
  WiFi.init(&Serial1);  // initialize ESP module

  if (WiFi.status() == WL_NO_SHIELD)   // check for the presence of the shield
  {
    Serial.println("WiFi shield not present");
    while (true);// don't continue
  }

  while ( status != WL_CONNECTED)  // attempt to connect to WiFi network
  {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);  // Connect to WPA/WPA2 network
  }

  Serial.println("You're connected to the network");  // you're connected now
  client.setServer(server, 1883);  //connect to MQTT server
  client.setCallback(callback);
}

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

void loop()
{
  {
    Serial.println("Outside Temperature and Humidity ");
    delay(1000); //Delay 1 sec.
    hum2 = dht2.readHumidity();
    temp2 = dht2.readTemperature() * 1.8 + 32.1;
    Serial.print("  Temp = " );
    Serial.print(temp2);
    Serial.println(" F");
    Serial.print("  Humidity = ");
    Serial.print(hum2);
    Serial.println(" %");
    Serial.print("");
    Serial.println("");
    delay(2000);       //  waits 2000 milliseconds (2 sec).
  }

  if (!client.connected())
  {
    reconnect();
  }

  client.loop();
}

void reconnect()
{
  while (!client.connected())  // Loop until we're reconnected
  {
    Serial.print("Attempting MQTT connection...");
    client.publish("command", "Attempting MQTT connection using ESP_MQTT-PUB-SUB...");
    String clientId = "ESP8266Client-";  // Create a random client ID
    clientId += String(random(0xffff), HEX);

    if (client.connect("ESP8266Client", mqtt_user, mqtt_password))   // Attempt to connect, just a name to identify the client
    {
      Serial.println("connected");
      client.publish("command", "connected");
      client.publish("command", "hello world");  // Once connected, publish an announcement...
      client.publish("command", "  Temp = ", temp2, " F");
      client.publish("command", "  Humidity = ", hum2, " %");
      client.subscribe("presence");   // ... and resubscribe
    }
    else
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);  // Wait 5 seconds before retrying
    }
  }
}

Here is the IDE serial output..........

[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 2.0.0
Attempting to connect to WPA SSID: *******
[WiFiEsp] Connected to *******
You're connected to the network
Outside Temperature and Humidity
Temp = F F
Humidity = % %

Attempting MQTT connection...[WiFiEsp] Connecting to 192...
connected
Outside Temperature and Humidity
Temp = F F
Humidity = ( %

Outside Temperature and Humidity
Temp = F F
Humidity = ( %

Here is the MQTT Terminal output.........

Client mosqsub|15719-BeeHiveSe received PUBLISH (d0, q0, r1, m0, 'command', ... (38 bytes))

Humidity = %Attempting MQTT conne

Client mosqsub|15719-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'command', ... (9 bytes))

connected

Client mosqsub|15719-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'command', ... (11 bytes))

hello world

Client mosqsub|15719-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'command', ... (70 bytes))

Temp = F Humidity = %Attempting MQTT connection...Attempting

Client mosqsub|15719-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'command', ... (37 bytes))

Humidity = %Attempting MQTT conn

Client mosqsub|15719-BeeHiveSe sending PINGREQ

Client mosqsub|15719-BeeHiveSe received PINGRESP

Client mosqsub|15719-BeeHiveSe sending PINGREQ

Client mosqsub|15719-BeeHiveSe received PINGRESP

Anyone able to help?

Make the following float because the DHT library returns float.

float hum2;  //Stores humidity value
float temp2; //Stores temperature value

Convert float to C string for MQTT publish.

char c_string[16];
dtostrf(temp2, 4, 2, c_string);
client.publish("temperature", c_string);
dtostrf(hum2, 4, 2, c_string);
client.publish("humidity", c_string);

Awesome! Thank you.

I"m getting the same output as before displaying in my MQTT terminal on my Pi 3.

I"m getting the same output as before displaying in my MQTT terminal on my Pi 3.

If you didn't change the code, why would you expect the output to change?

If you did change the code, why would you expect us to guess what you changed?

PaulS:
If you didn't change the code, why would you expect the output to change?

If you did change the code, why would you expect us to guess what you changed?

If you're not going to be helpful then don't comment.

1 Like

No one can help until you post the latest source code. The console output would also be useful so we know whether the temperature and humidity variables have reasonable values.

I just noticed that my missing data that is supposed to display with the Temp and Humidity
(i.e.
Temp = 75 F ;
Humidity = 25 %
)
is actually displaying in bytes (75 bytes)) or am I wrong?
I would like to be able to get a continuous stream of data that I could view from my Raspberry pi (later editing to retrieve every 15-30 minutes), eventually collecting the data into a spreadsheet of some sort.

This is my most recent code:

#include <WiFiEsp.h>
#include <WiFiEspClient.h>
#include <WiFiEspUdp.h>
#include <PubSubClient.h>

#include <Adafruit_Sensor.h>  //DHT22 sensor     
#include <DHT.h>  //DHT22     
#define DHTTYPE2 DHT22   // DHT 22  (AM2302)     
#define DHTPIN2 2     // what pin we're connected to     
DHT dht2(DHTPIN2, DHTTYPE2); // 11 works fine for ESP8266
int reading2;
int chk2;
float hum2;  //Stores humidity value
float temp2; //Stores temperature value

char ssid[] = "XXXXXX";           // your network SSID (name)
char pass[] = "XXXXXX";           // your network password
char* topic = "arduino_incoming";
char* server = "XXX.XXX.XX.XX";
#define mqtt_user "XXXXXX"
#define mqtt_password "XXXXXX"

int status = WL_IDLE_STATUS;   // the Wifi radio's status

WiFiEspClient espClient;
PubSubClient client(espClient);

void setup() {
  dht2.begin();           // initialize temperature sensor
  Serial.begin(115200);  // initialize serial for debugging
  Serial1.begin(115200);  // initialize serial for ESP module
  WiFi.init(&Serial1);  // initialize ESP module

  if (WiFi.status() == WL_NO_SHIELD)   // check for the presence of the shield
  {
    Serial.println("WiFi shield not present");
    while (true);// don't continue
  }

  while ( status != WL_CONNECTED)  // attempt to connect to WiFi network
  {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);  // Connect to WPA/WPA2 network
  }

  Serial.println("You're connected to the network");  // you're connected now
  client.setServer(server, 1883);  //connect to MQTT server
  client.setCallback(callback);
}

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

void loop()
{
  {
    Serial.println("Outside Temperature and Humidity ");
    delay(1000); //Delay 1 sec.
    hum2 = dht2.readHumidity();
    temp2 = dht2.readTemperature() * 1.8 + 32.1;
    Serial.print("  Temp = " );
    Serial.print(temp2);
    Serial.println(" F");
    Serial.print("  Humidity = ");
    Serial.print(hum2);
    Serial.println(" %");
    Serial.print("");
    Serial.println("");
    delay(10000);       //  waits 10000 milliseconds (10 sec).
    // delay(30000);       //  waits 30000 milliseconds (30 sec).
  }

  if (!client.connected())
  {
    reconnect();
  }
   client.loop();
}

void reconnect()
{
  while (!client.connected())  // Loop until we're reconnected
  {
    Serial.print("Attempting MQTT connection...");
    //    client.publish("garden", "Attempting MQTT connection using ESP_MQTT-PUB-SUB...");
    String clientId = "ESP8266Client-";  // Create a random client ID
    clientId += String(random(0xffff), HEX);

    char c_string[16];
    dtostrf(temp2, 4, 2, c_string);
    client.publish("temperature", c_string);
    dtostrf(hum2, 4, 2, c_string);
    client.publish("humidity", c_string);

    if (client.connect("ESP8266Client", mqtt_user, mqtt_password))   // Attempt to connect, just a name to identify the client
    {
      Serial.println("connected");
      //      client.publish("garden", "connected");
      //      client.publish("garden", "hello world");  // Once connected, publish an announcement...
      client.publish("Temp", "  Temp = ", temp2, " F");
      delay(1000); //Delay 1 sec.
      client.publish("Humidity", "  Humidity = ", hum2, " %");
      delay(1000); //Delay 1 sec.
      client.subscribe("Subscription");   // ... and resubscribe
    }
    else
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);  // Wait 5 seconds before retrying
    }
  }
}

This is the IDE output: (I shortened the output for viewing here)
[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 2.0.0
Attempting to connect to WPA SSID: MyRouter
[WiFiEsp] Connected to MyRouter
You're connected to the network
Outside Temperature and Humidity
Temp = 73.32 F
Humidity = 25.30 %

Attempting MQTT connection...[WiFiEsp] Connecting to 192.168.xx.xx
connected
Outside Temperature and Humidity
Temp = 73.32 F
Humidity = 25.20 %

Outside Temperature and Humidity
Temp = 73.32 F
Humidity = 25.20 %

Attempting MQTT connection...[WiFiEsp] Connecting to 192.168.xx.xx
connected
Outside Temperature and Humidity
Temp = 74.58 F
Humidity = 25.60 %

Outside Temperature and Humidity
Temp = 75.12 F
Humidity = 27.00 %

Attempting MQTT connection...[WiFiEsp] Connecting to 192.168.xx.xx
connected
Outside Temperature and Humidity
Temp = 77.28 F
Humidity = 29.50 %

Outside Temperature and Humidity
Temp = 87.90 F
Humidity = 38.30 %

This is from my Raspberry pi 3 terminal:(I shortened the output for viewing here)
pi@BeeHiveServer:~ $ mosquitto_sub -d -u oibdrew -P B33B3tt3rHav3MyHon3y -t Temp -t Humidity
Client mosqsub|23106-BeeHiveSe sending CONNECT
Client mosqsub|23106-BeeHiveSe received CONNACK
Client mosqsub|23106-BeeHiveSe sending SUBSCRIBE (Mid: 1, Topic: Temp, QoS: 0)
Client mosqsub|23106-BeeHiveSe sending SUBSCRIBE (Mid: 2, Topic: Humidity, QoS: 0)
Client mosqsub|23106-BeeHiveSe received SUBACK
Subscribed (mid: 1): 0
Client mosqsub|23106-BeeHiveSe received PUBLISH (d0, q0, r1, m0, 'Temp', ... (73 bytes))
Temp = F Humidity = %Attempting MQTT connection...ESP8266Client
Client mosqsub|23106-BeeHiveSe received SUBACK
Subscribed (mid: 2): 0
Client mosqsub|23106-BeeHiveSe received PUBLISH (d0, q0, r1, m0, 'Humidity', ... (25 bytes))
Humidity = %Attempti
Client mosqsub|23106-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'Temp', ... (73 bytes))
Temp = F Humidity = %Attempting MQTT connection...ESP8266Client
Client mosqsub|23106-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'Humidity', ... (25 bytes))
Humidity = %Attempti
Client mosqsub|23106-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'Temp', ... (74 bytes))
Temp = F Humidity = %Attempting MQTT connection...ESP8266Client-
Client mosqsub|23106-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'Humidity', ... (25 bytes))
Humidity = %Attempti
Client mosqsub|23106-BeeHiveSe sending PINGREQ
Client mosqsub|23106-BeeHiveSe received PINGRESP
Client mosqsub|23106-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'Temp', ... (74 bytes))
Temp = F Humidity = %Attempting MQTT connection...ESP8266Client-
Client mosqsub|23106-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'Humidity', ... (24 bytes))
Humidity = %Attempt
Client mosqsub|23106-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'Temp', ... (75 bytes))
Temp = F Humidity = %Attempting MQTT connection...ESP8266Client-
Client mosqsub|23106-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'Humidity', ... (24 bytes))
Humidity = %Attempt
Client mosqsub|23106-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'Temp', ... (75 bytes))
Temp = F Humidity = %Attempting MQTT connection...ESP8266Client-
Client mosqsub|23106-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'Humidity', ... (24 bytes))
Humidity = %Attempt
Client mosqsub|23106-BeeHiveSe sending PINGREQ
Client mosqsub|23106-BeeHiveSe received PINGRESP

  while (!client.connected())  // Loop until we're reconnected
  {
    Serial.print("Attempting MQTT connection...");
    //    client.publish("garden", "Attempting MQTT connection using ESP_MQTT-PUB-SUB...");
    String clientId = "ESP8266Client-";  // Create a random client ID
    clientId += String(random(0xffff), HEX);

    char c_string[16];
    dtostrf(temp2, 4, 2, c_string);
    client.publish("temperature", c_string);
    dtostrf(hum2, 4, 2, c_string);
    client.publish("humidity", c_string);

Lets look at just this snippet. While there is no client connected, publish some data using that non-connected client. Hmmm. I don't think Mr. Spock would approve.

    if (client.connect("ESP8266Client", mqtt_user, mqtt_password))   // Attempt to connect, just a name to identify the client
    {
      Serial.println("connected");
      //      client.publish("garden", "connected");
      //      client.publish("garden", "hello world");  // Once connected, publish an announcement...
      client.publish("Temp", "  Temp = ", temp2, " F");
      delay(1000); //Delay 1 sec.
      client.publish("Humidity", "  Humidity = ", hum2, " %");
      delay(1000); //Delay 1 sec.

Then, we try to connect. If successful, we publish again, using a different method that takes 4 arguments.

The method that PubSubClient has for publishing are:

   boolean publish(const char* topic, const char* payload);
   boolean publish(const char* topic, const char* payload, boolean retained);
   boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);

None of them take the 4 arguments you are supplying. So, you need to explain what you are trying to accomplish with the 4 non-commented out calls to publish().

You also need to explain the sticking your head in the sand calls to delay().

I used delay because I thought it would slow down the data coming to the terminal on the Pi and it wouldn't be so clustered, basically display smoother.