transferring data from ESP to Arduino and storing on a variable

Hey all :slight_smile: :slight_smile: I am trying to upload the data I get from DHT11 on a server. I know I can upload it immediately if I connect the DHT to the ESP8266, I still want to try to connect both of them though. So basically I want the arduino to measure the temperature, send it to the ESP which will then store it on a variable and upload it to the server. I can't manage to store it on a variable though. I connected it like this picture from this guy's guide :

if the RX & TX pins are connected I get the message which was given from the arduino : . If I try to write something on the monitor myself it is not getting printed. If I am disconnecting the TX & RX pins nothing comes (surprising) but I can print to the serial monitor myself and it stores the information I print on the variable. In the picture I uploaded the output at the top (<26>) is when TX & RX are connected, on the bottom they are not. I suppose I block the serial communication somehow by letting the arduino sending message there. Do you guys know how I can enjoy both world ? getting a message and storing it on a variable ? Thank you all in advance :slight_smile: Hier are the sketches :
Arduino :

#include <SoftwareSerial.h>
#include <EduIntro.h>

DHT11 dht11(D2); //adjusting the Sensor DATA pin to pin 7 of the Arduino

SoftwareSerial espSerial(5, 6);

void setup() {
  Serial.begin(9600);
  espSerial.begin(9600);
  delay(2000);
}
void loop()
{
  dht11.update();
  float temperature = dht11.readCelsius();
  Serial.print( "\t Temperature: ");
  Serial.print(temperature);
  Serial.println("°C");
  delay (1000);

  espSerial.print (temperature) ;
  espSerial.println (" >") ;

  delay(1000);
}

ESP (which sketch I took from the serial input guide) :

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

float dataNumber = 0;             // new for this version

void setup() {
  Serial.begin(9600);
}

void loop() {
  recvWithEndMarker();
  showNewNumber();
}

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '>';
  char rc;

  if (Serial.available() > 0) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}

void showNewNumber() {
  if (newData == true) {
    // dataNumber = 0;             // new for this version
    dataNumber = atof (receivedChars);   // new for this version
    Serial.print("This just in ... ");
    Serial.println(receivedChars);
    Serial.print("Data as Number ... ");    // new for this version
    Serial.println(dataNumber);     // new for this version
    newData = false;
  }
}

Thank you so much !

![u[pload.png|1013x518](upload://4gkdTaogfQ2vnm83fFMg62lNLtU.png)

You need to learn to read and draw schematics. It is the language of electronic circuits. Those Fritzing pictures are not. They give you a false sense of easy and are hard to read. You can draw schematics for the forum by hand and upload a photo taken with your smartphone.

I suspect you use the same Serial connection on the ESP for the communication to the Arduino and the Serial Monitor. You must not do that. Serial/UART is a single point to point interface. Check what other interfaces you have available on both boards and use that.

A few things I noted

  • You use the DHT11, have a look at the BME280 its cheaper, modern, uses I2C or SPI, and adds air pressure
  • I would move away from 5V electronics. Its old. This is your hobby. Use some new stuff.

in your code

  • numChars is a constant call it RX_BUFFER_SIZE
  • ndx - your code needs to be read more often than you type, use longer better variable names
  • showNewNumber - the function does not show it prints
  • in the recvWithEndMarker you could return newData and remove the need for the global variable
  • char endMarker is a constant you can use define and save the variable memory and make the code easier to read #define END_MARKER '>'
  • // new for this version, the word new should probably never be written to mark something as new. Things get old and the new will still be there. It is better to mark things as old. :slight_smile:
  • Have a look into your if statements. Think about inverting them and use it as escape from the function. This avoids cascading ifs. It might not always work but it is easier to read when you get more complex logic especially all the error checking often used in microcontroller code.
void printNumber() {
  if (!newData)  {
    return;
  }
  Serial.print("... ");
  ...
}

Using the library SerialTransfer.h, you can automatically communicate floats and other complex variables between Arduinos easily.

How to install