Hello.
I'm trying to make my first library, but I have some problem.
I have my sketch working, but when I add my library (UbidotsWeather.h) an error appeared but I don't know what is the problem.
Any help?
(I upload it as file because it is too long for the forum)
If you want help with an error, don't you suppose that you should share the error message?
Sorry, I wrote it in the title. Here is the error anyway
Arduino: 1.6.11 (Mac OS X), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 115200, 4M (3M SPIFFS)"
WeatherStationMaster:22: error: expected initializer before 'dht'
DHT dht(DHTPIN, DHTTYPE);
^
/Users/Mattia/Desktop/Arduino/WeatherStation/WeatherStationMaster/WeatherStationMaster.ino: In function 'void setup()':
WeatherStationMaster:164: error: 'dht' was not declared in this scope
dht.begin();
^
/Users/Mattia/Desktop/Arduino/WeatherStation/WeatherStationMaster/WeatherStationMaster.ino: In function 'void drawHeaderOverlay(OLEDDisplay*, OLEDDisplayUiState*)':
WeatherStationMaster:298: error: 'dht' was not declared in this scope
String temp = String(dht.readTemperature()) + "°C";
^
/Users/Mattia/Desktop/Arduino/WeatherStation/WeatherStationMaster/WeatherStationMaster.ino: In function 'void ubidotsDataUpload()':
WeatherStationMaster:310: error: 'dht' was not declared in this scope
float temp = dht.readTemperature();
^
exit status 1
expected initializer before 'dht'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Creating a local variable that immediately goes out of scope seems pointless.
So delete _token = token; and use Ubidots client(token);?
Pissing away resources on the String class seems pointless. Everything you do with them can be done with strings.
Sorry but I don't understand what it means.
And what can cause the error?
A link to the DHT library you are using would be in order. There are several DHT libraries, some containing a class called dht while others contain a class called DHT.
I use Ubidots client(_token); because I need to start another library.
How does that help, in the UbidotsWeather constructor, when that instance immediately goes out of scope?
name=ESP8266 Weather Station
version=1.1.1
author=Daniel Eichhorn
maintainer=Daniel Eichhorn <squix78@gmail.com>
sentence=ESP8266 based internet connected Weather Station
paragraph=ESP8266 based internet connected Weather Station
category=Display
url=https://github.com/squix78/esp8266-weather-station
architectures=esp8266
Oled library:
name=ESP8266 Oled Driver for SSD1306 display
version=3.2.3
author=Daniel Eichhorn, Fabrice Weinberg
maintainer=Daniel Eichhorn <squix78@gmail.com>
sentence=A I2C display driver for SSD1306 oled displays connected to an ESP8266
paragraph=A I2C display driver for SSD1306 oled displays connected to an ESP8266
category=Display
url=https://github.com/squix78/esp8266-oled-ssd1306
architectures=esp8266
The include guard is supposed to prevent you from including you class definition twice. That means that the include guard (the first two lines) need to use YOUR class name, not some other class name.
I retry to comment all the things related to my library and compile successfully.
The include guard is supposed to prevent you from including you class definition twice. That means that the include guard (the first two lines) need to use YOUR class name, not some other class name.
I just add #include "UbidotsWeather.h" and then compare the SAME error...
Remove the two #include statements from UbidotsWeather.h. Does the error go away? Add them, one at a time, to the sketch. Which one causes the problem?
I see the problem. Take a look at the Morse code example again. That appears to be the starting point for defining your class. Compare the class statement, it's opening and closing curly braces AND WHAT FOLLOWS THEM to those in your class.
MANY THANKS!!!
I add the semicolon and works fine!
I have another question :D.
In the .cpp file, I need to instance a library, Ubidots client(token);. But if I declare it in the constructor function (UbiUbi::UbiUbi) it gives me an error. Only if I put it in the update function works correctly. Is there a way to declare it once in the constructor function and not every time I call back the update one?
No IN the constructor. You declare client as a private instance of the class and create it at the same time as you create the UbiUbi instance.
Works well also this!
I have a problem now...The library must return the two String when I need it, but it returns nothing. But I'm sure that the data is present in the variable because I put a print in the library, after getting the two variable, and the values are correct. Is when I try to use it in the main sketch that seems empty.
This is the actual .cpp code:
#include "UbidotsWeather.h"
UbiUbi::UbiUbi(char* token, char* temp, char* hum) : client(token) {
_temp = temp;
_hum = hum;
}
void UbiUbi::update() {
String _ttt = String(client.getValue(_temp)); // Get the outdoor temperature from Ubidots cloud
Serial.println("Variabile temp presa");
Serial.println(_ttt); //return the correct value
String _hhh = String(client.getValue(_hum)); // Get the outdoor humidity from Ubidots cloud
Serial.println("Variabile hum presa");
Serial.println(_hhh); //return the correct value
}
String UbiUbi::ttt() {
return _ttt;
}
String UbiUbi::hhh() {
return _hhh;
}
void UbiUbi::update() {
String _ttt = String(client.getValue(_temp)); // Get the outdoor temperature from Ubidots cloud
Serial.println("Variabile temp presa");
Serial.println(_ttt); //return the correct value
String _hhh = String(client.getValue(_hum)); // Get the outdoor humidity from Ubidots cloud
Serial.println("Variabile hum presa");
Serial.println(_hhh); //return the correct value
Which class fields is that method valuing? None, because you have variables that are local to the function that hide the class fields.
Then, you call ubi.ttt() which doesn't update the class field before returning the contents of the field. That means that you must have called (and fixed) ubi.update() before calling ttt() or hhh().
Many, MANY thanks!
I remove the String before the variable and finally all works fine!
_ttt = String(client.getValue(_temp)); // Get the outdoor temperature from Ubidots cloud
Serial.println("Variabile temp presa");
Serial.println(_ttt);
_hhh = String(client.getValue(_hum)); // Get the outdoor humidity from Ubidots cloud
Serial.println("Variabile hum presa");
Serial.println(_hhh);