0

I am building a monitoring device for my chicken stable. Therefore, I want to measure temperature, humidity, and a reed switch every minute and send the results to a server, then sleep 60s and repeat. It works fine for a couple of hours, but then fails continously. I can see the esp connecting successfully to the access point (in the AP web interface), but the http request gives me a -1 (connection refused) status code on the esp (and doesn't reach the server. Can anyone tell me how to debug this further? Here's the ESP code:

#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>

// WiFi credentials.
const char* WIFI_SSID = "myssid";
const char* WIFI_PASS = "mypass";

Adafruit_BME280 bme; // I2C

void connect() {

  unsigned long wifiConnectStart = millis();

  // Connect to Wifi.
  //Serial.println();
  //Serial.println();
  //Serial.print("Connecting to ");
  //Serial.println(WIFI_SSID);

  WiFi.disconnect();
  delay(10);

  WiFi.persistent(false);
  WiFi.mode(WIFI_OFF);
  WiFi.mode(WIFI_STA);

  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    // Check to see if
    if (WiFi.status() == WL_CONNECT_FAILED) {
      //Serial.println("Failed to connect to WiFi. Please verify credentials: ");
      delay(10000);
    }

    delay(20);
    //Serial.print(".");
    // Only try for 15 seconds.
    if (millis() - wifiConnectStart > 15000) {
      //Serial.println("Failed to connect to WiFi");
      return;
    }
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  unsigned long timePassed = millis()-wifiConnectStart;
  //Serial.println("Connecting time in milliseconds: ");
  //Serial.println(timePassed);
}

void setup() {
  Serial.begin(115200);
  Serial.setTimeout(2000);  

  Serial.println("Device Started");
  Serial.println("-------------------------------------");
  Serial.println("Running Deep Sleep Firmware!");
  Serial.println("-------------------------------------");

  connect();

  if (!bme.begin()) 
  {
    Serial.println("Could not find BME280 sensor at 0x77");
  }

  bme.setSampling(Adafruit_BME280::MODE_FORCED,
                  Adafruit_BME280::SAMPLING_X1, // temperature
                  Adafruit_BME280::SAMPLING_X1, // pressure
                  Adafruit_BME280::SAMPLING_X1, // humidity
                  Adafruit_BME280::FILTER_OFF);

  pinMode(D5, INPUT_PULLUP);

  bme.takeForcedMeasurement();

  bool chickdoor = digitalRead(D5);

  float temperature = bme.readTemperature();
  Serial.print("Temperature = ");
  Serial.print(temperature);
  Serial.println(" Celsius");

  //int32_t pressure = bmp.readPressure();
  //Serial.print("Pressure = ");
  //Serial.print(pressure);
  //Serial.println(" Pascal");

  //Serial.println();

  float humidity = bme.readHumidity();
  Serial.print("Humidity = ");
  Serial.print(humidity);
  Serial.println("%");

  StaticJsonBuffer<300> JSONbuffer;
  JsonObject& JSONencoder = JSONbuffer.createObject();

  JSONencoder["temperature"] = temperature;
  JSONencoder["humidity"] = humidity;
  JSONencoder["chickdoor"] = chickdoor;

  char JSONmessageBuffer[300];
  JSONencoder.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
  Serial.println(JSONmessageBuffer);

  //http debug stuff:
  //Serial.print("DNS #1, #2 IP: ");
  //WiFi.dnsIP().printTo(Serial);
  //Serial.print(", ");
  //WiFi.dnsIP(1).printTo(Serial);
  //Serial.println();

  //IPAddress resolvedIP;

  //if (!WiFi.hostByName("my.domain.here", resolvedIP)) {
  //  Serial.println("DNS lookup failed.  Rebooting...");
  //  Serial.flush();
  //  ESP.reset();
  //}
  //Serial.print("my.domain.here");
  //Serial.print(" IP: ");
  //Serial.println(resolvedIP);

  WiFiClient client;
  HTTPClient http;

  http.begin(client, "http://my.domain.here/chickinfo");
  http.addHeader("Content-Type", "application/json"); //Specify content-type header

  int httpCode = http.POST(JSONmessageBuffer); //Send the request
  String payload = http.getString(); //Get the response payload
  Serial.print("HTTP status code: ");
  Serial.println(httpCode); //Print HTTP return code
  Serial.print("HTTP response: ");
  Serial.println(payload); //Print request response payload
  http.end(); //Close connection

  Serial.println("Going into deep sleep for 60 seconds");

  ESP.deepSleep(60e6); // 60e6 is 60 seconds
}

void loop() {
}
  • why is the server refusing the connection? – jsotola Oct 5 at 18:49
  • reduce your code to the least amount of code that still exhibits the problem – jsotola Oct 5 at 18:50
  • The server log did not show the connection at all @jsotola. I upgraded the board (and with it, the httpclient library) in the Arduino IDE and it has been working for 24 hours now. – Gasp0de Oct 6 at 20:14
  • post that as an answer and then you can accept it as a solution ... other people with similar problem will be able to find it – jsotola Oct 7 at 4:42

Your Answer

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

Browse other questions tagged or ask your own question.