Cannot send long string

Hi,

I send a long string from Electron to Arduino but cannot receive it from the Arduino side. But I can receive short message like ‘hello, world’.

The following is the long string:

{ “_id”: “9wxtjvq3vcYmabyyf”, “p0”: 1, “p1”: 1, “p2”: 1, “p3”: 3, “p4”: 3, “p5”: 5, “p6”: 2.9, “p7”: 3, “p8”: 1, “p9”: 1, “p11”: 90, “p12”: 80, “p13”: 0, “p14”: 21978.5, “p15”: 20500, “p16”: 18000, “p17”: 0, “p18”: 21978.5, “p19”: 20500, “p20”: 18000, “p21”: 0, “p22”: 21978.5, “p23”: 20500, “p24”: 18000, “p31”: 5, “p32”: 5, “p33”: 45, “p34”: 1, “p44”: 90, “p81”: 5, “p82”: 5, “p83”: 2, “p99”: 255, “createdAt”: “2016-07-20T08:19:57.466Z”, “deviceId”: “23002a000951343334363138”, “userId”: “wzNvA38prEgWhtnsf”}

Is there any limits for sending data via Serial1.println ?

Thanks.

What do you receive on the Arduino side?
What baudrate are you sending with?

I guess you are running out of free RX buffer space, since there is no handshaking in RX/TX communication.
If your Arduino can’t keep up with the speed you are sending data, you might end up with corrupted values.

A less likely scenario (due to the blocking default on the Electron side) would be that you are filling the TX buffer faster than it’ll be drained by sending the data across.

When I flash this code to two of my Photons, they can happily send your string either way

char msg[] = "{\"_id\": \"9wxtjvq3vcYmabyyf\", \"p0\": 1, \"p1\": 1, \"p2\": 1, \"p3\": 3, \"p4\": 3, \"p5\": 5, "
             "\"p6\": 2.9, \"p7\": 3, \"p8\": 1, \"p9\": 1, \"p11\": 90, \"p12\": 80, \"p13\": 0, \"p14\": 21978.5, "
             "\"p15\": 20500, \"p16\": 18000, \"p17\": 0, \"p18\": 21978.5, \"p19\": 20500, \"p20\": 18000, \"p21\": 0, "
             "\"p22\": 21978.5, \"p23\": 20500, \"p24\": 18000, \"p31\": 5, \"p32\": 5, \"p33\": 45, \"p34\": 1, \"p44\": 90, "
             "\"p81\": 5, \"p82\": 5, \"p83\": 2, \"p99\": 255, \"createdAt\": \"2016-07-20T08:19:57.466Z\", "
             "\"deviceId\": \"23002a000951343334363138\", \"userId\": \"wzNvA38prEgWhtnsf\"}";

void setup() 
{
    pinMode(D7, INPUT_PULLUP);  // jumper D7 to GND to make one device the sender
    Serial1.begin(115200);

    if (digitalRead(D7))    
    { // receiving device
        Serial.begin(115200);
    }
}

void loop() 
{
  if (digitalRead(D7))
  { // receiving device
    while(Serial1.available())
    {
      Serial.write(Serial1.read());
    }
  }
  else
  { // sending device
    Serial1.println(msg);
    delay(10);
  }
}

Thanks for your code.

Now I try to send 40 bytes each time and can receive data successfully. And I delay one second after each transmission.

I am using 9600 baud rate for the transmission between Electron and Arduino.

My Arduino code:

String readString = "";

void loop() {
  
  //sendAllData();
  
  while(Serial2.available()) {
    delay(20);
    char c = Serial2.read();
    readString += c;
  }
  
  readString.trim();
  
  if(readString.length() > 0) {
    Serial.println("Received: " + readString);
    readString = "";
  }
}

My Electron code:

Serial1.println("send parameters");
//Serial1.println("parameters:" + result);
for(int i=0; i<result.length(); ){
    Serial1.print(result.substring(i, i+40));
    Serial.println(result.substring(i, i+40));
    i = i+40;
    delay(1000);
}
Serial1.println(" ");
Serial1.println(" end ");