Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. #define APP_VERSION "1.07"  // verze aplikace
  2.  
  3. #include <Ethernet.h> // standardni knihovna pro W5100
  4. #include <EthernetUdp.h>
  5.  
  6. #include <Time.h>
  7. #include <RTClib.h> // https://github.com/adafruit/RTClib
  8. #include "Adafruit_LEDBackpack.h" // https://github.com/adafruit/Adafruit-LED-Backpack-Library
  9. #include <OneWire.h> // https://github.com/PaulStoffregen/OneWire   nebo http://www.pjrc.com/teensy/td_libs_OneWire.html
  10. #include <DallasTemperature.h> // https://github.com/milesburton/Arduino-Temperature-Control-Library
  11. #include "Tasker.h" // https://github.com/joysfera/arduino-tasker
  12.  
  13. #define _NTP_SYNCHRO_
  14. #define TASKER_MAX_TASKS 8  // maximalni pocet uloh pro tasker
  15. #define MAX_SENSORS_COUNT 5 // maximalni  pocet cidel DS18B20
  16. #define DISPLAY_ADDRESS   0x70 // I2C address of the display.  Stick with the default address of 0x70
  17.  
  18. #define MY_TIME_ZONE 1 * ONE_HOUR
  19. #define MY_DST eu_dst
  20. #define UNIX_EPOCH 2208988800UL
  21.  
  22. byte mac[] = { 0x12, 0x34, 0x56, 0x01, 0x01, 0x04 };
  23.  
  24. EthernetServer server(80);
  25. EthernetUDP udpClient;
  26.  
  27. Tasker tasker(false); // Petruv Tasker pro prepinani uloh
  28.  
  29. bool netReady = false;  // jede ethernet (dostal jsem IP)
  30.  
  31. const char timeServer[] = "ntp.nic.cz";  // NTP server
  32.  
  33. void setup()
  34. {
  35.   Serial.begin(115200);
  36.   Serial.print(F("Verze "));
  37.   Serial.println(F(APP_VERSION));
  38.  
  39.   initEthernet(0);
  40.  
  41.   tasker.setInterval(merTeplotu, 10000);  // mereni teploty
  42.   tasker.setInterval(nastavJas, 5000);  // nastaveni jasu display
  43.   tasker.setInterval(prepniDvojtecku, 500); // prepinani dvojtecky na hodinach kazdych 500 ms
  44. }
  45.  
  46. void loop()
  47. {
  48.     if (netReady)
  49.     {
  50.       EthernetClient client = server.available();
  51.       if (client) web(&client);  // prisel HTTP pozadavek, obslouzim klienta
  52.     }
  53.    
  54.     tasker.loop();
  55. }
  56.  
  57. void initEthernet(int)
  58. {
  59.   if (netReady) return;
  60.    
  61.   Serial.println(F("Ziskani IP z DHCP"));
  62.  
  63.   if (Ethernet.begin(mac) == 1)
  64.   {
  65.     Serial.print(F("IP: "));
  66.     Serial.println(Ethernet.localIP());
  67.     netReady = true;
  68.     setTime();
  69.   }
  70.   else
  71.   {
  72.     chyba(3);
  73.     tasker.setTimeout(initEthernet, 1000 * 60 * 5);  // za 5 minut zkusim znovu ziskat IP
  74.   }
  75. }
  76.  
  77. void web( EthernetClient * client)
  78. {
  79.   Serial.println(F("HTTP Req"));
  80.  
  81.   bool currLineBlank = true;
  82.  
  83.   while (client->connected())
  84.   {
  85.     if (client->available())
  86.     {
  87.       char c = client->read();
  88.  
  89.       if (c == '\n' && currLineBlank)
  90.       {
  91.         client->println(F("HTTP/1.1 200 OK"));
  92.         client->println(F("Access-Control-Allow-Origin: *"));  
  93.         client->println(F("Access-Control-Allow-Methods: GET"));
  94.         client->println(F("Content-Type: application/xml"));
  95.         client->println();
  96.         client->println(F("<?xml version = \"1.0\" encoding=\"UTF-8\"?>"));
  97.         client->print(F("<!--v"));
  98.         client->print(F(APP_VERSION));
  99.         client->println(F("-->"));
  100.         client->print(F("<cidla timestamp='"));
  101.         client->print(millis());
  102.         client->println(F("'>"));
  103.        
  104.         for (byte i = 0; i < sensors.getDeviceCount() ; i++)
  105.         {
  106.           client->print(F("<cidlo typ='DS18B20' adresa='"));
  107.           printDS18B20Address(&sensors, i, client);
  108.           client->print(F("' teplota='"));
  109.           client->print(sensors.getDeviceTemperature(i));
  110.           client->println(F("'>"));
  111.           client->print(F("<teplota>"));
  112.           client->print(sensors.getDeviceTemperature(i),1 );
  113.           client->println(F("</teplota>"));
  114.           client->println(F("</cidlo>"));
  115.         }
  116.        
  117. #if defined(_NTP_SYNCHRO_)
  118.         client->print(F("<ntp lastsynchronization='"));
  119.         client->print(lastSynchronization);
  120.         client->println(F("' />"));
  121. #endif
  122.  
  123.         client->print(F("</cidla>"));
  124.         break;
  125.       }
  126.  
  127.       if (c == '\n')  // you're starting a new line
  128.         currLineBlank = true;
  129.       else if (c != '\r') // you've gotten a character on the current line
  130.         currLineBlank = false;
  131.     }
  132.   }
  133.  
  134.   delay(5); // give the web browser time to receive the data
  135.  
  136.   client->stop();    // close the connection:
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement