Reading data from a serial sensor

I’m porting a PH sensor over from the Arduino, to the Sparkcore, and having some issues - the Sparkcore isn’t reading any data from the PH sensor over the serial port (I’ve tried monitoring it via USB and via the web app).

My code is as follows;


char ph_data[20]; //we make a 20 byte character array to hold incoming data from the pH.
byte received_from_sensor=0; //we need to know how many characters have been received.
float ph=0; //used to hold a floating point number that is the pH.
float i = 1;

void setup(){

Spark.variable("ph", &ph, INT);
Spark.variable("i", &i, INT);

Serial.begin(38400);         //enable the hardware serial port
 
delay(50); 
 
Serial.print("c\r");     //turn the pH Circuit into continues mode. 
delay(50);                 //on start up sometimes the first command is missed. 
Serial.print("c\r");     //so, let’s send it twice.
delay(50);                 //a short delay after the pH Circuit was taken out of continues mode is used to make sure we don’t over load it with commands.

}

void loop(){

 received_from_sensor=Serial.readBytesUntil(13,ph_data,20); //we read the data sent from pH Circuit until we see a <CR>. We also count how many character have been received.  
 Serial.println(ph_data);          //transmit that data received from the pH Circuit to the serial monitor.
 ph=atof(ph_data);                // convert a sting into a float

 
 i = i + 1;  //access this via the web app to test the value is changing
 
 delay(380); 

}


Can anyone spot any obvious problems? The PH sensor I’m using is this one - https://www.atlas-scientific.com/product_pages/embedded/ph.html

1 Like

@timcopeland, which serial port? The one connected to the Spark's USB socket, or the one attached to the TX & RX pins? In firmware, they are referred to as Serial (USB) and Serial1 (TX/RX pins).

Paul

2 Likes

Good questions @PaulRB - I didn’t realises there were two different serial ports on the Sparkcore (which in retrospect is really obvious).

That pretty much explains why my code wasn’t working - the PH circuit is connected to the RX and TX pins (Serial1), and I was try to access it on the serial port on the USB (the only time I need that is to preview the stream of PH data for debugging)

I’ll post an update to my code as a reference when I’ve tweaked it a bit more (but in short, it’s replacing Serial with Serial1 in most bits of the above code).

Tim