Advertisement
Guest User

Arduino issue

a guest
Aug 26th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.06 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Time.h>
  3. #include <Ethernet.h>
  4. #include <EthernetUdp.h>
  5. #include <LiquidCrystal.h>
  6.  
  7. //LCD
  8. LiquidCrystal lcd(9, 8 , 5, 4, 3, 2);
  9.  
  10. // Ethernet
  11. byte mac[] = {
  12.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  13. IPAddress ip(192,168,1,177);
  14. EthernetServer server(80);
  15. EthernetUDP Udp;
  16. unsigned int localPort = 8888;  // local port to listen for UDP packets
  17.  
  18. // NTP Servers (Time)
  19. IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov
  20. // IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov
  21. // IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov
  22.  
  23. const int timeZone = 1;     // Central European Time
  24. //const int timeZone = -5;  // Eastern Standard Time (USA)
  25. //const int timeZone = -4;  // Eastern Daylight Time (USA)
  26. //const int timeZone = -8;  // Pacific Standard Time (USA)
  27. //const int timeZone = -7;  // Pacific Daylight Time (USA)
  28.  
  29. //Custom Characters for LCD
  30. byte degreeCelcius[8] = {
  31.   0b11000,
  32.   0b11000,
  33.   0b00000,
  34.   0b00011,
  35.   0b00100,
  36.   0b00100,
  37.   0b00011,
  38.   0b00000
  39. };
  40.  
  41. byte thermIcon[8] = //icon for termometer
  42. {
  43.   B00100,
  44.   B01010,
  45.   B01010,
  46.   B01110,
  47.   B01110,
  48.   B11111,
  49.   B11111,
  50.   B01110
  51. };
  52.  
  53. byte musicNote[8] = {
  54.   B00001,
  55.   B00011,
  56.   B00101,
  57.   B01001,
  58.   B01001,
  59.   B01011,
  60.   B11011,
  61.   B11000
  62. };
  63.  
  64. byte signalIcon[8] = {
  65.   B11111,
  66.   B01110,
  67.   B01110,
  68.   B00100,
  69.   B00100,
  70.   B00100,
  71.   B00100,
  72.   B00100
  73. };
  74.  
  75. byte signalBar[8] = {
  76.   B00001,
  77.   B00001,
  78.   B00101,
  79.   B00101,
  80.   B10101,
  81.   B10101,
  82.   B10101,
  83.   B10101
  84. };
  85.  
  86. byte sigFail[8] = {
  87.   0b00000,
  88.   0b00000,
  89.   0b10001,
  90.   0b01010,
  91.   0b00100,
  92.   0b01010,
  93.   0b10001,
  94.   0b00000
  95. };
  96.  
  97. //Pins
  98. const int redLed = 7;
  99. const int piezo = 6;
  100. const int tempSensor = A3;
  101. const int radio = A5;
  102. const int relayOne = 14;
  103.  
  104. const float versionNo = 2.3;
  105.  
  106. //Variables
  107. int radioStat = 0;
  108. int radioButton = LOW;
  109.  
  110. int lightStat = 0;
  111. int lightButton = LOW;
  112.  
  113. int silMode = 0;
  114.  
  115. int nightMode = 0;
  116. int nightLate = 22;
  117. int nightEarly = 7;
  118.  
  119. int temp = 0;
  120.  
  121. int ethernetStat = 0; // 0 = off; 1 = on
  122. int mainStat = 0; // 0 = intilising, 1 = normal, 2 = fire, 3 = security, 4 = fault
  123. int ethernetConnection = 0;
  124.  
  125. //Time
  126. int tHour = 00;
  127. int tMin = 00;
  128. int tSec = 00;
  129. int tDay = 01;
  130. int tMonth = 01;
  131. int tYear = 1970;
  132.  
  133. //Troubleshooting
  134. int troubleshooting = 1; //0 = no; 1 = yes
  135.  
  136. String readString;
  137.  
  138. void setup() {
  139.  
  140.   //Pin Modes
  141.   pinMode(relayOne, OUTPUT);
  142.  
  143.   //Serial
  144.   Serial.begin(9600);
  145.   Serial.println("-------------------------------");
  146.   Serial.print("SmartRoom v");
  147.   Serial.println(versionNo);
  148.   Serial.println("-------------------------------");
  149.  
  150.   //LCD
  151.   lcd.begin(16,2);
  152.   lcd.print("SmartRoom v2.3");
  153.   lcd.setCursor(0,1);
  154.   lcd.print("Loading...");
  155.  
  156.   lcd.createChar(1, degreeCelcius);
  157.   lcd.createChar(2, thermIcon);
  158.   lcd.createChar(3, musicNote);
  159.   lcd.createChar(4, signalIcon);
  160.   lcd.createChar(5, signalBar);
  161.   lcd.createChar(6, sigFail);
  162.  
  163.   //Ethernet
  164.   Ethernet.begin(mac, ip);
  165.   server.begin();
  166.   Serial.print("Server IP address: ");
  167.   Serial.println(Ethernet.localIP());
  168.   Serial.println("Attempting to connect to the network..");
  169.  
  170.   lcd.clear();
  171.   lcd.print("Welcome James!");
  172.   lcd.setCursor(0,1);
  173.   lcd.print("Initialising...");
  174.  
  175.   ethernetConnection = 1;
  176.  
  177.   if (Ethernet.begin(mac) == 0) {
  178.     Serial.println("-------------------------------");
  179.     Serial.println("INTERNET CONNECTION FAILED");
  180.     Serial.println("-------------------------------");
  181.  
  182.     ethernetConnection = 0;
  183.     playTone(piezo, 3000, 600, silMode);
  184.     delay(150);
  185.     playTone(piezo, 2700, 600, silMode);
  186.     lcd.clear();
  187.     lcd.print("CONNECTION TO");
  188.     lcd.setCursor(0,1);
  189.     lcd.print("NETWORK FAILED");
  190.   }
  191.   Udp.begin(localPort);
  192.  
  193.   //Time
  194.   Serial.println("Gathering RTC info (NTP time)");
  195.   Serial.println("waiting for sync");
  196.   setSyncProvider(getNtpTime);
  197.  
  198.   mainStat = 1;  // Change status to normal
  199.   playTone(piezo, 3000, 200, silMode);
  200.   delay(200);
  201.   playTone(piezo, 3000, 200, silMode);
  202.   delay(800);
  203. }
  204.  
  205.  
  206. void loop() {
  207.  
  208.   // GET TEMPRATURE
  209.   int tempVal = analogRead(tempSensor);
  210.   float voltage = (tempVal/1024.0) * 5.0;
  211.   float temperature = (voltage - .5) * 100;
  212.  
  213.   temp = round(temperature);
  214.   // END TEMPRATURE
  215.  
  216.   if(lightStat == 1){
  217.     digitalWrite(relayOne, HIGH);
  218.   }
  219.   else{
  220.     if(lightStat == 0){
  221.       digitalWrite(relayOne, LOW);
  222.     }
  223.   }
  224.  
  225.  
  226.   if(mainStat == 1){ // DISPLAY MENU IF EVERYTHING IS OK
  227.     digitalClockDisplay();
  228.  
  229.     lcd.clear();
  230.     lcd.setCursor(0,0);
  231.     printDigitsLCD(tHour);
  232.     lcd.print(":");
  233.     printDigitsLCD(tMin);
  234.  
  235.     lcd.setCursor(6, 0);
  236.     printDigitsLCD(tDay);
  237.     lcd.print("/");
  238.     printDigitsLCD(tMonth);
  239.     lcd.print("/");
  240.     lcd.print(tYear);
  241.  
  242.     lcd.setCursor(0,1);
  243.     lcd.write(2);
  244.     lcd.print(temp);
  245.     lcd.write(1);
  246.  
  247.     lcd.setCursor(5,1);
  248.     lcd.write(3);
  249.     if(radioStat == 1){
  250.       lcd.print("ON!");
  251.     }
  252.     if(radioStat == 0){
  253.       lcd.print("OFF");
  254.     }
  255.  
  256.     lcd.setCursor(14,1);
  257.     lcd.write(4);
  258.     if(ethernetConnection == 1) {
  259.       lcd.write(5);
  260.     }
  261.     else{
  262.       lcd.write(6);
  263.     }
  264.  
  265.   }
  266.  
  267.   delay(1000);
  268.  
  269.  
  270.  
  271.   // Create a client connection
  272.   EthernetClient client = server.available();
  273.   if (client) {
  274.     while (client.connected()) {
  275.       if (client.available()) {
  276.         char c = client.read();
  277.  
  278.         //read char by char HTTP request
  279.         if (readString.length() < 100) {
  280.  
  281.           //store characters to string
  282.           readString += c;
  283.           //Serial.print(c);
  284.         }
  285.  
  286.         //if HTTP request has ended
  287.         if (c == '\n') {
  288.  
  289.           ///////////////
  290.           Serial.println(readString); //print to serial monitor for debuging
  291.  
  292.             client.println("HTTP/1.1 200 OK"); //send new page
  293.           client.println("Content-Type: text/html");
  294.           client.println();
  295.  
  296.           client.println("<HTML>");
  297.           client.println("<HEAD>");
  298.           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
  299.           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
  300.           client.println("<link rel='stylesheet' type='text/css' href='http://homeautocss.net84.net/a.css' />");
  301.           client.println("<TITLE>Home Automation</TITLE>");
  302.           client.println("</HEAD>");
  303.           client.println("<BODY>");
  304.           client.println("<H1>Home Automation</H1>");
  305.           client.println("<hr />");
  306.           client.println("<br />");
  307.  
  308.           client.println("<a href=\"/?radioon\"\">Turn On Radio</a>");
  309.           client.println("<a href=\"/?radiooff\"\">Turn Off Radio</a><br /><br />");
  310.  
  311.           client.println("<a href=\"/?lighton\"\">Turn On Light</a>");
  312.           client.println("<a href=\"/?lightoff\"\">Turn Off Light</a><br />");        
  313.  
  314.           client.println("</BODY>");
  315.           client.println("</HTML>");
  316.  
  317.           delay(1);
  318.           //stopping client
  319.           client.stop();
  320.  
  321.           ///////////////////// control arduino pin
  322.           if(readString.indexOf("?radioon") >0)//checks for off
  323.           {
  324.             radioStat = 0;
  325.             Serial.println("Light On");
  326.           }
  327.          
  328.           if(readString.indexOf("?lighton") >0)//checks for on
  329.           {
  330.             lightStat = 1;
  331.             Serial.println("Light On");
  332.           }
  333.  
  334.           if(readString.indexOf("?lightoff") >0)//checks for off
  335.           {
  336.             lightStat = 0;
  337.             Serial.println("Light Off");
  338.           }
  339.  
  340.           //clearing string for next read
  341.           readString="";
  342.  
  343.         }
  344.       }
  345.     }
  346.   }
  347.  
  348.   //TROUBLESHOOTING (SERIAL)
  349.   if(troubleshooting == 1){
  350.     Serial.println("__________________________________________");
  351.     printDigitsSerial(tHour);
  352.     Serial.print(":");
  353.     printDigitsSerial(tMin);
  354.     Serial.print(":");
  355.     printDigitsSerial(tSec);
  356.     Serial.print(" ");
  357.     printDigitsSerial(tDay);
  358.     Serial.print("/");
  359.     printDigitsSerial(tMonth);
  360.     Serial.print("/");
  361.     Serial.println(tYear);
  362.  
  363.     Serial.print("Server: ");
  364.     Serial.println(Ethernet.localIP());
  365.  
  366.     Serial.print("Main Status: ");
  367.     Serial.println(mainStat);
  368.  
  369.     Serial.print("Temprature: ");
  370.     Serial.println(temp);
  371.  
  372.     Serial.print("Radio Status: ");
  373.     Serial.println(radioStat);
  374.  
  375.     Serial.print("Light Status: ");
  376.     Serial.println(lightStat);
  377.  
  378.     Serial.print("Night mode: ");
  379.     Serial.println(nightMode);
  380.  
  381.   }
  382. }
  383.  
  384. void digitalClockDisplay(){
  385.   // digital clock display of the time
  386.   tHour = hour();
  387.   tMin = minute();
  388.   tSec = second();
  389.   tDay = day();
  390.   tMonth = month();
  391.   tYear = year();
  392. }
  393.  
  394. void printDigitsSerial(int digits){
  395.   // utility for digital clock display: prints preceding colon and leading 0
  396.   if(digits < 10){
  397.     Serial.print('0');
  398.   }
  399.   Serial.print(digits);
  400. }
  401.  
  402. void printDigitsLCD(int digits){
  403.   if(digits < 10){
  404.     lcd.print('0');
  405.   }
  406.   lcd.print(digits);
  407. }
  408.  
  409. /*-------- NTP code ----------*/
  410.  
  411. const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
  412. byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
  413.  
  414. time_t getNtpTime()
  415. {
  416.   while (Udp.parsePacket() > 0) ; // discard any previously received packets
  417.   Serial.println("Transmiting NTP Request");
  418.   sendNTPpacket(timeServer);
  419.   uint32_t beginWait = millis();
  420.   while (millis() - beginWait < 1500) {
  421.     int size = Udp.parsePacket();
  422.     if (size >= NTP_PACKET_SIZE) {
  423.       Serial.println("Received NTP Response");
  424.       Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
  425.       unsigned long secsSince1900;
  426.       // convert four bytes starting at location 40 to a long integer
  427.       secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
  428.       secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
  429.       secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
  430.       secsSince1900 |= (unsigned long)packetBuffer[43];
  431.       return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
  432.     }
  433.   }
  434.   Serial.println("No NTP Response!");
  435.   return 0; // return 0 if unable to get the time
  436. }
  437.  
  438. // send an NTP request to the time server at the given address
  439. void sendNTPpacket(IPAddress &address)
  440. {
  441.   // set all bytes in the buffer to 0
  442.   memset(packetBuffer, 0, NTP_PACKET_SIZE);
  443.   // Initialize values needed to form NTP request
  444.   // (see URL above for details on the packets)
  445.   packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  446.   packetBuffer[1] = 0;     // Stratum, or type of clock
  447.   packetBuffer[2] = 6;     // Polling Interval
  448.   packetBuffer[3] = 0xEC;  // Peer Clock Precision
  449.   // 8 bytes of zero for Root Delay & Root Dispersion
  450.   packetBuffer[12]  = 49;
  451.   packetBuffer[13]  = 0x4E;
  452.   packetBuffer[14]  = 49;
  453.   packetBuffer[15]  = 52;
  454.   // all NTP fields have been given values, now
  455.   // you can send a packet requesting a timestamp:                
  456.   Udp.beginPacket(address, 123); //NTP requests are to port 123
  457.   Udp.write(packetBuffer, NTP_PACKET_SIZE);
  458.   Udp.endPacket();
  459. }
  460.  
  461. void playTone (int x, int y, int z, int s){
  462.   if(s == 0){
  463.     tone(x, y, z);
  464.   }
  465. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement