Problems with serial communication

I have a project ready and I need to add an OLED display to it, but as the project is encapsulated and resined, it is not possible to add it directly to the Arduino I am using. As the project uses serial communication, I thought about adding another Arduino along with the OLED Display to receive data from the Arduino that is encapsulated and send it to the display, but the Arduino that should receive the data is not receiving anything. I need help to solve this problem, I'm using two Arduinos for the mini.

Transmitter

#include <EEPROM.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define EEPROM_UNIT sizeof(float)
#define ONE_WIRE_BUS 4
float freq;
float H2O;
float AR;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup() {
  pinMode(3, INPUT);
  pinMode(13, OUTPUT);
  sensors.begin();
  Serial.begin(9600); // Usando a porta serial padrão (Serial) para comunicação
}

void loop() {
  digitalWrite(13, HIGH);
  EEPROM.get(0, H2O);
  EEPROM.get(100, AR);
  lerFreq(500);
  float Frelativa = (freq - AR) / (H2O - AR);

  // Crie uma string para armazenar os dados dos sensores
  String sensorData = "Sinal de umidade: ";
  sensorData += String(Frelativa, 4); // Adicione o valor da umidade com 4 casas decimais
  sensorData += "\nTemperatura: ";
  sensors.requestTemperatures();
  sensorData += String(sensors.getTempCByIndex(0));
  sensorData += " °C";

  // Envie os dados pela porta serial
  Serial.println(sensorData);

  digitalWrite(13, LOW);
  delay(500);
}

void lerFreq(float intervalo) {
  float pulsos = 0;
  float tempoInicial = millis();
  float tempo = tempoInicial;
  boolean pulso;
  while (tempo <= (tempoInicial + intervalo)) {
    tempo = millis();
    if (digitalRead(3) == HIGH) {
      if (pulso == HIGH) {
        pulsos = pulsos + 1;
      }
      pulso = LOW;
    } else {
      pulso = HIGH;
    }
  }
  freq = pulsos / (intervalo / 1000);
}

void serialEvent() {
  while (Serial.available()) {
    String inputString = Serial.readStringUntil('\n');
    if (inputString.substring(0, 3) == "AR#") {
      for (int i = 1; i <= 10; i++) {
        lerFreq(1000);
        int soma = soma + freq;
        AR = soma / i;
      }
      Serial.println(AR);
      EEPROM.put(100, AR);
      Serial.println(AR);
    }
    if (inputString.substring(0, 3) == "H2O") {
      for (int i = 1; i <= 10; i++) {
        lerFreq(1000);
        int soma = soma + freq;
        H2O = soma / i;
      }
      Serial.println(H2O);
      EEPROM.put(0, H2O);
      Serial.println(H2O);
    }
  }
}

Receiver

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

float receivedData = 0.0; // Variável para armazenar os dados recebidos

void setup() {
  // Inicialize o display OLED
  if(!display.begin(SSD1306_I2C_ADDRESS, OLED_RESET)) {
    Serial.println(F("Falha ao inicializar o display OLED"));
    while (true); // Se falhar, pare a execução
  }

  // Limpe o buffer do display
  display.clearDisplay();
  display.display();

  // Inicialize a comunicação serial
  Serial.begin(9600);
}

void loop() {
  // Verifique se há dados disponíveis na porta serial
  if (Serial.available() > 0) {
    // Leia os dados da porta serial e armazene na variável receivedData
    receivedData = Serial.parseFloat();

    // Limpe o display
    display.clearDisplay();

    // Exiba os dados recebidos no display OLED
    display.setTextSize(1);      
    display.setTextColor(SSD1306_WHITE);  
    display.setCursor(0,0);     
    display.print(F("Dados Recebidos:"));
    display.setCursor(0,10);
    display.print(receivedData, 4); // Exibe os dados com 4 casas decimais

    // Atualize o display
    display.display();
  }
}

Sorry, but without a schematic to show the hardware setup, including which Arduino products you're using, we're going to be chasing our tails on this one.

Hello noname13

I assume that you have written the programme by yourself, then it is quite easy to find the error.

There's a trick to figuring out why something isn't working:

Use a logic analyzer to see what happens.
Put Serial.print statements at various places in the code to see the values of variables, especially ones that control the communications, and determine whether they meet your expectations.

It is extremely clever to code the transmitter and the receiver in one sketch. This way, the changes made affect both functional parts.

Have a nice day and enjoy coding in C++.

Any tip how You intend to do that schematically?

What is it displaying? How do you know this?

Questions:
1. Is Transmitter an Arduino UNO?
2. Is Transmitter encapsulated? If so, how do you get access to the Serial IO (TX/RX) lines?

3. Is Receiver an Arduino UNO?
4. Is your OLED connected with Receiver?

help us to help you.

Please provide a schematic that shows how you have connected the two arduinos to each other.

You should do your project in steps.

Testing if display is working at all:

Run a simple testcode on the receiver-arduino that just shows some text on the display and maybe a number that increments once every second.

Testing does serial receiving work at all:

write a small text-code that just receives bytes and does blink the onboard led one time for each received byte.

Your code uses the standard serial connection to the computer.
As long as you have the computer connected to the arduino the serial connection is occupied by the computer. You can't use the same serial connection in parallel connected to your computer and to your second arduino.

These are the reasons why it is so important that you post what exact microcontroller you are using and to what IO-pins you have connected the two arduinos together.

best regards Stefan

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