Arduino IDE con IoT: ESP32 + Sensores externos + IoT

OBJETIVO

Ahora vamos a utilizar el ESP32 SIN EL ARDUINO ALVIK podemos sacar la placa microcontroladora y ponerlo en una placa protoboard y experimentar con sensores y actuadores estándares en el mercado :

2024-07-15 13_08_38-esp32 nano arduino at DuckDuckGo.png  + 2024-07-15 13_09_30-placa protoboard at DuckDuckGo.png

Para ver varias posibilidades, vamos a ver estos sensores y actuadores (recomendamos ver estas páginas actuadores y sensores)

+2024-07-15 13_11_03-semaforo arduino at DuckDuckGo.png+sensorluzarduino.jpg+CCS811-KEYSTUDUUDIO.png

ESQUEMA DE CONEXIONES

2024-07-07 20_14_06-Alvik User Manual _ Arduino Documentation.png

2024-07-15 14_47_48-New ESP32 Project - Wokwi Simulator.png

2024-07-15 14_08_38-(1) Exploring the Arduino Nano ESP32 _ MicroPython & IoT Cloud - YouTube.png

DEVICES 

Nos vamos a Arduino Cloud, y en DEVICES añadimos el ESP32 y obtenemos el TOKEN o palabra secreta (si has hecho la práctica anterior, no es necesario pues ya tenemos el TOKEN o palabra secreta) como es similar al caso anterior, no lo desarrollamos. (Nos pedirá también el SSID y la contraseña de la red wifi)

VARIABLES

Añadimos las siguientes variables :

EL SCKETCH -LIBRERIA CCS811

Primero añadiríamos la librería de keystudio https://fs.keyestudio.com/KS0457 pero no lo permite Arduino Cloud, viendo las instrucciones, vemos que son las mismas que en los ejemplos de esta librería la de DF que es la que instalamos :

2024-07-15 13_41_58-DETECTOR CO2 Thing _ Arduino Cloud.png

esto provoca la incorporación de la línea 1 #include <DFRobot_CCS811.h>

EL SCKETCH -EL CÓDIGO

#include <DFRobot_CCS811.h>
/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/17c10209-3874-430a-877c-c082ff7dd38d 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  int cO2;
  int luz;
  bool luzdigital;
  bool rojo;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

//DFRobot_CCS811 CCS811(&Wire, /*IIC_ADDRESS=*/0x5A);
DFRobot_CCS811 CCS811;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();


  while(CCS811.begin() != 0){
        Serial.println("failed to init chip, please check if the chip connection is fine");
        delay(1000);
    }
  pinMode(1,OUTPUT);
  pinMode(0,INPUT);
  
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
   if(CCS811.checkDataReady() == true){
        Serial.print("CO2: ");
        Serial.print(CCS811.getCO2PPM());
        cO2=CCS811.getCO2PPM();
        Serial.print("ppm, TVOC: ");
        Serial.print(CCS811.getTVOCPPB());
        Serial.println("ppb");
        
    } else {
        Serial.println("Data is not ready!");
    }
    luz = analogRead(A0);
    if (rojo){
      digitalWrite(1,HIGH);
    }else{
      digitalWrite(1,LOW);
    }
    luzdigital=digitalRead(0);
  
  
}





/*
  Since Rojo is READ_WRITE variable, onRojoChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onRojoChange()  {
  // Add your code here to act upon Rojo change
}
DASHBOARD

2024-07-15 14_16_36-CO2 Dashboard _ Arduino Cloud.png

Alternativa : en vez de luz tendría que llamarse "oscuridad" que sea luz pero que vaya al revés

RESULTADO

ALTERNATIVA:  Que el semáforo visualice los niveles peligrosos de CO2, por ejemplo el umbral del amarillo 600-1.000

¿Te atreves a poner un servomotor?


Revision #13
Created 15 July 2024 13:06:48 by Javier Quintana
Updated 24 April 2025 11:32:42 by Javier Quintana