// Program: Date and time with RTC DS1302 module // Amendment and adaptation: Arduino and Cia // // Based on the original program Krodal and virtuabotixRTC library #include #include #include #include #include // Data wire is plugged into pin 2 on the Arduino #define ONE_WIRE_BUS 2 // Setup a oneWire instance to communicate with any OneWire devices // (not just Maxim/Dallas temperature ICs) OneWire oneWire(ONE_WIRE_BUS); // Pass our oneWire reference to Dallas Temperature. //DallasTemperature sensors(&oneWire); Servo monServo; #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,0,22 }; // gateway ip address static byte gwip[] = { 192,168,0,1 }; #endif // ethernet mac address - must be unique on your network static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 }; byte Ethernet::buffer[500]; //?????? BufferFiller bfill; //??????? const int onOffPin = 7; const int servo = 9; // Load the virtuabotixRTC library #include // Determine the pins connected to the module // myRTC (clock, data, RST) virtuabotixRTC myRTC (4, 5, 6); void setup () { Serial.begin(9600); // date and time of initial Information // After to set the entire information, comment the following line // (seconds, minutes, hours, day of week, day of month, month, year) //myRTC.setDS1302Time (30, 56, 12, 7, 25, 6, 2016); Serial.println("\n[backSoon]"); pinMode(onOffPin, OUTPUT); 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); // Servo moteur // Attacher la pin 9 à l'objet servo. // ATTN: le code initialise l'angle à 90 degrés par défaut. monServo.attach(servo); // 9 // remettre l'angle à 0 degrés monServo.write( 0 ); // Start up the library Dallas one wire // sensors.begin(); } void imprime_dia_da_semana (int day) { switch (day) { case 1: Serial.print("Dimanche"); break; case 2: Serial.print("Lundi"); break; case 3: Serial.print("Mardi"); break; case 4: Serial.print("Mercredi"); break; case 5: Serial.print("Jeudi"); break; case 6: Serial.print("Vendredi"); break; case 7: Serial.print("Samedi"); break; } } void homePage() { // long t = millis() / 1000; // word h = t / 3600; // byte m = (t / 60) % 60; // byte s = t % 60; // String html = ""; // // int len = html.length() + 1; // char tmp[len]; // //Serial.println(tmp); // html.toCharArray(tmp, len);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" "" // http-equiv='refresh' content='1'/>" "RBBB server" //"" "" //"")); //??????? "")); delay(1); bfill.emit_p(PSTR( "
"), PSTR("ON"), PSTR("OFF")); // bfill.emit_p(PSTR("Go to Google")); //bfill.emit_p(PSTR("

$F

"),23); //sensors.getTempCByIndex(0)); // bfill.emit_p(PSTR("")); // delay(1); // // delay(1); // //bfill = ether.tcpOffset(); // for (int i = 0; i < 3; i++) { //// // word pin = 0; // bfill.emit_p(PSTR(""), // PSTR("TOTO"), PSTR("X"), PSTR("O")); // delay(2); //// // } // bfill.emit_p(PSTR("
$F$F$F
")); bfill.emit_p(PSTR("")); delay(1); } const char okHeader[] PROGMEM = "HTTP/1.0 200 OK\r\n" "Content-Type: text/html\r\n" "Pragma: no-cache\r\n"; const char okCSS[] PROGMEM = "HTTP/1.0 200 OK\r\n" "Content-Type: text/css\r\n" "Pragma: no-cache\r\n"; static void configPage(const char* data) { bfill.emit_p(PSTR("$F\r\n" "

Send a wireless data packet

" "
" "

" "Data bytes (decimal)
" "Destination node " "(1..31, or 0 to broadcast)
" "

" "" "
"), okHeader); } static void cssPage() { const char css[] = "a.button {" "-webkit-appearance: button;" "-moz-appearance: button;" "appearance: button;" " text-decoration: none;" "color: initial;}"; bfill.emit_p(PSTR("$F\r\n$F"), okCSS, css); } static void sendPage(const char* data) { // // pick up submitted data, if present // const char* p = strstr(data, "b="); // byte d = getIntArg(data, "d"); // if (data[6] == '?' && p != 0 && 0 <= d && d <= 31) { // // prepare to send data as soon as possible in loop() // outDest = d & RF12_HDR_MASK ? RF12_HDR_DST | d : 0; // outCount = 0; // // convert the input string to a number of decimal data bytes in outBuf // ++p; // while (*p != 0 && *p != '&') { // outBuf[outCount] = 0; // while ('0' <= *++p && *p <= '9') // outBuf[outCount] = 10 * outBuf[outCount] + (*p - '0'); // ++outCount; // } //#if SERIAL // Serial.print("Send to "); // Serial.print(outDest, DEC); // Serial.print(':'); // for (byte i = 0; i < outCount; ++i) { // Serial.print(' '); // Serial.print(outBuf[i], DEC); // } // Serial.println(); //#endif // // redirect to home page // bfill.emit_p(PSTR( // "HTTP/1.0 302 found\r\n" // "Location: /\r\n" // "\r\n")); // return; // } // else show a send form bfill.emit_p(PSTR("$F\r\n" "

Send a wireless data packet

" "
" "

" "Data bytes (decimal)
" "Destination node " "(1..31, or 0 to broadcast)
" "

" "" "
"), okHeader); } void getTime() { // Reads the information from the CI myRTC.updateTime (); // Print the details in serial monitor Serial.print ("Data "); // Call the routine that prints the day of the week imprime_dia_da_semana (myRTC.dayofweek); Serial.print (", "); Serial.print (myRTC.dayofmonth); Serial.print ("/"); Serial.print (myRTC.month); Serial.print ("/"); Serial.print (myRTC.year); Serial.print (""); Serial.print (" Time "); // Adds a 0 if the time value is <10 if (myRTC.hours <10) { Serial.print ("0"); } Serial.print (myRTC.hours); Serial.print (":"); // Adds a 0 if the value of the minutes is <10 if (myRTC.minutes <10) { Serial.print ("0"); } Serial.print (myRTC.minutes); Serial.print (":"); // Adds a 0 if the value of the latter is <10 if (myRTC.seconds <10) { Serial.print ("0"); } Serial.println (myRTC.seconds); } void loop () { word len = ether.packetReceive(); word pos = ether.packetLoop(len); // check if valid tcp data is received if (pos) { delay(1); // necessary for my system bfill = ether.tcpOffset(); char* data = (char *) Ethernet::buffer + pos; Serial.println(data); // receive buf hasn't been clobbered by reply yet if (strncmp("GET / ", data, 6) == 0) { Serial.println("HomePage"); homePage(); } else if (strncmp("GET /css.css", data, 12) == 0) { Serial.println("CSS"); cssPage(); } else if (strncmp("GET /on", data, 7) == 0) { getTime(); Serial.println("ON"); digitalWrite(onOffPin, LOW); homePage(); // // Servo // // Passer de 0 a 180° par angle de 10 degré // for( int iAngle=0; iAngle<= 360; iAngle+=10 ) // { // monServo.write(iAngle); // delay( 250 ); // } } else if (strncmp("GET /off", data, 8) == 0) { getTime(); Serial.println("OFF"); digitalWrite(onOffPin, HIGH); homePage(); // // Angle décroissant progressif // for( int iAngle = 360; iAngle>=0; iAngle-- ) // { // monServo.write( iAngle ); // delay( 10 ); // } } else if (strncmp("GET /c", data, 6) == 0) { Serial.println("Config"); configPage(data); } else if (strncmp("GET /d", data, 6) == 0) { Serial.println("Data"); sendPage(data); } else { bfill.emit_p(PSTR( "HTTP/1.0 401 Unauthorized\r\n" "Content-Type: text/html\r\n" "\r\n" "

401 Unauthorized

")); } ether.httpServerReply(bfill.position()); // send web page data } // delay (1000); }