Why pH1 only fetching in esp8266?
Arduino Code:
#include <EEPROM.h>
#include <NewPing.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "DFRobot_EC.h"
#include "GravityTDS.h"
// Pin assignments
const int phPin1 = A0; // Pre-test pH sensor
const int phPin2 = A1; // Post-test pH sensor
const int ecPin1 = A2; // Pre-test EC sensor
const int ecPin2 = A3; // Post-test EC sensor
const int tempPin1 = 4; // Pre-test temperature sensor
const int tempPin2 = 5; // Post-test temperature sensor
const int tdsPin1 = A8; // Pre-test TDS sensor
const int tdsPin2 = A9; // Post-test TDS sensor
const int turbidityPin1 = A10; // Pre-test turbidity sensor
const int turbidityPin2 = A11; // Post-test turbidity sensor
const int TRIGGER_PIN = 6; // Ultrasonic sensor trigger
const int ECHO_PIN = 7; // Ultrasonic sensor echo
const int MAX_DISTANCE = 200; // Maximum distance in cm for a 21 cm tank
// Relay pin assignments
int relayValve = 8; // Solenoid valve relay
int relayPump1 = 9; // Pump 1 relay
int relayPump2 = 10; // Pump 2 relay
// Variables for sensor readings
float pH1, pH2, ec1, ec2, temp1, temp2, tds1, tds2, turbidity1, turbidity2, distance;
// Calibration coefficients (these will be loaded from EEPROM)
float EC1_K, EC2_K;
float TDS1_value, TDS2_value; // Store TDS values directly from EEPROM
float volt;
const float tempCoefficient = 0.003; // Adjust if needed
// Setup for temperature sensors
OneWire oneWire1(tempPin1);
OneWire oneWire2(tempPin2);
DallasTemperature sensors1(&oneWire1);
DallasTemperature sensors2(&oneWire2);
// Initialize EC sensor objects
DFRobot_EC ec1_sensor, ec2_sensor;
// Initialize TDS sensor objects
GravityTDS tds1_sensor, tds2_sensor;
// Calibration values for pH sensor 2
float voltage_4_01 = 3.02;
float voltage_6_86 = 2.25;
float voltage_9_18 = 2.05;
//ph solutions
float pH_4_01 = 4.01;
float pH_6_86 = 6.86;
float pH_9_18 = 9.18;
// EC Variables
float voltage, ecValue, temperature = 25;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
// Initialize pin modes
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(relayPump1, OUTPUT); // Set pump 1 relay as OUTPUT
pinMode(relayPump2, OUTPUT); // Set pump 2 relay as OUTPUT
pinMode(relayValve, OUTPUT); // Set valve relay as OUTPUT
digitalWrite(relayPump1, LOW);
digitalWrite(relayPump2, LOW);
digitalWrite(relayValve, LOW);
// Initialize sensors
sensors1.begin();
sensors2.begin();
ec1_sensor.begin();
ec2_sensor.begin();
tds1_sensor.setPin(tdsPin1);
tds2_sensor.setPin(tdsPin2);
tds1_sensor.setAref(5.0);
tds2_sensor.setAref(5.0);
tds1_sensor.setAdcRange(1024);
tds2_sensor.setAdcRange(1024);
tds1_sensor.begin();
tds2_sensor.begin();
}
void loop() {
long duration, inches, cm;
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
if (inches <= 6.00) {
digitalWrite(relayPump1,HIGH);
digitalWrite(relayPump2,HIGH);
digitalWrite(relayValve,HIGH);
} else if (inches > 6.00){
digitalWrite(relayPump1,LOW);
digitalWrite(relayPump2,LOW);
digitalWrite(relayValve,LOW);
}
// Read temperatures for compensation
sensors1.requestTemperatures();
temp1 = sensors1.getTempCByIndex(0);
sensors2.requestTemperatures();
temp2 = sensors2.getTempCByIndex(0);
// Set temperature for TDS sensors
tds1_sensor.setTemperature(temp1);
tds2_sensor.setTemperature(temp2);
// Update TDS readings
tds1_sensor.update();
tds2_sensor.update();
TDS1_value = tds1_sensor.getTdsValue();
TDS2_value = tds2_sensor.getTdsValue();
// Read sensors with temperature compensation
pH1 = readCalibratedPH1(phPin1, temp1);
pH2 = readCalibratedPH2(phPin2, temp2);
ec1 = readCalibratedEC1(ecPin1, temp1);
ec2 = readCalibratedEC2(ecPin2, temp2);
turbidity1 = readCalibratedTurbidity1();
turbidity2 = readCalibratedTurbidity2();
// Print raw values to Serial
Serial.print("pH1: "); Serial.println(pH1);
Serial.print("pH2: "); Serial.println(pH2);
Serial.print("EC1: "); Serial.println(ec1);
Serial.print("EC2: "); Serial.println(ec2);
Serial.print("Temp1: "); Serial.println(temp1);
Serial.print("Temp2: "); Serial.println(temp2);
Serial.print("TDS1: "); Serial.println(TDS1_value);
Serial.print("TDS2: "); Serial.println(TDS2_value);
Serial.print("Turbidity1: "); Serial.println(turbidity1);
Serial.print("Turbidity2: "); Serial.println(turbidity2);
Serial.print("Distance: "); Serial.println(inches);
// Print raw values to Serial (sending to ESP8266)
Serial1.print(pH1);
Serial1.print(pH2);
Serial1.print(",");
Serial1.print(ec1);
Serial1.print(",");
Serial1.print(ec2);
Serial1.print(",");
Serial1.print(temp1);
Serial.print(",");
Serial1.print(temp2);
Serial1.print(",");
Serial1.print(TDS1_value);
Serial1.print(",");
Serial1.print(TDS2_value);
Serial1.print(",");
Serial1.print(turbidity1);
Serial1.print(",");
Serial1.print(turbidity2);
Serial1.print(",");
Serial1.println(inches);
delay(1000); // 1-second delay
}
float readCalibratedPH1(int pin, float temp) {
sensors1.requestTemperatures(); // Request temperature from DS18B20
float temp1 = sensors1.getTempCByIndex(0);
int sensorValue = analogRead(pin);
double voltage = sensorValue * (5.0 / 1024.0); // Analog-to-Digital Conversion
// pH calculation logic for sensor 2
float pH_step_4_to_6 = (voltage_6_86 - voltage_4_01) / (pH_6_86 - pH_4_01);
float pH_step_6_to_9 = (voltage_9_18 - voltage_6_86) / (pH_9_18 - pH_6_86);
float pH = 0.0;
if (voltage > voltage_6_86) {
// For voltages between pH 4.01 and pH 6.86
pH = pH_4_01 + ((voltage - voltage_4_01) / pH_step_4_to_6);
} else {
// For voltages between pH 6.86 and pH 9.18
pH = pH_6_86 + ((voltage - voltage_6_86) / pH_step_6_to_9);
}
pH += tempCoefficient * (temp2 - 25.0);
return pH;
}
float readCalibratedPH2(int pin, float temp) {
sensors2.requestTemperatures(); // Request temperature from DS18B20
float temp2 = sensors1.getTempCByIndex(0);
int sensorValue = analogRead(pin);
double voltage = sensorValue * (5.0 / 1024.0); // Analog-to-Digital Conversion
// pH calculation logic for sensor 2
float pH_step_4_to_6 = (voltage_6_86 - voltage_4_01) / (pH_6_86 - pH_4_01);
float pH_step_6_to_9 = (voltage_9_18 - voltage_6_86) / (pH_9_18 - pH_6_86);
float pH = 0.0;
if (voltage > voltage_6_86) {
// For voltages between pH 4.01 and pH 6.86
pH = pH_4_01 + ((voltage - voltage_4_01) / pH_step_4_to_6);
} else {
// For voltages between pH 6.86 and pH 9.18
pH = pH_6_86 + ((voltage - voltage_6_86) / pH_step_6_to_9);
}
pH += tempCoefficient * (temp2 - 25.0);
return pH;
}
float readCalibratedEC1(int pin, float temp) {
static unsigned long timepoint = millis(); // Store the current time
// Check if 1 second has passed
if (millis() - timepoint > 1000U) {
timepoint = millis();
// Read temperature from DS18B20
sensors1.requestTemperatures(); // Request temperature readings
float temperature = sensors1.getTempCByIndex(0); // Get temperature in Celsius
// Read EC sensor voltage
voltage = analogRead(pin) / 1024.0 * 5000; // Convert ADC value to voltage (mV)
// Convert voltage to EC with temperature compensation
ecValue = ec1_sensor.readEC(voltage, temperature);
// Calibration process if needed (remove if not applicable)
// ec1_sensor.calibration(voltage, temperature);
}
return ecValue; // Return the EC value
}
float readCalibratedEC2(int pin, float temp) {
static unsigned long timepoint = millis(); // Store the current time
// Check if 1 second has passed
if (millis() - timepoint > 1000U) {
timepoint = millis();
// Read temperature from DS18B20
sensors2.requestTemperatures(); // Request temperature readings
float temperature = sensors2.getTempCByIndex(0); // Get temperature in Celsius
// Read EC sensor voltage
voltage = analogRead(pin) / 1024.0 * 5000; // Convert ADC value to voltage (mV)
// Convert voltage to EC with temperature compensation
ecValue = ec2_sensor.readEC(voltage, temperature);
// Calibration process if needed (remove if not applicable)
// ec2_sensor.calibration(voltage, temperature);
}
return ecValue; // Return the EC value
}
long microsecondsToInches(long microseconds){
return (microseconds / 74) / 2;
}
long microsecondsToCentimeters(long microseconds){
return (microseconds / 29) / 2;
}
float readCalibratedTurbidity1() {
return calculateTurbidity1(turbidityPin1);
}
float readCalibratedTurbidity2() {
return calculateTurbidity2(turbidityPin2); // You can modify this if needed for different calibration
}
float calculateTurbidity1(int pin) {
volt = 0;
for (int i = 0; i < 800; i++) {
volt += ((float)analogRead(pin) / 1023) * 5.00;
}
volt = volt / 800;
volt = round_to_dp(volt, 2);
if (volt < 2.5) {
return 3000; // NTU value for low voltage
} else {
return -1120.4 * square(volt) + 5742.3 * volt - 4353.8; // NTU calculation
}
}
float round_to_dp(float in_value, int decimal_place) {
float multiplier = powf(10.0f, decimal_place);
in_value = roundf(in_value * multiplier) / multiplier;
return in_value;
}
float calculateTurbidity2(int pin) {
volt = 0;
for (int i = 0; i < 800; i++) {
volt += ((float)analogRead(pin) / 1023) * 5.12;
}
volt = volt / 800;
volt = round_to_dp2(volt, 2);
if (volt < 2.5) {
return 3000; // NTU value for low voltage
} else {
return -1120.4 * square(volt) + 5742.3 * volt - 4353.8; // NTU calculation
}
}
float round_to_dp2(float in_value, int decimal_place) {
float multiplier = powf(10.0f, decimal_place);
in_value = roundf(in_value * multiplier) / multiplier;
return in_value;
}
Esp8266 code:
#include <SoftwareSerial.h>
// Define pins for SoftwareSerial (modify these if needed)
#define RX_PIN 14 // D5
#define TX_PIN 12 // D6
// Create a SoftwareSerial object
SoftwareSerial swSer(RX_PIN, TX_PIN);
#define BLYNK_TEMPLATE_ID "sample"
#define BLYNK_TEMPLATE_NAME "Auth"
#define BLYNK_AUTH_TOKEN "auth"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// WiFi credentials
char ssid[] = "user";
char pass[] = "pass";
void setup() {
Serial.begin(9600);
swSer.begin(9600);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
Serial.println("ESP8266 Software Serial is Connected");
}
void loop() {
Blynk.run();
if (swSer.available() > 0) {
String receivedData = swSer.readStringUntil('\n');
Serial.println("Received: " + receivedData);
// Parse data (assuming the same format as before)
int commaIndex = 0;
float pH1, pH2, ec1, ec2, temp1, temp2, TDS1_value, TDS2_value, turbidity1, turbidity2, distance;
pH1 = receivedData.substring(commaIndex, (commaIndex = receivedData.indexOf(',', commaIndex + 1))).toFloat();
pH2 = receivedData.substring(commaIndex, (commaIndex = receivedData.indexOf(',', commaIndex + 1))).toFloat();
ec1 = receivedData.substring(commaIndex, (commaIndex = receivedData.indexOf(',', commaIndex + 1))).toFloat();
ec2 = receivedData.substring(commaIndex, (commaIndex = receivedData.indexOf(',', commaIndex + 1))).toFloat();
temp1 = receivedData.substring(commaIndex, (commaIndex = receivedData.indexOf(',', commaIndex + 1))).toFloat();
temp2 = receivedData.substring(commaIndex, (commaIndex = receivedData.indexOf(',', commaIndex + 1))).toFloat();
TDS1_value = receivedData.substring(commaIndex, (commaIndex = receivedData.indexOf(',', commaIndex + 1))).toFloat();
TDS2_value = receivedData.substring(commaIndex, (commaIndex = receivedData.indexOf(',', commaIndex + 1))).toFloat();
turbidity1 = receivedData.substring(commaIndex, (commaIndex = receivedData.indexOf(',', commaIndex + 1))).toFloat();
turbidity2 = receivedData.substring(commaIndex, (commaIndex = receivedData.indexOf(',', commaIndex + 1))).toFloat();
distance = receivedData.substring(commaIndex).toFloat();
// Send data to Blynk
Blynk.virtualWrite(V0, pH1);
Blynk.virtualWrite(V1, pH2);
Blynk.virtualWrite(V2, ec1);
Blynk.virtualWrite(V3, ec2);
Blynk.virtualWrite(V4, temp1);
Blynk.virtualWrite(V5, temp2);
Blynk.virtualWrite(V6, TDS1_value);
Blynk.virtualWrite(V7, TDS2_value);
Blynk.virtualWrite(V8, turbidity1);
Blynk.virtualWrite(V9, turbidity2);
Blynk.virtualWrite(V10, distance);
}
}