wifi and deepsleep in esp8266 not working together

Hi everybody
i am working with nodemcu to make a data logger which is working well,:i makes a web page ,which can be reached by its ip address,…but when i put ESP.deepsleep(20e6); the web page is not created…strange! can someone please help me here
heres the code:

#include <Wire.h>
#include <SparkFunTSL2561.h> // library for luminosity sensor
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <OneWire.h>
#include <DallasTemperature.h> // library for temperature sensor
#define sleepTime 5
//temp related info

int temperature_pin = D3; // often dosent work if pin is otherthan D3 or D4…

OneWire oneWire(temperature_pin);

DallasTemperature sensors(&oneWire);

double Celcius = 0.00;

//moisture sensor related info

int moisture_pin = A0;

int moisture_reading;

// luminosity related info

//create object LUX


double lux_value;

//following three are parameters for function getTiming

//and have these particular datatypes with few predefined values

//which have their already meant purpose

unsigned char timer = 2;

unsigned int ms;

boolean sensitivity;
//username and password for net connection,which esp shall search for

char *username = “corph”, *password = “corph@786”;

double moisture_data;


ESP8266WebServer server(80);

String page;

void setup() {

// put your setup code here, to run once:

pinMode(moisture_pin, INPUT);


WiFi.begin(username, password);

// ds18b20 must be taken in parasitic mode or pull up resistor should be defined

//observed: it works if none of the above is done,…but some narrate it gets hot if not done so

//no proceeding till wifi connection is established

while (WiFi.status() != WL_CONNECTED) {




Serial.println(“Connected to Wifi \nAll info shall be on the website”);

Serial.println(“connected to”);


Serial.println(“IP ADDRESS”);

//IP should be noted and followed in browser


//creating an html web page, also sendind data to it…(gets executed after “server.handleClient();” ststement in loop)

server.on( “/”, () {

page = "


Moisture sensor

" + String(moisture_reading ) + “

Temperature sensor

" + String(Celcius) + " celcius

LUX sensor

" + String(lux_value) + " LUX

server.send(200, “text/html”, page);




void loop() {

// put your main code here, to run repeatedly:

// calling functions to collect data





// taking a nap to save battries

delay(100 );


//functions for particular sensor

void moisture() {

moisture_reading = analogRead(moisture_pin);

//changing voltage (data) form sensor to percentage

moisture_reading = map(moisture_reading, 1024, 0, 0, 100);


void temperature() {



Celcius = sensors.getTempCByIndex(0);


void lux() {


LUX.setTiming(sensitivity, timer, ms);


unsigned int data1, data2;

LUX.getData(data1, data2);

// data1 and data2 corresponds to data of lingt and infrared rays,

//passing them to following function, unaltered, gives luminosity

LUX.getLux(sensitivity, ms, data1, data2, lux_value); // this function is of boolean type


How do you expect the server to respond to requests when it is sleeping?

Well thank you for the response
i want the web page to be made just before it goes to sleep...
or if sleep time is less,like 20 seconds, while reset is connected to D0 on my esp board,which will wake it up just after sleep time is over...shouldn't web page be accessible at least once to check the data...
Thank you once again

The "web page" is the server response. If the server is sleeping, it can't send a response, regardless of whether you have saved the response as text, or generate it on the fly.

Unless the client wakes the server up, which I doubt, the server will sleep right through the client requests.

Unless the client wakes the server up

could you please say what you mean by client here...
also is there a way to save data which is accessible via IP ,just before node mcu goes to sleep
Thanks PaulS

There is a way to store whatever you want, but when you visit a webpage you actually send a request to a server, which in response sends you the data. Your nodeMCU is the server that has the data for the webpage. you can send a request all you want, but since it is sleeping it is not going to send you anything ! (Man, i remember the days when computer programmers wrote the programming manuals, and then i got a manual written by a C-programmer ...)

Thanks Deva_Rishi for you reply
Now can someone suggest me a way where in I would get data on web page before my device goes to sleep
thanks again

The "web page" is pulled by a client. It is provided by the server, in response to the client request. If the server is asleep, it can NOT respond to a client request, regardless of whether you have gotten the response ready before going to sleep, or not.

Get over the idea that a server can go to sleep.

Thanks again PaulS
I think i m getting your point....for that reason i put a delay of like 10 seconds or more ,may be more than a minute so that i would be able to load web page before it reaches to sleep,but still isnt working.Is my this approach right?
Also what should I do,how will i get data while saving battery of my device

Also what should I do,how will i get data while saving battery of my device

The sleeping device should NOT be a server. That device needs to be a client, pushing data to a server. The client (web browser) would access that server, to get the stored data.

The web browser/client will NOT access the sleeping-Arduino-as-server, because the Arduino will not be a server.

Well,if i put a delay of 10 minutes delay before going to sleep,during this time if client requests data,shouldnt web page be formed?
Thanks PaulS for quick responses

Well,if i put a delay of 10 minutes delay before going to sleep,during this time if client requests data,shouldnt web page be formed?

If the client knows when the Arduino is going to go to sleep, and makes the request before that happens, yes, the web page will be formed/served.

But, that's a huge if.

You are trying to do two things which cannot be done together.

If you want the esp to be a web server, it must be awake and ready all the time to respond to requests from clients (a client is any other device like a pc, tablet, phone etc that wants to access the pages on the server). You cannot use sleep modes or delays, these will prevent the server from responding.

If you want the esp to run for long periods on battery power, it must spend most of its time in sleep mode and and wake up only occasionally, read the sensors, connect to the network, send the sensor data, disconnect from the network and return to sleep in a short time as possible.

If you want long battery life and access to the sensor data on a Web page at any time, you must use some other device as the web server. This could be a pc, a web server somewhere on the internet, a raspberry pi, even another esp (one which is not battery powered).

Thank you PaulRB
Is there a way,where esp will go to sleep only when ,at least one request from client is made.

Yes, it is possible to make your sketch so that the esp waits for a request from a client, respond to that request, then goes to sleep. Or it could go to sleep if no client request is received within a timeout. But this will greatly reduce battery life.

please do gimme some resources to go for it and please gimme advice related to it,if any?...Thanks

What kind of resources? Have a look at my code here if you like. But I think you have most of the code you need in the sketch you posted earlier (posted incorrectly, by the way). You just need to use it in the correct way, with an understanding of how severs, clients and low power sensor nodes should work.

Advice: don't waste your time, it's not a very useful idea as far as I can see. Use another device as your web server.

You could also send a request to the ESP, "requesting it goes to sleep" at some point you will have to wake it up though.

Thank you Deva_Rishi...but could you please tell me how exactly can i do that

 server.on( "/", [] () {

    page = "<h1>DATA FROM SENSORS</h1><h3>Moisture sensor  </h3><h5>" + String(moisture_reading ) + "</h5><h3>Temperature sensor  </h3><h5>" + String(Celcius) + " celcius</h5><h3>LUX sensor  </h3><h5>" + String(lux_value) + " LUX</h5>";

    server.send(200, "text/html", page);


this is the code that gets executed if a request is made.
you could just add the

delay(100 );

after the server.send()
On the other hand it would probably be better to make that conditional, by adding a button to the page using a form and test if the ‘button on the page’ has been pressed using server.hasArg() & server.arg() I do have to inform you that your page is not properly formatted. There is no header no html open and closing tags etc… this will cause trouble for most webbrowsers, more so when submitting a form. Also using a String to put the page in is fine, but you should declare it as a local variable. server.on() can also just call a function so it doesn’t need to be all within your setup() this will improve the readability.server.on("/", handleRoot); and define

void handleRoot() {
  String page;

elsewhere in your code.