I’ve been trying to get this project working for weeks now, and I can’t seem to figure out what the issue is with my code. I am using some graphs, a terminal, notifications, email, and the RTC. My code basically monitors temperatures and reports if they’re out of range. I can modify the code with fake temperature values so it can be tested by other people without all the hardware, but right now I’ll just post my code. Sorry about the long code, I’ve simplified it as much as I can. I am running an Etherten (Uno + Ethernet shield combined). Is there anything obvious that could be causing the disconnects? (Usually the disconnects happen after a notification and email has been sent out, or after the terminal has been used)
Thanks!
#include <SPI.h>
#include <DHT.h>
#include <OneWire.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <WidgetRTC.h>
#define DHTPIN 7
#define DHTTYPE DHT22
#define DS18B20_ID 0x28
const String freezerNames[] = {"FREEZER_NAME", "FREEZER_NAME", "FREEZER_NAME", "FREEZER_NAME", "FREEZER_NAME"};
const char auth[] = "c9a2677b716d4e2e87ff9cccee1f1f26";
const byte arduino_mac [] = {0x9E, 0x7E, 0x7C, 0x5D, 0x5D, 0x9E};
const byte pins[] = {A1, A2, A0, A4, A3, 2, 3, 6};
const byte vPins[] = {V0, V1, V2, V3, V4};
byte data[10];
byte emailOverflow = 0;
byte maxTemp = 30; //Change this once the project is ready to be installed
String errors[5];
String message;
String entered;
boolean fan = true;
boolean fError;
boolean pError;
WidgetRTC rtc;
BlynkTimer Timer;
DHT dht(DHTPIN, DHTTYPE);
WidgetTerminal terminal(V7);
BLYNK_WRITE(V7) {
entered = param.asStr();
if (entered.indexOf("update") == 0) {
for (int i = 0; i < 5; i++) {
terminal.println(freezerNames[i] + ": " + String(data[i]) + "°C. " + errors[i]);
terminal.flush();
}
terminal.println("Lab temperature: " + String(dht.readTemperature(true)) + "°F.");
terminal.println("Lab humidity: " + String(dht.readHumidity()) + "%.");
terminal.println("Maximum freezer temperature: " + String(maxTemp));
terminal.flush();
} else if (entered.indexOf("maxTemp") == 0) {
maxTemp = entered.substring(entered.indexOf(":") + 1).toInt();
terminal.print(F("Maximum freezer temperature changed to "));
terminal.println(String(maxTemp) + "°C.");
terminal.flush();
} else {
terminal.println(F("Invalid command."));
terminal.flush();
}
}
void mainM() {
digitalWrite(pins[7], fan);
fan = !fan;
fError = false; pError = false;
Blynk.virtualWrite(V5, dht.readTemperature(true));
Blynk.virtualWrite(V6, dht.readHumidity());
for (int i = 0; i < 5; i++) {
data[i + 5] = data[i];
data[i] = dallas(pins[i], 0);
if (data[i] == 85) {
pError = true;
errors[i] = "[UNPLUGGED]";
} else if (data[i] > maxTemp && data[i + 5] > maxTemp) {
fError = true;
errors[i] = "[TOO WARM]";
} else {
errors[i] = "";
Blynk.virtualWrite(vPins[i], data[i]);
}
}
if (fError && pError) {
message = "Freezer(s) too warm and probe(s) unplugged! " + getTime();
set('R');
} else if (fError) {
message = "Freezer(s) too warm! " + getTime();
set('R');
} else if (pError) {
message = "Probe(s) unplugged! " + getTime();
set('Y');
} else if (!fError && !pError) {
emailOverflow = 0;
message = "No errors " + getTime();
set('G');
}
if ((fError || pError) && emailOverflow < 2) {
Blynk.email("UMGC Alert!", message);
Blynk.notify("UMGC Alert! " + message);
Blynk.run();
emailOverflow++;
}
terminal.println(message);
terminal.flush();
}
void setup() {
for (int i = 0; i < 8; i++) {
if (i < 5) dallas(pins[i], 1);
else if (i >= 5 && i < 8) pinMode(pins[i], OUTPUT);
}
set('Y');
dht.begin();
Timer.setInterval(60000L, mainM);
Blynk.begin(auth, "blynk-cloud.com", 8442, arduino_mac);
set('G');
rtc.begin();
}
void loop() {
Blynk.run();
Timer.run();
}
void set(char color) {
switch (color) {
case 'R':
digitalWrite(3, HIGH);
digitalWrite(2, LOW);
break;
case 'G':
digitalWrite(3, LOW);
digitalWrite(2, HIGH);
break;
case 'Y':
digitalWrite(3, HIGH);
digitalWrite(2, HIGH);
break;
}
}
String getTime() {
if (minute() < 10) return "[" + String(hour()) + ":0" + minute() + ", " + String(month()) + "/" + day() + "/" + year() + "]";
else return "[" + String(hour()) + ":" + minute() + ", " + String(month()) + "/" + day() + "/" + year() + "]";
}
int16_t dallas(int x, byte start) {
OneWire ds(x);
byte i;
byte data[2];
int16_t result;
byte addr[8];
ds.reset();
ds.select(addr);
delay(1000);
if (!ds.search(addr)) return 85;
do {
ds.reset();
ds.write(0xCC);
ds.write(0xBE);
for (i = 0; i < 2; i++) data[i] = ds.read();
result = (data[1] << 8) | data[0];
result >>= 4; if (data[1] & 128) result |= 61440;
if (data[0] & 8) ++result;
ds.reset();
ds.write(0xCC);
ds.write(0x44, 1);
if (start) delay(1000);
} while (start--);
return result;
}