Serial Communication extract String

Hello Friends,
hope all iz well
just a simple thing how to extract string data getting thru serial communications
NODE MCU to Arduino Mega, vise versa
communications are established and getting correct data, just want to extract

Node MCU sender

void loop() {
  while (Serial.available())
  {
    pinStatus = Serial.readString();
    pinStatus_OLD = String(switch1) + String(switch2) + String(switch3) + String(switch4) + String(usDistance);
    Serial.println(pinStatus.substring(0,4) +  String(usDistance));
    if (pinStatus.substring(0, 4) != pinStatus_OLD) {
      updateState(pinStatus.substring(0, 4), pinStatus_OLD);
      //pinStatus_OLD = pinStatus;
    }
   connectedFlag = true;
  }
readSensor();
}

Mega Receiver

void wifi_control()
{
  if(Serial1.available()) {
    bt_data = Serial1.readString();
    Serial.println(bt_data);
        if(bt_data == "A1") { onStartChange();  EEPROM.update(0,LOW);  } // if 'A1' received Turn on Relay1
        if(bt_data == "A0") { onOffChange();    EEPROM.update(0,HIGH); } // if 'A0' received Turn off Relay1
        if(bt_data == "B1") { onManualChange(); EEPROM.update(1,LOW);  } // if 'B1' received Turn on Relay2
        if(bt_data == "B0") { onOffChange();    EEPROM.update(1,HIGH); } // if 'B0' received Turn off Relay2
        if(bt_data == "C1") { onLed1Change();   EEPROM.update(2,LOW);  } // if 'C1' received Turn on LED1
        if(bt_data == "C0") { onLed1OffChange();EEPROM.update(2,HIGH); } // if 'C0' received Turn off LED1
        if(bt_data == "D1") { onLed2Change();   EEPROM.update(3,LOW);  } // if 'D1' received Turn on LED2
        if(bt_data == "D0") { onLed2OffChange();EEPROM.update(3,HIGH); } // if 'D0' received Turn off LED2
       // if(bt_data == "LO") { wifiOn(); }
       // if(bt_data == "HI") { wifiOff(); }
        //if(bt_data == "W1") { digitalWrite(WIFI_LED, HIGH); } // if 'W1' Turn on WiFi LED
        //if(bt_data == "W0") { digitalWrite(WIFI_LED, LOW); } // if 'W0' Turn off WiFi LED
        delay(200);
        pinStatus = String(!digitalRead(RelayPin1)) + String(!digitalRead(RelayPin2)) + String(!digitalRead(Led_Light1)) + String(!digitalRead(Led_Light2)) + String(usDistance);
        //Serial.print(pinStatus);
  }
}

the starting 4 digit i.e, 0000 or 0011 is switch(relay) on/off value and another 3 or 4 digit is ultraSonic sensor data in MM, want to extract subString ultraSonic sensor

@cowboydaniel doesn't exist. It is now @outbackhut

why is your sender checking Serial.available()? why not simply Serial.print() or test your receiver using the serial monitor and you entering the strings (e.g. "A0")?

what's the purpose of the above line? where is the value of pinStatus used?

To convert the end of the string to a 'float' value you can store in a variable, use '.toFloat()'.

void loop() 
{
  if (Serial.available())
  {
    String pinStatus = Serial.readString();
    boolean pinState1 = pinStatus[0] == '1';
    boolean pinState2 = pinStatus[1] == '1';
    boolean pinState3 = pinStatus[2] == '1';
    boolean pinState4 = pinStatus[3] == '1';
    float distance =  pinStatus.substring(4).toFloat();

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