Advertisement
Guest User

aaaa

a guest
Dec 1st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.11 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <SimpleDHT.h>
  5. #include <OneWire.h>
  6. #include <DallasTemperature.h>
  7.  
  8. const char* ssid = "Gesanet_Wyremb";
  9. const char* password = "wyrembowski870316";
  10. ESP8266WebServer server(80);
  11.  
  12. int praca = 0;
  13. float temp = 0;
  14. float wilg = 0;
  15. float nastawionatemp = 50;
  16. int przekaznik = 16;
  17.  
  18. #define ONE_WIRE_BUS 0
  19. OneWire oneWire(ONE_WIRE_BUS);
  20. DallasTemperature sensors(&oneWire);
  21.  
  22. /*
  23. Demonstration Code  Driving LCD 5110 CMOS Driven Display by Arduino
  24.  
  25. RST--------- reset
  26. CE---------- chip selection
  27. DC---------- data/commands choice
  28. DIN--------- serial data line
  29. CLK--------- serial Clock Speed
  30. 3.3V-------- VCC
  31. LIGHT------- back light control terminal
  32. GND--------- power negative
  33. */
  34.  
  35. #define PIN_SCE   12 //pin GPIO12 ON ESP , pin SCE ON LCD // D6
  36. #define PIN_RESET 2 //pin GPI02 on ESP , pin RST on LCD // D4
  37. #define PIN_DC    14 //pin GPIO14 on ESP , pin DC on LCD // D5
  38. #define PIN_SDIN  4 //pin GPIO4 on ESP , pin SDIN on LCD // D2
  39. #define PIN_SCLK 5 //pin GPIO5 on ESP  , pin SLCK on LCD // D1
  40.  
  41. #define LCD_C     LOW
  42. #define LCD_D     HIGH
  43. int count=0;
  44. char dat[4];
  45. char disp_tab[]={'0','1','2','3','4','5','6','7','8','9'};
  46. #define LCD_X     84
  47. #define LCD_Y     48
  48. char buf[72]="A_simple_example_of_interfacing_with_the_84_x_48_pixel_Nokia_3310_LCD!!";
  49. static const byte ASCII[][5] =
  50. {
  51.  {0x00, 0x00, 0x00, 0x00, 0x00} //Char 20  
  52. ,{0x00, 0x00, 0x5f, 0x00, 0x00} //Char 21 !
  53. ,{0x00, 0x07, 0x00, 0x07, 0x00} //Char 22 "
  54. ,{0x14, 0x7f, 0x14, 0x7f, 0x14} //Char 23 #
  55. ,{0x24, 0x2a, 0x7f, 0x2a, 0x12} //Char 24 $
  56. ,{0x23, 0x13, 0x08, 0x64, 0x62} //Char 25 %
  57. ,{0x36, 0x49, 0x55, 0x22, 0x50} //Char 26 &
  58. ,{0x00, 0x05, 0x03, 0x00, 0x00} //Char 27 '
  59. ,{0x00, 0x1c, 0x22, 0x41, 0x00} //Char 28 (
  60. ,{0x00, 0x41, 0x22, 0x1c, 0x00} //Char 29 )
  61. ,{0x14, 0x08, 0x3e, 0x08, 0x14} //Char 2a *
  62. ,{0x08, 0x08, 0x3e, 0x08, 0x08} //Char 2b +
  63. ,{0x00, 0x50, 0x30, 0x00, 0x00} //Char 2c ,
  64. ,{0x08, 0x08, 0x08, 0x08, 0x08} //Char 2d -
  65. ,{0x00, 0x60, 0x60, 0x00, 0x00} //Char 2e .
  66. ,{0x20, 0x10, 0x08, 0x04, 0x02} //Char 2f /
  67. ,{0x3e, 0x51, 0x49, 0x45, 0x3e} //Char 30 0
  68. ,{0x00, 0x42, 0x7f, 0x40, 0x00} //Char 31 1
  69. ,{0x42, 0x61, 0x51, 0x49, 0x46} //Char 32 2
  70. ,{0x21, 0x41, 0x45, 0x4b, 0x31} //Char 33 3
  71. ,{0x18, 0x14, 0x12, 0x7f, 0x10} //Char 34 4
  72. ,{0x27, 0x45, 0x45, 0x45, 0x39} //Char 35 5
  73. ,{0x3c, 0x4a, 0x49, 0x49, 0x30} //Char 36 6
  74. ,{0x01, 0x71, 0x09, 0x05, 0x03} //Char 37 7
  75. ,{0x36, 0x49, 0x49, 0x49, 0x36} //Char 38 8
  76. ,{0x06, 0x49, 0x49, 0x29, 0x1e} //Char 39 9
  77. ,{0x00, 0x36, 0x36, 0x00, 0x00} //Char 3a :
  78. ,{0x00, 0x56, 0x36, 0x00, 0x00} //Char 3b ;
  79. ,{0x08, 0x14, 0x22, 0x41, 0x00} //Char 3c <
  80. ,{0x14, 0x14, 0x14, 0x14, 0x14} //Char 3d =
  81. ,{0x00, 0x41, 0x22, 0x14, 0x08} //Char 3e >
  82. ,{0x02, 0x01, 0x51, 0x09, 0x06} //Char 3f ?
  83. ,{0x32, 0x49, 0x79, 0x41, 0x3e} //Char 40 @
  84. ,{0x7e, 0x11, 0x11, 0x11, 0x7e} //Char 41 A
  85. ,{0x7f, 0x49, 0x49, 0x49, 0x36} //Char 42 B
  86. ,{0x3e, 0x41, 0x41, 0x41, 0x22} //Char 43 C
  87. ,{0x7f, 0x41, 0x41, 0x22, 0x1c} //Char 44 D
  88. ,{0x7f, 0x49, 0x49, 0x49, 0x41} //Char 45 E
  89. ,{0x7f, 0x09, 0x09, 0x09, 0x01} //Char 46 F
  90. ,{0x3e, 0x41, 0x49, 0x49, 0x7a} //Char 47 G
  91. ,{0x7f, 0x08, 0x08, 0x08, 0x7f} //Char 48 H
  92. ,{0x00, 0x41, 0x7f, 0x41, 0x00} //Char 49 I
  93. ,{0x20, 0x40, 0x41, 0x3f, 0x01} //Char 4a J
  94. ,{0x7f, 0x08, 0x14, 0x22, 0x41} //Char 4b K
  95. ,{0x7f, 0x40, 0x40, 0x40, 0x40} //Char 4c L
  96. ,{0x7f, 0x02, 0x0c, 0x02, 0x7f} //Char 4d M
  97. ,{0x7f, 0x04, 0x08, 0x10, 0x7f} //Char 4e N
  98. ,{0x3e, 0x41, 0x41, 0x41, 0x3e} //Char 4f O
  99. ,{0x7f, 0x09, 0x09, 0x09, 0x06} //Char 50 P
  100. ,{0x3e, 0x41, 0x51, 0x21, 0x5e} //Char 51 Q
  101. ,{0x7f, 0x09, 0x19, 0x29, 0x46} //Char 52 R
  102. ,{0x46, 0x49, 0x49, 0x49, 0x31} //Char 53 S
  103. ,{0x01, 0x01, 0x7f, 0x01, 0x01} //Char 54 T
  104. ,{0x3f, 0x40, 0x40, 0x40, 0x3f} //Char 55 U
  105. ,{0x1f, 0x20, 0x40, 0x20, 0x1f} //Char 56 V
  106. ,{0x3f, 0x40, 0x38, 0x40, 0x3f} //Char 57 W
  107. ,{0x63, 0x14, 0x08, 0x14, 0x63} //Char 58 X
  108. ,{0x07, 0x08, 0x70, 0x08, 0x07} //Char 59 Y
  109. ,{0x61, 0x51, 0x49, 0x45, 0x43} //Char 5a Z
  110. ,{0x00, 0x7f, 0x41, 0x41, 0x00} //Char 5b [
  111. ,{0x02, 0x04, 0x08, 0x10, 0x20} //Char 5c ¥
  112. ,{0x00, 0x41, 0x41, 0x7f, 0x00} //Char 5d ]
  113. ,{0x04, 0x02, 0x01, 0x02, 0x04} //Char 5e ^
  114. ,{0x40, 0x40, 0x40, 0x40, 0x40} //Char 5f _
  115. ,{0x00, 0x01, 0x02, 0x04, 0x00} //Char 60 `
  116. ,{0x20, 0x54, 0x54, 0x54, 0x78} //Char 61 a
  117. ,{0x7f, 0x48, 0x44, 0x44, 0x38} //Char 62 b
  118. ,{0x38, 0x44, 0x44, 0x44, 0x20} //Char 63 c
  119. ,{0x38, 0x44, 0x44, 0x48, 0x7f} //Char 64 d
  120. ,{0x38, 0x54, 0x54, 0x54, 0x18} //Char 65 e
  121. ,{0x08, 0x7e, 0x09, 0x01, 0x02} //Char 66 f
  122. ,{0x0c, 0x52, 0x52, 0x52, 0x3e} //Char 67 g
  123. ,{0x7f, 0x08, 0x04, 0x04, 0x78} //Char 68 h
  124. ,{0x00, 0x44, 0x7d, 0x40, 0x00} //Char 69 i
  125. ,{0x20, 0x40, 0x44, 0x3d, 0x00} //Char 6a j
  126. ,{0x7f, 0x10, 0x28, 0x44, 0x00} //Char 6b k
  127. ,{0x00, 0x41, 0x7f, 0x40, 0x00} //Char 6c l
  128. ,{0x7c, 0x04, 0x18, 0x04, 0x78} //Char 6d m
  129. ,{0x7c, 0x08, 0x04, 0x04, 0x78} //Char 6e n
  130. ,{0x38, 0x44, 0x44, 0x44, 0x38} //Char 6f o
  131. ,{0x7c, 0x14, 0x14, 0x14, 0x08} //Char 70 p
  132. ,{0x08, 0x14, 0x14, 0x18, 0x7c} //Char 71 q
  133. ,{0x7c, 0x08, 0x04, 0x04, 0x08} //Char 72 r
  134. ,{0x48, 0x54, 0x54, 0x54, 0x20} //Char 73 s
  135. ,{0x04, 0x3f, 0x44, 0x40, 0x20} //Char 74 t
  136. ,{0x3c, 0x40, 0x40, 0x20, 0x7c} //Char 75 u
  137. ,{0x1c, 0x20, 0x40, 0x20, 0x1c} //Char 76 v
  138. ,{0x3c, 0x40, 0x30, 0x40, 0x3c} //Char 77 w
  139. ,{0x44, 0x28, 0x10, 0x28, 0x44} //Char 78 x
  140. ,{0x0c, 0x50, 0x50, 0x50, 0x3c} //Char 79 y
  141. ,{0x44, 0x64, 0x54, 0x4c, 0x44} //Char 7a z
  142. ,{0x00, 0x08, 0x36, 0x41, 0x00} //Char 7b {
  143. ,{0x00, 0x00, 0x7f, 0x00, 0x00} //Char 7c |
  144. ,{0x00, 0x41, 0x36, 0x08, 0x00} //Char 7d }
  145. ,{0x10, 0x08, 0x08, 0x10, 0x08} //Char 7e ←
  146. ,{0x78, 0x46, 0x41, 0x46, 0x78} // Char 7f →
  147. };
  148.  
  149. void LcdCharacter(char character)
  150. {
  151.   LcdWrite(LCD_D, 0x00);
  152.   for (int index = 0; index < 5; index++)
  153.   {
  154.     LcdWrite(LCD_D, ASCII[character - 0x20][index]);
  155.   }
  156.   LcdWrite(LCD_D, 0x00);
  157. }
  158.  
  159. void LcdClear(void)
  160. {
  161.   for (int index = 0; index < LCD_X * LCD_Y / 8; index++)
  162.   {
  163.     LcdWrite(LCD_D, 0x00);
  164.   }
  165. }
  166.  
  167. void LcdInitialise(void)
  168. {
  169.   pinMode(PIN_SCE, OUTPUT);
  170.   pinMode(PIN_RESET, OUTPUT);
  171.   pinMode(PIN_DC, OUTPUT);
  172.   pinMode(PIN_SDIN, OUTPUT);
  173.   pinMode(PIN_SCLK, OUTPUT);
  174.   digitalWrite(PIN_RESET, LOW);
  175.   digitalWrite(PIN_RESET, HIGH);
  176.  
  177.   LcdWrite(LCD_C, 0x21 );  // LCD Extended Commands
  178.   LcdWrite(LCD_C, 0xB1 );  // Set LCD Contrast
  179.   LcdWrite(LCD_C, 0x04 );  // Set Temp coefficient 0x04
  180.   LcdWrite(LCD_C, 0x14 );  // LCD bias mode 1:48 0x13
  181.   LcdWrite(LCD_C, 0x0C );  // LCD in normal mode
  182.   LcdWrite(LCD_C, 0x20 );
  183.   LcdWrite(LCD_C, 0x0C );
  184. }
  185.  
  186. void LcdString(char *characters)
  187. {
  188.   while (*characters)
  189.   {
  190.     LcdCharacter(*characters++); // Populate the Characters
  191.   }
  192. }
  193.  
  194. void LcdWrite(byte dc, byte data)
  195. {
  196.   digitalWrite(PIN_DC, dc); //Set Digital Pin 5
  197.   digitalWrite(PIN_SCE, LOW); //Set Digital Pin 7 to Off
  198.   shiftOut(PIN_SDIN, PIN_SCLK, MSBFIRST, data);
  199.   digitalWrite(PIN_SCE, HIGH);
  200. }
  201. void gotoXY(int x, int y)
  202. {
  203.   LcdWrite( 0, 0x80 | x);  // LCD Column.
  204.   LcdWrite( 0, 0x40 | y);  // LCD ROW  
  205.  
  206. }
  207.  
  208.  
  209. bool is_authentified(){
  210.   Serial.println("Enter is_authentified");
  211.   if (server.hasHeader("Cookie")){  
  212.     Serial.print("Found cookie: ");
  213.     String cookie = server.header("Cookie");
  214.     Serial.println(cookie);
  215.     if (cookie.indexOf("ESPSESSIONID=1") != -1) {
  216.       Serial.println("Authentification Successful");
  217.       return true;
  218.     }
  219.   }
  220.   Serial.println("Authentification Failed");
  221.   return false;  
  222. }
  223.  
  224. //login page, also called for disconnect
  225. void handleLogin(){
  226.   String msg;
  227.   if (server.hasHeader("Cookie")){  
  228.     Serial.print("Found cookie: ");
  229.     String cookie = server.header("Cookie");
  230.     Serial.println(cookie);
  231.   }
  232.   if (server.hasArg("DISCONNECT")){
  233.     Serial.println("Disconnection");
  234.     String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=0\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
  235.     server.sendContent(header);
  236.     return;
  237.   }
  238.   if (server.hasArg("USERNAME") && server.hasArg("PASSWORD")){
  239.     if (server.arg("USERNAME") == "admin" &&  server.arg("PASSWORD") == "admin" ){
  240.       String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=1\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
  241.       server.sendContent(header);
  242.       Serial.println("Log in Successful");
  243.       return;
  244.     }
  245.   msg = "Wrong username/password! try again.";
  246.   Serial.println("Log in Failed");
  247.   }
  248.  
  249.   String content = "<html><body>";
  250.   content += "<form action='/login' method='POST'>Aby przegladac ta stronÄ™ musisz byc zalogowany<br>";
  251.   content += "User:<br><input type='text' name='USERNAME' placeholder='user name'><br>";
  252.   content += "Password:<br><input type='password' name='PASSWORD' placeholder='password'><br>";
  253.   content += "<input type='submit' name='SUBMIT' value='Submit'></form>" + msg + "<br>";
  254.   server.send(200, "text/html", content);
  255. }
  256.  
  257. void handleRoot(){
  258.   Serial.println("Enter handleRoot");
  259.   String header;
  260.   if (!is_authentified()){
  261.     String header = "HTTP/1.1 301 OK\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
  262.     server.sendContent(header);
  263.     return;
  264.   }
  265.  
  266.   //poczÄ…tek tabeli
  267.   String content = "<html><body><H2>Witaj</H2><br><br>";
  268.   content += "<table style='width:30%; border-style: solid; border-color: red;'>";
  269.   content += "<tr><th>Modul</th><th>Czujnik 1</th><th>Czujnik 2</th></tr>";
  270.   content += "<tr>";
  271.   content += "<td>";
  272.   content += "<center>Temperatura</center>";
  273.   content += "</td>";
  274.   content += "<td>";
  275.   content += "<center>";
  276.   content += temp;
  277.   content += "</center>";
  278.   content += "</td>";
  279.   content += "<td>";
  280.   content += "<center>";
  281.   content += temp;
  282.   content += "</center>";
  283.   content += "</td>";
  284.   content += "</tr>";
  285.  
  286.   content += "<tr>";
  287.   content += "<td>";
  288.   content += "<center>Wilgoc</center>";
  289.   content += "</td>";
  290.   content += "<td>";
  291.   content += "<center>";
  292.   content += wilg;
  293.   content += "</center>";
  294.   content += "</td>";
  295.   content += "<td>";
  296.   content += "<center>";
  297.   content += wilg;
  298.   content += "</center>";
  299.   content += "</td>";
  300.   content += "</tr>";
  301.   content += "</center>";
  302.  
  303.   content += "</table>";
  304.   content += "<br><br>";
  305.   //koniec tabeli
  306.  
  307.  
  308.   content += "<a href=\"/przek1on\"\"><button>Blok1 on</button></a>&nbsp;&nbsp;";
  309.   content += "<a href=\"/przek1off\"\"><button>Blok1 off</button></a>&nbsp;&nbsp;";
  310.   if (digitalRead(przekaznik) == LOW){
  311.    content += "<font color='red'>Wylaczone</font><br><br>";
  312.   }else{
  313.     content += "<font color='green'>Wlaczone</font><br><br>";
  314.   }
  315.  
  316.   content += "<br><br>";
  317.   content += "<a href=\"/praca1\"\"><button>Praca 1</button></a>&nbsp;&nbsp;";
  318.   content += "<a href=\"/praca2\"\"><button>Praca 2</button></a>&nbsp;&nbsp;";
  319.  
  320.   content += "<a href=\"/login?DISCONNECT=YES\">Wyloguj</a></body></html>";
  321.   server.send(200, "text/html", content);
  322. }
  323.  
  324. //no need authentification
  325. void handleNotFound(){
  326.   String message = "File Not Found\n\n";
  327.   message += "URI: ";
  328.   message += server.uri();
  329.   message += "\nMethod: ";
  330.   message += (server.method() == HTTP_GET)?"GET":"POST";
  331.   message += "\nArguments: ";
  332.   message += server.args();
  333.   message += "\n";
  334.   for (uint8_t i=0; i<server.args(); i++){
  335.     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  336.   }
  337.   server.send(404, "text/plain", message);
  338. }
  339.  
  340. void prz1on(){
  341.   digitalWrite(przekaznik, HIGH);
  342.   String header = "HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
  343.   server.sendContent(header);
  344. }
  345. void prz1off(){
  346.   digitalWrite(przekaznik, LOW);
  347.   String header = "HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
  348.   server.sendContent(header);
  349. }
  350. void praca1(){
  351.   praca = 0;
  352.   String header = "HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
  353.   server.sendContent(header);
  354. }
  355. void praca2(){
  356.   praca = 1;
  357.   String header = "HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
  358.   server.sendContent(header);
  359. }
  360.  
  361.  
  362. int woda_czekanie = 2000; //czas ile ma woda działać
  363. int wsekundach = woda_czekanie / 1000;
  364.  
  365. void setup() {
  366.   Serial.begin(115200);
  367.   WiFi.begin(ssid, password);
  368.   Serial.println("");
  369.  
  370.   pinMode(przekaznik, OUTPUT);
  371.   digitalWrite(przekaznik, LOW);
  372.  
  373.   // Wait for connection
  374.   while (WiFi.status() != WL_CONNECTED) {
  375.     delay(500);
  376.     Serial.print(".");
  377.   }
  378.  
  379.   Serial.println("");
  380.   Serial.print("Connected to ");
  381.   Serial.println(ssid);
  382.   Serial.print("IP address: ");
  383.   Serial.println(WiFi.localIP());
  384.  
  385.   server.on("/", handleRoot);
  386.   server.on("/login", handleLogin);
  387.   server.on("/przek1on", prz1on);
  388.   server.on("/przek1off", prz1off);
  389.   server.on("/praca1", praca1);
  390.   server.on("/praca2", praca2);
  391.    
  392.   server.onNotFound(handleNotFound);
  393.   //here the list of headers to be recorded
  394.   const char * headerkeys[] = {"User-Agent","Cookie"} ;
  395.   size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*);
  396.   //ask server to track these headers
  397.   server.collectHeaders(headerkeys, headerkeyssize );
  398.   server.begin();
  399.   Serial.println("HTTP server started");
  400.  
  401.   LcdInitialise();
  402.   sensors.begin();
  403. }
  404.  
  405. char myIpString[24];
  406. char mytempnow[24];
  407. char nastawiona[24];
  408. char jakapraca[24];
  409. void loop() {
  410.   server.handleClient();
  411.    // start working...
  412.   Serial.println("=================================");
  413.   // read without samples.
  414.   byte temperature = 0;
  415.   sensors.requestTemperatures();
  416.   temp = sensors.getTempCByIndex(0);
  417.  
  418.   IPAddress myIp = WiFi.localIP();
  419.   sprintf(myIpString, "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]);
  420.   sprintf(jakapraca, "%d", praca);
  421.   dtostrf(temp, 4, 2, mytempnow);
  422.   LcdClear();
  423.  
  424.   gotoXY(0, 0);
  425.   LcdString("Temp: ");
  426.   LcdString(mytempnow);
  427.  
  428.   gotoXY(0, 1);
  429.   dtostrf(nastawionatemp, 4, 2, nastawiona);
  430.   LcdString("Nast: ");
  431.   LcdString(nastawiona);
  432.  
  433.   gotoXY(0, 2);
  434.   LcdString(myIpString);
  435.  
  436.   gotoXY(0, 4);
  437.   LcdString("Praca: ");
  438.   LcdString(jakapraca);
  439.  
  440.  
  441.  
  442.  
  443.   if (praca == 0){
  444.     if (temp > nastawionatemp){
  445.       prz1on();
  446.     }else{
  447.       prz1off();
  448.     }
  449.   }
  450.  
  451.   if (praca == 1){
  452.     if (temp < nastawionatemp){
  453.       prz1on();
  454.     }else{
  455.       prz1off();
  456.     }
  457.   }
  458.    
  459.   Serial.println(sensors.getTempCByIndex(0));  
  460.   delay(100);
  461. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement