#include <LiquidCrystal.h>
#include <WiFi.h>
#include <PubSubClient.h>
unsigned long suma_pot = 0;
unsigned long suma_nivel = 0;
unsigned long muestras = 0;
int potenciometro = 0, nivel = 0, referencia = 0, nivel_mm;
unsigned long tiempo_lectura = 0, tiempo_mqtt = 5000;
unsigned long tiempo_muestreo = 100;
float T = 100.0, error_int = 0.0;
const char* mqttServer = "instrumentacion-uji.dynv6.net";
const int mqttPort = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
LiquidCrystal lcd(12,13,17,16,27,14);
void PubMQTT(int nivel, int referencia)
{
char str[3]={' ',' ',' '};
sprintf(str, "%d",nivel);
client.publish("mesa01/deposito/nivel",(uint8_t*) str,3,true);
sprintf(str, "%d",referencia);
client.publish("mesa01/deposito/referencia",(uint8_t*) str,3,true);
}
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
ledcAttach(26, 1000,12); //Se configura el PWM
//frecuencia 1KHz y resolucion 2^12
lcd.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "",6);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
lcd.print(".");
}
lcd.setCursor(0,1);
lcd.print(" Connected!");
delay(500);
client.setServer(mqttServer, mqttPort);
while (!client.connected())
{
lcd.setCursor(0,0);
lcd.print("Connecting to MQTT...");
if (client.connect("ESP32Client" )) {
lcd.setCursor(0,1);
lcd.print("connected");
}
else
{
lcd.setCursor(0,1);
lcd.print("failed: ");
lcd.print(client.state());
delay(2000);
}
}
delay(500);
}
void loop() {
suma_pot += analogRead(39);
suma_nivel += analogRead(36);
muestras++;
if (millis()>tiempo_lectura) {
tiempo_lectura += tiempo_muestreo;
potenciometro = suma_pot / muestras;
nivel = suma_nivel / muestras;
suma_pot = 0;
suma_nivel = 0;
muestras = 0;
lcd.setCursor(0,0);
referencia = potenciometro * 500.0 / 4093.0;
nivel_mm = nivel * 500.0 / 4093.0 + 60;
float error = (referencia-nivel_mm);
error_int = error_int + T*error;
float tension = 0.3*(error+ error_int/8000.0);
if (tension>15) {
tension = 15.0;
error_int = error_int - T*error;
}
if (tension<0) {
tension = 0;
error_int = error_int - T*error;
}
ledcWrite(26,(int) (tension*4093.0/15.0));
lcd.setCursor(0,0);
lcd.print("R:");
lcd.print(referencia);
lcd.print("mm T:");
lcd.print(tension);
lcd.print("V ");
lcd.setCursor(0,1);
lcd.print("N:");
lcd.print(nivel_mm);
lcd.print("mm I:");
lcd.print(error_int/8000);
}
if (millis()>tiempo_mqtt) {
tiempo_mqtt += 1000;
PubMQTT(nivel_mm, referencia);
while (!client.connected())
{ Serial.print("Connecting to MQTT...");
if (client.connect("ESP32Client" ))
Serial.println("connected");
else
{ Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
}
}
/* Apartado 1
#include <LiquidCrystal.h>
unsigned long suma_pot = 0;
unsigned long suma_nivel = 0;
unsigned long muestras = 0;
int potenciometro = 0;
unsigned long tiempo_lectura = 0;
unsigned long tiempo_muestreo = 100;
LiquidCrystal lcd(12,13,17,16,27,14);
void setup() {
lcd.begin(16, 2);
ledcAttach(26, 1000,12); //Se configura el PWM
//frecuencia 1KHz y resolucion 2^12
}
void loop() {
suma_pot += analogRead(39);
muestras++;
if (millis()>tiempo_lectura) {
tiempo_lectura += tiempo_muestreo;
potenciometro = suma_pot / muestras;
suma_pot = 0;
muestras = 0;
ledcWrite(26,potenciometro);
lcd.setCursor(0,0);
float tension = potenciometro * 15.0 / 4093;
lcd.print(tension);
}
}
*/
/* APARTADO 2
#include <LiquidCrystal.h>
unsigned long suma_pot = 0;
unsigned long suma_nivel = 0;
unsigned long muestras = 0;
int potenciometro = 0, nivel = 0;
unsigned long tiempo_lectura = 0;
unsigned long tiempo_muestreo = 100;
LiquidCrystal lcd(12,13,17,16,27,14);
void setup() {
lcd.begin(16, 2);
ledcAttach(26, 1000,12); //Se configura el PWM
//frecuencia 1KHz y resolucion 2^12
}
void loop() {
suma_pot += analogRead(39);
suma_nivel += analogRead(36);
muestras++;
if (millis()>tiempo_lectura) {
tiempo_lectura += tiempo_muestreo;
potenciometro = suma_pot / muestras;
nivel = suma_nivel / muestras;
suma_pot = 0;
suma_nivel = 0;
muestras = 0;
ledcWrite(26,potenciometro);
lcd.setCursor(0,0);
float tension = potenciometro * 15.0 / 4093;
float nivel_mm = nivel * 500.0 / 4093 + 60;
lcd.setCursor(0,0);
lcd.print(tension);
lcd.print("V ");
lcd.setCursor(0,1);
lcd.print(nivel_mm);
lcd.print("mm ");
}
}*/
/* APARTADO 3
#include <LiquidCrystal.h>
unsigned long suma_pot = 0;
unsigned long suma_nivel = 0;
unsigned long muestras = 0;
int potenciometro = 0, nivel = 0;
unsigned long tiempo_lectura = 0;
unsigned long tiempo_muestreo = 100;
LiquidCrystal lcd(12,13,17,16,27,14);
void setup() {
lcd.begin(16, 2);
ledcAttach(26, 1000,12); //Se configura el PWM
//frecuencia 1KHz y resolucion 2^12
}
void loop() {
suma_pot += analogRead(39);
suma_nivel += analogRead(36);
muestras++;
if (millis()>tiempo_lectura) {
tiempo_lectura += tiempo_muestreo;
potenciometro = suma_pot / muestras;
nivel = suma_nivel / muestras;
suma_pot = 0;
suma_nivel = 0;
muestras = 0;
lcd.setCursor(0,0);
int referencia = potenciometro * 500.0 / 4093;
int nivel_mm = nivel * 500.0 / 4093.0 + 60;
float tension;
if (referencia>nivel_mm) tension = 0.9*15.0;
if (referencia<nivel_mm) tension = 0;
ledcWrite(26,(int) (tension*4093.0/15.0));
lcd.setCursor(0,0);
lcd.print("R:");
lcd.print(referencia);
lcd.print("mm T:");
lcd.print(tension);
lcd.print("V ");
lcd.setCursor(0,1);
lcd.print("N:");
lcd.print(nivel_mm);
lcd.print("mm ");
}
}
*/
/* APARTADO 4
#include <LiquidCrystal.h>
unsigned long suma_pot = 0;
unsigned long suma_nivel = 0;
unsigned long muestras = 0;
int potenciometro = 0, nivel = 0;
unsigned long tiempo_lectura = 0;
unsigned long tiempo_muestreo = 100;
LiquidCrystal lcd(12,13,17,16,27,14);
void setup() {
lcd.begin(16, 2);
ledcAttach(26, 1000,12); //Se configura el PWM
//frecuencia 1KHz y resolucion 2^12
}
void loop() {
suma_pot += analogRead(39);
suma_nivel += analogRead(36);
muestras++;
if (millis()>tiempo_lectura) {
tiempo_lectura += tiempo_muestreo;
potenciometro = suma_pot / muestras;
nivel = suma_nivel / muestras;
suma_pot = 0;
suma_nivel = 0;
muestras = 0;
lcd.setCursor(0,0);
int referencia = potenciometro * 500.0 / 4093;
int nivel_mm = nivel * 500.0 / 4093.0 + 60;
float tension = 0.5*(referencia-nivel_mm);
if (tension>15) tension = 15.0;
if (tension<0) tension = 0;
ledcWrite(26,(int) (tension*4093.0/15.0));
lcd.setCursor(0,0);
lcd.print("R:");
lcd.print(referencia);
lcd.print("mm T:");
lcd.print(tension);
lcd.print("V ");
lcd.setCursor(0,1);
lcd.print("N:");
lcd.print(nivel_mm);
lcd.print("mm ");
}
}
*/
/* APARTADO 5
#include <LiquidCrystal.h>
unsigned long suma_pot = 0;
unsigned long suma_nivel = 0;
unsigned long muestras = 0;
int potenciometro = 0, nivel = 0;
unsigned long tiempo_lectura = 0;
unsigned long tiempo_muestreo = 100;
float T = 100.0, error_int = 0.0;
LiquidCrystal lcd(12,13,17,16,27,14);
void setup() {
lcd.begin(16, 2);
ledcAttach(26, 1000,12); //Se configura el PWM
//frecuencia 1KHz y resolucion 2^12
}
void loop() {
suma_pot += analogRead(39);
suma_nivel += analogRead(36);
muestras++;
if (millis()>tiempo_lectura) {
tiempo_lectura += tiempo_muestreo;
potenciometro = suma_pot / muestras;
nivel = suma_nivel / muestras;
suma_pot = 0;
suma_nivel = 0;
muestras = 0;
lcd.setCursor(0,0);
int referencia = potenciometro * 500.0 / 4093;
int nivel_mm = nivel * 500.0 / 4093.0 + 60;
float error = (referencia-nivel_mm);
error_int = error_int + T*error;
float tension = 0.3*(error+ error_int/8000.0);
if (tension>15) {
tension = 15.0;
error_int = error_int - T*error;
}
if (tension<0) {
tension = 0;
error_int = error_int - T*error;
}
ledcWrite(26,(int) (tension*4093.0/15.0));
lcd.setCursor(0,0);
lcd.print("R:");
lcd.print(referencia);
lcd.print("mm T:");
lcd.print(tension);
lcd.print("V ");
lcd.setCursor(0,1);
lcd.print("N:");
lcd.print(nivel_mm);
lcd.print("mm I:");
lcd.print(error_int/8000);
}
}
*/