简体   繁体   中英

ESP32 serial communucation between 2 of them

I'm trying to do a small project between which involves measuring temperature with an ESP32, send it through a serial connection to another ESP32, and make the latter write on the the Serial connection with my computer. I am using some LEDs for monitoring.

The code for the sender is:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#include <HardwareSerial.h>

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Set Global variables

/////////////////////////////////////////////////////////////////////////////////////
// set the LCD number of columns and rows
#define LED1 32
#define RXD2 16
#define TXD2 17
#define DHTPIN 33     // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11

int lcdColumns = 16;
int lcdRows = 2;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);  

/////////////////////////////////////////////////////////////////////////////////////
// Serial Communication
HardwareSerial Sender(2); // use Sender

/////////////////////////////////////////////////////////////////////////////////////
DHT dht(DHTPIN, DHTTYPE);

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Setup function
void setup() {
  Wire.begin();
  Sender.begin(19200,SERIAL_8N1, RXD2, TXD2);
  lcd.init();
  lcd.backlight();
  lcd.print("Finished loading");
  pinMode(LED1, OUTPUT);
  dht.begin();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Loop function
void loop() {
  float t1 = dht.readTemperature();
  float h = dht.readHumidity();
  
  Sender.print(String(t1));
  lcd.setCursor(0,0);
  lcd.print("Temp1(C): " + String(t1) + "\n");
  lcd.setCursor(0,1);
  lcd.print("Humid: " + String(h));
  //lcd.print("Temp2(C): " + String(t2) + "\n");

  digitalWrite(LED1, HIGH);
  delay(200);
  digitalWrite(LED1, LOW);
  delay(200);
}

The code for the receiver is:

#include <HardwareSerial.h>

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Set Global variables

/////////////////////////////////////////////////////////////////////////////////////
// set the Serial Port Data
#define RXD2 16
#define TXD2 17
HardwareSerial Receiver(2); // use Receiver

/////////////////////////////////////////////////////////////////////////////////////
// set other relevant variables
#define LED 33
String message;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Setup function
void setup() {
  
  // Setup the Serial communication
  Receiver.begin(19200,SERIAL_8N1, RXD2, TXD2);

  // LED check
  pinMode(LED,OUTPUT);
  digitalWrite(LED, HIGH);
  delay(200);
  digitalWrite(LED,LOW);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Loop function
void loop() {
  
  while(Receiver.available())
  {
    message = Receiver.read();
    Serial.println("Message received: " + message);
    digitalWrite(LED, HIGH);
    delay(200);
    digitalWrite(LED,LOW);
    delay(200);
  }
}

I checked that the sensor is correctly measuring and changing, but the receiver is not receiving correctly the information, or at least it isn't able to send it to the Serial connection.

通过使用另一个 HardwareSerial 实例而不是默认的 Serial 实例初始化与计算机的通信,消息被发送。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM