1

I have two different Vernier science probes connected to a Vernier shield that connects to an Arduino Uno. I have the Uno connected to an ESP8266 via the Tx.

Here is my Uno code:

String sensor1 ="Conductivity Probe ";
float interceptSensor1 = 0.0;
float slopeSensor1 = 960;
String sensor2 ="pH Probe ";
float interceptSensor2 = 13.720;
float slopeSensor2 = -3.838;
int timeBetweenReadings = 2000; // in ms

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

void loop() {
  float analogSensor1 = analogRead(A0);
  float voltageSensor1 = analogSensor1 / 1023 * 5.0;
  // convert from count to raw voltage
  float sensor1Reading= interceptSensor1 + voltageSensor1 * slopeSensor1;
  //converts voltage to sensor reading
  String sensor1Output = sensor1 + sensor1Reading;
  Serial.println(sensor1Output);
  delay(timeBetweenReadings);
  // delay in between reads for stability
  float analogSensor2 = analogRead(A1);
  float voltageSensor2 = analogSensor2 / 1023 * 5.0;
  // convert from count to raw voltage
  float sensor2Reading= interceptSensor2 + voltageSensor2 * slopeSensor2; //converts voltage to sensor reading
  String sensor2Output = sensor2 + sensor2Reading;
  Serial.println(sensor2Output);
  delay(timeBetweenReadings);
  // delay in between reads for stability
}

On the serial monitor every two seconds I get one line of output and it alternates back and forth between the sensors:

Conductivity Probe 807.04

pH Probe 4.12

Conductivity Probe 802.35

pH Probe 4.13

Conductivity Probe 802.35

pH Probe 4.12

Since I've only got a simple serial line to work with but I need to send multiple, different types of data to the ESP8266, my strategy is to send one line (ex. Conductivity Probe 807.04) and use logic on the ESP8226 to parse the string, isolate the float number (807.04), and use Conductivity Probe to determine how that number gets further used (in my case, to publish to an MQTT feed called ElectroConductivity).

The Tx of the Uno + sensors is wired to the Rx of the ESP8266, and here's the ESP8266 code:

  while (Serial.available() == 0) {
  }
  sensorReading = Serial.readString();
  Serial.println(sensorReading);

The data comes in large groups instead of line by line:

pH Probe 4.12
Conductivity Probe 807.04
pH Probe 4.12
Conductivity Probe 807.04

I'm new to serial comms and Arduino in general. I'm guessing that there's an issue with how serial is read by the ESP8266.

EDIT

When I use sensorReading = Serial.readStringUntil('\n'); instead my output looks like this:

Conductivity PrpH Probe 4.12

Conductivity Probe 816.42

pH Probe 4.12

Conductivity Probe 825.8Conductivity Probe 821.11

Conductivity Probe 821.11

pH Probe pH Probe 4.12

ConductivitypH Probe 4.12
  • 1
    What's your question? – user31481 Aug 17 '17 at 14:36
  • The question is why is he getting the data at the other end of the serial link in blocks rather than line by line. – Andrew Aug 17 '17 at 14:45
2

readString() will keep reading until it times out. Instead try readStringUntil('\n') to read until you get a new line character.

  • Thanks! That seemed to have done it. BTW, it's \n, not /n. Was getting conductivity cut off when using the former, haha. So readString() will just read as much as it can before timeout? I never explicitly set a timeout for readString(), so I'm assuming that if it's not explicitly set then there's a default of like 4 seconds or something? – user3135509 Aug 17 '17 at 14:56
  • Bah, it didn't work. Please see updated post. – user3135509 Aug 17 '17 at 14:58
  • @user3135509 oops. Fixed the \ Vs / typo. That new behavior is weird, it's almost as if something isn't keeping up and the serial receive buffer is overflowing between loops. Do you have a lot of other code in the loop or is it just reading and printing for now? At this point I'd be tempted to read the incoming serial data byte by byte rather than using the readString functions, that way you gain a lot of flexibility and can ignore any timeout related issues. – Andrew Aug 17 '17 at 15:06
  • On the Uno side, everything you see in the OP is what's in the loop. On the receiving side of the ESP8266, there are other things in the loop, such as setting up MQTT subscriptions and publications. So, uh, having other code running in the loop() function, code that's unrelated to serial, would cause the serial buffer to overflow? – user3135509 Aug 17 '17 at 15:11
  • 1
    @user3135509 it depends on how long the other stuff takes and how fast the serial data is coming in. It looks like you have 2 seconds between lines so that should be plenty of time, as a sanity check maybe comment the other stuff out and see if it starts working or not. – Andrew Aug 17 '17 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.