Hello I'm having problem with serial communication between Arduino pro mini and ESP32. For receiver I'm using this code:
char mystring[20]; //Initialized variable to store receive
void setup() {
Serial.begin(115200); // Begin the Serial at 9600 Baud
}
void loop() {
if(Serial.available()){
Serial.readBytes(mystring,15); //Read the serial data
Serial.println(mystring); //Print data on Serial Monitor
}
}
for transmit:
char mystring[16] = "not working? "; //String data which is to be sent
void setup() {
Serial.begin(115200); // Begin the Serial at 9600 Baud rate
}
void loop() {
Serial.write(mystring,16); //Write the serial data
delay(1000);
}
But reciever getting this:
n��wK�i��
K�i��
�
��wK�i��
�i��
When i use serial communication between 2 pro minis I am getting it worked, but with ESP32 it just doesn't works.... Please i need help
Well first think I tried was that I only connected RX, TX, gnd and 3.3V and print data from esp32 to pro mini OLED display, but still I got some weird things. After that, I tried connect to pro mini with programmer and listen to communication, but still nothing.
try the following between an ESP32 Tx pin 17 Rx pin 16 and pro mini Tx pin 9 Rx pin 8
connect ESP32 Tx pin 17 to pro mini Rx pin 8
connect ESP32 Rx pin 16 to pro mini Tx pin 9
pro mini code
// Arduino pro mini AltSoftSerial test using pin 8 Rx and 9 Tx
#include <AltSoftSerial.h>
#include <SPI.h>
#include <Wire.h>
// pro mini RX pin 8
// pro mini TX pin 9
AltSoftSerial simSerial;
void setup() {
Serial.begin(57600); // on pro mini 115200 gives problems ???
Serial.println("AltSoftSerial test pro mini pin 8 Rx and 9 Tx");
simSerial.begin(38400);
}
void loop() {
if (Serial.available()) {
char command = Serial.read();
//Serial.println(command);
simSerial.print(command);
}
while (simSerial.available()) {
char reponse = simSerial.read();
Serial.print(reponse);
}
}
ESP32 code
// ESP32 Serial1 test - for loopback test connect pins 16 and 17
#define RXD1 16
#define TXD1 17
void setup() {
// initialize both serial ports:
Serial.begin(115200);
//Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
Serial1.begin(38400, SERIAL_8N1, RXD1, TXD1);
Serial.println();
Serial.println("\n\nESP32 serial1 test Rx pin 16 Tx pin 17");
Serial.write(" for loopback test connect pin 16 to pin 17\n");
}
void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
// read from port 0, send to port 1:
if (Serial.available()) {
int inByte = Serial.read();
//Serial.write(inByte);
Serial1.write(inByte);
}
}
pro mini serial monitor output when text received from ESP32
AltSoftSerial test pro mini pin 8 Rx and 9 Tx
hello from ESP32
1234567890
abcdefgh
ESP32 serial monitor output when text received from pro mini
ESP32 serial1 test Rx pin 16 Tx pin 17
for loopback test connect pin 16 to pin 17
hello from pro mini
0987654321
abcdefghijklmnopqrstuvwxyz
double hunger = 20;
double happiness = 10;
double health = 15;
double discipline = 100;
double weight = 1;
double age = 0;
void setup() {
Serial.begin(57600);
hunger = 100.12345;
happiness = 100.1234;
health = 100.12345;
discipline = 100.1234;
weight = 1.1234;
age = 0.1234567;
}
void loop() {
// Převedi hodnoty z double na unsigned long
unsigned long hunger_int = (unsigned long)(hunger * 100000.0);
unsigned long happiness_int = (unsigned long)(happiness * 10000.0);
unsigned long health_int = (unsigned long)(health * 100000.0);
unsigned long discipline_int = (unsigned long)(discipline * 10000.0);
unsigned long weight_int = (unsigned long)(weight * 10000.0);
unsigned long age_int = (unsigned long)(age * 10000000.0);
// Odešli hodnoty přes sériovou linku
Serial.write((byte*)&hunger_int, sizeof(unsigned long));
Serial.write((byte*)&happiness_int, sizeof(unsigned long));
Serial.write((byte*)&health_int, sizeof(unsigned long));
Serial.write((byte*)&discipline_int, sizeof(unsigned long));
Serial.write((byte*)&weight_int, sizeof(unsigned long));
Serial.write((byte*)&age_int, sizeof(unsigned long));
delay(1000); // Případné zpoždění mezi odesláním dat
}
rec:
double hunger = 20;
double happiness = 10;
double health = 15;
double discipline = 100;
double weight = 1;
double age = 0;
unsigned long hunger_int = 1234567891;
unsigned long happiness_int = 1234567891;
unsigned long health_int = 1234567891;
unsigned long discipline_int = 1234567891;
unsigned long weight_int = 1234567891;
unsigned long age_int = 1234567891;
void setup() {
Serial.begin(57600);
// Nastavení rychlosti sériové komunikace na 57600 baudů (musí odpovídat rychlosti v druhém kódu)
}
void loop() {
if (Serial.available() >= 6 * sizeof(unsigned long)) {
// Pokud jsou dostupná data (6x unsigned long odpovídající 6 proměnným)
// Přečti data a ulož je do proměnných
Serial.readBytes((char*)&hunger_int, sizeof(unsigned long));
Serial.readBytes((char*)&happiness_int, sizeof(unsigned long));
Serial.readBytes((char*)&health_int, sizeof(unsigned long));
Serial.readBytes((char*)&discipline_int, sizeof(unsigned long));
Serial.readBytes((char*)&weight_int, sizeof(unsigned long));
Serial.readBytes((char*)&age_int, sizeof(unsigned long));
// Převedi hodnoty zpět na původní typ double
hunger = (double)hunger_int / 100000.0;
happiness = (double)happiness_int / 10000.0;
health = (double)health_int / 100000.0;
discipline = (double)discipline_int / 10000.0;
weight = (double)weight_int / 10000.0;
age = (double)age_int / 10000000.0;
// Zde můžete provést další zpracování s načtenými hodnotami, pokud je to potřeba
// Vypiš hodnoty pro kontrolu
Serial.print("hunger: ");
Serial.println(hunger, 6);
Serial.print("happiness: ");
Serial.println(happiness, 5);
Serial.print("health: ");
Serial.println(health,6);
Serial.print("discipline: ");
Serial.println(discipline, 5);
Serial.print("weight: ");
Serial.println(weight,5);
Serial.print("age: ");
Serial.println(age,8);
}
}
if for some reason your receiver starts (or restarts) after the the sender and you have already some bytes in transit, you have no way to sync.
it would be more robust if you had some sort of start and/or end marker to be able to synchronise both processes
side note : if you were to store the payload in a structure, it would be easier to send and receive. (careful on types as a double might be 4 bytes only on a 8 bit µC and 8 bytes on a 32bit µC likes the ESP32)