Skip to main content

ESP32 + Sensores externos + IoT

OBJETIVO

Ahora vamos a utilizar el ESP32 SIN EL ARDUINO ALVIK podemos sacar el chip  y ponerlo en una placa protoboard y experimentar que se puede utilizar 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

Vamos a ver estos sensores y actuadores (para saber más sobre actuadores y sensores)

  • Un led de salida simple, para practicar salida digital en mi caso voy a elegir este gracioso semáforo
  • Un sensor LDR pero para practicar los dos tipos de señal, uno que proporcione tanto señal analógica como señal digital.
  • Un sensor CO2  CCS811 con protocolo I2C

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

ESQUEMA DE CONEXIONES
    SEMAFORO LED ROJO al D1 del ESP32 MODULO SENSOR LDR
      SEÑAL DIGITAL al D0 del ESP32 SEÑAL ANALÓGICA al A0 del ESP32 MODULO SENSOR CO2

       

      DESARROLLO
      1. Nos vamos a Arudino 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)
        1. Nos pedirá también el SSID y la contraseña de la red wifi
      2. Añadimos las siguientes variables
        1. CO2 tipo int y Read
        2. luz  tipo int y Read
        3. luzdigital tipo bool y Read
        4. rojo tipo bool y Read&Write
      3. El Sketch
      EL SCKETCH -LIBRERIA CCS811

      Primero añadimos 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
      • Tenemos las variables definidas en las líneas 10-13 : 
        • int cO2;
        •  int luz;
        •  bool luzdigital;
        •  bool rojo;
      • Definimos una variable de tipo el sensor CCS811 en la línea 23 DFRobot_CCS811 CCS811;
      • En Setup en las líneas 48-21 arrancamos ese sensor:
        •  while(CCS811.begin() != 0){
                  Serial.println("failed to init chip, please check if the chip connection is fine");
                  delay(1000);
            }
      • Definimos los pines digitales 0 y 1  como entrada y salida respectivamente:  
        • pinMode(1,OUTPUT);
            pinMode(0,INPUT);

      #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
      }
      RESULTADO