Wastewater pH and TDS Monitoring Code
Wastewater pH and TDS Monitoring Code
The program uses different serial communication strategies for debugging and external device communication. Serial.begin is used for USB debugging via the default Serial port, while espSerial, defined as Serial1, is used for communication with an ESP module handling commands and responses. This dual usage allows for development phase debugging, ensuring accurate data transmission and command processing through different channels .
The ADC (Analog-to-Digital Converter) reading from the pH sensor is converted into a voltage level using the formula: voltagePH = phanalog * (3.3 / 1023.0). Subsequently, the pH value is calculated using the equation: pH = 14 - (7 + ((2.5 - voltagePH) / 0.55)). This transformation assumes a linear relationship between voltage and pH with calibration factors applied to match the sensor's characteristics to standard pH values .
The setup function initializes hardware by setting up the I2C for communication with the LCD, beginning the serial communication, and configuring the MAX6675 thermocouple sensor. It also configures GPIO pins for solenoid control. Initial LCD messages indicate system readiness. Sensor-specific initializations, like setting the SPI speed for the MAX6675, ensure the devices operate reliably according to their respective communication protocols .
Debugging mode is enabled if the 'debug' boolean flag is set to true, which prompts initialization of USB serial communication for debug logging. This mode provides real-time feedback on command processing by outputting logs to the console, aiding in traceability and troubleshooting. When enabled, it can impact program execution by adding overhead to processing and potentially introduce delays, which is why it is optional .
The virtuinoRun function manages incoming serial data from the espSerial object, looking for specific start and end characters to process incoming Virtuino commands. Upon detection, it reads the command, executes Virtuino-specific actions, and sends responses back through the ESP module. This ensures the main loop can handle incoming requests efficiently without interruption, maintaining device control and interactivity .
The program checks if the pH of the wastewater is below 6 or above 8. If below 6, it activates Solenoid 1 to adjust the pH by turning the solenoid ON, signaling through the digitalWrite command. Similarly, if the pH is above 8, Solenoid 2 is activated to correct the pH level. The components involved in this process include the pH sensor and solenoids connected to pins 14 and 15, which are controlled with the digitalWrite function when certain conditions are met .
The program employs if statements to evaluate pH conditions. If the pH is less than 6, it activates solenoid 1 (acid adjustment) by writing HIGH to its GPIO pin, and similarly, if the pH exceeds 8, it activates solenoid 2 (base adjustment). These checks ensure corrective actions are executed only when necessary, showing a clear relation between monitored pH levels and hardware response .
Turbidity is calculated by reading a voltage value from the turbidity sensor, which is converted to a turbidity value using the formula: turbidity = 100 - (volt * 20). This conversion assumes a linear relationship between voltage and turbidity, implying that when voltage varies, the turbidity linearly decreases or increases based on this function. This approach simplifies complex turbidity calculations by assuming a consistent sensor output behavior .
The floating-point array 'V' acts as a memory mechanism to store various sensor values and control signals, indexed correspondingly to represent different states or measurements like temperature, turbidity, and pH. This approach allows different parts of the program to track, update, and react to these stored values dynamically, facilitating a cohesive control and monitoring system across the code's functions .
The setup uses a MAX6675 thermocouple to measure temperature. It initializes the thermocouple with specified data, clock, and select pins, and retrieves the temperature using the getTemperature method. The temperature reading is then stored in an array V and displayed on an LCD screen with a formatted output that includes 'T=' followed by the temperature in Celsius .