0
votes

I am starting with the creation of arduino libraries. I have errors when including libraries that are already created.

This is to include the DHT22 sensor library in another library using the Arduino Ide.

References:

Arduino c++ classes, How to make instance variables of another class/library

https://www.arduino.cc/en/Hacking/LibraryTutorial

Library DHT22:

https://github.com/adafruit/DHT-sensor-library

Used files:

This file temperaturaPOO.h

#ifndef temperaturaPOO_h
#define temperaturaPOO_h
#include "Arduino.h" 
#include <DHT.h>

DHT dht(1, DHT22);

class temperaturaPOO {  
    int sensor_humedad;
    int sensor_temperatura;
    public:
        temperaturaPOO(int); // ?? x
        void mostrar();     
};
#endif

This file temperaturaPOO.cpp

#include <temperaturaPOO.h>
temperaturaPOO::temperaturaPOO(int pin){
    DHT dht(pin, DHT22);
}

void temperaturaPOO::mostrar()
{
    sensor_humedad = dht.readHumidity();  
    sensor_temperatura = dht.readTemperature();

    Serial.print("Humidity:");
    Serial.println(sensor_humedad);
    Serial.print("Temperature:");
    Serial.println(sensor_temperatura);
}

This file blinkLedPOO.ino

#include <temperaturaPOO.h>
temperaturaPOO temp(1); // se instancia
void setup() {
    Serial.begin(9600);
}
void loop() {
    temp.mostrar();
}
3
where is temperaturaPOO.h? Is it in library folder or in the same folder with .ino file?dzuda11
is in the library folder.Diego Dorado
This is the procedure: I create a folder with the name temperaturePOO. Within it there are two files and a folder, this folder also has the name of temperaturePOO and inside it is only the file temperaturaPOO.ino.Outside it are the files: - temperaturaPOO.h - temperaturePOO.cpp What I do is zip the main folder and then import it as a normal arduino library.Diego Dorado
Can you tell me why do you have DHT dht(1, DHT22); in .h file and what error do you get in console?dzuda11
Archiving built core (caching) in: C:\Users\Diego\AppData\Local\Temp\arduino_cache_455009\core\core_arduino_avr_nano_cpu_atmega328_0c812875ac70eb4a9b385d8fb077f54c.a libraries\temperaturaPOO\temperaturaPOO.cpp.o (symbol from plugin): In function dht': (.text+0x0): multiple definition of dht' sketch\temperaturaPOO.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here collect2.exe: error: ld returned 1 exit status exit status 1 Error compilando para la tarjeta Arduino Nano.Diego Dorado

3 Answers

0
votes

"You have the problem because you tried to define DHT dht(1, DHT22); multiple time.

Try this:
your .h file should look like

#ifndef temperaturaPOO_h
#define temperaturaPOO_h
#include "Arduino.h" 
#include <DHT.h>

class temperaturaPOO {  
private:
    DHT dht;            //define dth here
    int sensor_humedad;
    int sensor_temperatura;
    public:
        temperaturaPOO(int); // ?? x
        void mostrar();     
};
#endif

and .cpp

#include "temperaturaPOO.h"
temperaturaPOO::temperaturaPOO(int pin){
    dth = DHT(pin, DHT22);
}

void temperaturaPOO::mostrar()
{
    sensor_humedad = dht.readHumidity();  
    sensor_temperatura = dht.readTemperature();

    Serial.print("Humidity:");
    Serial.println(sensor_humedad);
    Serial.print("Temperature:");
    Serial.println(sensor_temperatura);
}
0
votes

The answer is in one of the links you posted. The comments explain how to initialize objects in a constructor. When you pass a member constructor, you want to use initializer list.

The header should look something like this.

#ifndef temperaturaPOO_h
#define temperaturaPOO_h
#include "Arduino.h" 
#include <DHT.h>

class temperaturaPOO {  
    int sensor_humedad;
    int sensor_temperatura;
public:
    temperaturaPOO(int);
    void mostrar();

private:
    DHT dht;
};
#endif

And here is the implementation. Look how dht is initialized.

#include <temperaturaPOO.h>

temperaturaPOO::temperaturaPOO(int pin): dht(pin, DHT22) {}

void temperaturaPOO::mostrar()
{
    sensor_humedad = dht.readHumidity();  
    sensor_temperatura = dht.readTemperature();

    Serial.print("Humidity:");
    Serial.println(sensor_humedad);
    Serial.print("Temperature:");
    Serial.println(sensor_temperatura);
}
0
votes

Thanks, it works!!

I did not read the sensor data, so I added these two lines at the beginning of the method and it works properly.

temperaturaPOO.cpp

void temperaturaPOO::mostrar()
{
    dht.begin(); // add this
    delay(2000); // add this
    sensor_humedad = dht.readHumidity();  
    sensor_temperatura = dht.readTemperature();
    Serial.print("Humidity:");
    Serial.println(sensor_humedad);
    Serial.print("Temperature:");
    Serial.println(sensor_temperatura);
}

I have one doubt in the initialization of the attributes of the constructor:

How do you read this line, in other words how to be documented to make a good understanding, thank you!

This Line:

temperaturaPOO::temperaturaPOO(int pin): dht(pin, DHT22) {}