Arduino and Esp32 serial communication and data to Thingspeak not working

Hi! I have build a soil condition monitoring system using Arduino Nano and Esp32 to send the measured data to Thingspeak server. The Nano and Esp32 are communicating through TX and RX pins. I tought that the RX in Esp32 goes to TX in Arduino? If I put the pins other way the serial monitor of Esp32 shows the same values Arduino measures with the sensors. If I put the wires to the pins how they should go, the serial monitor of Esp32 shows 0 for all the values? Esp32 connects to wifi and sends the data to thingspeak but only the 0 values, not the correct ones.

I have tested the esp32 and it receives bytes from Arduino but I think it does not know how to sort them into different values. Please help.

Here is Arduinos code:

[10.54] Anton Store

#include <SPI.h>
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 5

#define RE 8
#define DE 7

const byte code[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
byte values[11];
SoftwareSerial mod(2, 3);

const int AirValue = 645; //you need to replace this value with Value_1
const int WaterValue = 254; //you need to replace this value with Value_2
int soilMoistureValue = 0;
int soilmoisturepercent = 0;

float temperature;
int nitrogen;
int phosphorous;
int potassium;

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

struct MyVariable
{
byte soilmoisturepercent;
byte nitrogen;
byte phosphorous;
byte potassium;
byte temperature;
};
MyVariable variable;

void setup()
{
Serial.begin(115200);
mod.begin(115200);
sensors.begin();
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
}

void loop()
{
byte val;
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
if (mod.write(code, sizeof(code)) == 8)
{
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 11; i++)
{
//Serial.print(mod.read(),HEX);
values[i] = mod.read();
Serial.print(values[i], HEX);
}
Serial.println();
}
nitrogen = values[4];
phosphorous = values[6];
potassium = values[8];
delay(1500);

soilMoistureValue = analogRead(A0); //put Sensor into soil
//Serial.println(soilMoistureValue);
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
{
if (soilmoisturepercent >= 100)
{
soilmoisturepercent = 100;
}
else if (soilmoisturepercent <= 0)
{
soilmoisturepercent = 0;
}
else if (soilmoisturepercent > 0 && soilmoisturepercent < 100)
{
soilmoisturepercent = soilmoisturepercent;
}
}
delay(1500);

sensors.requestTemperatures();
temperature = sensors.getTempCByIndex(0);

variable.soilmoisturepercent = soilmoisturepercent;
variable.nitrogen = nitrogen;
variable.phosphorous = phosphorous;
variable.potassium = potassium;
variable.temperature = temperature;
delay(1500);

Serial.print("Soil Moisture: ");
Serial.print(variable.soilmoisturepercent);
Serial.println("%");

Serial.print("Nitrogen: ");
Serial.print(variable.nitrogen);
Serial.println(" mg/kg");
Serial.print("Phosphorous: ");
Serial.print(variable.phosphorous);
Serial.println(" mg/kg");
Serial.print("Potassium: ");
Serial.print(variable.potassium);
Serial.println(" mg/kg");

Serial.print("Temperature: ");
Serial.print(variable.temperature);
Serial.println("*C");

Serial.println();

Serial.println("Data Packet Sent");
Serial.println("");
delay(10000);
}

And here is the code for Esp32:

#include <WiFi.h>
#include <SPI.h>

String apiKey = "FSHD2JR6VANGPC2U";

const char* ssid = "OnePlus5T";
const char* password = "********";

const char* server = "api.thingspeak.com";

int incomingByte = 0;

struct MyVariable
{
byte soilmoisturepercent;
byte nitrogen;
byte phosphorous;
byte potassium;
byte temperature;
};
MyVariable variable;

WiFiClient client;

void setup()
{
Serial.begin(115200);
Serial.println("Receiver Started....");
Serial.print("Connecting to ");
Serial.println(ssid);
Serial.println();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}

void loop()
{
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

            // say what you got:
            Serial.print("I received: ");
            Serial.println(incomingByte, DEC);

}
else Serial.print("I received nothing ");

{

Serial.print("Soil Moisture: ");
Serial.print(variable.soilmoisturepercent);
Serial.println("%");

Serial.print("Nitrogen: ");
Serial.print(variable.nitrogen);
Serial.println(" mg/kg");
Serial.print("Phosphorous: ");
Serial.print(variable.phosphorous);
Serial.println(" mg/kg");
Serial.print("Potassium: ");
Serial.print(variable.potassium);
Serial.println(" mg/kg");

Serial.print("Temperature: ");
Serial.print(variable.temperature);
Serial.println("*C");

Serial.println();

if (client.connect(server, 80))
{
String postStr = apiKey;
postStr += "&field1=";
postStr += String(variable.soilmoisturepercent);
postStr += "&field2=";
postStr += String(variable.nitrogen);
postStr += "&field3=";
postStr += String(variable.phosphorous);
postStr += "&field4=";
postStr += String(variable.potassium);
postStr += "&field5=";
postStr += String(variable.temperature);
postStr += "\r\n\r\n\r\n\r\n\r\n";

    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
    delay(10000);
    Serial.println("Data Sent to Server");
  }
    client.stop();

}
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Please put the code in code tags.

Describe the level shifters you are using so the 5V Uno does not blow up the 3.3V ESP.

Post a schematic.

Post a few images of the project.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.