Using Serial.print() on a Core Vs Photon

Hi,

I’ve been playing with a MPU9250 clone module, and gradually getting to grips with this little Time Vampire. :slight_smile:

I’m at the stage were I have a working module and at the stage were I need to get the calibration sorted. I’ve been looking at MagMaster and MagViewer programmes to assist.

My problem is getting the photon of feed via the USB serial port sensor data to collect the data for calibration calculation.

MagMaster wanst to see data in XX,YY,ZZ string format with a CR with at least 100ms between feeds.

I’ve managed to get it MagMaster to read test data, from an Uno, a Particle Core (infact I can get live data from the Core) but not a photon.

I can see the data from all devices, showing apparently correctly formatted data with Putty.

Here is a test program outputting static data, that works fine on a Core, but not on a Photon.

float xv, yv, zv;

void setup()
{   
  Serial.begin(9600);
 
      
}

void loop()
{
  xv= 10;
  yv= 21;
  zv= 9;
  

  Serial.flush(); 
  Serial.print(xv); 
  Serial.print(",");
  Serial.print(yv);
  Serial.print(",");
  Serial.print(zv);
  Serial.println();

  delay(100); 
} 

I suspect there is something different between the serial drivers for the Core and Photon.

Has anyone any ideas, or tricks to get the photon to behave like the Core?

Thanks

Liam

I’d try something like this - on all platforms

const char endOfLine[] = "\r"; // find out what your device needs and apply 
...
  char txt[32];
  snprintf(txt, sizeof(txt), "%02d,%02d,%02d%s", (int)xv, (int)yv, (int)zv, endOfLine);
  Serial.print(txt);

My suspicion would be on different line endings between platforms, so I provided one way to choose your own.
If you only need the “coordinates” as integers, I’d not use float for the variables but int and drop the (int) typecast.
You could also use a different format placeholder for the floats (e.g. %02.0f)

1 Like

Hi Sruff,

Thanks for that and I gave your code a shot and hey presto it worked. But I’m not sure for the right reasons.

I needed \r\n

And then the MagMaster picked up on it.

As soon as I placed a delay greater that 1ms, it stopped working… Strange. The error reports as ‘No Serial data’

So I played around with sending explicit control codes in the traditional serial.print statement with no delay it worked. eg:-

Serial.print("\r\n");

and then returning to:-

Serial.println();

And again it worked without any delay.

Which is odd as the documention says

There sould be atleast 100ms.

As the data will be taken from an i2c device there will be time in the sensor read that exceeds 5ms. The Core and the Uno can send data every 1000ms and the MagMaster accepts it.

So whilst this is a new development, it leaves me thinking along the lines of someing in the driver… But I’m guessing.

Any thoughts?

Thanks for your help.

Liam