Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h> //kniznica SPI
- #include <Ethernet.h> //kniznica k ethernet shieldu
- #include <OneWire.h> //Onewire kniznica
- #include <DallasTemperature.h> //knižnica senzorov DS18B20
- #define ONE_WIRE_BUS 8 //definovany pin OneWire zbernice
- OneWire oneWire(ONE_WIRE_BUS); //inicializacia pinu
- DallasTemperature sensors(&oneWire); //priradenie ds18b20 senzorov na onewire zbernicu
- const int relechladenie = 6; //rele chladenia
- const int relekurenie = 7; //rele kurenia
- const int dazdovysenzor = A0; //analog pin pre dazdovy senzor
- const int releokruh1 = 5; //rele okruhu1
- const int releokruh2 = 4; //rele okruhu2
- const int releokruh3 = 3; //rele okruhu3
- const int releokruh4 = 2; //rele okruhu4
- byte mac[] = { 0x20, 0x1A, 0x06, 0x75, 0x8C, 0xAA };
- char server[] = "www.anemonka.php5.sk";
- IPAddress dnServer(192, 168, 0, 1);
- IPAddress gateway(192, 168, 0, 1);
- IPAddress subnet(255, 255, 255, 0);
- IPAddress ip(192, 168, 0, 45);
- EthernetClient client;
- String readString;
- int x=0;
- char lf=10;
- int pocitadlo=0;
- void setup() {
- pinMode(dazdovysenzor, INPUT);
- pinMode(relechladenie, OUTPUT);
- pinMode(relekurenie, OUTPUT);
- pinMode(releokruh1, OUTPUT);
- pinMode(releokruh2, OUTPUT);
- pinMode(releokruh3, OUTPUT);
- pinMode(releokruh4, OUTPUT);
- Serial.begin(9600);
- if (Ethernet.begin(mac) == 0) { //V PRIPADE ZLYHANIA NASTAVENIA DHCP
- Serial.println("Chyba konfiguracie, manualne nastavenie");
- Ethernet.begin(mac, ip, dnServer, gateway, subnet);
- }
- }
- void odosli_data(){
- sensors.requestTemperatures();
- delay(1000);
- int dazd = analogRead(dazdovysenzor);
- client.stop();
- if (client.connect(server, 80)) {
- Serial.println("Pripojenie uspesne na webserver - odosielam teploty");
- client.print("GET /system/arduino/data.php?teplota=");
- client.print(sensors.getTempCByIndex(0));
- client.print("&dazd=");
- client.print(dazd);
- client.println(" HTTP/1.1");
- client.println("Host: www.anemonka.php5.sk");
- client.println("Connection: close");
- client.println();
- client.stop();
- Serial.println("Teploty odoslane na web");
- pocitadlo=0;
- } else {
- Serial.println("Pripojenie zlyhalo - teploty neodoslane");
- pocitadlo++;
- }
- }
- void aktualizuj_vystupy(){
- client.stop();
- if (client.connect(server, 80)) {
- Serial.println("Pripojenie uspesne na webserver - vykurovanie chladenie");
- client.print("GET /system/vykurovanie_chladenie.php");
- client.println(" HTTP/1.1");
- client.println("Host: www.anemonka.php5.sk");
- client.println("Connection: close");
- client.println();
- client.stop();
- Serial.println("Uspesne aktualizacia chladenia a vykurovania z internetu");
- pocitadlo=0;
- } else {
- Serial.println("Pripojenie zlyhalo - chladenie a vykurovanie");
- pocitadlo++;
- }
- }
- void aktualizuj_okruhy(){
- client.stop();
- if (client.connect(server, 80)) {
- Serial.println("Pripojenie uspesne na webserver - aktualizacia okruhov");
- client.print("GET /system/okruhy.php");
- client.println(" HTTP/1.1");
- client.println("Host: www.anemonka.php5.sk");
- client.println("Connection: close");
- client.println();
- client.stop();
- Serial.println("Uspesne aktualizacia okruhov z internetu");
- pocitadlo=0;
- } else {
- Serial.println("Pripojenie zlyhalo - aktualizacia okruhov");
- pocitadlo++;
- }
- }
- void vystup_okruh1(){
- client.stop();
- if (client.connect(server, 80)){
- client.print("GET /system/values/stavokruh1.txt");
- client.println(" HTTP/1.1");
- client.println("Host: www.anemonka.php5.sk"); // ADRESA HOSTA, NA KTOREHO BOL MIERENY REQUEST (NIE PHP SUBOR)
- client.println("Connection: close"); //UKONCENIE PRIPOJENIA ZA HTTP HLAVICKOU
- client.println();
- while(client.connected() && !client.available())
- while (client.connected() || client.available()) { //pramenna
- char c = client.read(); //dostan bity z buffera
- if (c==lf) x=(x+1); //pocitaj
- else if (x==12) readString += c; //nasa premenna
- }
- if(readString=="ZAP"){
- digitalWrite(releokruh1, LOW);
- Serial.println("Zapinam okruh1");
- }else if(readString=="VYP"){
- digitalWrite(releokruh1, HIGH);
- Serial.println("Vypinam okruh1");
- }
- client.stop(); //ukonc spojenie
- readString = ("");
- x=0;
- }else{
- Serial.println("Pripojenie neuspesne pre stav okruhu1");
- }
- }
- void vystup_okruh2(){
- client.stop();
- if (client.connect(server, 80)){
- client.print("GET /system/values/stavokruh2.txt");
- client.println(" HTTP/1.1");
- client.println("Host: www.anemonka.php5.sk");
- client.println("Connection: close");
- client.println();
- while(client.connected() && !client.available())
- while (client.connected() || client.available()) { //pramenna
- char c = client.read(); //dostan bity z buffera
- if (c==lf) x=(x+1); //pocitaj
- else if (x==12) readString += c; //nasa premenna
- }
- if(readString=="ZAP"){
- digitalWrite(releokruh2, LOW);
- Serial.println("Zapinam okruh2");
- }else if(readString=="VYP"){
- digitalWrite(releokruh2, HIGH);
- Serial.println("Vypinam okruh2");
- }
- client.stop(); //ukonc spojenie
- readString = ("");
- x=0;
- }else{
- Serial.println("Pripojenie neuspesne pre stav okruhu2");
- }
- }
- void vystup_okruh3(){
- client.stop();
- if (client.connect(server, 80)){
- client.print("GET /system/values/stavokruh3.txt");
- client.println(" HTTP/1.1");
- client.println("Host: www.anemonka.php5.sk");
- client.println("Connection: close");
- client.println();
- while(client.connected() && !client.available())
- while (client.connected() || client.available()) { //pramenna
- char c = client.read(); //dostan bity z buffera
- if (c==lf) x=(x+1); //pocitaj
- else if (x==12) readString += c; //nasa premenna
- }
- if(readString=="ZAP"){
- digitalWrite(releokruh3, LOW);
- Serial.println("Zapinam okruh3");
- }else if(readString=="VYP"){
- digitalWrite(releokruh3, HIGH);
- Serial.println("Vypinam okruh3");
- }
- client.stop(); //ukonc spojenie
- readString = ("");
- x=0;
- }else{
- Serial.println("Pripojenie neuspesne pre stav okruhu3");
- }
- }
- void vystup_okruh4(){
- client.stop();
- if (client.connect(server, 80)){
- client.print("GET /system/values/stavokruh4.txt");
- client.println(" HTTP/1.1");
- client.println("Host: www.anemonka.php5.sk");
- client.println("Connection: close");
- client.println();
- while(client.connected() && !client.available())
- while (client.connected() || client.available()) { //pramenna
- char c = client.read(); //dostan bity z buffera
- if (c==lf) x=(x+1); //pocitaj
- else if (x==12) readString += c; //nasa premenna
- }
- if(readString=="ZAP"){
- digitalWrite(releokruh4, LOW);
- Serial.println("Zapinam okruh4");
- }else if(readString=="VYP"){
- digitalWrite(releokruh4, HIGH);
- Serial.println("Vypinam okruh4");
- }
- client.stop(); //ukonc spojenie
- readString = ("");
- x=0;
- }else{
- Serial.println("Pripojenie neuspesne pre stav okruhu4");
- }
- }
- void vystup_chladenia(){
- client.stop();
- if (client.connect(server, 80)){
- client.print("GET /system/values/stavchladenie.txt");
- client.println(" HTTP/1.1");
- client.println("Host: www.anemonka.php5.sk"); // ADRESA HOSTA, NA KTOREHO BOL MIERENY REQUEST (NIE PHP SUBOR)
- client.println("Connection: close"); //UKONCENIE PRIPOJENIA ZA HTTP HLAVICKOU
- client.println();
- while(client.connected() && !client.available())
- while (client.connected() || client.available()) { //pramenna
- char c = client.read(); //dostan bity z buffera
- if (c==lf) x=(x+1); //pocitaj
- else if (x==12) readString += c; //nasa premenna
- }
- if(readString=="ZAP"){
- digitalWrite(relechladenie, LOW);
- Serial.println("Zapinam chladenie");
- }else if(readString=="VYP"){
- digitalWrite(relechladenie, HIGH);
- Serial.println("Vypinam chladenie");
- }
- client.stop(); //ukonc spojenie
- readString = ("");
- x=0;
- }else{
- Serial.println("Neuspesne pripojenie pre rele chaladenia");
- }
- }
- void softReset(){
- asm volatile (" jmp 0");
- }
- void kontrola_resetu(){
- client.stop();
- if (client.connect(server, 80)){
- client.println("GET /system/values/reset.txt");
- client.println(" HTTP/1.1");
- client.println("Host: www.anemonka.php5.sk"); // ADRESA HOSTA, NA KTOREHO BOL MIERENY REQUEST (NIE PHP SUBOR)
- client.println("Connection: close"); //UKONCENIE PRIPOJENIA ZA HTTP HLAVICKOU
- client.println();
- while(client.connected() && !client.available())
- while (client.connected() || client.available()) { //pramenna
- char c = client.read(); //dostan bity z buffera
- if (c==lf) x=(x+1); //pocitaj
- else if (x==12) readString += c; //nasa premenna
- }
- if(readString=="RST"){
- Serial.println("RESET VYZIADANY");
- readString = ("");
- x=0;
- client.stop();
- if (client.connect(server, 80)) {
- Serial.println("Pripojenie uspesne na webserver - restartujem");
- client.print("GET /system/arduino/potvrdreset.php");
- client.println(" HTTP/1.1");
- client.println("Host: www.anemonka.php5.sk");
- client.println("Connection: close");
- client.println();
- client.stop();
- Serial.println("RESET VYKONANY");
- softReset();
- } else {
- Serial.println("Pripojenie pre reset zlyhalo");
- pocitadlo++;
- }
- }
- }else{
- Serial.println("Pripojenie neuspesne pre reset");
- }
- }
- void vystup_kurenia(){
- client.stop();
- if (client.connect(server, 80)){
- client.print("GET /system/values/stavkurenie.txt");
- client.println(" HTTP/1.1");
- client.println("Host: www.anemonka.php5.sk"); // ADRESA HOSTA, NA KTOREHO BOL MIERENY REQUEST (NIE PHP SUBOR)
- client.println("Connection: close"); //UKONCENIE PRIPOJENIA ZA HTTP HLAVICKOU
- client.println();
- while(client.connected() && !client.available())
- while (client.connected() || client.available()) { //pramenna
- char c = client.read(); //dostan bity z buffera
- Serial.print(c);
- if (c==lf) x=(x+1); //pocitaj
- else if (x==12) readString += c; //nasa premenna
- }
- if(readString=="ZAP"){
- digitalWrite(relekurenie, LOW);
- Serial.println("Zapinam kurenie");
- }else if(readString=="VYP"){
- digitalWrite(relekurenie, HIGH);
- Serial.println("Vypinam kurenie");
- }
- client.stop(); //ukonc spojenie
- readString = ("");
- x=0;
- }else{
- Serial.println("Neuspesne pripojenie pre rele kurenia");
- }
- }
- void loop() {
- if (Ethernet.begin(mac) == 0) {
- Serial.println("Chyba konfiguracie, manualne nastavenie");
- Ethernet.begin(mac, ip, dnServer, gateway, subnet);
- }
- if(pocitadlo>=10){
- Serial.println("Vysoka chybovost, vypinam relatka");
- digitalWrite(relekurenie, HIGH);
- digitalWrite(relechladenie, HIGH);
- digitalWrite(releokruh1, HIGH);
- digitalWrite(releokruh2, HIGH);
- digitalWrite(releokruh3, HIGH);
- digitalWrite(releokruh4, HIGH);
- delay(120000);
- pocitadlo=0;
- }else{
- delay(500);
- odosli_data();
- delay(500);
- aktualizuj_vystupy();
- delay(1000);
- vystup_kurenia();
- delay(1000);
- vystup_chladenia();
- delay(500);
- aktualizuj_okruhy();
- delay(500);
- vystup_okruh1();
- delay(500);
- vystup_okruh2();
- delay(500);
- vystup_okruh3();
- delay(500);
- vystup_okruh4();
- delay(500);
- kontrola_resetu();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement