Wifi manager and deep sleep

Hello Blynkers. I used this symple code for web setting ssid, pass, token, and virtual pin number.
All works fine!
But only with funktion “timer.setInterval(10000L, sendTemps);”
But I need to use funktion “ESP.deepSleep(10000000,WAKE_RF_DEFAULT);”
When I put in on the end of fuktion “send Temp” it will make what must to do and go sleeping.
After then wake up but going CRAZY. The microcontroller flashes rapidly and nothing more.
Maybe it’s not possible to use Wifi manger and funktion deepsleep togehter, right?
Or I have to place the DeepSleep in another place? Which?
I’ve been trying it for two days, I do not know anymore.
Thanks for Ideas

#define BLYNK_PRINT Serial    
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>         
#include <SimpleTimer.h>
#include <ArduinoJson.h>      
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 14
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#include <Ticker.h>
Ticker ticker;

char blynk_token[34] = "BLYNK_TOKEN";
char blynk_pin[34] = "VIRTUAL_PIN";

bool shouldSaveConfig = false; 
SimpleTimer timer;
int v;

void tick() {
  int state = digitalRead(BUILTIN_LED);  
  digitalWrite(BUILTIN_LED, !state);    
}

void saveConfigCallback () {
  Serial.println("Should save config");
  shouldSaveConfig = true;
  ticker.attach(0.2, tick);
}

void sendTemps () {
  ////THIS IS NUMBER OF SET'S VIRTUAL PIN
  v = atoi(blynk_pin);
  Serial.print("TEMP virtualPin:   ");
  Serial.println(v);
  ///TEMPERATURE FUNKTION
  unsigned long start = millis();
  sensors.requestTemperatures();
  unsigned long stop = millis();
  start = millis();
  sensors.setWaitForConversion(false); 
  sensors.requestTemperatures();
  sensors.setWaitForConversion(true);
  stop = millis();
  int resolution = 9;
  delay(750/ (1 << (12-resolution)));
  
  Blynk.virtualWrite(v, sensors.getTempCByIndex(0));
  Serial.println(sensors.getTempCByIndex(0));
  Serial.println("-------------------");
  Serial.println("It's sleeping Time..........");
/////////THIS IS MY PROBLEM//////////////
//ESP.deepSleep(10000000,WAKE_RF_DEFAULT);
  delay(100);
}
void setup() {
  Serial.begin(115200);
  Serial.println();
  pinMode(BUILTIN_LED, OUTPUT);
  ticker.attach(0.6, tick);
  Serial.println("Mounting FS...");

  if (SPIFFS.begin()) {
    Serial.println("Mounted file system");
    if (SPIFFS.exists("/config.json")) {
      Serial.println("Reading config file");
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        Serial.println("Opened config file");
        size_t size = configFile.size();
        std::unique_ptr<char[]> buf(new char[size]);
        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        json.printTo(Serial);
        if (json.success()) {
          Serial.println("\nparsed json");
          strcpy(blynk_token, json["blynk_token"]);
          strcpy(blynk_pin, json["blynk_pin"]);
        } else {
          Serial.println("Failed to load json config");
        }
      }
    }
  } else {
    Serial.println("Failed to mount FS");
  }
  WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 33);  
  WiFiManagerParameter custom_blynk_pin("virtual", "blynk pin", blynk_pin, 24);
  Serial.println(blynk_token);
  Serial.println(blynk_pin);
  WiFiManager wifiManager;
  wifiManager.setSaveConfigCallback(saveConfigCallback);
  wifiManager.addParameter(&custom_blynk_token); 
  wifiManager.addParameter(&custom_blynk_pin); 
  wifiManager.setTimeout(180); 

  if (!wifiManager.autoConnect("Temperature", "")) {
    Serial.println("Failed to connect and hit timeout");
    delay(3000);
    ESP.reset();
    delay(5000);
  }
  Serial.println("Connected !!"); 
  ticker.detach();
  digitalWrite(BUILTIN_LED, HIGH);
  strcpy(blynk_token, custom_blynk_token.getValue());  
  strcpy(blynk_pin, custom_blynk_pin.getValue()); 
  if (shouldSaveConfig) {     
    Serial.println("saving config");
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["blynk_token"] = blynk_token;
    json["blynk_pin"] = blynk_pin;
    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("Failed to open config file for writing");
    }
    json.printTo(Serial);
    json.printTo(configFile);
    configFile.close();
  }
  Serial.println("local ip");
  Serial.println(WiFi.localIP());
  ////////THIS FUNKTION TAKE OUT/////////
  timer.setInterval(10000L, sendTemps);
  Blynk.config(blynk_token);
  Blynk.connect();
  sensors.begin();
}

void loop() { 
  Blynk.run(); 
  timer.run(); 
}

They work fine together.

Looks OK.

Is it sleeping OK apart from the odd behaviour when it wakes?

You could try increasing the delay after the deepSleep call to 3000 from 100 as some users suggests it can take a long time to go to sleep.

Also try without the WAKE_RF_DEFAULT.

Is it sleeping OK apart from the odd behaviour when it wakes?

Actually, with the connected GPIO16 and reset pin and then the connection to the voltage does not start at all and crazy immediately.

After the start of the program and the subsequent connection of GPIO16 and RESET PIN, he will do his work and go to sleep. And after waking up crazy.

IMHO microcontroller and wake-up pin is OK, tested on another code

You could try increasing the delay after the deepSleep call to 3000 from 100 as some users suggests it can take a long time to go to sleep.
Also try without the WAKE_RF_DEFAULT.

without success

This is your problem then.

You can’t normally flash an ESP with GPIO16 and reset connected without using a resistor (size varies from system to system) but you should be able to power it up.

Precisely which ESP do you have and how are you powering it?

You can’t normally flash an ESP with GPIO16 and reset connected without using a resistor …

You are, of course, right. But I know that

Precisely which ESP do you have and how are you powering it?

I have two Node MCUs and one Wemos mini D1. Power supply is no problem.

This simple sketch works on all microcontrollers.

#define BLYNK_PRINT Serial   
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 14

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

char auth[] = "...........";
char ssid[] = "............";
char pass[] = "...........";

void setup() {
  Blynk.begin(auth, ssid, pass);
  sensors.begin();
}
void loop() {
  Blynk.run();
  sendTemps();
}
void sendTemps() {
  sensors.requestTemperatures();
  Blynk.virtualWrite(98, sensors.getTempCByIndex(0));
  ESP.deepSleep(10000000,WAKE_RF_DEFAULT);
}

so it’s not a hardware problem

Maybe the SPIFFS has corrupted. Trying formatting and starting again.

with this funktion SPIFFS.format(); ? It’s done

As I say, I’ve been playing with it for hours. I’m desperate, so I ask here :slight_smile:
Thank you for your advice

Yes that’s right but in setup() not loop().

Somewhere I make a mistake. Because even after the formatting is still in the chip wifi credentials - this is not correct.
I’ll find the right way and let you know