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