I have written a sketch for a friend that brews beer. I am not very experienced writing programs, but I have gotten to a point where I can have the Linux side send emails. It sends 3 types of emails, one on start-up or reset, one periodic, and one when a temp gets too high.
Everything was working well until I try to add in Yun Server/Client code from the bridge/temperaturewebpanel example. When I do, it stops sending some of the emails. It looks like the code is still executing, but the email does not get sent. I suspect the text file is not getting completed before the ssmtp command tries to send the text file.
I have had other problems while writing the program which made the emails magically stop. I have tried so many things to diagnose the problem, with no progress.
I believe what may be happening is that either:
1)the strings are not fully written to the file before the email is sent
2)the server code may be running a Linux command asynchronouslly and not finishing before my command tries to run
3)the Linux side may get overwhelmed by trying to send emails and check for incoming clients
If anyone could suggest what could be causing the problem, I would love to hear it so I can test it. Is there a better way to write the text file? Is there something obvious I am missing?
I tried to have the ssmtp send a file that was already created and it worked, which is why I think it could be how the file is written to.
Below is the code. If the red lines are removed, the code works well, but it does not have server functions. (I had to remove most of program because it was too long. The .ino is attached
Thanks to everyone who took a look. I have been obsessing over it.
#include <Bridge.h>
#include <FileIO.h>
#include <YunServer.h>
#include <YunClient.h>
YunServer server;
long time_between_OverTemp_Alert_at_Fermenter_1 = 60000;
int fermenter_1_OverTemp_alert_in_F = 80;
long time_between_datalog_email = 65000;
long time_between_datalog_write = 60000;
int calibration_pin = A1;
int thermsistor_pin_at_Fermenter_1 = A0;
double raw_brew_value_at_Fermenter_1 = 0;
double beer_temp_in_F_at_Fermenter_1 = 0;
double ActRestValue_at_Fermenter_1 = 10000;
double calibration_value = 0;
int ResetEmailSent = 0;
long last_OverTemp_alert_time_at_Fermenter_1 = 0;
long last_datalog_email = 0;
long last_datalog_write = 0;
void setup() {
Serial.begin(9600);
while (!Serial) { ; }
FileSystem.begin();
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Bridge.begin();
digitalWrite(13, HIGH);
server.listenOnLocalhost();
server.begin();
}
void loop() {
YunClient client = server.accept();
if (client) {
String command = client.readString();
command.trim(); //kill whitespace
Serial.println(command);
// is "temperature" command?
if (command == "temperature") {
client.print("Current time on yun");
}
client.stop();
}
delay(50); // Poll every 50ms
calibration_value = map(analogRead(calibration_pin), 0, 1023, -1000, 1000);
calibration_value = calibration_value/100;
raw_brew_value_at_Fermenter_1 = analogRead(thermsistor_pin_at_Fermenter_1); // variable for reading the pushbutton status
beer_temp_in_F_at_Fermenter_1 = log(((10240000/raw_brew_value_at_Fermenter_1) - ActRestValue_at_Fermenter_1)); //added the /10000 to try and add resolution to the rawadc number because it is difficult to hold a decimal as a variable)
beer_temp_in_F_at_Fermenter_1 = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * beer_temp_in_F_at_Fermenter_1 * beer_temp_in_F_at_Fermenter_1 ))* beer_temp_in_F_at_Fermenter_1 );
beer_temp_in_F_at_Fermenter_1 = beer_temp_in_F_at_Fermenter_1 - 273.15; // Convert Kelvin to Celcius = Temp - 273.15; // Convert Kelvin to Celcius
beer_temp_in_F_at_Fermenter_1 = (beer_temp_in_F_at_Fermenter_1 * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
beer_temp_in_F_at_Fermenter_1 = beer_temp_in_F_at_Fermenter_1 + calibration_value;
if (ResetEmailSent < 1) {
Serial.println("Beginning to Send Reset Email");
String ResetEmailFrom = ("From: BrewerNotifier ");
String ResetEmailSubject = ("Subject: The Notifier Reset.");
ResetEmailSubject += (" The Temp in Fermenter 1 is: ");
ResetEmailSubject += beer_temp_in_F_at_Fermenter_1;
ResetEmailSubject += " deg F ";
String ResetEmailContent = ("The Notifier Reset at ");
ResetEmailContent += getTimeStamp();
ResetEmailContent += (" The Temp in Fermenter 1 is: ");
ResetEmailContent += beer_temp_in_F_at_Fermenter_1;
ResetEmailContent += " deg F ";
String ResetEmailBlankLine = ("");
File dataFile = FileSystem.open("/mnt/sd/ResetEmail.txt", FILE_APPEND);
if (dataFile) {
dataFile.println(ResetEmailFrom);
dataFile.println(ResetEmailSubject);
dataFile.println(ResetEmailBlankLine);
dataFile.println(ResetEmailContent);
dataFile.close();
}
else {
Serial.println("error opening ResetEmail.txt");
}
Process SendResetEmail;
SendResetEmail.runShellCommand("cat /mnt/sd/ResetEmail.txt | ssmtp domenic***@gmail.com");
ResetEmailSent = 1;
Serial.println("Reset Email Should be in your inbox");
Process DeleteRestartEmail;
DeleteRestartEmail.runShellCommand("rm /mnt/sd/ResetEmail.txt"); //delete the old file
}
delay(1000);
}
// This function return a string with the time stamp
String getTimeStamp() {
String result;
Process time;
// date is a command line utility to get the date and the time
// in different formats depending on the additional parameter
time.begin("date");
time.addParameter("+%D-%T"); // parameters: D for the complete date mm/dd/yy
// T for the time hh:mm:ss
time.run(); // run the command
// read the output of the command
while(time.available()>0) {
char c = time.read();
if(c != 'n')
result += c;
}
return result;
}
Sean_Notifier_Yun_V5B.ino (14.7 KB)