D1 Mini Reboot dosnt´t work right

I am using a D1 Mini with two sensors

  1. DHT22
  2. BH1750
    and run the following code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#include <Wire.h>
#include <BH1750.h>

#include "/arduino_secrets.h"

#define wifi_ssid SECRET_SSID
#define wifi_password SECRET_SSID_PASS

#define mqtt_server SECRET_MQTT_IP
#define mqtt_port SECRET_MQTT_PORT

#define DHTPIN D7
#define DHTTYPE DHT22

#define battPin A0

DHT dht(DHTPIN, DHTTYPE);
BH1750 lightMeter;

const String mqtt_device_name = "Device100"; //String(system_get_chip_id());    //devicename / chipid
const uint deep_sleep_time = 1000000 * 60;  //60s // deep sleep is in us

const String out_temp = "sensor/" + mqtt_device_name + "/temp";
const String out_humi = "sensor/" + mqtt_device_name + "/humi";
const String out_lux = "sensor/" + mqtt_device_name + "/lux";
const String out_batt = "sensor/" + mqtt_device_name + "/batt";

float humi;
float tempC;

float lux;

float batt;

IPAddress ip(192, 168, 50, 100); // sensor IP 
IPAddress gateway(192, 168, 168, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress dns(192, 168, 168, 1);

WiFiClient espClient;
PubSubClient client;

void setup() {
  Serial.begin(74880);
  Wire.begin();
  lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE_2);
  dht.begin();  // initialize the sensor
  setup_wifi();
  client.setClient(espClient);
  client.setServer(mqtt_server, mqtt_port);
  delay(1);
  Serial.println("id=" + mqtt_device_name);//print device name / chip id
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);
  WiFi.config(ip, gateway, subnet, dns);
  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(mqtt_device_name.c_str())) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}


void send_data_mqtt(float humi, float temp, float lux, float batt) {
  client.publish(out_humi.c_str(), String(humi).c_str());
  client.publish(out_temp.c_str(), String(temp).c_str());
  client.publish(out_lux.c_str(), String(lux).c_str());
  client.publish(out_batt.c_str(), String(batt).c_str());
  Serial.println(batt);
}

void get_sensor_data() {
  String output = "";
  // read humidity
  humi = dht.readHumidity();
  // read temperature as Celsius
  tempC = dht.readTemperature();
  // testing if there was an reading
  if (isnan(humi)) {
    humi = -50.00;
  }
  output = output + humi + ", ";
  if (isnan(tempC)) {
    tempC = -50.00;
  }
  output = output + tempC + ", ";
  Serial.println(output);
}

void get_battery() {
  batt = analogRead(battPin);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  if (lightMeter.measurementReady()) {
    lux = lightMeter.readLightLevel();
    get_battery();
    get_sensor_data();
    send_data_mqtt(humi, tempC, lux, batt);
    delay(10);
    ESP.deepSleep(deep_sleep_time);
    }
}

But the D1 Mini doesn't run the code again after the deep sleep or reboot.
I need to reboot at least twice, or disconnect the power

And sometimes i get this:
rst cause:2, boot mode:(3,6)

What about your wiring? Do you have Gpio16 connected to RST?

Yes i do,
forgot to upload wiring

Before that i had the DHT22 on D4

D1 mini is tricky board, so many "clones" and version around here. I have never used one (and hopefully never will), so not so much help from here.
Sure you have dht on pin D7 ? No solder bridges there to D8?

Yes, there are no solder bridges and the Sensor is on D7, else i wouldn't get any other value then my error values.

But there is still a bit of solder in the hole of D4

As long as there are no bridges shouldn't matter.
If you have a multimeter you could verify connection between Gpio16 and rst and that there are no solder bridges there either.
Some people use 3-digits resistor between Gpio16 and rst, some diodes. I have never studied the effect of them.
Other observations;
delay between mqtt and deepsleep is quite short.
Your I2C doesn't have pullups if not on that module.
You read battery voltage of your 5V supply? Without voltage divider. Explain that...

If you have a multimeter you could verify connection between Gpio16 and rst

will do that tomorrow morning

and that there are no solder bridges there either.

there aren't any visible

Some people use 3-digits resistor between Gpio16 and rst, some diodes.

i am just using a normal solid core wire

