dht22 показывает "nan" данные
- Войдите на сайт для отправки комментариев
Ср, 20/01/2021 - 10:02
Привет! Пытаюсь отправить данные с dht22 в базу mysql в определенное время с помощью ds3231. Но вместо реальных данных всегда получаю «nan». В чем может быть проблема?
//idSensor 1
//fridgeName горячий цех-шкаф холодильный
#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"
#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#include <TimeLib.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Newer Ethernet shields have a MAC address printed on a sticker on the shield
IPAddress server(192, 168, 7, 1); //IPv4 address
// set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);
EthernetClient client;
void setup() {
Serial.begin(9600);
// wait for Arduino Serial Monitor
while (!Serial) ;
// get and set the time from the RTC
setSyncProvider(RTC.get);
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
// create the alarms, to trigger functions at specific times
Alarm.alarmRepeat(8, 35, 0, Morningalarm);
Ethernet.begin(mac);
dht.begin();
}
void loop() {
digitalClockDisplay();
// wait one second between each clock display in serial monitor
Alarm.delay(1000);
}
int idSensor = 1;
String fridgeName = "горячий%20цех-шкаф%20холодильный";
float hum = dht.readHumidity(); //Reading the humidity and storing in hum
float temp = dht.readTemperature(); //Reading the temperature as Celsius and storing in temp
float fah = dht.readTemperature(true); //reading the temperature in Fahrenheit
float heat_index = dht.computeHeatIndex(fah, hum); //Reading the heat index in Fahrenheit
float heat_indexC = dht.convertFtoC(heat_index); //Converting the heat index in Celsius
void Morningalarm() {
if (client.connect(server, 80)) {
Serial.println("connected");
//client.print("GET /data.php?temperature=25&humidity=27&heat_index=24"); //Connecting and Sending values to database
client.print("GET /data.php?");
client.print("temperature=");
client.print(temp);
client.print("&humidity=");
client.print(hum);
client.print("&heat_index=");
client.print(heat_indexC);
client.print("&idSensor=");
client.print(idSensor);
client.print("&fridgeName=");
client.print(fridgeName);
client.println(" HTTP/1.1");
client.println("Host: 192.168.7.1");
client.println("Connection: close");
client.println();
Serial.print("GET /data.php?");
Serial.print("temperature=");
Serial.print(temp);
Serial.print("&humidity=");
Serial.print(hum);
Serial.print("&heat_index=");
Serial.print(heat_indexC);
Serial.print("&idSensor=");
Serial.print(idSensor);
Serial.print("&fridgeName=");
Serial.print(fridgeName);
Serial.println(" HTTP/1.1");
client.stop(); //Closing the connection
} else {
//if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void digitalClockDisplay() {
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
dht22 подключен к 5v, ds3231 к 3.3v

Опять сразу космический корабль... отдельно работает DHT на тестовом скетче? Что в Serial Monitor видно, какие показания?
Отдельно dht работает.
Serial monitor:
Проблема в том, что получение данных с датчика расположено вне исполняемого кода. Скорее всего это как объявление переменных и срабатывает, но единственный раз и до вызова dht.begin().
Строки 48 - 52 после строки 54 поставьте.
Если отдельно работает, то сначала делайте все в простом лупе, без библиотек типа alarm*. Заработает - переходите к усложнению.
А вообще - дюпонт может просто плохо контачить в загибе под некоторым углом.
"Строки 48 - 52 после строки 54 поставьте."
Спасибо! Теперь работает:
Заметил. Тут что-то с библиотекой.Установил библиотеку для DHT21, а датчик поставил DHT11. Работает без NAN. Наоборот не работает.
часы реального времени там явно лишние