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

Moderator: igrr

User avatar
By bluesmoke
#45165 I have a NodeMCU running a sketch that checks a temp sensor then goes into deep sleep for 15 minutes, and repeats. Everything seem to work well for days (between 3 and 5 days). Eventually, though, it stops sending data to my web service. The odd thing is, I can't just reset it and have it work again. In fact, I can't even unplug it and have it work again. I have to unplug it for at least several hours, then it happily starts running again for another several days.

Any ideas on what might be happening? I've tried several different power supplies, etc.

Thanks!

The sketch:
Code: Select all
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
//onewire appears to be included by ESP8266WiFi lib

// Read internal vcc rather than voltage on ADC pin (A0 must be floating)
ADC_MODE(ADC_VCC);

// Assign to pin D3 of your NodeMCU to the DS18B20
#define ONE_WIRE_BUS D3

// Create a onewire instanace
OneWire oneWire(ONE_WIRE_BUS);

// Declare a DS18B20 Instance and assing the OneWire reference to it.
DallasTemperature sensors(&oneWire);

DeviceAddress tempDeviceAddress;

const int sensorPower = D5;
const int resolution = 10;
const int attempts = 2;
const int httpPort = 80;

// 15 minutes
const unsigned long sleepMicros = 945000000;

const char* ssid     = "Wireless";
const char* password = "Zx898yxchky9";

const char* host = "www.xxxxxxx.com";
const char* sensorID = "esp00001";

float tempReading;
int batVcc;
int connectAttempts;

void goToSleep() {
  //Serial.println("Going to sleep");
  digitalWrite(sensorPower, LOW);
  ESP.deepSleep(sleepMicros, WAKE_RF_DEFAULT);
}

void myDelay(int ms) {
    int i;
    for(i=1;i!=ms;i++) {
          delay(1);
          if(i%100 == 0) {
                  ESP.wdtFeed();
                  yield();
          }
    }
}

void setup() {
  pinMode(sensorPower, OUTPUT);
  digitalWrite(sensorPower, HIGH);

  connectAttempts = 0;

  //Start the DallasTemperature Library
  sensors.begin();
  sensors.getAddress(tempDeviceAddress, 0);
  sensors.setResolution(tempDeviceAddress, resolution);
  sensors.setWaitForConversion(false);
  sensors.requestTemperatures(); // Tell the DS18B20 to get make a measurement
  myDelay(100);

  WiFi.begin(ssid, password);
  myDelay(200);

  while (WiFi.status() != WL_CONNECTED) {
    myDelay(200);
  }

  tempReading = sensors.getTempCByIndex(0);
  myDelay(100);
  batVcc = ESP.getVcc();

}

void loop() {
  if (connectAttempts >= attempts) {
    goToSleep();
  }

  myDelay(1000);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  myDelay(100);
  if (!client.connect(host, httpPort)) {
    connectAttempts++;
    return;
  }

  // We now create a URI for the request
  String url = "/sensornet/html/";
  url += "espnet.php";
  url += "?sid=";
  url += sensorID;
  url += "&t=";
  url += tempReading;
  url += "&b=";
  url += batVcc;

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  myDelay(1500);

  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
  }

  goToSleep();

}