Other observations;
delay between mqtt and deepsleep is quite short.

will test later if it affects it, but in an earlier version it didn't work with less

Your I2C doesn't have pullups if not on that module.

should be on the module as far as i know but not sure
got the sensors from berrybase

You read battery voltage of your 5V supply? Explain that...

i am reading the voltage from the batteries and power the d1 mini with them via the 5V Pin
not sure why they aren't on the image

You need voltage divider to read that battery voltage. Maybe it's not shown on the image... :wink:

You need voltage divider to read that battery voltage. Maybe it's not shown on the image... :wink:

copied it from this
hoping that would it work

There is no voltage divider on this image either.
4.2V to A0. Not good.
It would be easier to debug your wakeup problem without these extra components.

So it will constantly be above 3.3v unless it is fully empty ?! What reading were you expecting.
Remove it first for now and see if the issues with not restarting properly disappears.

currently not even having any batteries in it,
iirc there should be on on the d1 mini or at least it works on the projects using exactly that schematic

until it stops working....

until it stops working....

the schematic is of a DIY Version of slimevr, a successful product

but i only copied the battery sensing circuit of it

It doesn't look like long lasting successful product to me... Maybe I'm wrong.
@Deva_Rishi , what you think?

I would not connect it that way. Seems a little pointless as well. Some esp8266 boards do have a voltage divider on them to begin with, since the ADC actually only has a range of 0-1v I have no idea whether your board does, but feeding more than the range is not recommended. If the inout range would be 3.3v to 0v though and you read the battery level like that, it would always read 100% The LiPo varies between 4.2 and 3.7 lineair in charge %.

That is probably a good thing. That charging module is not for load-sharing ! As long as you charge while power the wemos is not connected, all is good, but the current draw from the rest of the schematic will confuse the TP4056 and it will not stop charging even when the battery is already full. The consequences of that can be severe, usually not to bad, but your battery may explode. Also the size of the battery will have to be be calibrated to the charging current (or vice versa) Usually the TP4056 chargers are set for 1A, which would be fine for anything larger than 500mAH You can change the charging current by modifying the resistor that is connected to pin 2 of the TP4056.

But unfortunately for you, that it doesn't have a battery in it doesn't mean that there is no voltage applied to A0. Also that depends on the circuit of the charger. The protection circuit is switched usually thru GND, so actually Bat+ & Out+ are connected. Anyway remove the charger and if you do want to use it, modify the circuit so it does allow for load-sharing. The proper circuit for battery level measurement should include an Op-Amp in my opinion.

So far not for you.

2 Likes

@wuzader I have been using the sensors in the link below for many years.

I use ~0.5K resistor between D0 and RST. I would not use a simple wire link, I think there is a possibility of a short circuit that way if D0 is high when the USB-serial chip tries to reset the board or the reset button is pushed.

I also use 180K in series with A0 for measuring Li-ion/Li-Po battery voltage. This resistor alters the Mini's on-board voltage divider, allowing higher voltage to be measured.

I have not updated the GitHub page to reflect this, but I have replaced the AAA size Li-ion batteries & holders shown in the photos with 1800mAh Li-Po packs. This has extended battery life from less than 3 weeks to around 12 weeks. Unlike the AAA size batteries, the Li-Po packs are protected against over-discharge, which is important.

Completely forgot that. So it's becoming 400k/100k extending range up to 5V.

1 Like

Update:

If you have a multimeter you could verify connection between Gpio16 and rst

connection is there

Other observations;
delay between mqtt and deepsleep is quite short.

without changes i get this in console:

  1. Reset:
    ets Jan 8 2013,rst cause:2, boot mode:(3,6)
  2. Reset:
load 0x4010f000, len 3424, room 16

tail 0

chksum 0x2e

load 0x3fff20b8, len 40, room 8

tail 0

chksum 0x2b

csum 0x2b

v00047470

~ld

rf cal sector: 1020

freq trace enable 0

rf[112] : 0�

and my serial print stuff:

Connecting to WLAN

.......

WiFi connected

IP address:

192.168.50.100

id=Device100

Attempting MQTT connection...connected

50.30, 23.10,

924.00

with delay 20:
no change
with delay 200:
no changes
->code is back to 10

trying it with a 220 R or would be a 1k, 10k or 180k be better?