#include <SPI.h>
#include <Ethernet.h>
#include <String.h>
#include <Udp.h>
#include <Time.h>
#include <LiquidCrystal.h>
/*##########################################################################################
# user adjustable parameters
##########################################################################################*/
const int timezone = 1;
const int displaytime = 20000;
const int timesync = 30;
LiquidCrystal lcd(8, 9, 4,5 , 6, 7);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,70,222 };
byte gw[] = { 192,168,70,254 };
byte timeServer[] = {192, 168, 70, 100};
Server server(80);
/*##########################################################################################
##########################################################################################*/
unsigned long int timer1;
time_t prevDisplay;
unsigned int localPort = 8888;
const int NTP_PACKET_SIZE= 48;
byte packetBuffer[ NTP_PACKET_SIZE];
boolean numberDisplay = false;
String line1;
String line2;
String line3;
String line4;
boolean updated = false;
void setup()
{
lcd.begin(20, 4);
lcd.setCursor(4,1);
lcd.print("PhoneDisplay");
Ethernet.begin(mac, ip, gw);
server.begin();
Udp.begin(localPort);
setSyncInterval(timesync);
setSyncProvider(SetNetworkTime);
delay(5000);
}
void loop()
{
String readString;
if (updated) {
updateDisplay();
updated = false;
}
if( now() != prevDisplay)
{
prevDisplay = now();
digitalClockDisplay();
}
if (millis() - timer1 > displaytime) {
numberDisplay = false;
line2 = "";
line3 = "";
line4 = "";
}
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));
line3 = readString.substring(readString.indexOf("|",17)+1, readString.indexOf(" HTTP"));
line4 = "Incoming Call";
numberDisplay = true;
updated = 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();
}
now();
}
unsigned long sendNTPpacket(byte *address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.sendPacket( packetBuffer,NTP_PACKET_SIZE, address, 123); //NTP requests are to port 123
}
unsigned long SetNetworkTime(void)
{
sendNTPpacket(timeServer); // send an NTP packet to a time server
const unsigned long seventyYears = 2208988800UL;
unsigned long highWord;
unsigned long lowWord ;
unsigned long secsSince1900;
unsigned long epoch=0;
delay(100);
if ( Udp.available() ) {
Udp.readPacket(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
highWord = word(packetBuffer[40], packetBuffer[41]);
lowWord = word(packetBuffer[42], packetBuffer[43]);
secsSince1900 = highWord << 16 | lowWord;
epoch = secsSince1900 - seventyYears;
setTime(epoch);
}
else if (timeStatus() == timeNotSet){
setTime(epoch);
}
return (epoch);
}
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();
updated = true;
}
void printDigits(int digits, String line){
if(digits < 10)
line += '0';
line += digits;
}
void updateDisplay()
{
lcd.clear();
// delay(100);
lcd.setCursor(0,0);
lcd.print(line1);
lcd.setCursor(0,1);
lcd.print(line2);
lcd.setCursor(0,2);
lcd.print(line3);
lcd.setCursor(0,3);
lcd.print(line4);
updated = false;
}