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:
#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
while (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.