string over serial communication

how can i send a string over serial communication? do i have to send it in bytes and convert it or whats the best way to do this also the string will change depending on the information input from the sender arduino

Maybe look at the c_str() method of the String class ( if that is what you mean by “string” )

Then you are sending a standard data type and most tutorials should cover this.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

...R

Serial monitor of Arduino is a very useful feature.Serial monitor is used to see receive data, send data,print data and so on.Serial monitor is connected to the Arduino through serial communication. This serial communication occurs using RX (pin 0) and TX (pin 1) terminal of Arduino.


myaarpmedicare.com

i think im also confused with how to send im using a nano and esp8266 my string is in a variable and it wont let me serial write the variable

int buttonPin = 4;
int buttonState;
int count = 0;


void setup() {
  // put your setup code here, to run once:
pinMode(buttonPin, INPUT);
Serial.begin(115200);



}

void loop() 
{
  // Checks if the button state has change
  buttonState = digitalRead(buttonPin);
  
  
  

  if (buttonState == HIGH){ 
    //Serial.println("on");
    while (buttonState == HIGH){
    
      count += 1;
      //Serial.println(count);
      delay(1000);

    
      buttonState = digitalRead(buttonPin);
      if (buttonState == LOW){ 
        //Serial.println("off");
    
        unsigned long finalCount = count;
        int hours = finalCount / 3600UL;
        int minutes = (finalCount - hours * 3600UL) / 60UL;
        int seconds = (finalCount - hours * 3600UL - minutes * 60UL);
   
        String hourString = String("Hours:%20") + String(hours) + String("%20");
        //Serial.println(hourString);
        
        String minuteString = String("Minutes:%20") + String(minutes) + String("%20");
        //Serial.println(minuteString);
        
        String secondString = String("Seconds:%20") + String(seconds) + String("%20%C2");
        //Serial.println(secondString);
        String theTime = hourString + minuteString + secondString;
        Serial.println(theTime);  
        
   
        Serial.write(theTime);
      }
        
        break;
        
      
    }
  }
  count = 0;
  delay(1000);
}

Use Serial.print()

...R

CSV (comma separated values) terminated by a newline is a common data format
Check out my example of parsing GPS data
https://www.forward.com.au/pfod/ArduinoProgramming/Serial_IO/index.html#GPS

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