ESP32 OLED HC-12: Can`t send messages correctly

Hello everyone, I am using an ESP32-WROOM-32D, 0.96 inch SSD1306 OLED display, HC-12 Wireless Transceiver Module and 4 buttons for my project. The main idea is that you scroll trough the keyboard on the display with first two buttons, when standing on the preferred character/function by clicking the third button you select it and write it on the display(buffer), when satisfied with what`s typed by clicking the fourth button the message is being send to the other identical board. I am able to send some messages, but most messages get either cut to or strange symbols are displayed.

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

#define RXD2 16
#define TXD2 17
#define HC12 Serial2
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
#define buttonOne 34
#define buttonTwo 35
#define buttonThree 32
#define buttonFour 33

byte incomingByte;
String readBuffer = "";
String writeBuffer = "";
String sendBuffer = "";

int capsLock = 0;
int buttonState = 0;
int buttonOnePressed = 0;
int buttonTwoPressed = 0;
int buttonThreePressed = 0;
int buttonFourPressed = 0;

Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT);

void setup()
{
  Serial.begin(9600);
  HC12.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.drawRect(0, 53, 128, 11, WHITE);
  display.display();

  pinMode(buttonOne, INPUT);
  pinMode(buttonTwo, INPUT);
  pinMode(buttonThree, INPUT);
  pinMode(buttonFour, INPUT);
}

void loop()
{
  buttonOnePressed = digitalRead(buttonOne);
  buttonTwoPressed = digitalRead(buttonTwo);
  buttonThreePressed = digitalRead(buttonThree);
  buttonFourPressed = digitalRead(buttonFour);

  if (buttonOnePressed == 1)
  {
    buttonState = buttonState - 1;
    delay(150);
    if(buttonState < -15)
    {
      buttonState = 26;
    }
  }

  if (buttonTwoPressed == 1)
  {
    buttonState = buttonState + 1;
    delay(150);
    if(buttonState > 26)
    {
      buttonState = -15;
    }
  }

  switch (buttonState)
  {
    case -15:
      writeBuffer = "_";
      break;
    case -14:
      writeBuffer = "!";
      break;
    case -13:
      writeBuffer = "?";
      break;
    case -12:
      if (capsLock == 0)
      {
        writeBuffer = "Caps Lock";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "CAPS LOCK";
      }
      break;
    case -11:
      writeBuffer = "<-";
      break;
    case -10:
      writeBuffer = "SPACE";
      break;
    case -9:
      writeBuffer = "9";
      break;
    case -8:
      writeBuffer = "8";
      break;
    case -7:
      writeBuffer = "7";
      break;
    case -6:
      writeBuffer = "6";
      break;
    case -5:
      writeBuffer = "5";
      break;
    case -4:
      writeBuffer = "4";
      break;
    case -3:
      writeBuffer = "3";  
      break;
    case -2:
      writeBuffer = "2";
      break;
    case -1:
      writeBuffer = "1";
      break;
    case 0:
      writeBuffer = "0";
      break;
    case 1:
      if (capsLock == 0)
      {
        writeBuffer = "a";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "A";
      }
      break;
    case 2:
      if (capsLock == 0)
      {
        writeBuffer = "b";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "B";
      }
      break;
    case 3:
      if (capsLock == 0)
      {
        writeBuffer = "c";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "C";
      }
      break;
    case 4:
      if (capsLock == 0)
      {
        writeBuffer = "d";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "D";
      }
      break;
    case 5:
      if (capsLock == 0)
      {
        writeBuffer = "e";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "E";
      }
      break;
    case 6:
      if (capsLock == 0)
      {
        writeBuffer = "f";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "F";
      }
      break;
    case 7:
      if (capsLock == 0)
      {
        writeBuffer = "g";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "G";
      }
      break;
    case 8:
      if (capsLock == 0)
      {
        writeBuffer = "h";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "H";
      }
      break;
    case 9:
      if (capsLock == 0)
      {
        writeBuffer = "i";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "I";
      }
      break;
    case 10:
      if (capsLock == 0)
      {
        writeBuffer = "j";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "J";
      }
      break;
    case 11:
      if (capsLock == 0)
      {
        writeBuffer = "k";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "k";
      }
      break;
    case 12:
      if (capsLock == 0)
      {
        writeBuffer = "l";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "L";
      }
      break;
    case 13:
      if (capsLock == 0)
      {
        writeBuffer = "m";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "M";
      }
      break;
    case 14:
      if (capsLock == 0)
      {
        writeBuffer = "n";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "N";
      }
      break;
    case 15:
      if (capsLock == 0)
      {
        writeBuffer = "o";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "O";
      }
      break;
    case 16:
      if (capsLock == 0)
      {
        writeBuffer = "p";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "P";
      }
      break;
    case 17:
      if (capsLock == 0)
      {
        writeBuffer = "q";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "Q";
      }
      break;
    case 18:
      if (capsLock == 0)
      {
        writeBuffer = "r";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "R";
      }
      break;
    case 19:
      if (capsLock == 0)
      {
        writeBuffer = "s";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "S";
      }
      break;
    case 20:
      if (capsLock == 0)
      {
        writeBuffer = "t";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "T";
      }
      break;
    case 21:
      if (capsLock == 0)
      {
        writeBuffer = "u";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "U";
      }
      break;
    case 22:
      if (capsLock == 0)
      {
        writeBuffer = "v";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "V";
      }
      break;
    case 23:
      if (capsLock == 0)
      {
        writeBuffer = "w";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "W";
      }
      break;
    case 24:
      if (capsLock == 0)
      {
        writeBuffer = "x";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "X";
      }
      break;
    case 25:
      if (capsLock == 0)
      {
        writeBuffer = "y";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "Y";
      }
      break;
    case 26:
      if (capsLock == 0)
      {
        writeBuffer = "z";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "Z";
      }
      break;
    default:
      
      break;
  }
  display.clearDisplay();
  display.drawRect(0, 53, 128, 11, WHITE);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(2, 55);
  display.print(writeBuffer);
  display.setCursor(0, 0);
  display.print(sendBuffer);
  display.setCursor(0, 25);
  display.print(readBuffer);
  display.display();

  if (buttonThreePressed == 1)
  {
    if (writeBuffer == "<-")
    {
      int L = sendBuffer.length();
      sendBuffer = sendBuffer.substring(0,L - 1);
      writeBuffer = "";
    }
    else if (writeBuffer == "SPACE")
    {
      writeBuffer = " ";
    }
    else if (writeBuffer == "Caps Lock")
    {
      capsLock = 1;
      writeBuffer = "";
    }
    else if (writeBuffer == "CAPS LOCK")
    {
      capsLock = 0;
      writeBuffer = "";
    }
    sendBuffer = sendBuffer + writeBuffer;
    writeBuffer = "";
    delay(150);
  }
  
  if (buttonFourPressed == 1)
  {
    HC12.print(sendBuffer);
    sendBuffer = "";
    delay(150);
  }

  if (HC12.available())
  {
    readBuffer = "";
  }
  
  while (HC12.available())
  {
    incomingByte = HC12.read();
    readBuffer += char(incomingByte);
  }
  delay(20);
}

Can someone help me?

Try separating the tx and Rx. A strong signal may swamp the Rx so it can’t be read properly.
G

to test if the transmitted HF is disturbing the wires you could remove the HC-12-modules and wire a direct connection Tx---to--Rx Rx---to--Tx from ESP32 to ESP32.

If the code behaves the same you know it is not the HF but in the code.

If receiving works correct you know the HF is disturbing

best regards Stefan

1 Like

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