Code Structure Advice

code structure advice

i modified (simplified) your code and tested by simulating your devices

  • i reordered functions so that they are defined before used
  • i de-Capitalized symbol names following standard practice to only Capitalize constants
  • i think it odd that you configure the pin (pinMode()) in relayControl. aren't the pins known in advance?
  • i simplified processInput() to remove redundant code. however your use of substring limits you to single digit pins.
  • not sure why you use "<>" when you could just read a \n terminated line using Serial.readBytesUntil().
  • i simplified processSerialData() so that it restarts whenever if detects a "<" and handles overflow
  • processSerialData() invokes processInput() when ">" is detected
  • i left GetSensorAddress(), but it is not used
  • loop() simply calls processSerialData() (no need for "newData")

hope this is the type of advice you're looking for

consider

#undef MyHW
#ifdef MyHW
typedef int*  DeviceAddress;

struct OneWire  {
    OneWire (int)   { };
};

struct DallasTemperature  {
    DallasTemperature (OneWire *) { };
    void begin (void)  { };
    int  getDeviceCount (void)       { return 3; };
    int  getTempCByIndex (int)       { return 12; };
    int  requestTemperatures (void)  { return 34; };
};

#else
# include <OneWire.h>
# include <DallasTemperature.h>
#endif


#define RELAY_OFF 0
#define RELAY_ON 1

// -----------------------------------------------------------------------------
void relayControl (
    int             pin,
    unsigned char   state)
{
    pinMode (pin, OUTPUT);
    digitalWrite (pin, state);
    Serial.println ("RELAY,PIN," + String(pin) + "," + String(state));
}

// -----------------------------------------------------------------------------
void processTemperature (
    int Pin)
{
    String serialDataTemp = "";
    String address = "";
    float tempC;

    OneWire OneWireBus (Pin);
    DallasTemperature Sensor (&OneWireBus);
    Sensor.begin ();
    Sensor.requestTemperatures ();

    for (int i = 0; i < Sensor.getDeviceCount(); i++)
    {
        tempC = Sensor.getTempCByIndex (i);
        Serial.println ("TEMP,PIN," + String(Pin) + "," + String(tempC));
    }
}

// -----------------------------------------------------------------------------
void processInput (
    String data)
{
    String cmd = data.substring (0, 5);
    int    p   = data.substring (5, 7).toInt();
    String opt = data.substring (7, 9);

    if (cmd == "relay") {
        if (opt == "on")
            relayControl (p, RELAY_ON);
        else
            relayControl (p, RELAY_OFF);
    }

    else if (cmd == "tstat") {
        if (opt == "aa")
            processTemperature (p);
    }
}

// -----------------------------------------------------------------------------
char      receivedChars [80];

void processSerialData ()
{
    static byte ndx = 0;
    const char StartMarker = '<';
    const char EndMarker = '>';

    if (! Serial.available ())
        return;

    char c = Serial.read ();

    if (StartMarker == c)
        ndx = 0;

    else if (EndMarker == c)  {
        receivedChars[ndx] = '\0';
        Serial.println (receivedChars);
        processInput (receivedChars);
    }

    else  {
        receivedChars [ndx++] = c;

        if (sizeof(receivedChars) <= ndx)       // overflow
            ndx = 0;
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (115200);
}

// -----------------------------------------------------------------------------
void loop ()
{
    processSerialData ();
}