I have a WiFi module ESP8266 + NodeMCU v3 to which I have connected two DS18B20 sensors to pin D7.
For some reason, the sensors are not being detected.
Please advise why.
- #include <Arduino.h>
- #include "TemperatureSensorController.cpp"
- TemperatureSensorController temperatureSensorController;
- void setup()
- {
- Serial.begin(115200);
- temperatureSensorController = TemperatureSensorController(D7);
- }
- void loop()
- {
- temperatureSensorController.printTemperatures();
- delay(1000);
- }
- #include <DallasTemperature.h>
- #ifndef MYCLASS_H
- #define MYCLASS_H
- class TemperatureSensorController
- {
- public:
- TemperatureSensorController()
- {
- }
- TemperatureSensorController(uint8_t oneWireBus) : TemperatureSensorController()
- {
- _oneWire = OneWire(oneWireBus);
- _sensors = DallasTemperature(&_oneWire);
- }
- void printTemperatures()
- {
- Serial.println("Print Temperatures");
- Serial.println("");
- delay(100);
- _sensors.begin();
- delay(100);
- _sensors.requestTemperatures();
- Serial.print("Device count: ");
- Serial.println(_sensors.getDeviceCount());
- Serial.println("");
- for (int i = 0; i < _sensors.getDeviceCount(); i++)
- {
- Serial.print("Device no. ");
- Serial.print(i + 1);
- Serial.print(" Temperature: ");
- Serial.print(_sensors.getTempCByIndex(i));
- Serial.println(" °C");
- delay(100);
- }
- Serial.println("");
- Serial.println("-----------------------");
- Serial.println("");
- }
- public:
- OneWire _oneWire;
- DallasTemperature _sensors;
- };
- #endif
The physical connections are fine - running the code where everything is in the main.cpp file, the readings are done correctly.
- #include <Arduino.h>
- #include "TemperatureSensorController.cpp"
- OneWire oneWire = OneWire(D7);
- DallasTemperature sensors = DallasTemperature(&oneWire);
- void setup()
- {
- Serial.begin(115200);
- sensors.begin();
- }
- void loop()
- {
- Serial.println("Print Temperatures");
- Serial.println("");
- delay(100);
- sensors.begin();
- delay(100);
- sensors.requestTemperatures();
- Serial.print("Device count: ");
- Serial.println(sensors.getDeviceCount());
- Serial.println("");
- for (int i = 0; i < sensors.getDeviceCount(); i++)
- {
- Serial.print("Device no. ");
- Serial.print(i + 1);
- Serial.print(" Temperature: ");
- Serial.print(sensors.getTempCByIndex(i));
- Serial.println(" °C");
- delay(100);
- }
- Serial.println("");
- Serial.println("-----------------------");
- Serial.println("");
- delay(5000);
- }