Distinguish between two different messages in Serial

Hello,

I am using PubSubClient (MQTT) and Wire.h for communication via an Arduino.
Both of these libraries have an “OnRecieve” function that will be called when Serial is available. I am hoping to call one of these functions depending on the first 7 bytes of the message instead of both of these causing errors

code:

#include <PubSubClient.h>
#include "WiFiEsp.h"
#include <SoftwareSerial.h>
#include <Servo.h> 
#include <Wire.h>
 
SoftwareSerial espSerial(10, 11); // RX, TX
long int baudRate = 9600;
 
char ssid[] = "RPi2";             // your network SSID (name)
char pass[] = "Raspberry";        // your network password
int status = WL_IDLE_STATUS;      // the Wifi radio's status
char server[] = "192.168.2.1";    // IP address of the MQTT server
char topic[] = "MandemOnTheWall";            // Default topic string
char clientId[] = "Mandem1";      // Cliwent id: Must be unique on the broker
 
WiFiEspClient wifi;               // Initialize the Ethernet client object
PubSubClient mqttClient(wifi);    // Initialize the MQTT client
int servoPin = 9;
Servo Servo1; 
String startOfPayload = "";
 
void setup() {
  Serial.begin(9600);
  pinMode(servoPin, OUTPUT); 
  Servo1.attach(servoPin); 
  //Wire.begin(9);                // join i2c bus with address #8
  //Wire.onReceive(onReceive); // register event
   
  // Set baud rate of ESP8266 to 9600 regardless of original setting
  set_esp8266_baud_rate(baudRate);
  espSerial.begin(baudRate);
  espSerial.print("AT+UART_CUR=");
  espSerial.print(baudRate);
  espSerial.print(",8,1,0,0\r\n");
  WiFi.init(&espSerial);
   
 
  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }
 
  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }
 
  // you're connected now, so print out the data
  Serial.println("You're connected to the network");
   
  printWifiStatus();
   
  mqttClient.setServer(server, 1883);
  mqttClient.setCallback(onReceive);
  lock();
}
 
void loop() {
  char message[256];
  int i;
 
  for (i=0; i<256; i++) {
    message[i] = 0;
  }
 
  if (Serial.available()) {
    Serial.readBytesUntil("\r",message, 255);
    mqttClient.publish(topic,message);
  }
   
  if (!mqttClient.connected()) {
    reconnect();
  }
   
  mqttClient.loop();
  //lock();
 
}
 
void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
 
  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
 
  // print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}
 
void onReceive(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}
 
void reconnect() {
  // Loop until we're reconnected
  while (!mqttClient.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (mqttClient.connect(clientId)) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      mqttClient.publish(topic,"hello world");
      // ... and resubscribe
      mqttClient.subscribe(topic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(mqttClient.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
 
void set_esp8266_baud_rate(long int baud_rate){
  long int baud_rate_array[] = {1200,2400,4800,9600,19200,38400,57600,74880,115200,230400};
  int i, j, pause=10;
  String response;
 
  Serial.println("Setting ESP8266 baud rate...");
  for (j=0; j<5; j++){
    for (int i=0; i<10; i++){
      espSerial.begin(baud_rate_array[i]);
      espSerial.print("AT\r\n");
      delay(pause);
      if (espSerial.available()) {
        response=espSerial.readString();
        response.trim();
        if (response.endsWith("OK")) {
          espSerial.print("AT+UART_CUR=");
          espSerial.print(baud_rate);
          espSerial.println(",8,1,0,0");
          delay(pause);
          if (espSerial.available()) {
            response=espSerial.readString();
          }
          espSerial.begin(baudRate);
          delay(pause);
          espSerial.println("AT");
          delay(pause);
          if (espSerial.available()) {
            response=espSerial.readString();
            response.trim();
            if (response.endsWith("OK"))
              break;
            else {
              Serial.println("Trying again...");
            }
          }
          else {
            Serial.println("Trying again...");
          }
        }
      }
    }
    if (response.endsWith("OK"))
      break;
  }
  espSerial.begin(baudRate);
  delay(pause);
  espSerial.println("AT");
  delay(pause);
  if (espSerial.available()) {
    response=espSerial.readString();
    response.trim();
    if (response.endsWith("OK")) {
      Serial.print("\r\nBaud rate is now ");
      Serial.println(baudRate);
    }
    else {
      Serial.println("Sorry - could not set baud rate");
      Serial.println("Try powering off and on again");
      Serial.println("Don't try to use 115200");
    }
  }
}
void MQTTOnReceiveEvent() {
  while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
  if (x == 1){
    unlock();
  }
  if (x == 0){
    lock();
  }
  Serial.println("That was a I2C Connection");
}
void unlock(){
  Servo1.write(0);
}
void lock(){
  Servo1.write(90);
}

Thank you for your help.

I am hoping to call one of these functions depending on the first 7 bytes of the message instead of both of these causing errors

What errors would they be ?

Yeah ignored the problem and wired up another arduino, thanks though