Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Time.h>
- #include <Ethernet.h>
- #include <EthernetUdp.h>
- #include <LiquidCrystal.h>
- //LCD
- LiquidCrystal lcd(9, 8 , 5, 4, 3, 2);
- // Ethernet
- byte mac[] = {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- IPAddress ip(192,168,1,177);
- EthernetServer server(80);
- EthernetUDP Udp;
- unsigned int localPort = 8888; // local port to listen for UDP packets
- // NTP Servers (Time)
- IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov
- // IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov
- // IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov
- const int timeZone = 1; // Central European Time
- //const int timeZone = -5; // Eastern Standard Time (USA)
- //const int timeZone = -4; // Eastern Daylight Time (USA)
- //const int timeZone = -8; // Pacific Standard Time (USA)
- //const int timeZone = -7; // Pacific Daylight Time (USA)
- //Custom Characters for LCD
- byte degreeCelcius[8] = {
- 0b11000,
- 0b11000,
- 0b00000,
- 0b00011,
- 0b00100,
- 0b00100,
- 0b00011,
- 0b00000
- };
- byte thermIcon[8] = //icon for termometer
- {
- B00100,
- B01010,
- B01010,
- B01110,
- B01110,
- B11111,
- B11111,
- B01110
- };
- byte musicNote[8] = {
- B00001,
- B00011,
- B00101,
- B01001,
- B01001,
- B01011,
- B11011,
- B11000
- };
- byte signalIcon[8] = {
- B11111,
- B01110,
- B01110,
- B00100,
- B00100,
- B00100,
- B00100,
- B00100
- };
- byte signalBar[8] = {
- B00001,
- B00001,
- B00101,
- B00101,
- B10101,
- B10101,
- B10101,
- B10101
- };
- byte sigFail[8] = {
- 0b00000,
- 0b00000,
- 0b10001,
- 0b01010,
- 0b00100,
- 0b01010,
- 0b10001,
- 0b00000
- };
- //Pins
- const int redLed = 7;
- const int piezo = 6;
- const int tempSensor = A3;
- const int radio = A5;
- const int relayOne = 14;
- const float versionNo = 2.3;
- //Variables
- int radioStat = 0;
- int radioButton = LOW;
- int lightStat = 0;
- int lightButton = LOW;
- int silMode = 0;
- int nightMode = 0;
- int nightLate = 22;
- int nightEarly = 7;
- int temp = 0;
- int ethernetStat = 0; // 0 = off; 1 = on
- int mainStat = 0; // 0 = intilising, 1 = normal, 2 = fire, 3 = security, 4 = fault
- int ethernetConnection = 0;
- //Time
- int tHour = 00;
- int tMin = 00;
- int tSec = 00;
- int tDay = 01;
- int tMonth = 01;
- int tYear = 1970;
- //Troubleshooting
- int troubleshooting = 1; //0 = no; 1 = yes
- String readString;
- void setup() {
- //Pin Modes
- pinMode(relayOne, OUTPUT);
- //Serial
- Serial.begin(9600);
- Serial.println("-------------------------------");
- Serial.print("SmartRoom v");
- Serial.println(versionNo);
- Serial.println("-------------------------------");
- //LCD
- lcd.begin(16,2);
- lcd.print("SmartRoom v2.3");
- lcd.setCursor(0,1);
- lcd.print("Loading...");
- lcd.createChar(1, degreeCelcius);
- lcd.createChar(2, thermIcon);
- lcd.createChar(3, musicNote);
- lcd.createChar(4, signalIcon);
- lcd.createChar(5, signalBar);
- lcd.createChar(6, sigFail);
- //Ethernet
- Ethernet.begin(mac, ip);
- server.begin();
- Serial.print("Server IP address: ");
- Serial.println(Ethernet.localIP());
- Serial.println("Attempting to connect to the network..");
- lcd.clear();
- lcd.print("Welcome James!");
- lcd.setCursor(0,1);
- lcd.print("Initialising...");
- ethernetConnection = 1;
- if (Ethernet.begin(mac) == 0) {
- Serial.println("-------------------------------");
- Serial.println("INTERNET CONNECTION FAILED");
- Serial.println("-------------------------------");
- ethernetConnection = 0;
- playTone(piezo, 3000, 600, silMode);
- delay(150);
- playTone(piezo, 2700, 600, silMode);
- lcd.clear();
- lcd.print("CONNECTION TO");
- lcd.setCursor(0,1);
- lcd.print("NETWORK FAILED");
- }
- Udp.begin(localPort);
- //Time
- Serial.println("Gathering RTC info (NTP time)");
- Serial.println("waiting for sync");
- setSyncProvider(getNtpTime);
- mainStat = 1; // Change status to normal
- playTone(piezo, 3000, 200, silMode);
- delay(200);
- playTone(piezo, 3000, 200, silMode);
- delay(800);
- }
- void loop() {
- // GET TEMPRATURE
- int tempVal = analogRead(tempSensor);
- float voltage = (tempVal/1024.0) * 5.0;
- float temperature = (voltage - .5) * 100;
- temp = round(temperature);
- // END TEMPRATURE
- if(lightStat == 1){
- digitalWrite(relayOne, HIGH);
- }
- else{
- if(lightStat == 0){
- digitalWrite(relayOne, LOW);
- }
- }
- if(mainStat == 1){ // DISPLAY MENU IF EVERYTHING IS OK
- digitalClockDisplay();
- lcd.clear();
- lcd.setCursor(0,0);
- printDigitsLCD(tHour);
- lcd.print(":");
- printDigitsLCD(tMin);
- lcd.setCursor(6, 0);
- printDigitsLCD(tDay);
- lcd.print("/");
- printDigitsLCD(tMonth);
- lcd.print("/");
- lcd.print(tYear);
- lcd.setCursor(0,1);
- lcd.write(2);
- lcd.print(temp);
- lcd.write(1);
- lcd.setCursor(5,1);
- lcd.write(3);
- if(radioStat == 1){
- lcd.print("ON!");
- }
- if(radioStat == 0){
- lcd.print("OFF");
- }
- lcd.setCursor(14,1);
- lcd.write(4);
- if(ethernetConnection == 1) {
- lcd.write(5);
- }
- else{
- lcd.write(6);
- }
- }
- delay(1000);
- // Create a client connection
- EthernetClient client = server.available();
- if (client) {
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- //read char by char HTTP request
- if (readString.length() < 100) {
- //store characters to string
- readString += c;
- //Serial.print(c);
- }
- //if HTTP request has ended
- if (c == '\n') {
- ///////////////
- Serial.println(readString); //print to serial monitor for debuging
- client.println("HTTP/1.1 200 OK"); //send new page
- client.println("Content-Type: text/html");
- client.println();
- client.println("<HTML>");
- client.println("<HEAD>");
- client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
- client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
- client.println("<link rel='stylesheet' type='text/css' href='http://homeautocss.net84.net/a.css' />");
- client.println("<TITLE>Home Automation</TITLE>");
- client.println("</HEAD>");
- client.println("<BODY>");
- client.println("<H1>Home Automation</H1>");
- client.println("<hr />");
- client.println("<br />");
- client.println("<a href=\"/?radioon\"\">Turn On Radio</a>");
- client.println("<a href=\"/?radiooff\"\">Turn Off Radio</a><br /><br />");
- client.println("<a href=\"/?lighton\"\">Turn On Light</a>");
- client.println("<a href=\"/?lightoff\"\">Turn Off Light</a><br />");
- client.println("</BODY>");
- client.println("</HTML>");
- delay(1);
- //stopping client
- client.stop();
- ///////////////////// control arduino pin
- if(readString.indexOf("?radioon") >0)//checks for off
- {
- radioStat = 0;
- Serial.println("Light On");
- }
- if(readString.indexOf("?lighton") >0)//checks for on
- {
- lightStat = 1;
- Serial.println("Light On");
- }
- if(readString.indexOf("?lightoff") >0)//checks for off
- {
- lightStat = 0;
- Serial.println("Light Off");
- }
- //clearing string for next read
- readString="";
- }
- }
- }
- }
- //TROUBLESHOOTING (SERIAL)
- if(troubleshooting == 1){
- Serial.println("__________________________________________");
- printDigitsSerial(tHour);
- Serial.print(":");
- printDigitsSerial(tMin);
- Serial.print(":");
- printDigitsSerial(tSec);
- Serial.print(" ");
- printDigitsSerial(tDay);
- Serial.print("/");
- printDigitsSerial(tMonth);
- Serial.print("/");
- Serial.println(tYear);
- Serial.print("Server: ");
- Serial.println(Ethernet.localIP());
- Serial.print("Main Status: ");
- Serial.println(mainStat);
- Serial.print("Temprature: ");
- Serial.println(temp);
- Serial.print("Radio Status: ");
- Serial.println(radioStat);
- Serial.print("Light Status: ");
- Serial.println(lightStat);
- Serial.print("Night mode: ");
- Serial.println(nightMode);
- }
- }
- void digitalClockDisplay(){
- // digital clock display of the time
- tHour = hour();
- tMin = minute();
- tSec = second();
- tDay = day();
- tMonth = month();
- tYear = year();
- }
- void printDigitsSerial(int digits){
- // utility for digital clock display: prints preceding colon and leading 0
- if(digits < 10){
- Serial.print('0');
- }
- Serial.print(digits);
- }
- void printDigitsLCD(int digits){
- if(digits < 10){
- lcd.print('0');
- }
- lcd.print(digits);
- }
- /*-------- NTP code ----------*/
- const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
- byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
- time_t getNtpTime()
- {
- while (Udp.parsePacket() > 0) ; // discard any previously received packets
- Serial.println("Transmiting NTP Request");
- sendNTPpacket(timeServer);
- uint32_t beginWait = millis();
- while (millis() - beginWait < 1500) {
- int size = Udp.parsePacket();
- if (size >= NTP_PACKET_SIZE) {
- Serial.println("Received NTP Response");
- Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
- unsigned long secsSince1900;
- // convert four bytes starting at location 40 to a long integer
- secsSince1900 = (unsigned long)packetBuffer[40] << 24;
- secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
- secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
- secsSince1900 |= (unsigned long)packetBuffer[43];
- return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
- }
- }
- Serial.println("No NTP Response!");
- return 0; // return 0 if unable to get the time
- }
- // send an NTP request to the time server at the given address
- void sendNTPpacket(IPAddress &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.beginPacket(address, 123); //NTP requests are to port 123
- Udp.write(packetBuffer, NTP_PACKET_SIZE);
- Udp.endPacket();
- }
- void playTone (int x, int y, int z, int s){
- if(s == 0){
- tone(x, y, z);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement