KK2002

Internetowy Termometr

Apr 5th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. //#define wyswietlacz
  2. #define zegar
  3.  
  4. #ifdef zegar
  5. #include <Wire.h>
  6. #include <DS3231.h>
  7. #endif
  8.  
  9. #ifdef wyswietlacz
  10. #include <SPI.h>
  11. #include <Adafruit_GFX.h>
  12. #include <Adafruit_PCD8544.h>
  13. #endif
  14.  
  15. #include <EtherCard.h>
  16. #include "DHT.h"
  17.  
  18.  
  19. #define DHTPIN A2
  20.  
  21. #define DHTTYPE DHT11
  22.  
  23. DHT dht(DHTPIN, DHTTYPE);
  24.  
  25. static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
  26. static byte myip[] = { 192,168,0,254 };
  27. static byte gwip[] = { 192,168,0,1 };
  28. byte Ethernet::buffer[500];
  29. BufferFiller bfill;
  30.  
  31. #ifdef wyswietlacz
  32. Adafruit_PCD8544 display = Adafruit_PCD8544(2, 3, 4, 6, 5);
  33. #endif
  34.  
  35. #ifdef zegar
  36. DS3231 clock;
  37. RTCDateTime dt;
  38. #endif
  39.  
  40. int temp = 0;
  41. int wilg = 0;
  42. int tempF = 0;
  43. int Timer= 0;
  44.  
  45. #ifdef wyswietlacz
  46. boolean change = false;
  47. int Ctime = 0;
  48. #endif
  49.  
  50. #ifdef zegar
  51. unsigned char godz = 0;
  52. unsigned char minu = 0;
  53. #endif
  54.  
  55. void setup ()
  56. {
  57. #ifdef zegar
  58.   clock.begin();
  59. #endif
  60.   if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0)
  61.   {
  62.     Serial.println("Brak polaczenia");
  63.   }
  64.   ether.staticSetup(myip, gwip);
  65.   dht.begin();
  66.  
  67. #ifdef wyswietlacz
  68.   display.begin();
  69.   display.setContrast(70);
  70.   display.clearDisplay();
  71. #endif
  72.  
  73. }
  74.  
  75.  
  76. static void OdczytDanych()
  77. {
  78. #ifdef zegar
  79.   dt = clock.getDateTime();
  80.   wilg = dht.readHumidity();
  81.   godz = dt.hour;
  82.   minu = dt.minute;
  83. #endif
  84.  
  85.   temp = dht.readTemperature();
  86.   tempF = dht.readTemperature(true);
  87. }
  88. #ifdef zegar
  89. static word homePage()
  90. {
  91.   bfill = ether.tcpOffset();
  92.   bfill.emit_p(PSTR(
  93. "HTTP/1.0 200 OK\r\n"
  94. "Content-Type: text/html\r\n"
  95. "\r\n"
  96. "<html>"
  97.   "<head>"
  98.   "<meta http-equiv=Content-Type content=text/html; charset=UTF-8 />"
  99.   "<title>"
  100.     "Serwer Karola"
  101.   "</title></head>"
  102.   "<body style=background-color:#B5FFFF;text-align:CENTER;font-family:cursive>"
  103.     "<h1 style=font-size:35px>Serwer</h1>"
  104.       "<p style=font-size:25px;>Temperatura: <a style=color:red>$D°C $D°F</a></p>"
  105.       "<p style=font-size:25px;>Wilgotność: <a style=color:blue>$D%</a></p>"
  106.       "<p style=font-size:25px;>Czas: <a style=color:green>$D:$D</a></p>"
  107.     "</body>"),
  108.   temp, tempF, wilg, godz, minu);
  109.   return bfill.position();
  110. }
  111. #else
  112. static word homePage()
  113. {
  114.   bfill = ether.tcpOffset();
  115.   bfill.emit_p(PSTR(
  116. "HTTP/1.0 200 OK\r\n"
  117. "Content-Type: text/html\r\n"
  118. "\r\n"
  119. "<html>"
  120.   "<head>"
  121.   "<meta http-equiv=Content-Type content=text/html; charset=UTF-8 />"
  122.   "<title>"
  123.     "Serwer Karola"
  124.   "</title></head>"
  125.   "<body style=background-color:#B5FFFF;text-align:CENTER;font-family:cursive>"
  126.     "<h1 style=font-size:35px>Serwer</h1>"
  127.       "<p style=font-size:25px;>Temperatura: <a style=color:red>$D°C $D°F</a></p>"
  128.       "<p style=font-size:25px;>Wilgotność: <a style=color:blue>$D%</a></p>"
  129.     "</body>"),
  130.   temp, tempF, wilg);
  131.   return bfill.position();
  132. }
  133. #endif
  134.  
  135. void loop ()
  136. {
  137. #ifdef wyswietlacz
  138.    display.setRotation(2);  // rotate 180 degrees counter clockwise, can also use values of 2 and 3 to go further.
  139.    display.setTextSize(1);
  140.    display.setTextColor(BLACK);
  141.    display.setCursor(3,3);
  142.    display.println("TEMPE-");
  143.    display.setCursor(4,12);
  144.    display.println("RATURA");
  145.    display.setCursor(47,3);
  146.    display.println("WILGO-");
  147.    display.setCursor(47,12);
  148.    display.println("TNOSC");
  149.    display.drawLine(42, 0, 42, 48, BLACK);
  150.    display.setTextSize(2);
  151.    display.setCursor(4,28);
  152.    if (Ctime >= 20)
  153.    {
  154.      change = !change;
  155.      Ctime = 0;
  156.    }
  157.    Ctime = Ctime + 1;
  158.    if (change == false)
  159.    {
  160.      display.print(tempF);
  161.      display.print("F");
  162.    }
  163.    else
  164.    {
  165.      display.print(temp);
  166.      display.print("C");
  167.    }
  168.    display.setCursor(47,28);
  169.    display.print(wilg);
  170.    display.print("%");
  171.    display.display();
  172.    delay(1000);
  173. #endif
  174.    
  175.    Timer  = Timer+1;
  176.    if(Timer==1)
  177.    {
  178.     OdczytDanych();
  179.    }
  180.    if(Timer==1100)
  181.    {
  182.     Timer=0;
  183.    }
  184.  
  185.   word len = ether.packetReceive();
  186.   word pos = ether.packetLoop(len);
  187.  
  188.   if (pos)
  189.   {
  190.     OdczytDanych();
  191.     ether.httpServerReply(homePage());
  192. #ifdef wyswietlacz
  193.   display.clearDisplay();
  194. #endif
  195.   }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment