Hello, I’ve got a problem with my project. I want to build timer with 2 relays controlled and programmed over the LAN. Now I’ve got clock with LED display and I can switch on/off 2 relays from web page. I can also switch on/off relays at chosen date and time by using 5 variables (int miesiac, int dzien etc.) but it can be done only from sketch. I don’t know how what to do to change those variables from the web page. Is it even possible with my hardware? - Arduino Uno, Mini ENC28J60, DS1307RTC, TM1637 and 2 relays module.
#include <EtherCard.h>
#include <TM1637.h>
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#define CLK 9
#define DIO 7
int RelayPins[] = {
2,3,4,5};
int clockblink = LOW;
int checktime = 01;
//deklaracje zmiennych dla pierwszego przekaźnika
int relay1 = HIGH;
int miesiac = 4;
int dzien = 11;
int minuta = 4;
int godzina = 23;
int rok = 2018;
//deklaracje zmiennych dla drugiego przekaźnika
int relay2 = HIGH;
int miesiac2 = 4;
int dzien2 = 11;
int minuta2 = 5;
int godzina2 = 23;
int rok2 = 2018;
unsigned long previousMillis = 0;
const long interval = 1000;
unsigned long previousMillis2 = 0;
const long interval2 = 900;
boolean PinStatus[] = {
1,2,3,4};
// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[500]; // tcp/ip send and receive buffer
BufferFiller bfill;
//-------------
const char http_OK[] PROGMEM =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"Pragma: no-cache\r\n\r\n";
const char http_Found[] PROGMEM =
"HTTP/1.0 302 Found\r\n"
"Location: /\r\n\r\n";
const char http_Unauthorized[] PROGMEM =
"HTTP/1.0 401 Unauthorized\r\n"
"Content-Type: text/html\r\n\r\n"
"<h1>401 Unauthorized</h1>";
//------------
void homePage()
{
bfill.emit_p(PSTR("$F"
"<meta charset=\"utf-8\">"
"<title>ArduinoPIN Webserver</title>"
"Gniazdo 1: <a href=\"?ArduinoPIN1=$F\">$F</a>
"
"Gniazdo 2: <a href=\"?ArduinoPIN2=$F\">$F</a>
"),
http_OK,
PinStatus[1]?PSTR("off"):PSTR("on"),
PinStatus[1]?PSTR("<font color=\"green\"><b>WŁĄCZ</b></font>"):PSTR("<font color=\"red\">WYŁĄCZ</font>"),
PinStatus[2]?PSTR("off"):PSTR("on"),
PinStatus[2]?PSTR("<font color=\"green\"><b>WŁĄCZ</b></font>"):PSTR("<font color=\"red\">WYŁĄCZ</font>"));
}
//------------------------
//void Page1()
//{
//}
//------------------------
TM1637 Display1(CLK, DIO);
int8_t Cyfry[] = {0,1,2,3};
int godziny;
int minuty;
void setup() {
Serial.begin(9600);
while (!Serial) ; // wait for serial
delay(200);
Serial.println("DS1307RTC Read Test");
Serial.println("-------------------");
Display1.set();
Display1.init();
checktime = 01;
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0);
if (!ether.dhcpSetup());
ether.printIp("My Router IP: ", ether.myip);
// ethernet interface ip address
static byte myip[] = { 192,168,1,200 };
// gateway ip address
static byte gwip[] = { 192,168,1,1 };
ether.staticSetup(myip);
ether.printIp("My SET IP: ", ether.myip);
//-----
for(int i = 0; i <= 4; i++)
{
pinMode(RelayPins[i],OUTPUT);
digitalWrite (RelayPins[i],HIGH);
PinStatus[i]=false;
}
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
void loop() {
tmElements_t tm;
//miganie dwukropkiem na wyświetlaczu zegara
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (clockblink == LOW) {
clockblink = HIGH;
Display1.point(POINT_ON);
} else {
clockblink = LOW;
Display1.point(POINT_OFF);
}
}
if (RTC.read(tm)) {
//wyświetlanie w monitorze portu szeregowego aktulenego czasu i daty gdy sekundy w RTC = 00
unsigned long currentMillis2 = millis();
if (currentMillis2 - previousMillis2 >= interval2) {
previousMillis2 = currentMillis2;
checktime = tm.Second;
if (checktime == 00) {
Serial.print("Time = ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();
//warunek dla programowania załączenia pierwszego przekaźnika
if (tm.Minute == minuta && tm.Hour == godzina && tm.Day == dzien && tm.Month == miesiac) {
digitalWrite (2,relay1);
}
//warunek dla programowania załączenia drugiego przekaźnika
if (tm.Minute == minuta2 && tm.Hour == godzina2 && tm.Day == dzien2 && tm.Month == miesiac) {
digitalWrite (3,relay2);
}
}
checktime = 01;
}
godziny = tm.Hour;
minuty = tm.Minute;
ObliczCyfry(godziny, minuty);
strona();
} else {
if (RTC.chipPresent()) {
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
} else {
Serial.println("DS1307 read error! Please check the circuitry.");
Serial.println();
}
delay(9000);
}
}
void ObliczCyfry(int godz, int minu)
{
int8_t Digit0 = minu %10 ;
int8_t Digit1 = (minu % 100) / 10 ;
int8_t Digit2 = godz % 10 ;
int8_t Digit3 = (godz % 100) / 10 ;
Cyfry[3] = Digit0 ;
Cyfry[2] = Digit1 ;
Cyfry[1] = Digit2 ;
Cyfry[0] = Digit3 ;
Display1.display(Cyfry);
}
void strona ()
{
word len = ether.packetReceive(); // check for ethernet packet
word pos = ether.packetLoop(len); // check for tcp packet
if (pos) {
bfill = ether.tcpOffset();
char *data = (char *) Ethernet::buffer + pos;
if (strncmp("GET /", data, 5) != 0) {
bfill.emit_p(http_Unauthorized);
}
else {
data += 5;
if (data[0] == ' ') {
homePage(); // Return home page
for (int i = 0; i <= 3; i++)digitalWrite(RelayPins[i],!PinStatus[i+1]);
}
// "16" = "?ArduinoPIN1=on ".
else if (strncmp("?ArduinoPIN1=on ", data, 16) == 0) {
PinStatus[1] = true;
bfill.emit_p(http_Found);
}
else if (strncmp("?ArduinoPIN2=on ", data, 16) == 0) {
PinStatus[2] = true;
bfill.emit_p(http_Found);
}
//------------------------------------------------------
else if (strncmp("?ArduinoPIN1=off ", data, 17) == 0) {
PinStatus[1] = false;
bfill.emit_p(http_Found);
}
else if (strncmp("?ArduinoPIN2=off ", data, 17) == 0) {
PinStatus[2] = false;
bfill.emit_p(http_Found);
}
//---------------------------
else {
// Page not found
bfill.emit_p(http_Unauthorized);
}
}
ether.httpServerReply(bfill.position()); // send http response
}
}