IoT pet feeder. Se cuelga cada cierto tiempo.

Hola,

Este es mi primer proyecto Arduino. He seguido estas indicaciones (https://create.arduino.cc/projecthub/circuito-io-team/iot-pet-feeder-10a4f3), después de mucho pelear con el módulo ESP8266-01 he conseguido que todo funcione. He omitido el sensor de movimiento y el altavoz.

El problema que tengo es que funciona durante unos cuantos minutos, algunas veces horas, y pasado ese tiempo se cuelga (la única luz de mi ESP8266 empieza a parpadear continuamente, la opción que menos pasa). Otras veces, el módulo sigue con conexión y tengo ping con su IP pero no llega respuesta desde dweet.io (pero, si reinicio todo el sistema, vuelve a funcionar).

La fuente de alimentación es de 12v y 3.000mA

He probado conectando únicamente el módulo ESP directo a la placa Arduino UNO. Exactamente igual, funciona solamente durante un tiempo.

Adjunto el código (básicamente el mismo que el de la web, quitando las partes que no uso)

A ver si alguien puede echarme una mano.

Gracias.

#include "Arduino.h"
#include "Servo.h"
#include <ArduinoHttpClient.h>
#include <WiFiEspAT.h>

const int PROGMEM ServoRestPosition   = 20;  //Starting position
const int PROGMEM ServoTargetPosition = 150; //Position when event is detected

unsigned long previousMillis = 0;
const long interval = 1000;

#if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1)
#include <SoftwareSerial.h>
SoftwareSerial Serial1(10, 11);
#define AT_BAUD_RATE 9600
#else
#define AT_BAUD_RATE 115200
#endif

Servo servo;
WiFiClient wifi;
HttpClient client = HttpClient(wifi, "dweet.io", 80);

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial1.begin(AT_BAUD_RATE);
  WiFi.init(Serial1);

  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println();
    Serial.println(F("Communication with WiFi module failed!"));
    // don't continue
    while (true);
  }

  // waiting for connection to Wifi network set with the SetupWiFiConnection sketch
  Serial.println(F("Waiting for connection to WiFi"));
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
  }
  Serial.println();
  Serial.println(F("Connected to WiFi network."));

  servo.attach(3); //Pin 3
  servo.write(ServoRestPosition);

  servo.detach();

}

void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {


    // send the GET request
    Serial.println(F("making GET request"));
    client.get("/get/latest/dweet/for/prueba");

    String response = client.responseBody();
    Serial.print(F("Response: "));
    Serial.println(response);

    Serial.println(response.length());

    // now parse the response looking for "content":
    int labelStart = response.indexOf("content\":");
    // find the first { after "content":
    int contentStart = response.indexOf("{", labelStart);
    // find the following } and get what's between the braces:
    int contentEnd = response.indexOf("}", labelStart);
    String content = response.substring(contentStart + 1, contentEnd);
    Serial.println(content);

    // now get the value after the colon, and convert to an int:
    int valueStart = content.indexOf(":");
    String valueString = content.substring(valueStart + 1);
    int number = valueString.toInt();
    Serial.print(F("Value string: "));
    Serial.println(valueString);
    Serial.print(F("Actual value: "));
    Serial.println(number);
    if (number == 1) {
      // The servo will rotate to target position and back to resting position with an interval of 500 milliseconds (0.5 seconds)
      servo.attach(3);         // 1. attach the servo to correct pin to control it.
      servo.write(ServoTargetPosition);  // 2. turns servo to target position. Modify target position by modifying the 'ServoTargetPosition' definition above.
      delay(500);                              // 3. waits 500 milliseconds (0.5 sec). change the value in the brackets (500) for a longer or shorter delay in milliseconds.
      servo.write(ServoRestPosition);    // 4. turns servo back to rest position. Modify initial position by modifying the 'ServoRestPosition' definition above.
      delay(500);                              // 5. waits 500 milliseconds (0.5 sec). change the value in the brackets (500) for a longer or shorter delay in milliseconds.
      servo.detach();                    // 6. release the servo to conserve power. When detached the servo will NOT hold it's position under stress.

      client.get("/dweet/for/prueba?servo=0");
    }


    Serial.println(F("Wait 10 seconds\n"));
  }
}

Tengo entendido que los millis() se desbordan por este motivo se usan relojes, o se resetea la variable,

jazpiroz:
Tengo entendido que los millis() se desbordan por este motivo se usan relojes, o se resetea la variable,

Tenía puestos delay y leí que podría ser eso, y lo cambié a millis. Lo de usar relojes o resetear variable... no sé lo que es :frowning: