Advertisement
Guest User

Untitled

a guest
Dec 1st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.48 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 += "<br><br>";
  321. content += "<a href=\"/dodajtemp\"\"><button>Dodaj Temperaturę</button></a>&nbsp;&nbsp;";
  322. content += "<a href=\"/ujmijtemp\"\"><button>Odejmij Temperaturę</button></a>&nbsp;&nbsp;";
  323.  
  324. content += "<a href=\"/login?DISCONNECT=YES\">Wyloguj</a></body></html>";
  325. server.send(200, "text/html", content);
  326. }
  327.  
  328. //no need authentification
  329. void handleNotFound(){
  330. String message = "File Not Found\n\n";
  331. message += "URI: ";
  332. message += server.uri();
  333. message += "\nMethod: ";
  334. message += (server.method() == HTTP_GET)?"GET":"POST";
  335. message += "\nArguments: ";
  336. message += server.args();
  337. message += "\n";
  338. for (uint8_t i=0; i<server.args(); i++){
  339. message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  340. }
  341. server.send(404, "text/plain", message);
  342. }
  343.  
  344. void prz1on(){
  345. digitalWrite(przekaznik, HIGH);
  346. String header = "HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
  347. server.sendContent(header);
  348. }
  349. void prz1off(){
  350. digitalWrite(przekaznik, LOW);
  351. String header = "HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
  352. server.sendContent(header);
  353. }
  354. void praca1(){
  355. praca = 0;
  356. String header = "HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
  357. server.sendContent(header);
  358. }
  359. void praca2(){
  360. praca = 1;
  361. String header = "HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
  362. server.sendContent(header);
  363. }
  364. void dodajtemp(){
  365. nastawionatemp += 5;
  366. }
  367. void ujmijtemp(){
  368. nastawionatemp -= 5;
  369. }
  370.  
  371.  
  372. int woda_czekanie = 2000; //czas ile ma woda działać
  373. int wsekundach = woda_czekanie / 1000;
  374.  
  375. void setup() {
  376. Serial.begin(115200);
  377. WiFi.begin(ssid, password);
  378. Serial.println("");
  379.  
  380. pinMode(przekaznik, OUTPUT);
  381. digitalWrite(przekaznik, LOW);
  382.  
  383. Serial.println("");
  384. Serial.print("Connected to ");
  385. Serial.println(ssid);
  386. Serial.print("IP address: ");
  387. Serial.println(WiFi.localIP());
  388.  
  389. server.on("/", handleRoot);
  390. server.on("/login", handleLogin);
  391. server.on("/przek1on", prz1on);
  392. server.on("/przek1off", prz1off);
  393. server.on("/praca1", praca1);
  394. server.on("/praca2", praca2);
  395. server.on("/dodajtemp", dodajtemp);
  396. server.on("/ujmijtemp", ujmijtemp);
  397.  
  398. server.onNotFound(handleNotFound);
  399. //here the list of headers to be recorded
  400. const char * headerkeys[] = {"User-Agent","Cookie"} ;
  401. size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*);
  402. //ask server to track these headers
  403. server.collectHeaders(headerkeys, headerkeyssize );
  404. server.begin();
  405. Serial.println("HTTP server started");
  406.  
  407. LcdInitialise();
  408. sensors.begin();
  409. }
  410.  
  411. char myIpString[24];
  412. char mytempnow[24];
  413. char nastawiona[24];
  414. char jakapraca[24];
  415. void loop() {
  416. server.handleClient();
  417. // start working...
  418. Serial.println("=================================");
  419. // read without samples.
  420. byte temperature = 0;
  421. sensors.requestTemperatures();
  422. temp = sensors.getTempCByIndex(0);
  423.  
  424. if (WiFi.status() != WL_CONNECTED){
  425. sprintf(myIpString, "%s", "Wifi off");
  426. }else{
  427. IPAddress myIp = WiFi.localIP();
  428. sprintf(myIpString, "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]);
  429. }
  430.  
  431. sprintf(jakapraca, "%d", praca);
  432. dtostrf(temp, 4, 2, mytempnow);
  433.  
  434. LcdClear();
  435.  
  436. gotoXY(0, 0);
  437. LcdString("Temp: ");
  438. LcdString(mytempnow);
  439.  
  440. gotoXY(0, 1);
  441. dtostrf(nastawionatemp, 4, 2, nastawiona);
  442. LcdString("Nast: ");
  443. LcdString(nastawiona);
  444.  
  445. gotoXY(0, 2);
  446. LcdString(myIpString);
  447.  
  448. gotoXY(0, 4);
  449. LcdString("Praca: ");
  450. LcdString(jakapraca);
  451.  
  452.  
  453.  
  454.  
  455. if (praca == 0){
  456. if (temp > nastawionatemp){
  457. prz1on();
  458. }else{
  459. prz1off();
  460. }
  461. }
  462.  
  463. if (praca == 1){
  464. if (temp < nastawionatemp){
  465. prz1on();
  466. }else{
  467. prz1off();
  468. }
  469. }
  470.  
  471. Serial.println(sensors.getTempCByIndex(0));
  472. delay(100);
  473. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement