Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #include <TimerOne.h>
- #include <util.h>
- #include <EthernetUdp.h>
- #include <EthernetServer.h>
- #include <EthernetClient.h>
- #include <Ethernet.h>
- #include <Dns.h>
- #include <Dhcp.h>
- #include <SPI.h>
- #include <OneWire.h>
- #include <stdlib.h>
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- IPAddress defaultIp(192,168,1,177);
- IPAddress timeServer(217,31,205,226);
- const int NTP_PACKET_SIZE= 48;
- byte packetBuffer[ NTP_PACKET_SIZE];
- unsigned int localPort = 80; // local port to listen for UDP packets
- byte addr[8];
- byte ow_dev_count=0;
- float temps[9]; //načtené teploty
- byte rele[]={30, 31, 32, 33, 34, 35, 36, 37};
- //bool relePointer[]={false, false, false, false, false, false, false, false};
- byte kotel = 38;
- float user_temps[9][5]; //uživatelem nastavené teploty
- float default_temps[9]; //výchozí teploty
- int startTime[9][5]; //zde se uchovávají časy, kdy mají být dané teploty aktivní
- int endTime[9][5];
- int intTime;
- int hour;
- int minute;
- int second;
- int httpCounter=0;
- int checkTempCounter=0;
- int maintainEthernet=0;
- char getRequest[100];
- bool owMeasure = true;
- OneWire senzory[8]={OneWire(42),OneWire(43),OneWire(44),OneWire(45),OneWire(46),OneWire(47),OneWire(48),OneWire(49)};
- LiquidCrystal lcd(22,23,24,25,26,27);
- // Initialize the Ethernet server library
- // with the IP address and port you want to use
- // (port 80 is default for HTTP):
- EthernetServer server(80);
- EthernetClient client;
- EthernetUDP Udp;
- void setup() {
- // Open serial communications and wait for port to open:
- Serial.begin(9600);
- Timer1.initialize(1000000);
- Timer1.attachInterrupt(interrupt);
- lcd.begin(16,2);
- lcd.clear();
- lcd.noCursor();
- pinMode(kotel,OUTPUT);
- digitalWrite(kotel,LOW);
- for(int i=0;i<=7;i++)
- {
- pinMode(rele[i],OUTPUT);
- digitalWrite(rele[i],LOW);
- }
- // start the Ethernet connection and the server:
- if (Ethernet.begin(mac) == 0) {
- Serial.println("Failed to configure Ethernet using DHCP");
- // no point in carrying on, so do nothing forevermore:
- Ethernet.begin(mac,defaultIp);
- }
- printIpAddress();
- server.begin();
- Serial.print("server is at ");
- Serial.println(Ethernet.localIP());
- Udp.begin(localPort);
- initVariables();
- ow_dev_count = countDev();
- Serial.print(ow_dev_count, DEC);
- Serial.println(" Devices found.");
- getTime();
- }
- void loop() {
- client = server.available();
- if (client) {
- Serial.println("new client");
- // an http request ends with a blank line
- boolean currentLineIsBlank = true;
- httpCounter=0;
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- //Serial.write(c);
- //Serial.write(c);
- if(httpCounter<80)
- {
- getRequest[httpCounter] = c;
- httpCounter++;
- }
- // if you've gotten to the end of the line (received a newline
- // character) and the line is blank, the http request has ended,
- // so you can send a reply
- if (c == '\n' && currentLineIsBlank) {
- // send a standard http response header
- printWeb();
- break;
- }
- if (c == '\n') {
- // you're starting a new line
- currentLineIsBlank = true;
- }
- else if (c != '\r') {
- // you've gotten a character on the current line
- currentLineIsBlank = false;
- }
- }
- }
- // give the web browser time to receive the data
- delay(1);
- // close the connection:
- client.stop();
- Serial.println("client disconnected");
- }
- }
- int countDev()
- {
- byte i=0;
- int devCount=0;
- for(i=0;i<=7;i++)
- {
- if(senzory[i].search(addr)==true)
- {
- Serial.println("Device found.");
- devCount++;
- senzory[i].reset_search();
- }
- else
- {
- break;
- }
- }
- return devCount;
- }
- void initVariables()
- {
- int i,j = 0;
- for(i=0;i<=7;i++)
- {
- temps[i] = 85.0;
- default_temps[i] = 18.0;
- for(j=0;j<=5;j++)
- {
- user_temps[i][j] = 15;
- startTime[i][j] = -100;
- endTime[i][j] = -100;
- }
- }
- user_temps[0][0]=15;
- startTime[0][0]=-100;
- startTime[0][1]=-100;
- default_temps[0]=18;
- }
- void printWeb()
- {
- short int room_num;
- short int temp_num;
- char req_temp[6];
- char req_time[6];
- if(isdigit(getRequest[5])==1)
- {
- room_num = getRequest[5]- '0';
- if(getRequest[7]=='t')
- {
- if(isdigit(getRequest[8]==1))
- {
- temp_num = getRequest[8]- '0';
- }
- for(int i=0;i<=3;i++)
- {
- req_temp[i] = getRequest[10+i];
- }
- user_temps[room_num][temp_num] = atof(req_temp);
- for(int i=0;i<=3;i++)
- {
- req_time[i]=getRequest[15+i];
- }
- req_time[4] = '/n';
- startTime[room_num][temp_num]=atoi(req_time);
- Serial.println(startTime[room_num][temp_num],DEC);
- for(int i=0;i<=3;i++)
- {
- req_time[i]=getRequest[20+i];
- }
- req_time[4] = '/n';
- endTime[room_num][temp_num]=atoi(req_time);
- }
- if(getRequest[7]=='d')
- {
- for(int i=0;i<=3;i++)
- {
- req_temp[i] = getRequest[9+i];
- default_temps[room_num] = atof(req_temp);
- }
- }
- printHeader();
- client.println("<body>");
- client.println("<table align=\"center\" bordercolor=\"black\" border=\"2px\">");
- client.println("<tr><td>Nastavená teplota</td><td>Startovací čas</td><td>Koncový čas</td></tr>");
- for(int i=0;i<=5;i++)
- {
- client.print("<tr><td>");
- client.print(user_temps[room_num][i]);
- client.print(" °C</td><td>");
- client.print(startTime[room_num][i],DEC);
- client.print("</td><td>");
- client.print(endTime[room_num][i],DEC);
- client.println("</td></tr>");
- }
- client.println("</table>");
- client.println("<div align=\"center\">");
- client.print("<h1>Místnost č. ");
- client.println(room_num,DEC);
- client.println("</h1>");
- client.print("<h1>Výchozí teplota: ");
- client.print(default_temps[room_num]);
- client.println("°C</h1>");
- client.print("<h1> Naměřená teplota: ");
- client.print(temps[room_num]);
- client.println(" °C</h1>");
- client.print("<h1>");
- client.print("Timestamp: ");
- client.print(hour,DEC);
- client.print(":");
- if(minute<10)
- {
- client.print("0");
- }
- client.print(minute,DEC);
- client.print(":");
- if(second<10)
- {
- client.print("0");
- }
- client.print(second,DEC);
- client.println("<h1>");
- client.println("</div>");
- client.println("</body>");
- client.println("</html>");
- }
- if(getRequest[5]==' ')
- {
- printHeader();
- client.println("<body>");
- client.print("Počet Nalezených senzorů: ");
- client.println(ow_dev_count,DEC);
- client.println("</body>");
- client.println("</html>");
- }
- if(getRequest[5]=='s')
- {
- ow_dev_count=countDev();
- printHeader();
- client.println("<body>");
- client.print("Počet Nalezených senzorů: ");
- client.println(ow_dev_count,DEC);
- client.println("</body>");
- client.println("</html>");
- }
- }
- void printHeader()
- {
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("Connection: close"); // the connection will be closed after completion of the response
- client.println();
- client.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML4.01//EN\">");
- client.println("<html>");
- client.println("<head>");
- client.print("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
- client.println("</head>");
- }
- unsigned long sendNTPpacket(IPAddress& address)
- {
- memset(packetBuffer, 0, NTP_PACKET_SIZE);
- packetBuffer[0] = 0b11100011;
- packetBuffer[1] = 0;
- packetBuffer[2] = 6;
- packetBuffer[3] = 0xEC;
- packetBuffer[12] = 49;
- packetBuffer[13] = 0x4E;
- packetBuffer[14] = 49;
- packetBuffer[15] = 52;
- Udp.beginPacket(address, 123);
- Udp.write(packetBuffer,NTP_PACKET_SIZE);
- Udp.endPacket();
- }
- void getTime()
- {
- Serial.println("NTP_Time");
- sendNTPpacket(timeServer);
- delay(1000);
- if ( Udp.parsePacket() ) {
- Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
- //the timestamp starts at byte 40 of the received packet and is four bytes,
- // or two words, long. First, esxtract the two words:
- unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
- unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
- // combine the four bytes (two words) into a long integer
- // this is NTP time (seconds since Jan 1 1900):
- unsigned long secsSince1900 = highWord << 16 | lowWord;
- Serial.print("Seconds since Jan 1 1900 = " );
- Serial.println(secsSince1900);
- // now convert NTP time into everyday time:
- Serial.print("Unix time = ");
- // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
- const unsigned long seventyYears = 2208988800UL;
- // subtract seventy years:
- unsigned long epoch = secsSince1900 - seventyYears;
- // print Unix time:
- Serial.println(epoch);
- // print the hour, minute and second:
- Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
- hour = ((epoch % 86400L) / 3600) + 2;
- Serial.print(hour, DEC); // print the hour (86400 equals secs per day)
- minute = (epoch % 3600) / 60;
- Serial.print(':');
- if ( minute < 10 ) {
- // In the first 10 minutes of each hour, we'll want a leading '0'
- Serial.print('0');
- }
- Serial.print(minute, DEC); // print the minute (3600 equals secs per minute)
- second = epoch % 60;
- Serial.print(':');
- if ( second < 10 ) {
- // In the first 10 seconds of each minute, we'll want a leading '0'
- Serial.print('0');
- }
- Serial.println(second, DEC); // print the second
- }
- }
- void interrupt()
- {
- second++;
- checkTempCounter++;
- maintainEthernet++;
- if(second>=60)
- {
- minute++;
- second = 0;
- }
- if(minute>=60)
- {
- hour++;
- minute = 0;
- }
- if(hour>=24)
- {
- hour = 0;
- }
- if(checkTempCounter>=10)
- {
- checkTempCounter=0;
- checkTemp();
- }
- if(maintainEthernet>=600)
- {
- maintainEthernet = 0;
- getTime();
- if(Ethernet.maintain()==2)
- {
- printIpAddress();
- }
- }
- timeToInteger();
- if(owMeasure==true)
- {
- convertTemp();
- owMeasure=false;
- }
- else
- {
- readTemp();
- owMeasure=true;
- }
- }
- void convertTemp()
- {
- for(int i=0;i<ow_dev_count;i++)
- {
- if(senzory[i].reset()==1)
- {
- senzory[i].write(0xCC,0);
- senzory[i].write(0x44,0);
- }
- }
- }
- void readTemp()
- {
- int intTemp;
- float floatTemp;
- for(int i=0;i<ow_dev_count;i++)
- {
- if(senzory[i].reset()==1)
- {
- senzory[i].write(0xCC,0);
- senzory[i].write(0xBE,0);
- intTemp = senzory[i].read();
- intTemp = (senzory[i].read() <<8 ) + intTemp;
- if((intTemp & 0x800)==0x800)
- {
- intTemp=~intTemp;
- intTemp++;
- intTemp = intTemp * -1;
- }
- floatTemp = intTemp;
- floatTemp = floatTemp * 0.0625;
- temps[i] = floatTemp;
- }
- }
- }
- void timeToInteger() //funkce, která konvertuje čas na hodnotu typu integer
- { //např čas 16:25 je zkonvertován na 1625
- int lhour;
- int lmin;
- lhour = hour;
- lmin = minute;
- lhour = lhour*100;
- lhour = lhour + minute;
- intTime = lhour;
- }
- void checkTemp()
- {
- short int i,j;
- short int default_temp_flag=0;
- for(i=0;i<ow_dev_count;i++)
- {
- default_temp_flag=0;
- for(j=0;j<=5;j++)
- {
- if(startTime[i][j]>=0 && endTime[i][j]>=0)
- {
- if((intTime>=startTime[i][j]) && (intTime<=endTime[i][j]))
- {
- default_temp_flag=1;
- if(temps[i]>user_temps[i][j]+0.2)
- {
- digitalWrite(rele[i],LOW);
- }
- if(temps[i]<user_temps[i][j]-0.2)
- {
- digitalWrite(rele[i],HIGH);
- digitalWrite(kotel,HIGH);
- }
- }
- }
- }
- if(default_temp_flag == 0)
- {
- if(temps[i]>default_temps[i]+0.2)
- {
- digitalWrite(rele[i],LOW);
- }
- if(temps[i]<default_temps[i]-0.2)
- {
- digitalWrite(rele[i],HIGH);
- }
- }
- }
- if((digitalRead(rele[0])==LOW) && (digitalRead(rele[1])==LOW) && (digitalRead(rele[2])==LOW) && (digitalRead(rele[3])==LOW) && (digitalRead(rele[4])==LOW) && (digitalRead(rele[5])==LOW) && (digitalRead(rele[6])==LOW) && (digitalRead(rele[7])==LOW))
- {
- digitalWrite(kotel,LOW);
- }
- else
- {
- digitalWrite(kotel,HIGH);
- }
- }
- void printIpAddress()
- {
- lcd.print("IP adresa:");
- lcd.setCursor(0,1);
- lcd.print(Ethernet.localIP());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement