Skip to content

Instantly share code, notes, and snippets.

@genebean
Last active August 29, 2015 14:16
Show Gist options
  • Save genebean/ebe751a77a9213386251 to your computer and use it in GitHub Desktop.
Save genebean/ebe751a77a9213386251 to your computer and use it in GitHub Desktop.
Arduino UNO with a NIC and LCD
// include the libraries:
#include <Wire.h>
#include <LiquidCrystal.h>
#include <EtherCard.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(10, 9, 5, 4, 3, 2);
// networking
#define STATIC 0 // set to 1 to disable DHCP (adjust myip/gwip values below)
#if STATIC
// ethernet interface ip address
static byte myip[] = { 192, 168, 1, 200 };
// gateway ip address
static byte gwip[] = { 192, 168, 1, 1 };
#endif
// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
byte Ethernet::buffer[500]; // tcp/ip send and receive buffer
const char page[] PROGMEM =
"HTTP/1.0 503 Service Unavailable\r\n"
"Content-Type: text/html\r\n"
"Retry-After: 600\r\n"
"\r\n"
"<html>"
"<head><title>"
"Service Temporarily Unavailable"
"</title></head>"
"<body>"
"<h3>This service is currently unavailable</h3>"
"<p><em>"
"The main server is currently off-line.<br />"
"Please try again later."
"</em></p>"
"</body>"
"</html>"
;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(20, 4);
// Print a message to the LCD.
/* lcd.print("Playing on a 20 char");
lcd.setCursor(0,1);
lcd.print("wide LCD."); */
// networking
Serial.begin(57600);
Serial.println("\n[backSoon]");
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
#if STATIC
ether.staticSetup(myip, gwip);
#else
if (!ether.dhcpSetup())
Serial.println("DHCP failed");
#endif
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
uint8_t* myip = ether.myip;
uint8_t* gwip = ether.gwip;
uint8_t* dnsip = ether.dnsip;
lcd.print("IP: "); lcd.print(myip[0]); lcd.print("."); lcd.print(myip[1]); lcd.print("."); lcd.print(myip[2]); lcd.print("."); lcd.print(myip[3]);
lcd.setCursor(0, 1);
lcd.print("GW: "); lcd.print(gwip[0]); lcd.print("."); lcd.print(gwip[1]); lcd.print("."); lcd.print(gwip[2]); lcd.print("."); lcd.print(gwip[3]);
lcd.setCursor(0, 2);
lcd.print("DNS: "); lcd.print(dnsip[0]); lcd.print("."); lcd.print(dnsip[1]); lcd.print("."); lcd.print(dnsip[2]); lcd.print("."); lcd.print(dnsip[3]);
}
void loop() {
// total seconds since reset
int ts = millis() / 1000;
// translate ts to seconds, minutes, and hours
int s = ts % 60;
int m = (ts / 60) % 60;
int h = (ts / 60 / 60) % 60;
// set the cursor to column 0, line 3
// (note: line 0 is the first row, since counting begins with 0):
lcd.setCursor(0, 3);
// print the time since reset:
lcd.print("Run time: ");
// print hours
if (h < 10) {
// print a leading zero when needed
lcd.print("0");
}
lcd.print(h);
lcd.print(':');
// print minutes
if (m < 10) {
// print a leading zero when needed
lcd.print("0");
}
lcd.print(m);
lcd.print(':');
// print seconds
if (s < 10) {
// print a leading zero when needed
lcd.print("0");
}
lcd.print(s);
// wait for an incoming TCP packet, but ignore its contents
if (ether.packetLoop(ether.packetReceive())) {
memcpy_P(ether.tcpOffset(), page, sizeof page);
ether.httpServerReply(sizeof page - 1);
}
}
// include the libraries:
#include <Wire.h>
#include <LiquidCrystal.h>
#include <UIPEthernet.h>
/* initialize the library with the numbers of the interface pins
* LCD RS pin to digital pin 9 (12 in tutorials)
* LCD Enable pin to digital pin 6 (11 in tutorials)
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
*/
LiquidCrystal lcd(9, 6, 5, 4, 3, 2);
// ethernet mac address - must be unique on your network
static byte mac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
EthernetServer server(80);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(20, 4);
// networking
Serial.begin(57600);
/// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for (;;)
;
}
server.begin();
Serial.print("IP Address: "); Serial.println(Ethernet.localIP());
Serial.print("Gateway Address: "); Serial.println(Ethernet.gatewayIP());
Serial.print("Subnet Mask: "); Serial.println(Ethernet.subnetMask());
Serial.print("DNS Server Address: "); Serial.println(Ethernet.dnsServerIP());
lcd.print("IP: "); lcd.print(Ethernet.localIP());
lcd.setCursor(0, 1);
lcd.print("GW: "); lcd.print(Ethernet.gatewayIP());
lcd.setCursor(0, 2);
lcd.print("DNS: "); lcd.print(Ethernet.dnsServerIP());
}
void loop() {
// total seconds since reset
int ts = millis() / 1000;
// translate ts to seconds, minutes, and hours
int s = ts % 60;
int m = (ts / 60) % 60;
int h = (ts / 60 / 60) % 60;
// set the cursor to column 0, line 3
// (note: line 0 is the first row, since counting begins with 0):
lcd.setCursor(0, 3);
// print the time since reset:
lcd.print("Run time: ");
// print hours
if (h < 10) {
// print a leading zero when needed
lcd.print("0");
}
lcd.print(h);
lcd.print(':');
// print minutes
if (m < 10) {
// print a leading zero when needed
lcd.print("0");
}
lcd.print(m);
lcd.print(':');
// print seconds
if (s < 10) {
// print a leading zero when needed
lcd.print("0");
}
lcd.print(s);
// listen for incoming clients
EthernetClient client = server.available();
if (client)
{
Serial.println("-> New Connection");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
// 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)
{
client.println("<html><title>Hello World!</title><body><h3>Hello World!</h3></body>");
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(10);
// close the connection:
client.stop();
Serial.println(" Disconnected\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment