I am working on a few temperature sensors for my house. Two of the three are working just fine. But for some reason the third is giving me issues. The code for all 3 was downloaded from Sinric Pro using their zero code option. The DHT22 is connected to an ESP-12E and the sensor has a built in resistor.
After uploading the code to the board and it is powered through the USB port on my computer, everything works just fine and return the temperature and humidity. I had this running for a few days to make sure. However, when I unplug and power the board through any other power source, the DHT22 returns 0 for temperature and 1 or 2 for humidity initially and does not update anymore (it's set to update every minute).
When I plug the board back into my laptop, after trying with an external power supply, the DHT22 returns 0 degrees and 1% humidity but doesn't update. If I upload the code to the board again, everything works fine but as soon as I unplug the board and try with an external power source again, the problem returns.
I don't think it is a power supply problem. If it was a power supply problem, the device would function properly if I plugged it back into the computer. But, after removing it from the computer, plugging it into my USB adapter and then back into the computer, it doesn't work.
The power supply/USB adapter I have is 5V, 2.4A and the exact same one is powering the other two sensors just fine. I've tried switching power supplies to the other ones I have with zero success.
CNC Machining Service
I've tried different cables, different power supplies (I bought 3 of everything and the other two function perfectly fine with those plugs) and just can't figure it out. My code is below. Thank you for any insight in advance.
UPDATE: I tried 3 different DHT22s and 2 different ESP-12E boards and the result was the same.
/*
- Example for how to use SinricPro Temperaturesensor device:
-
- setup a temperature sensor device
-
- send temperature event to SinricPro server when temperature has changed
- DHT Library used in this example: GitHub - markruys/arduino-DHT: Efficient DHT library for Arduino · GitHub
- If you encounter any issues:
-
- check the readme.md at esp8266-esp32-sdk/README.md at master · sinricpro/esp8266-esp32-sdk · GitHub
-
- ensure all dependent libraries are installed
-
- open serial monitor and check whats happening
-
- check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
-
- visit Issues · sinricpro/esp8266-esp32-sdk · GitHub and check for existing issues or open a new one
*/
- visit Issues · sinricpro/esp8266-esp32-sdk · GitHub and check for existing issues or open a new one
#define ENABLE_DEBUG
#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif
#include <Arduino.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
#include <WiFi.h>
#endif
#include "SinricPro.h"
#include "SinricProTemperaturesensor.h"
#include "DHT.h" // GitHub - markruys/arduino-DHT: Efficient DHT library for Arduino · GitHub
#define WIFI_SSID "my wifi"
#define WIFI_PASS "my password"
#define APP_KEY "from sinric pro"
#define APP_SECRET "from sinric pro"
#define TEMP_SENSOR_ID "from sinric pro"
#define BAUD_RATE 115200 // Change baudrate to your need (used for serial monitor)
#define EVENT_WAIT_TIME 60000 // send event every 60 seconds
#define DHT_PIN 14
DHT dht; // DHT sensor
float temperature; // actual temperature
float humidity; // actual humidity
float lastTemperature; // last known temperature (for compare)
float lastHumidity; // last known humidity (for compare)
unsigned long lastEvent = (-EVENT_WAIT_TIME); // last time event has been sent
/* handleTemperatatureSensor()
-
- Checks if Temperaturesensor is turned on
-
- Checks if time since last event > EVENT_WAIT_TIME to prevent sending too much events
-
- Get actual temperature and humidity and check if these values are valid
-
- Compares actual temperature and humidity to last known temperature and humidity
-
- Send event to SinricPro Server if temperature or humidity changed
*/
void handleTemperaturesensor() {
unsigned long actualMillis = millis();
if (actualMillis - lastEvent < EVENT_WAIT_TIME) return; //only check every EVENT_WAIT_TIME milliseconds
- Send event to SinricPro Server if temperature or humidity changed
if (SinricPro.isConnected() == false) {
Serial.printf("Not connected to Sinric Pro...!\r\n");
return;
}
// temperature = dht.getTemperature(); // get actual temperature in °C
temperature = dht.getTemperature() * 1.8f + 29.9; // get actual temperature in °F
humidity = dht.getHumidity(); // get actual humidity
if (isnan(temperature) || isnan(humidity)) { // reading failed...
Serial.printf("DHT reading failed!\r\n"); // print error message
return; // try again next time
}
if (temperature == lastTemperature || humidity == lastHumidity) return; // if no values changed do nothing...
SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID]; // get temperaturesensor device
bool success = mySensor.sendTemperatureEvent(temperature, humidity); // send event
if (success) { // if event was sent successfuly, print temperature and humidity to serial
Serial.printf("Temperature: %2.1f Celsius\tHumidity: %2.1f%%\r\n", temperature, humidity);
} else { // if sending event failed, print error message
Serial.printf("Something went wrong...could not send Event to server!\r\n");
}
lastTemperature = temperature; // save actual temperature for next compare
lastHumidity = humidity; // save actual humidity for next compare
lastEvent = actualMillis; // save actual time for next compare
}
// setup function for WiFi connection
void setupWiFi() {
Serial.printf("\r\n[Wifi]: Connecting");
#if defined(ESP8266)
WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.setAutoReconnect(true);
#elif defined(ESP32)
WiFi.setSleep(false);
WiFi.setAutoReconnect(true);
#endif
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
Serial.printf(".");
delay(250);
}
IPAddress localIP = WiFi.localIP();
Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
}
// setup function for SinricPro
void setupSinricPro() {
// add device to SinricPro
SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID];
// setup SinricPro
SinricPro.onConnected({ Serial.printf("Connected to SinricPro\r\n"); });
SinricPro.onDisconnected({ Serial.printf("Disconnected from SinricPro\r\n"); });
SinricPro.begin(APP_KEY, APP_SECRET);
}
// main setup function
void setup() {
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
dht.setup(DHT_PIN);
setupWiFi();
setupSinricPro();
}
void loop() {
SinricPro.handle();
handleTemperaturesensor();
}
