Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Joseph!
#58658 Hi to all,

I am newbie in the ESP8266 programming. Basically, I wanted to send temperature and battery info to a server in order to save it to a database:

Code: Select all#include <DHT11.h>
#include<ESP8266WiFi.h>

const char* ssid = "SSID";
const char* password = "PASSWORD";
const char* host = "192.168.0.X";

extern "C" {
#include "user_interface.h"
#include "c_types.h"
}

int err;
float temp, hum;
ADC_MODE(ADC_VCC);

String webString = "";     
String mac = "";
int pin = 2;
DHT11 dht11(pin);

void setup() {
  // put your setup code here, to run once:
//Serial.begin(115200);
delay(100);

WiFi.mode(WIFI_STA);

WiFi.begin(ssid,password);
int j = 0;
while (WiFi.status() != WL_CONNECTED){
  delay(500);
  j++;
  if (j == 50){break;}
  //Serial.print(".");
}

//Serial.println(WiFi.localIP());
if(WiFi.status() == WL_CONNECTED) { 
    mac = WiFi.macAddress();
    WiFiClient client;
    const int httpPort = 80;
    if (!client.connect(host,httpPort)){ 
      return;
    }
   
   
    if((err = dht11.read(hum, temp)) == 0)   
              {
              }
    else
              {
                temp=99;
              }
    int vcc = ESP.getVcc();
    String vc;
    vc = String((int)vcc);
    webString=String((int)temp);   
    String url = "/Temp/t.php?value=";

    client.print(String("GET ") + url + webString + String("&mac=") + mac + String("&vc=") + vc + " HTTP/1.1\r\n" + "Host:" + host  + "\r\n" + "Connection: close\r\n\r\n");

   
    //1200000000us = 20 min
  }
ESP.deepSleep(1200000000,WAKE_RF_DEFAULT);
}

void loop() {
}


Firstly, I read somewhere (I think it was in a Spressif document about low power applications) that the DEEPSLEEP has to be used in the Setup() part, avoiding to put it in the Loop() part of the code, but I have seen several examples using the DEEPSLEEP mode in the Loop() part... Am I wrong about this issue?

But my problem really is that my ESP8266 stops sending data from time to time. I put the device working with a LiPo battery sending data every 20 minutes and everything was working fine. I attached this code because I didn't controlled if it was connected or not and it was entering in a infinite loop when the WiFi was disabled :oops:

Code: Select allwhile (WiFi.status() != WL_CONNECTED){
  delay(500);
  j++;
  if (j == 50){break;}
  //Serial.print(".");
}

//Serial.println(WiFi.localIP());
if(WiFi.status() == WL_CONNECTED) { 
}


I have a short between GPIO16 and RST, and it usually works fine (with the data submission every 20 minutes it was working for more than a week without any problem), but now I am sending data every 2 minutes and it hangs from time to time (sometimes after 4 hours, other after 8 hours...) and I don't now why... I think my code is not very good but it works (sometimes :? )

Do you have any idea about what is happening? I will really appreciate any help on this...

Thank you in advance.
User avatar
By 8n1
#58675 Hi,
You need to go sure the server received the request. Right now you are just doing a client.print() and instantly going to deep sleep.
Check this out:
https://github.com/esp8266/Arduino/blob ... xamples.md

And also everything else inside the doc folder.
https://github.com/esp8266/Arduino/blob/master/doc

And no, that's not true. You can call the deep sleep function from wherever you want. But it takes a short time to activate deepsleep and code called right after it will get executed. I always put a infinite loop with a small delay to make the watchdog happy right after the deepsleep function.
User avatar
By Joseph!
#58793 Hi AcmeUK and 8n1,

Thank you for your answers, I will check the delay and the other stuff.

Knowing that it can be used the loop area for the deepsleep mode, I will try to do more things like saving info in a variable and send collected data in one time.

I will let you know about my progress...