Arduino <-> Core: SoftwareSerial and Serial1

OK @peekay123 I’m finally crying uncle trying to get this SerialCommand library working for me. I’m attempting to go spark to spark with publish/subscribe and then core to arduino Mega with the SerialCommand. Even though I am using the library on the arduino side, your port adds support for serial port modification which the original does not. My spark code is a modified version of this hackster project and the arduino code is just the hardware only example, but with Serial3 added in. I modified SerialCommand.h to define Serial3.

The publish/subscribe part works fine.

My scope says something is being pushed out of D1 on the spark for each button push, but it must not be the right thing.

Thanks as always.

My Spark code:

#include "Serial2/Serial2.h"

int sendLedPin = D2; //choose the pin for the send (local) LED
int inputPin = D3; //choose the input pin (for a pushbutton)
int receiveLedPin = D4; //choose the pin for the receive (Core1-dependent LED)
int val = 0; //variable for reading the pushbutton pin status
int sendLedVal = 0; //variable for keeping the state of the local pushbutton dependent LED (D2)

void ledTwoToggle(const char *toggle, const char *onOff); //handler function for Spark.subscribe()

void setup() {
    
    Serial.begin(9600);
    Serial2.begin(9600);
    pinMode(receiveLedPin, OUTPUT); //Set the pin controlling the Core1-dependent LED as an OUTPUT
    pinMode(sendLedPin, OUTPUT); //Set the pin controlling the local pushbutton-dependent LED as an OUTPUT
    pinMode(inputPin, INPUT); //Set the pin connected to the pushbutton as an INPUT
    digitalWrite(sendLedPin, LOW); //Ensure the local LED is set to off to begin with
    
    Spark.publish("Core2Toggle", "State", 60, PRIVATE); //Set up Spark.publish() so that the state of the local LED is published to the Spark Cloud PRIVATELY
    Spark.subscribe("Core1Toggle", ledTwoToggle, "xxxxxxxxxxxxxxxxxxxxxxxxx"); //Set up Spark.subscribe() so that the state of Core1's Led is recorded and handled by ledTwoToggle
}

void loop() {
    
    while(Serial2.available())
		Serial.write(Serial2.read());
		
    while(Serial.available())
		Serial2.write(Serial.read());
    
}

void ledTwoToggle(const char *toggle, const char *onOff){ //handler function for Spark.subscribe()
    if (strcmp(onOff, "ON") == 0){ //if sendLed on Core1 is ON according to Spark.publish()
        digitalWrite(receiveLedPin, HIGH); //then turn on receiveLed
        Serial2.print("ON");
        Serial2.print('\r');
        Serial.print("ledON");
    } else if (strcmp(onOff, "OFF") == 0){ //if sendLed on Core1 is OFF according to Spark.publish()
        digitalWrite(receiveLedPin, LOW); //then turn off receiveLed
        Serial2.print("OFF");
        Serial2.print('\r');
        Serial.print("ledOFF");
    }
}

My Arduino Code:

#include <SerialCommand.h>


#define arduinoLED 13   // Arduino LED on board

SerialCommand SCmd;   // The demo SerialCommand object

void setup()
{  
  pinMode(arduinoLED,OUTPUT);      // Configure the onboard LED for output
  digitalWrite(arduinoLED,LOW);    // default to LED off

  Serial.begin(9600);
  Serial3.begin(9600);

  // Setup callbacks for SerialCommand commands 
  SCmd.addCommand("ON",LED_on);       // Turns LED on
  SCmd.addCommand("OFF",LED_off);        // Turns LED off
  //SCmd.addCommand("HELLO",SayHello);     // Echos the string argument back
  //SCmd.addCommand("P",process_command);  // Converts two arguments to integers and echos them back 
  SCmd.addDefaultHandler(unrecognized);  // Handler for command that isn't matched  (says "What?") 
  Serial.println("Ready"); 

}

void loop()
{  
  SCmd.readSerial();     // We don't do much, just process serial commands
}


void LED_on()
{
  Serial.println("LED on"); 
  digitalWrite(arduinoLED,HIGH);  
}

void LED_off()
{
  Serial.println("LED off"); 
  digitalWrite(arduinoLED,LOW);
}




// This gets set as the default handler, and gets called when no other command matches. 
void unrecognized()
{
  Serial.println("What?"); 
}