Guest User

Untitled

a guest
Aug 17th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.11 KB | None | 0 0
  1. // Present a "Will be back soon web page", as stand-in webserver.
  2. // 2011-01-30 <[email protected]> http://opensource.org/licenses/mit-license.php
  3. // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
  4.  
  5. #include <Wire.h>
  6. #include "RTClib.h"
  7. #include <EtherCard.h>
  8. #include "DHT.h"
  9.  
  10. RTC_DS1307 rtc;
  11. DHT dht(7, DHT11);
  12. const byte dhtPowerPin PROGMEM = 8; //dht power pin
  13.  
  14. const byte wPin PROGMEM = A0; //power from wall
  15. const byte iPin PROGMEM = 9; //side button
  16. const byte rPin PROGMEM = 3; //LED MODULE PINS
  17. const byte gPin PROGMEM = 6;
  18. const byte bPin PROGMEM = 5;
  19.  
  20. //ethernet interface attributes
  21. static byte myip[] = { 192,168,1,114 };
  22. static byte gwip[] = { 192,168,1,1 };
  23. static byte dnsip[] = { 8,8,8,8 };
  24. static byte mymac[] = { 0x72,0x69,0x69,0x2D,0x38,0x31 };
  25.  
  26. byte Ethernet::buffer[500]; // tcp/ip send and receive buffer
  27. BufferFiller bfill;                      //
  28. char results[6];
  29.  
  30. void setup(){
  31.   Serial.begin(9600);
  32.   pinMode(rPin, OUTPUT);
  33.   pinMode(gPin, OUTPUT);
  34.   pinMode(bPin, OUTPUT);
  35.   pinMode(iPin, INPUT_PULLUP);
  36.   pinMode(wPin, INPUT);
  37.   pinMode(dhtPowerPin,OUTPUT); //DHT11 Setup & Start
  38.   digitalWrite(dhtPowerPin, HIGH);
  39.   dht.begin();
  40.  
  41.   results[0] = { "Thursday 12/12/12 12:12:12 AM" };                 //
  42.  
  43.   if (! rtc.begin()) {
  44.     Serial.println("Couldn't find RTC");
  45.     while (1);
  46.   }
  47.   if (! rtc.isrunning()) {
  48.     Serial.println("RTC is NOT running!");
  49.     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  50.   }
  51.   Serial.println("\n[backSoon]");
  52.  
  53.   if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0)
  54.     Serial.println( "Failed to access Ethernet controller");
  55.  
  56.   Serial.println("\nStatic");
  57.   ether.staticSetup(myip, gwip, dnsip);
  58.  
  59.  
  60.   ether.printIp("IP:  ", ether.myip);
  61.   ether.printIp("GW:  ", ether.gwip);  
  62.   ether.printIp("DNS: ", ether.dnsip);
  63. }
  64.  
  65. void RGBLED(String method, int pin, String dir, int t, byte r, byte i) {
  66.   /*
  67.    *RGB Module control
  68.    *method -> fade or blink
  69.    *pin ----> pin of led module
  70.    *dir ----> "high"=bright and "low"=dim
  71.    *t=delay, r=range, i=increment
  72.    *fade call: RGBLED("fade", rPin, "high", 5, 255, 1);
  73.    *blink call: RGBLED("blink", bPin, "high", 500, 125, 0);
  74.    */
  75.   if (method == "fade") {
  76.     if (dir == "high") { //up
  77.       for ( byte bri = 0; bri < r; bri += i ){ analogWrite(pin, bri); delay(t); }
  78.     } else { //"LOW"
  79.       for ( byte bri = r; bri > 0; bri -= i ){ analogWrite(pin, bri); delay(t); }
  80.       analogWrite(pin, 0);
  81.     }
  82.   } else { //"blink"
  83.     analogWrite(pin, r); delay(t);
  84.   }
  85. }
  86.  
  87. int temp;
  88. int hum;
  89. void GetTemps() {
  90.   byte fails = 0;
  91.   while (fails < 3) {
  92.     digitalWrite(3, HIGH); // turn on dht module
  93.     delay(2000);
  94.     int h = dht.readHumidity();
  95.     int f = dht.readTemperature(true);
  96.     if (isnan(h) || isnan(f)) {
  97.       Serial.println("Failed to read from DHT sensor!");
  98.       for (byte x = 0; x < 3; x++) { RGBLED("blink", bPin, "high", 250, 255, 0); RGBLED("blink", bPin, "low", 250, 0, 0);}
  99.       fails += 1;
  100.     } else {
  101.       //String comment = "Humidity: " + String(h) + " %\t" + "Temperature: " + String(f) + "\n";
  102.       temp = f; hum = h;
  103.       //digitalWrite(3, LOW); // turn off dht module
  104.       for (byte x = 0; x < 3; x++) { RGBLED("blink", bPin, "high", 250, 255, 0); RGBLED("blink", bPin, "low", 250, 0, 0);}
  105.       delay(1000);
  106.       return;
  107.     }
  108.   }
  109.   temp = "N/A"; hum = "N/A";
  110.   //digitalWrite(3, LOW); // turn off dht module
  111.   return;
  112. }
  113.  
  114. char rtcDay = ""; char rtcDate = "";
  115. byte rtcHour = 0; byte rtcMinute = 0; byte rtcSecond = 0; char rtcState = "AM";
  116. void DateAndTime() {
  117.     char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"};
  118.     DateTime now = rtc.now();
  119.     byte hourNow = now.hour();    
  120.     String state;
  121.     if ((hourNow >= 12) && (hourNow <= 24)) { state = "PM"; } else { state = "AM"; }
  122.     if ((hourNow == 0) || (hourNow == 24)) { hourNow = 12; } else if (hourNow > 12) { hourNow -= 12; }
  123.  
  124.     rtcDay = daysOfTheWeek[now.dayOfTheWeek()];
  125.     //rtcDate = char(now.month()) += char(now.day()); // + "/" + char(now.year());
  126.     rtcHour = hourNow;
  127.     rtcMinute = now.minute();
  128.     rtcSecond = now.second();
  129.     //rtcState = state;
  130.     return;
  131. }
  132.  
  133. static word homepage() {
  134.   bfill = ether.tcpOffset();
  135.   bfill.emit_p(PSTR(
  136.     "HTTP/1.0 200 OK\r\n"
  137.     "Content-Type: text/html\r\n"
  138.     "Pragma: no-cache\r\n"
  139.     "\r\n"
  140.     "<meta http-equiv='refresh' content='1'/>"
  141.     "<title>RBBB server</title>"
  142.     "<p>$F $D:$D:$D</p>"), rtcDay, rtcHour, rtcMinute, rtcSecond);
  143.   return bfill.position();
  144. }
  145.  
  146. void loop(){
  147.   // wait for an incoming TCP packet, but ignore its contents
  148.   word len = ether.packetReceive();
  149.   word pos = ether.packetLoop(len);
  150.   if (pos){
  151.     DateAndTime();
  152.     ether.httpServerReply(homepage()); // send web page data
  153.   } else {
  154.     if (! digitalRead(iPin)){
  155.       RGBLED("fade", rPin, "high", 5, 255, 1);
  156.       RGBLED("fade", rPin, "low", 5, 255, 1);
  157.       RGBLED("fade", gPin, "high", 5, 255, 1);
  158.       RGBLED("fade", gPin, "low", 5, 255, 1);
  159.       RGBLED("fade", bPin, "high", 5, 255, 1);
  160.       RGBLED("fade", bPin, "low", 5, 255, 1);    
  161.     }
  162.   }
  163. }
Add Comment
Please, Sign In to add comment