1

The setup includes an WEMOS/ESP8266 R1 mini compatible board, a DHT11, a photo resistor and a magnetic reed.

The libs used are:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "DHT.h"

Compilation/loading is successful and the code starts normally, publishes and subscribes correctly.

Executable segment sizes:
ICACHE : 32768           - flash instruction cache 
IROM   : 259412          - code in flash         (default or ICACHE_FLASH_ATTR) 
IRAM   : 27337   / 32768 - code in IRAM          (IRAM_ATTR, ISRs...) 
DATA   : 1524  )         - initialized variables (global, static) in RAM/HEAP 
RODATA : 2300  ) / 81920 - constants             (global, static) in RAM/HEAP 
BSS    : 26416 )         - zeroed variables      (global, static) in RAM/HEAP 
Sketch uses 290573 bytes (27%) of program storage space. Maximum is 1044464 bytes.
Global variables use 30240 bytes (36%) of dynamic memory, leaving 51680 bytes for local variables. Maximum is 81920 bytes.
esptool.py v3.0
Serial port /dev/ttyUSB0
Connecting....
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 5c:cf:7f:ad:11:6b
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 294720 bytes to 215795...
..

However the serial printing commands (ie Serial.println) in the code stop working for no apparent reason. The code/module is working, it is not dead.

I suspected json serialization but various changes that I tried didn't solve it. Then I thought that the dht lib is responsible, because serial printing stops in the dht function routine. But the adafruit stand alone examples work alright.

Here is the initial printing until it stops:

WiFi connected.
IP address: 
192.168.xx.xx
ESP Mac Address: xx:xx:xx:xx:xx:xx
Subnet Mask: 255.255.255.0
Gateway IP: 192.168.xx.xx
DNS: 192.168.xx.xx
RRSI: -84
mqtt status in setup: 0
Humidity: 34.00% || Temperature: 24.00°C 
before ser

This is the code function that requests dht data:


StaticJsonDocument<128> doc_dht;
 
void request_temp_hum() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  char  h_ch[10];
  char  t_ch[10];
  char buffer[256];
  doc_dht.clear();
  memset (h_ch, 0, sizeof(h_ch));
  memset (t_ch, 0, sizeof(t_ch));
  if (isnan(h) || isnan(t)) {
        Serial.println("Failed to read from the DHT sensor, check wiring.");
        return;
  }
  Serial.print("Humidity: ");
  Serial.print(h);
  //Print out the Temperature
  Serial.print("% || Temperature: ");
  Serial.print(t);
  Serial.println("°C ");
  
  dtostrf(t, 5, 2, t_ch);
  
  dtostrf(h, 5, 2, h_ch);
  
  doc_dht["sensor"] = "dht11";
  doc_dht["temp"] = t_ch;
  doc_dht["humi"] = h_ch;
  
  Serial.println("before serialization");  
  serializeJson(doc_dht, buffer);
  Serial.println("after serialization..");
  client.publish("Home/info/dht", buffer);
  Serial.println("after publish");
  }

This is the mqtt publishes from the device:

02.05.2023 13:11:31 Home/info/dht {"sensor":"dht11","temp":"24.00","humi":"34.00"}
...
02.05.2023 13:26:38 Home/info/dht {"sensor":"dht11","temp":"25.00","humi":"34.00"}

Apart from the fact that I can't debug my code any further, it is evident that something is wrong and it might have further implications that I can't foresee. So why does serial printing stop working?

2
  • You can call Serial.flush() after each println() to make sure the log is sent before moving to the next line. May 2, 2023 at 10:53
  • Thank you for the suggestion, I used Serial.flush() is various points in the program and I was able to move on and further debug. The issue must be the fact that I used GPIO1 (TX) for controlling a relay module.
    – thermike
    May 3, 2023 at 14:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.