'Error when implement an nodemcu 8266 with DHT22
I implemented a nodemcu 8266 as a api rest server with a temperature sensor (DHT22) in a Mac with macOS Monterey, when I read the temperature and humidity in a simple loop I don't have any problem, the implementation looks like this
#include <DHT.h>
#include <DHT_U.h>
float hum, temp;
DHT dht(D1, DHT22);
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
temp = dht.readTemperature();
hum = dht.readHumidity();
Serial.println("Temperatura: " + String(temp) + " humedad: " + String(hum));
}
but when I implement a server api and try to read temperature to return that information the IDE return an error impossible to read
like this
User exception (panic/abort/assert)
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
Panic core_esp8266_main.cpp:137 __yield
>>>stack>>>
ctx: sys
sp: 3fffeab0 end: 3fffffb0 offset: 0000
3fffeab0: 5c93ba25 b85ced93 00000000 40102074
3fffeac0: 000000fe 00000000 00000000 00000000
3fffead0: 00000000 00000000 00000000 3ffeee38
3fffeae0: 00000000 3ffe8a04 4bc6a7f0 3ffeec2c
...
...
3fffffa0: feefeffe feefeffe feefeffe 40207f46
<<<stack<<<
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
ets Jan 8 2013,rst cause:2, boot mode:(3,7)
load 0x4010f000, len 3460, room 16
tail 4
chksum 0xcc
load 0x3fff20b8, len 40, room 4
tail 4
chksum 0xc9
csum 0xc9
v0004a0b0
~ld
My code is
#include <DHT.h>
#include <DHT_U.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncWebServer.h>
#include <FS.h>
const char* ssid = "xxxxxxx";
const char* password = "xxxxxxx";
float hum, temp;
DHT dht(D1, DHT22);
void getSensorInformation(AsyncWebServerRequest *request)
{
Serial.println("2...");
temp = dht.readTemperature();
Serial.println("1...");
hum = dht.readHumidity();
String message = "Temperatura: " + String(temp) + " humedad: " + String(hum);
Serial.println(message);
request->send(200, "text/plain", message);
}
void getIpAddress(AsyncWebServerRequest *request) {
String ip = WiFi.localIP().toString();
String message = "ip: " + ip;
Serial.println(message);
request->send(200, "text/plain", message);
}
AsyncWebServer server(80);
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
void InitServer()
{
server.on("/ip", HTTP_GET, getIpAddress);
server.on("/info", HTTP_GET, getSensorInformation);
server.onNotFound(notFound);
server.begin();
Serial.println("HTTP server started");
}
void ConnectWiFi_STA() {
WiFi.begin(ssid, password);
Serial.println("Connecting...");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
ConnectWiFi_STA();
dht.begin();
InitServer();
}
void loop() {
}
I create two methods, first getIpAddress
that returns the IP of my computer and another that returns the sensor data, first method it works, but the second print until 2...
, so I think it failure in this line temp = dht.readTemperature();
but I don't know why
PS: Sorry about my english my main language is spanish.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|