#include #include #include #include //Motor de polea const int motorA = 22; const int motorB = 23; const int frecuencia = 5000; const int canalA = 0; const int canalB = 1; const int resolucion = 8; //Valores de 0 a 255 bool bPolea = false; const int nPoleaTiempo = 1500; const int nVelocidad = 255; // 255 maximo const int oneWireBus = 25; // GPIO where the DS18B20 is connected to (temperature sensor) #define TdsSensorPin 35 //GPIO where the TDS sensor is connected to #define VREF 3.3 // analog reference voltage(Volt) of the ADC #define SCOUNT 20 // sum of sample point int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC int analogBufferTemp[SCOUNT]; int analogBufferIndex = 0; int copyIndex = 0; float averageVoltage = 0; float tdsValue = 0; float temperature = 0; //Sensors temperature and tds OneWire oneWire(oneWireBus); // Setup a oneWire instance to communicate with any OneWire devices DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature sensor int getMedianNum(int bArray[], int iFilterLen); //Wifi connecction and server const char* ssid = "TheVickyZone";//No soy wifi const char* password = "theVicky123"; // const String SERVER_ADDRESS = "http://192.168.46.199:3000"; void SendData(float temp, float tds, float oxi); ///////////////////////////////////////////////Sensor de oxigeno inicio #define DO_PIN 34 #define VREFO 5000 //VREF (mv) #define ADC_RES 1024 //ADC Resolution //Single-point calibration Mode=0 //Two-point calibration Mode=1 #define TWO_POINT_CALIBRATION 0 #define READ_TEMP (25) //Current water temperature ℃, Or temperature sensor function //Single point calibration needs to be filled CAL1_V and CAL1_T #define CAL1_V (131) //mv #define CAL1_T (25) //℃ //Two-point calibration needs to be filled CAL2_V and CAL2_T //CAL1 High temperature point, CAL2 Low temperature point #define CAL2_V (1300) //mv #define CAL2_T (15) //℃ const uint16_t DO_Table[41] = { 14460, 14220, 13820, 13440, 13090, 12740, 12420, 12110, 11810, 11530, 11260, 11010, 10770, 10530, 10300, 10080, 9860, 9660, 9460, 9270, 9080, 8900, 8730, 8570, 8410, 8250, 8110, 7960, 7820, 7690, 7560, 7430, 7300, 7180, 7070, 6950, 6840, 6730, 6630, 6530, 6410}; uint8_t Temperaturet; uint16_t ADC_Raw; uint16_t ADC_Voltage; uint16_t DO; int16_t readDO(uint32_t voltage_mv, uint8_t temperature_c) { #if TWO_POINT_CALIBRATION == 00 uint16_t V_saturation = (uint32_t)CAL1_V + (uint32_t)35 * temperature_c - (uint32_t)CAL1_T * 35; return (voltage_mv * DO_Table[temperature_c] / V_saturation); #else uint16_t V_saturation = (int16_t)((int8_t)temperature_c - CAL2_T) * ((uint16_t)CAL1_V - CAL2_V) / ((uint8_t)CAL1_T - CAL2_T) + CAL2_V; return (voltage_mv * DO_Table[temperature_c] / V_saturation); #endif } ////////////////////////////////////////////////////Sensor de oxigeno fin void setup() { delay(10); Serial.begin(115200); pinMode(TdsSensorPin, INPUT); sensors.begin(); //Inicializamos sensor de temperatura //Inicializamos y conectamos a la red wifi WiFi.begin(ssid, password); Serial.print("Conectando..."); while (WiFi.status() != WL_CONNECTED) { //Check for the connection delay(500); Serial.print("."); } Serial.print("Conectado con éxito, mi IP es: "); Serial.println(WiFi.localIP()); //Configuracion de motores ledcSetup(canalA, frecuencia, resolucion);//Configurar funcionalidad PWM motorA ledcSetup(canalB, frecuencia, resolucion);//configurar funcionalidad PWM motorB ledcAttachPin(motorA, canalA);//Asociamos el canal A al motor A ledcAttachPin(motorB, canalB);//Asociamos el canal B al moto B } void loop() { //Bajar polea ledcWrite(canalA, nVelocidad); ledcWrite(canalB, 0); delay(nPoleaTiempo); //Parar polea ledcWrite(canalA, 0); ledcWrite(canalB, 0); Serial.println("Polea abajo"); delay(5000); //Esperar 5 segundos antes de medir la temperatura sensors.requestTemperatures(); //Solicitar temperatura float temperature = sensors.getTempCByIndex(0); //Guardar temperatura //Leer y guardar 20 muestras del sensor de tds for(int i = 0; i < 20; i++){ float tempF = analogRead(TdsSensorPin); analogBuffer[i] = tempF; //read the analog value and store into the buffer Serial.println(String(tempF)); delay(40);//Esperar 40 milisegundos } //Copiar las muestras al buffer temporal for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++){ analogBufferTemp[copyIndex] = analogBuffer[copyIndex]; } //Calcular promedio de las 20 muestras obtenidas averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF / 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value float compensationCoefficient = 1.0 + 0.02 * (temperature - 25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0)); valor-original:25 float compensationVolatge = averageVoltage / compensationCoefficient; //temperature compensation tdsValue = (133.42 * compensationVolatge * compensationVolatge * compensationVolatge - 255.86 * compensationVolatge * compensationVolatge + 857.39 * compensationVolatge) * 0.5; //convert voltage value to tds value //Medicion sensor de oxigeno ADC_Raw = analogRead(DO_PIN); ADC_Voltage = uint32_t(VREFO) * ADC_Raw / ADC_RES; float fOxigeno = (readDO(ADC_Voltage, 25))/1000; //Enviar los valores por get SendData(temperature,tdsValue,fOxigeno); String strvalores = "T: "+String(temperature)+" TDS: "+String(tdsValue)+" Oxi: "+String(fOxigeno); Serial.println(strvalores); //Subir polea ledcWrite(canalA, 0); ledcWrite(canalB, nVelocidad); delay(nPoleaTiempo); //Detener polea ledcWrite(canalA, 0); ledcWrite(canalB, 0); Serial.println("Polea arriba"); //Esperar 1 minuto delay(3000); } int getMedianNum(int bArray[], int iFilterLen) { int bTab[iFilterLen]; for (byte i = 0; i < iFilterLen; i++) bTab[i] = bArray[i]; int i, j, bTemp; for (j = 0; j < iFilterLen - 1; j++) { for (i = 0; i < iFilterLen - j - 1; i++) { if (bTab[i] > bTab[i + 1]) { bTemp = bTab[i]; bTab[i] = bTab[i + 1]; bTab[i + 1] = bTemp; } } } if ((iFilterLen & 1) > 0) bTemp = bTab[(iFilterLen - 1) / 2]; else bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2; return bTemp; } void SendData(float temp, float tds, float oxi){ if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status HTTPClient http; // String strTemp = String(); // String strTds = String(24.6); // String strOxi = String(27.8); String fullURL = SERVER_ADDRESS + "/save-values?temp=" + String(temp) + "&tds=" + String(tds) + "&oxi=" + String(oxi); http.begin(fullURL); //Indicamos el destino http.addHeader("Content-Type", "plain-text"); //Preparamos el header text/plain si solo vamos a enviar texto plano sin un paradigma llave:valor. int codigo_respuesta = http.GET(); //Enviamos el post pasándole, los datos que queremos enviar. (esta función nos devuelve un código que guardamos en un int) if(codigo_respuesta>0){ Serial.println("Código HTTP ► " + String(codigo_respuesta)); //Print return code if(codigo_respuesta == 200){ String cuerpo_respuesta = http.getString(); Serial.println("El servidor respondió ▼ "); Serial.println(cuerpo_respuesta); } } else{ Serial.print("Error enviando POST, código: "); Serial.println(codigo_respuesta); } http.end(); //libero recursos } else{ Serial.println("Error en la conexión WIFI"); } }