✅ Práctica 23
▷ #TSCLab #TCLab #ESP32 #Arduino #Control #MACI
En el siguiente blog se presenta la vigésima tercera práctica del laboratorio de control de temperatura y velocidad de un motor.
Objetivo general:
- Conectar el TSC-Lab a la plataforma ThingSpeak.
Objetivos específicos:
- Enviar los valores sensados y estados de los transistores a ThingSpeak.
- Visualizar los valores sensados y estados de los transistores en ThingSpeak.
Materiales:
- ThingSpeak
- TSC-Lab
Introducción:
En la práctica anterior se aprenció a realizar una conexión WiFi con el TSC-Lab. Ahora se le sacará provecho a dicha conexión para poder enviar información y posteriormente visualizarla en ThingSpeak, la cual es una plataforma abierta de aplicaciones, diseñada para permitir conectar personas con objetos, en este caso con la MACI y el TSC-Lab.
Procedimiento:
Nota: Se asume que la placa del ESP-32 y las bibliotecas de las prácticas anteriores han sido previamente instaladas en el IDE de Arduino. Para esta práctica debe instalar la librería de ThingSpeak.
- Abrir el siguiente enlace y crearse una cuenta en ThingSpeak en caso de que no la tenga. Para el registro se puede utilizar cualquier cuenta de correo.
- Aparecerá en pantalla algo como en la imagen que se muestra abajo, se debe dar clic en "New Channel":
- Llenar los campos como se muestra en la imagen y presionar "Save channel":
- Copiar el código en el IDE de Arduino:
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 23 ***************************** | |
This practice is about ThingSpeak (Http) connection | |
By: Kevin E. Chica O | |
Reviewed: Víctor Asanza | |
More information: https://tsc-lab.blogspot.com/ | |
More examples: https://github.com/vasanza/TSC-Lab | |
Dataset: http://ieee-dataport.org/4138 | |
*/ | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
//GPIO pin 0clie 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); | |
//Status of transistors | |
int t1 = 0; | |
int t2 = 0; | |
//set parameters | |
int period=12; //medium period | |
int freq=20000; // sampling time | |
// WiFi | |
#include <WiFi.h> | |
WiFiClient client; | |
const char *ssid = "Chica Orellana"; // Enter your WiFi name | |
const char *password = "ManchasyBombon1998"; // Enter WiFi password | |
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros | |
unsigned long myChannelNumber = 1523638; //your ID | |
const char * myWriteAPIKey = "AQ1OY0GGI3F0HCHO"; // your API-KEY | |
void setup() { | |
delay(1000); | |
Serial.begin(115200); | |
sensors1.begin(); //Sensor 1 starts | |
sensors2.begin(); //Sensor 2 starts | |
//transistor 1 | |
pinMode(16, OUTPUT); | |
//transitor 2 | |
pinMode(17, OUTPUT); | |
//wifi | |
WiFi.mode(WIFI_STA); | |
connect_wifi(); | |
ThingSpeak.begin(client); // Initialize ThingSpeak | |
Serial.println("Choose any case: "); | |
} | |
void loop() { | |
if (Serial.available()) | |
{ | |
String string = Serial.readStringUntil('\n'); | |
if (string == "case_1") { | |
Serial.println("Case 1 started"); | |
for (int i = 1; i <= 10; i++) { | |
//transistor 1 deactivate | |
t1 = 0; | |
//transistor 2 deactivate | |
t2 = 0; | |
readData(); | |
readData(); | |
} | |
Serial.println("Case 1 finished"); | |
Serial.println("Choose any case: "); | |
} | |
else if (string == "case_2") { | |
Serial.println("Case 2 started"); | |
for (int i = 1; i <= 10; i++) { | |
//transistor 1 activate | |
digitalWrite(16, HIGH); | |
t1 = 1; | |
readData(); | |
//transistor 1 deactivate | |
t1 = 0; | |
digitalWrite(16, LOW); | |
readData(); | |
} | |
Serial.println("Case 2 finished"); | |
Serial.println("Choose any case: "); | |
} | |
else if (string == "case_3") { | |
Serial.println("Case 3 started"); | |
for (int i = 1; i <= 10; i++) { | |
//transistor 2 activate | |
digitalWrite(17, HIGH); | |
t2 = 1; | |
readData(); | |
//transistor 2 deactivate | |
t2 = 0; | |
digitalWrite(17, LOW); | |
readData(); | |
} | |
Serial.println("Case 3 finished"); | |
Serial.println("Choose any case: "); | |
} | |
else if (string == "case_4") { | |
Serial.println("Case 4 started"); | |
for (int i = 1; i <= 10; i++) { | |
//transistor 1 activate | |
digitalWrite(16, HIGH); | |
t1 = 1; | |
//transistor 2 activate | |
digitalWrite(17, HIGH); | |
t2 = 1; | |
readData(); | |
//transistor 1 deactivate | |
t1 = 0; | |
digitalWrite(16, LOW); | |
//transistor 2 deactivate | |
t2 = 0; | |
digitalWrite(17, LOW); | |
readData(); | |
} | |
Serial.println("Case 4 finished"); | |
Serial.println("Choose any case: "); | |
} | |
} | |
} | |
//method to read data for 12 minute | |
void readData() { | |
uint32_t timer = period * 60000L; | |
for ( uint32_t tStart = millis(); (millis() - tStart) < timer; ) { | |
connect_wifi(); | |
//The command is sent to read the temperature | |
sensors1.requestTemperatures(); | |
//Obtain the temperature in ºC of sensor 1 | |
float temp1 = sensors1.getTempCByIndex(0); | |
//The command is sent to read the temperature | |
sensors2.requestTemperatures(); | |
//Obtain the temperature in ºC of sensor 2 | |
float temp2 = sensors2.getTempCByIndex(0); | |
//print to display the temperature change | |
Serial.print(temp1); | |
Serial.print(","); | |
Serial.print(temp2); | |
Serial.print(","); | |
Serial.print(t1); | |
Serial.print(","); | |
Serial.println(t2); | |
public_ThingSpeak(temp1,temp2,t1,t2); | |
delay(freq); | |
} | |
} | |
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 public_ThingSpeak(float temp1,float temp2, int trans1, int trans2){ | |
// set the fields with the values | |
ThingSpeak.setField(1, (temp1)); | |
ThingSpeak.setField(2, (temp2)); | |
ThingSpeak.setField(3, (trans1)); | |
ThingSpeak.setField(4, (trans2)); | |
// write to the ThingSpeak channel | |
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); | |
if(x == 200){ | |
Serial.println("Channel update successful."); | |
} | |
else{ | |
Serial.println("Problem updating channel. HTTP error code " + String(x)); | |
} | |
//delay(20000); // Wait 20 seconds to update the channel again | |
} |
Repositories: https://github.com/vasanza/TSC-Lab/tree/main/Practice14
- Del código anterior se debe cambiar las variables "myChannelNumber" y "myWriteAPIKey", situadas en las líneas 42 y 43, respectivamente. En la plataforma ThingSpeak, la primera variable se encuentra en la parte superior del canal creado y la segunda en la pestaña "Api Keys", tal cual como se muestra en las siguientes imágenes:
- Cargar el código al TSC-Lab. De no tener ningún problema los datos se puede visualizar en la pestaña "Private View" y se vería algo así:
Comments
Post a Comment