0

Here is my code which sends some data to the firebase cloud functions. The code works well and the nodemcu sends the data to the cloud for about half an hour and then stops sending the messages.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

#define ONE_WIRE_BUS 5

// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);
String data="";
const char* ssid = "***************";
const char* password = "**************";

const char* host = "**************************";
const int httpsPort = 443;

String url = "/getData";
float Celcius=0;
float Fahrenheit=0;
int sensor_light;
int sensor_temp;
int sensor_moist;
void setup() {
ESP.wdtDisable();
pinMode(16,OUTPUT);//moist
pinMode(0,OUTPUT);//light
Serial.begin(115200);  //Serial connection
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
ESP.wdtEnable(WDTO_8S); 
}
void loop()
{ 
 client.connect(host, httpsPort);
 int l,m,t;
 data=" ";
 l=get_light();
 t=get_temp();
 m=get_moist();
 if(WiFi.status()== WL_CONNECTED){
    data+="{";
    data+="\"";
    data+="Node-Id";
    data+="\"";
    data+=":";
    data+=1;
    data+=",";
    data+="\"";
    data+="light";
    data+="\"";
    data+=":";
    data+=l;
    data+=",";
    data+="\"";
    data+="temperature";
    data+="\"";
    data+=":";
    data+=t;
    data+=",";
    data+="\"";
    data+="humidity";
    data+="\"";
    data+=":";
    data+=m;
    data+=",";
    data+="\"";
    data+="Latitude";
    data+="\"";
    data+=":";
    data+=29.3589362;
    data+=",";
    data+="\"";
    data+="Longitude";
    data+="\"";
    data+=":";
    data+=79.5506528;
    data+="} ";
    Serial.println(data);
    client.print(String("POST ") + url + " HTTP/1.1\r\n" +
           "Host: " + host + "\r\n" +
           "Connection: close\r\n"+   
           "Content-Length: " + data.length()+ "\r\n" +
           "Content-Type: application/json;charset=UTF-8\r\n\r\n"+
            data +"\r\n");
    //  Serial.println("Sent"); 
    String line = client.readStringUntil('\n');
    Serial.println(line);
    while(!client.connected())
        { client.stop();
          ESP.wdtFeed();
         }
     }
    delay(4000); 
    }

    int get_light()
    {   
    digitalWrite(0, HIGH); 
    sensor_light=0;
    sensor_light = analogRead(A0);
    digitalWrite(0, LOW);
    delay(2000);
    return sensor_light; 
    }
    int get_temp()
    {
    int  sum=0,i;
    for(i=0;i<10;i++)
      {
    sensors.requestTemperatures(); 
    Celcius=sensors.getTempCByIndex(0);
    delay(250);
    sum+=Celcius;
    }
    Celcius=sum/20;
    delay(2000);
    return Celcius;
    }

    int get_moist()
    { 
     digitalWrite(16, HIGH); 
    sensor_moist=0;
    sensor_moist = analogRead(A0);
    digitalWrite(16, LOW);
    delay(2000);
    return sensor_moist;
    }

If I do not use ESP.wdtFeed() and other similar functions then I am getting Soft wdt reset error. I am using the laptop to power it. Any help?

  • do not wait until the client disconnects. send, wait a second and disconnect – Juraj Mar 17 '18 at 17:34
  • Yes, it is 200 Ok. I will change the code and update here. – Mreno sert Mar 17 '18 at 17:36
  • esp8266 has more memory then Atmega, but += on String is a memory killer. I would bet that it is the problem why the sketch fails after a hour. use sprintf function with char buffer – Juraj Mar 17 '18 at 17:38
  • Ok, I will check this method also. Thanks for the reply. How will I implement it as I do not the exact size of the data? Any small help would do the work. – Mreno sert Mar 17 '18 at 17:44
  • @Juraj I did as you said but now I am getting 500 Internal Server Error. – Mreno sert Mar 17 '18 at 19:04
-2

Remove the return in setup function and check. And set regular yield () statements to ensure smooth running of background tasks. And use if statement in the loop for !client.connected()

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.