✅ Práctica 28
▷ #TSCLab #TCLab #ESP32 #Arduino #Control #MACI
En el siguiente blog se presenta la vigésima quinta práctica del laboratorio de control de temperatura y velocidad de un motor.
Objetivo general:
- Enviar los valores sensados de temperatura del TSC-Lab por WiFi a Node-Red y visualizarlos.
Materiales:
- Node-Red
- TSC-Lab
Introducción:
En la práctica anterior se aprendió a utilizar y familiarizarse con Node-Red, el envío de información se lo hizo por medio de comunicación serial. Sin embargo, no tiene mucho sentido que se esté enviando información a Node-Red por el puerto serial cuando se puede aprovechar el ESP-32 para conectarse a internet por medio de Wi-Fi. Se usará el protocolo HTTP para conectarse al servidor donde se aloja el servidor que se está ejecutando Node-Red, lo cual permitirá que cualquier dispositivo tenga acceso a su información.
Procedimiento:
Nota: se asume que está instalado Node-Red, que está familiarizado con el entorno y que sabe instalar librerías. En caso de no ser así, puede revisar la practica anterior dando clic aquí.
- Abrir el entorno de Node-Red e instalar la librería: node-red-contrib-hostip.
- Importar el proyecto que se encuentra disponible aquí.
- Deshabilitar la entrada de puerto serial y el dashboard del proyecto anterior dando doble clic en los bloques y seleccionando Enabled para que se cambie a Disabled. Recomiendo esto para cada proyecto con el fin de evitar confliectos cuando realice uno nuevo.
- Dar Deploy al proyecto importado.
- Seleccionar el ícono de Debug.
- Anotar el addres que se muestra en el debug, en el object 1. En mi caso fue 192.168.0.101.
- Desabilitar el bloque de msg.payload.
- Importar un nuevo proyecto el cual se encuentra disponible aquí.
- Copiar y cargar el siguiente código al TSC-Lab. Realice las respectivas modificaciones tanto de red como de serverName. Si en el monitor serial se imprime HTTP Response code: 200, significa que la carga y conexión han sido exitosas. Puede desconectar el cable USB y alimentarlo con la fuente externa.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
****************************** TSC-Lab ******************************* | |
***************************** PRACTICE 25 ***************************** | |
This practice is aboutNode-Red (with wi-fi) | |
By: Kevin E. Chica O | |
More information: https://tsc-lab.blogspot.com/ | |
*/ | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
//GPIO pin 0 is set as OneWire bus | |
OneWire ourWire1(0); | |
//GPIO pin 4 is set as OneWire bus | |
OneWire ourWire2(4); | |
//A variable or object is declared for our sensor 1 | |
DallasTemperature sensors1(&ourWire1); | |
//A variable or object is declared for our sensor 2 | |
DallasTemperature sensors2(&ourWire2); | |
#include <WiFi.h> | |
#include <HTTPClient.h> | |
// WiFi | |
const char *ssid = "your_ssid"; // Enter your WiFi name | |
const char *password = "your_password"; // Enter WiFi password | |
//Your Domain name with URL path or IP address with path | |
const char* serverName = "http://192.168.0.101:1880/update-sensor"; //repleace 192.168.0.101 for your addres | |
String temperatura1 = ""; | |
String temperatura2 = ""; | |
float temp1; | |
float temp2; | |
void setup() { | |
//sensors | |
sensors1.begin(); //Sensor 1 starts | |
sensors2.begin(); //Sensor 2 starts | |
// Set software serial baud to 115200; | |
Serial.begin(115200); | |
// connecting to a WiFi network | |
//wifi | |
WiFi.mode(WIFI_STA); | |
} | |
void loop() { | |
connect_wifi(); | |
readData(); | |
publicData(); | |
delay(1000); | |
} | |
void readData() { | |
//The command is sent to read the temperature | |
sensors1.requestTemperatures(); | |
//Obtain the temperature in ºC of sensor 1 | |
temp1 = sensors1.getTempCByIndex(0); | |
temperatura1 = String(temp1); | |
//The command is sent to read the temperature | |
sensors2.requestTemperatures(); | |
//Obtain the temperature in ºC of sensor 2 | |
temp2 = sensors2.getTempCByIndex(0); | |
temperatura2 = String(temp2); | |
//print to display the temperature change | |
} | |
void connect_wifi(){ | |
// Connect or reconnect to WiFi | |
if(WiFi.status() != WL_CONNECTED){ | |
Serial.print("Attempting to connect to SSID: "); | |
Serial.println(ssid); | |
while(WiFi.status() != WL_CONNECTED){ | |
WiFi.begin(ssid, password); // Connect to WPA/WPA2 network. Change this line if using open or WEP network | |
Serial.print("."); | |
delay(5000); | |
} | |
Serial.println("\nConnected."); | |
} | |
} | |
void publicData(){ | |
WiFiClient client; | |
HTTPClient http; | |
// Your Domain name with URL path or IP address with path | |
http.begin(client, serverName); | |
// Specify content-type header | |
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); | |
// Data to send with HTTP POST | |
String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=DS18B20&value1="+temperatura1+"&value2="+temperatura2; | |
// Send HTTP POST request | |
int httpResponseCode = http.POST(httpRequestData); | |
Serial.print("HTTP Response code: "); | |
Serial.println(httpResponseCode); | |
// Free resources | |
http.end(); | |
} |
Repositories: https://github.com/vasanza/TSC-Lab/tree/main/Practice25
- Visualice los resultados en el dashboard de NodeRed.
Comments
Post a Comment