part 2
/*########################################################################################*/
void digitalClockDisplay()
{
// digital clock display of the time
time_t utc, local;
utc = now();
local = utc + timezone *60 * 60;
line1 = "";
if (hour(local) < 10)
line1 += "0";
line1 += hour(local);
line1 += ":";
if (minute() < 10)
line1 += "0";
line1 += minute();
line1 += ":";
if (second() < 10)
line1 += "0";
line1 += second();
if (timeStatus() == timeNeedsSync)
line1 += "* ";
else
line1 += " ";
if (day() < 10)
line1 += "0";
line1 += day();
line1 += "/";
if (month() < 10)
line1 += "0";
line1 += month();
line1 += "/";
line1 +=year();
bitWrite(updated, 0, 1);
}
/*########################################################################################*/
void printDigits(int digits, String line){
if(digits < 10)
line += '0';
line += digits;
}
/*########################################################################################*/
void updateDisplay()
{
if (bitRead(updated, 0) == 1)
{
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print(line1);
}
if (bitRead(updated, 1) == 1)
{
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(line2);
}
if (bitRead(updated, 2) == 1)
{
lcd.setCursor(0,2);
lcd.print(" ");
lcd.setCursor(0,2);
lcd.print(line3);
}
if (bitRead(updated, 3) == 1)
{
lcd.setCursor(0,3);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print(line4);
}
updated = 0;
}
/*########################################################################################*/
void connectClient()
{
String readCommand;
Serial.println("Connecting...");
if (imapClient.connect())
{
Serial.println("Connecting OK");
readCommand = imapCommand("arduino login username password", "Login", (char *)responseOK, true);
if (readCommand.length() <= 0)
{
imapClient.flush();
imapClient.stop();
}
else
{
readCommand = imapCommand("arduino select mailboxname", "Mailbox", (char *)responseOK, false);
if (readCommand.length() <= 0)
{
imapClient.flush();
imapClient.stop();
}
else
{
readCommand = imapCommand("arduino idle", "IDLE", "+", true);
if (readCommand.length() <= 0)
{
imapClient.flush();
imapClient.stop();
}
}
}
}
else
{
Serial.println("connection failed");
}
}
/*########################################################################################*/
String imapCommand(String Command, char* Message, char *Response, boolean outputR)
{
String dummy;
Serial.print(Message);
Serial.println("...");
imapClient.println(Command);
// while (imapClient.available() == 0);
dummy=imapRead(Response, outputR);
if (!outputR) dummy = "OK";
Serial.print(Message);
if (dummy.length() > 0) Serial.println(" OK");
else Serial.println(" NK");
return dummy;
}
/*########################################################################################*/
String imapRead(char *Response, boolean outputR)
{
String readClient;
unsigned long timer;
timer = millis();
while (millis() - timer < 5000 && !imapClient.available());
if (imapClient.available())
{
while (imapClient.available())
{
char c = imapClient.read();
if (outputR) readClient += c;
}
readClient = readClient.toUpperCase();
if (readClient.indexOf(Response) > -1)
{
return readClient;
}
else
{
return "";
}
}
else
{
return "";
}
}
/*########################################################################################*/
void parseImap(String readCommand)
{
String extract;
int index = 0;
int oldindex = 0;
index = readCommand.indexOf("\n");
while (index != -1)
{
extract = readCommand.substring(oldindex, index-1);
if (extract.indexOf("EXISTS") > -1)
{
Serial.println(extract);
}
if (extract.indexOf("RECENT") > -1)
{
Serial.println(extract);
}
if (extract.indexOf("(MESSAGES ") > -1)
{
mailMessages = extract.substring(extract.indexOf("(MESSAGES ") + 10, extract.indexOf(")"));
bitWrite(updated, 1, 1);
}
if (extract.indexOf("(UNSEEN ") > -1)
{
newMessages = extract.substring(extract.indexOf("(UNSEEN ") + 8, extract.indexOf(")"));
bitWrite(updated, 2, 1);
}
if (updated > 0)
{
line2 = "Inbox : " + mailMessages;
line3 = "Unread : " + newMessages;
line4 = "Mailbox info";
bitWrite(updated, 3, 1);
}
oldindex = index+1;
index = readCommand.indexOf("\n", oldindex);
}
}
/*########################################################################################*/
void getImapStatus()
{
String dummy;
dummy = imapCommand("done", "DONE", (char *)responseOK, true);
if (dummy.length() > 0)
{
parseImap(dummy);
}
dummy = imapCommand("arduino status mailboxname (messages)", "STATUS messages", (char *)responseOK, true);
if (dummy.length() > 0)
{
parseImap(dummy);
}
dummy = imapCommand("arduino status mailboxname (unseen)", "STATUS unseen", (char *)responseOK, true);
if (dummy.length() > 0)
{
parseImap(dummy);
}
dummy = imapCommand("arduino idle", "IDLE", "+", true);
if (dummy.length() > 0)
{
parseImap(dummy);
}
}
/*########################################################################################*/
void phoneClient()
{
String readString;
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
readString += c;
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
if (readString.startsWith("GET /ASTERISK_IN|")) {
if (readString.startsWith("GET / HTTP/1.1")) {
}
else {
line2 = readString.substring(17,readString.indexOf("|",17));
bitWrite(updated, 1, 1);
line3 = readString.substring(readString.indexOf("|",17)+1, readString.indexOf(" HTTP"));
bitWrite(updated, 2, 1);
line4 = "Incoming Call";
bitWrite(updated, 3, 1);
numberDisplay = true;
timer1 = millis();
}
}
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}