Hello everyone! I'm currently having problems with my arduino uno webserver using the ethernet shield v2. Everything works fine for about 5 hours and afterwards seemingly stops responding with this error(https://cdn.discordapp.com/attachments/821414861178142731/860420975459434506/unknown.png). Any pointers?
#include <SPI.h>
#include <Ethernet.h>
#include <DS3231.h>
DS3231 rtc(SDA, SCL);
#include <notes.h>
//warning
#define buzzer 2
bool warningSong = false;
//door
#define dirPin A0
#define stepPin A1
#define openSwitch A3
#define closeSwitch A2
#define frequencyDelay 180
//solenoid-water
#define watertrigPin 5
#define waterechoPin 6
#define solenoidPin 7
double low_bound = 5.0;
double high_bound = 2.0;
double waterBucketHeight = 12.5;
double gallons_full;
double gallons_per_inch = 1.36;
double waterDistance;
bool solenoidOn = true;
//food
#define foodtrigPin 8
#define foodechoPin 9
double foodBucketHeight = 15.7;
double pounds_full;
double pounds_per_inch = 3.821;
double foodDistance;
//timer
bool timerOn = false;
bool autoDoor = false;
int morning = 11;
int night = 21;
int closeHours[] = {
21,
22,
23,
24,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
};
int openHours[] = {
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
};
byte mac[] = {
0xDE,
0xAD,
0xBE,
0xEF,
0xFE,
0xED
};
IPAddress ip(192, 168, 254, 99);
EthernetServer server(80);
#define REQ_BUF_SZ 100
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
void setup() {
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
//solenoid
pinMode(solenoidPin, OUTPUT);
pinMode(watertrigPin, OUTPUT);
pinMode(waterechoPin, INPUT);
digitalWrite(solenoidPin, HIGH);
//door
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(openSwitch, INPUT_PULLUP);
pinMode(closeSwitch, INPUT_PULLUP);
//food
pinMode(foodtrigPin, OUTPUT);
pinMode(foodechoPin, INPUT);
Serial.begin(9600);
rtc.begin();
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found.");
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start the server
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}
char StrContains(char *str, char *sfind)
{
char found = 0;
char index = 0;
char len;
len = strlen(str);
if (strlen(sfind) > len) {
return 0;
}
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (strlen(sfind) == found) {
return 1;
}
}
else {
found = 0;
}
index++;
}
return 0;
}
void playwarning() {
for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
// calculates the duration of each note
divider = melody[thisNote + 1];
if (divider > 0) {
// regular note, just proceed
noteDuration = (wholenote) / divider;
} else if (divider < 0) {
// dotted notes are represented with negative durations!!
noteDuration = (wholenote) / abs(divider);
noteDuration *= 1.5; // increases the duration in half for dotted notes
}
// we only play the note for 90% of the duration, leaving 10% as a pause
tone(buzzer, melody[thisNote], noteDuration * 0.9);
// Wait for the specief duration before playing the next note.
delay(noteDuration);
// stop the waveform generation before the next note.
noTone(buzzer);
}
}
//door open
void open(bool warningSong) {
if (digitalRead(openSwitch) != LOW) {
if (warningSong) {
playwarning();
warningSong = false;
}
while (digitalRead(openSwitch) != LOW) {
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(frequencyDelay);
}
}
}
//door close
void close(bool warningSong) {
if (digitalRead(closeSwitch) != LOW) {
if (warningSong) {
playwarning();
warningSong = false;
}
while (digitalRead(closeSwitch) != LOW) {
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(frequencyDelay);
}
}
}
void get_water_measurement_amount() {
long duration;
// Clears the trigPin
digitalWrite(watertrigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(watertrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(watertrigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(waterechoPin, HIGH);
// Calculating the waterDistance
waterDistance = ((duration * 0.034) / 2.0) / 2.54;
gallons_full = gallons_per_inch * (waterBucketHeight - waterDistance);
}
void trigger_water_low_bound() {
while (waterDistance > high_bound && waterDistance < 15.0) {
digitalWrite(solenoidPin, LOW);
Serial.println(waterDistance);
Serial.println("FAUCET ON, GALLONS: ");
Serial.println(gallons_full);
get_water_measurement_amount();
}
digitalWrite(solenoidPin, HIGH);
Serial.println("FAUCET OFF, GALLONS: ");
Serial.println(gallons_full);
}
void get_food_measurement_amount() {
long duration;
// Clears the trigPin
digitalWrite(foodtrigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(foodtrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(foodtrigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(foodechoPin, HIGH);
// Calculating the waterDistance
foodDistance = ((duration * 0.034) / 2.0) / 2.54;
pounds_full = pounds_per_inch * (foodBucketHeight - foodDistance);
}
void timeCheck() {
Time currentTime = rtc.getTime();
int currentHour = currentTime.hour;
for (int cnt = 0; cnt < 10; cnt++) {
if (openHours[cnt] == currentHour) {
open(false);
}
}
for (int cnt = 0; cnt < 14; cnt++) {
if (closeHours[cnt] == currentHour) {
close(true);
}
}
}
void(* resetFunc) (void) = 0;
void loop() {
// Create a client connection
EthernetClient client = server.available();
get_food_measurement_amount();
get_water_measurement_amount();
if (Ethernet.linkStatus() == LinkOFF) {
timeCheck();
if (waterDistance >= low_bound && waterDistance < 15.0) {
trigger_water_low_bound();
}
}
else {
if (timerOn && autoDoor) {
Time currentTime = rtc.getTime();
int currentHour = currentTime.hour;
if (currentHour == morning || currentHour == night) {
timeCheck();
}
if (solenoidOn) {
if (waterDistance >= low_bound && waterDistance < 15.0) {
trigger_water_low_bound();
}
}
}
}
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
//if HTTP request has ended
if (c == '\n' && currentLineIsBlank) {
//now output HTML data header
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: text/html"));
client.println(F("Connection: close")); // the connection will be closed after completion of the response
client.println();
client.println(F("<!DOCTYPE HTML>"));
client.println(F("<HTML>"));
client.println(F("<HEAD>"));
client.println(F("<meta name='viewport' content='width=device-width, initial-scale=1.0'>"));
client.println(F("<TITLE></TITLE>"));
client.println(F("</HEAD>"));
client.println(F("<script>"));
client.println(F("if(typeof window.history.pushState == 'function') {"));
client.println(F("window.history.pushState({}, 'Hide', 'http://192.168.254.99/');"));
client.println(F("}"));
client.println(F("</script>"));
client.println(F("<BODY>"));
client.println(F("<FORM ACTION='/' method=get style = 'display:flex; flex-direction:column; text-align:center; width: 300px; margin-left: auto; margin-right: auto; margin-top: 50px;'>")); //uses IP/port of web page
if (timerOn) {
client.println(F("<p>"));
client.println(F("time:"));
client.println(rtc.getTimeStr());
client.println(F("</p>"));
client.println(F("<p>temperature: "));
client.println(((rtc.getTemp()) * 1.8) + 16.0);
client.println(F("degrees</p>"));
}
client.println(F("<p>door: "));
if (digitalRead(openSwitch) == LOW) {
client.println(F("<b>OPEN</b>"));
}
if (digitalRead(closeSwitch) == LOW) {
client.println(F("<b>CLOSED</b>"));
}
client.println(F("</p>"));
client.println(F("<button TYPE=SUBMIT name = 'OPENDOORNOWARN'>open door</button>"));
client.println(F("<button TYPE=SUBMIT name = 'CLOSEDOORNOWARN' style = 'margin-bottom: 10px;'>close door</button>"));
client.println(F("<button TYPE=SUBMIT name = 'OPENDOORWARN'>open door (warning)</button>"));
client.println(F("<button TYPE=SUBMIT name = 'CLOSEDOORWARN'>close door (warning)</button>"));
client.println(F("<p>solenoid: "));
if (solenoidOn) {
client.println(F("<b>ON</b>"));
} else {
client.println(F("<b>OFF</b>"));
}
client.println(F("</p>"));
client.println(F("<button TYPE=SUBMIT name = 'SOLENOIDON'>solenoid on</button>"));
client.println(F("<button TYPE=SUBMIT name = 'SOLENOIDOFF'>solenoid off</button>"));
client.println(F("<p>auto door: "));
if (autoDoor) {
client.println(F("<b>ON</b>"));
} else {
client.println(F("<b>OFF</b>"));
}
client.println(F("</p>"));
client.println(F("<button TYPE=SUBMIT name = 'AUTOON'>auto door on</button>"));
client.println(F("<button TYPE=SUBMIT name = 'AUTOOFF'>auto door off</button>"));
client.println(F("<p>food: "));
if (pounds_full < 25.0) {
client.println(F("<b>LOW</b>; "));
}
client.println(pounds_full);
client.println(F(" pounds</p>"));
client.println(F("<p>water: "));
if (waterDistance > low_bound) {
client.println(F("<b>LOW</b>;"));
}
client.println(gallons_full);
client.println(F(" gallons</p>"));
client.println(F("<button TYPE=SUBMIT name = 'TIMERON' style = 'width: 100px; text-align:center; margin-left: auto; margin-right: auto;'>timer on</button>"));
client.println(F("<button TYPE=SUBMIT name = 'TIMEROFF' style = 'width: 100px; text-align:center; margin-left: auto; margin-right: auto;'>timer off</button>"));
client.println(F("<button TYPE=SUBMIT name = 'RESET' style = 'width: 100px; text-align:center; margin-left: auto; margin-right: auto;'>reset arduino</button>"));
client.println(F("</FORM>"));
client.println("");
client.println(F("</BODY>"));
client.println(F("</HTML>"));
if (c == '\n' && currentLineIsBlank) {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
delay(1);
//stopping client
client.stop();
if (StrContains(HTTP_req, "OPENDOORNOWARN")) {
open(false);
}
if (StrContains(HTTP_req, "OPENDOORWARN")) {
open(true);
}
if (StrContains(HTTP_req, "CLOSEDOORNOWARN")) {
close(false);
}
if (StrContains(HTTP_req, "CLOSEDOORWARN")) {
close(true);
}
if (StrContains(HTTP_req, "SOLENOIDOFF")) {
solenoidOn = false;
}
if (StrContains(HTTP_req, "SOLENOIDON")) {
solenoidOn = true;
}
if (StrContains(HTTP_req, "AUTOON")) {
autoDoor = true;
}
if (StrContains(HTTP_req, "AUTOOFF")) {
autoDoor = false;
}
if (StrContains(HTTP_req, "TIMERON")) {
timerOn = true;
}
if (StrContains(HTTP_req, "TIMEROFF")) {
timerOn = false;
}
if (StrContains(HTTP_req, "RESET")) {
resetFunc();
}
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
}
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
}
}
}
}