Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Present a "Will be back soon web page", as stand-in webserver.
- // 2011-01-30 <[email protected]> http://opensource.org/licenses/mit-license.php
- // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
- #include <Wire.h>
- #include "RTClib.h"
- #include <EtherCard.h>
- #include "DHT.h"
- RTC_DS1307 rtc;
- DHT dht(7, DHT11);
- const byte dhtPowerPin PROGMEM = 8; //dht power pin
- const byte wPin PROGMEM = A0; //power from wall
- const byte iPin PROGMEM = 9; //side button
- const byte rPin PROGMEM = 3; //LED MODULE PINS
- const byte gPin PROGMEM = 6;
- const byte bPin PROGMEM = 5;
- //ethernet interface attributes
- static byte myip[] = { 192,168,1,114 };
- static byte gwip[] = { 192,168,1,1 };
- static byte dnsip[] = { 8,8,8,8 };
- static byte mymac[] = { 0x72,0x69,0x69,0x2D,0x38,0x31 };
- byte Ethernet::buffer[500]; // tcp/ip send and receive buffer
- BufferFiller bfill; //
- char results[6];
- void setup(){
- Serial.begin(9600);
- pinMode(rPin, OUTPUT);
- pinMode(gPin, OUTPUT);
- pinMode(bPin, OUTPUT);
- pinMode(iPin, INPUT_PULLUP);
- pinMode(wPin, INPUT);
- pinMode(dhtPowerPin,OUTPUT); //DHT11 Setup & Start
- digitalWrite(dhtPowerPin, HIGH);
- dht.begin();
- results[0] = { "Thursday 12/12/12 12:12:12 AM" }; //
- if (! rtc.begin()) {
- Serial.println("Couldn't find RTC");
- while (1);
- }
- if (! rtc.isrunning()) {
- Serial.println("RTC is NOT running!");
- rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- }
- Serial.println("\n[backSoon]");
- if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0)
- Serial.println( "Failed to access Ethernet controller");
- Serial.println("\nStatic");
- ether.staticSetup(myip, gwip, dnsip);
- ether.printIp("IP: ", ether.myip);
- ether.printIp("GW: ", ether.gwip);
- ether.printIp("DNS: ", ether.dnsip);
- }
- void RGBLED(String method, int pin, String dir, int t, byte r, byte i) {
- /*
- *RGB Module control
- *method -> fade or blink
- *pin ----> pin of led module
- *dir ----> "high"=bright and "low"=dim
- *t=delay, r=range, i=increment
- *fade call: RGBLED("fade", rPin, "high", 5, 255, 1);
- *blink call: RGBLED("blink", bPin, "high", 500, 125, 0);
- */
- if (method == "fade") {
- if (dir == "high") { //up
- for ( byte bri = 0; bri < r; bri += i ){ analogWrite(pin, bri); delay(t); }
- } else { //"LOW"
- for ( byte bri = r; bri > 0; bri -= i ){ analogWrite(pin, bri); delay(t); }
- analogWrite(pin, 0);
- }
- } else { //"blink"
- analogWrite(pin, r); delay(t);
- }
- }
- int temp;
- int hum;
- void GetTemps() {
- byte fails = 0;
- while (fails < 3) {
- digitalWrite(3, HIGH); // turn on dht module
- delay(2000);
- int h = dht.readHumidity();
- int f = dht.readTemperature(true);
- if (isnan(h) || isnan(f)) {
- Serial.println("Failed to read from DHT sensor!");
- for (byte x = 0; x < 3; x++) { RGBLED("blink", bPin, "high", 250, 255, 0); RGBLED("blink", bPin, "low", 250, 0, 0);}
- fails += 1;
- } else {
- //String comment = "Humidity: " + String(h) + " %\t" + "Temperature: " + String(f) + "\n";
- temp = f; hum = h;
- //digitalWrite(3, LOW); // turn off dht module
- for (byte x = 0; x < 3; x++) { RGBLED("blink", bPin, "high", 250, 255, 0); RGBLED("blink", bPin, "low", 250, 0, 0);}
- delay(1000);
- return;
- }
- }
- temp = "N/A"; hum = "N/A";
- //digitalWrite(3, LOW); // turn off dht module
- return;
- }
- char rtcDay = ""; char rtcDate = "";
- byte rtcHour = 0; byte rtcMinute = 0; byte rtcSecond = 0; char rtcState = "AM";
- void DateAndTime() {
- char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"};
- DateTime now = rtc.now();
- byte hourNow = now.hour();
- String state;
- if ((hourNow >= 12) && (hourNow <= 24)) { state = "PM"; } else { state = "AM"; }
- if ((hourNow == 0) || (hourNow == 24)) { hourNow = 12; } else if (hourNow > 12) { hourNow -= 12; }
- rtcDay = daysOfTheWeek[now.dayOfTheWeek()];
- //rtcDate = char(now.month()) += char(now.day()); // + "/" + char(now.year());
- rtcHour = hourNow;
- rtcMinute = now.minute();
- rtcSecond = now.second();
- //rtcState = state;
- return;
- }
- static word homepage() {
- bfill = ether.tcpOffset();
- bfill.emit_p(PSTR(
- "HTTP/1.0 200 OK\r\n"
- "Content-Type: text/html\r\n"
- "Pragma: no-cache\r\n"
- "\r\n"
- "<meta http-equiv='refresh' content='1'/>"
- "<title>RBBB server</title>"
- "<p>$F $D:$D:$D</p>"), rtcDay, rtcHour, rtcMinute, rtcSecond);
- return bfill.position();
- }
- void loop(){
- // wait for an incoming TCP packet, but ignore its contents
- word len = ether.packetReceive();
- word pos = ether.packetLoop(len);
- if (pos){
- DateAndTime();
- ether.httpServerReply(homepage()); // send web page data
- } else {
- if (! digitalRead(iPin)){
- RGBLED("fade", rPin, "high", 5, 255, 1);
- RGBLED("fade", rPin, "low", 5, 255, 1);
- RGBLED("fade", gPin, "high", 5, 255, 1);
- RGBLED("fade", gPin, "low", 5, 255, 1);
- RGBLED("fade", bPin, "high", 5, 255, 1);
- RGBLED("fade", bPin, "low", 5, 255, 1);
- }
- }
- }
Add Comment
Please, Sign In to add comment