Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- #include <SimpleDHT.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- const char* ssid = "Gesanet_Wyremb";
- const char* password = "wyrembowski870316";
- ESP8266WebServer server(80);
- int praca = 0;
- float temp = 0;
- float wilg = 0;
- float nastawionatemp = 50;
- int przekaznik = 16;
- #define ONE_WIRE_BUS 0
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- /*
- Demonstration Code Driving LCD 5110 CMOS Driven Display by Arduino
- RST--------- reset
- CE---------- chip selection
- DC---------- data/commands choice
- DIN--------- serial data line
- CLK--------- serial Clock Speed
- 3.3V-------- VCC
- LIGHT------- back light control terminal
- GND--------- power negative
- */
- #define PIN_SCE 12 //pin GPIO12 ON ESP , pin SCE ON LCD // D6
- #define PIN_RESET 2 //pin GPI02 on ESP , pin RST on LCD // D4
- #define PIN_DC 14 //pin GPIO14 on ESP , pin DC on LCD // D5
- #define PIN_SDIN 4 //pin GPIO4 on ESP , pin SDIN on LCD // D2
- #define PIN_SCLK 5 //pin GPIO5 on ESP , pin SLCK on LCD // D1
- #define LCD_C LOW
- #define LCD_D HIGH
- int count=0;
- char dat[4];
- char disp_tab[]={'0','1','2','3','4','5','6','7','8','9'};
- #define LCD_X 84
- #define LCD_Y 48
- char buf[72]="A_simple_example_of_interfacing_with_the_84_x_48_pixel_Nokia_3310_LCD!!";
- static const byte ASCII[][5] =
- {
- {0x00, 0x00, 0x00, 0x00, 0x00} //Char 20
- ,{0x00, 0x00, 0x5f, 0x00, 0x00} //Char 21 !
- ,{0x00, 0x07, 0x00, 0x07, 0x00} //Char 22 "
- ,{0x14, 0x7f, 0x14, 0x7f, 0x14} //Char 23 #
- ,{0x24, 0x2a, 0x7f, 0x2a, 0x12} //Char 24 $
- ,{0x23, 0x13, 0x08, 0x64, 0x62} //Char 25 %
- ,{0x36, 0x49, 0x55, 0x22, 0x50} //Char 26 &
- ,{0x00, 0x05, 0x03, 0x00, 0x00} //Char 27 '
- ,{0x00, 0x1c, 0x22, 0x41, 0x00} //Char 28 (
- ,{0x00, 0x41, 0x22, 0x1c, 0x00} //Char 29 )
- ,{0x14, 0x08, 0x3e, 0x08, 0x14} //Char 2a *
- ,{0x08, 0x08, 0x3e, 0x08, 0x08} //Char 2b +
- ,{0x00, 0x50, 0x30, 0x00, 0x00} //Char 2c ,
- ,{0x08, 0x08, 0x08, 0x08, 0x08} //Char 2d -
- ,{0x00, 0x60, 0x60, 0x00, 0x00} //Char 2e .
- ,{0x20, 0x10, 0x08, 0x04, 0x02} //Char 2f /
- ,{0x3e, 0x51, 0x49, 0x45, 0x3e} //Char 30 0
- ,{0x00, 0x42, 0x7f, 0x40, 0x00} //Char 31 1
- ,{0x42, 0x61, 0x51, 0x49, 0x46} //Char 32 2
- ,{0x21, 0x41, 0x45, 0x4b, 0x31} //Char 33 3
- ,{0x18, 0x14, 0x12, 0x7f, 0x10} //Char 34 4
- ,{0x27, 0x45, 0x45, 0x45, 0x39} //Char 35 5
- ,{0x3c, 0x4a, 0x49, 0x49, 0x30} //Char 36 6
- ,{0x01, 0x71, 0x09, 0x05, 0x03} //Char 37 7
- ,{0x36, 0x49, 0x49, 0x49, 0x36} //Char 38 8
- ,{0x06, 0x49, 0x49, 0x29, 0x1e} //Char 39 9
- ,{0x00, 0x36, 0x36, 0x00, 0x00} //Char 3a :
- ,{0x00, 0x56, 0x36, 0x00, 0x00} //Char 3b ;
- ,{0x08, 0x14, 0x22, 0x41, 0x00} //Char 3c <
- ,{0x14, 0x14, 0x14, 0x14, 0x14} //Char 3d =
- ,{0x00, 0x41, 0x22, 0x14, 0x08} //Char 3e >
- ,{0x02, 0x01, 0x51, 0x09, 0x06} //Char 3f ?
- ,{0x32, 0x49, 0x79, 0x41, 0x3e} //Char 40 @
- ,{0x7e, 0x11, 0x11, 0x11, 0x7e} //Char 41 A
- ,{0x7f, 0x49, 0x49, 0x49, 0x36} //Char 42 B
- ,{0x3e, 0x41, 0x41, 0x41, 0x22} //Char 43 C
- ,{0x7f, 0x41, 0x41, 0x22, 0x1c} //Char 44 D
- ,{0x7f, 0x49, 0x49, 0x49, 0x41} //Char 45 E
- ,{0x7f, 0x09, 0x09, 0x09, 0x01} //Char 46 F
- ,{0x3e, 0x41, 0x49, 0x49, 0x7a} //Char 47 G
- ,{0x7f, 0x08, 0x08, 0x08, 0x7f} //Char 48 H
- ,{0x00, 0x41, 0x7f, 0x41, 0x00} //Char 49 I
- ,{0x20, 0x40, 0x41, 0x3f, 0x01} //Char 4a J
- ,{0x7f, 0x08, 0x14, 0x22, 0x41} //Char 4b K
- ,{0x7f, 0x40, 0x40, 0x40, 0x40} //Char 4c L
- ,{0x7f, 0x02, 0x0c, 0x02, 0x7f} //Char 4d M
- ,{0x7f, 0x04, 0x08, 0x10, 0x7f} //Char 4e N
- ,{0x3e, 0x41, 0x41, 0x41, 0x3e} //Char 4f O
- ,{0x7f, 0x09, 0x09, 0x09, 0x06} //Char 50 P
- ,{0x3e, 0x41, 0x51, 0x21, 0x5e} //Char 51 Q
- ,{0x7f, 0x09, 0x19, 0x29, 0x46} //Char 52 R
- ,{0x46, 0x49, 0x49, 0x49, 0x31} //Char 53 S
- ,{0x01, 0x01, 0x7f, 0x01, 0x01} //Char 54 T
- ,{0x3f, 0x40, 0x40, 0x40, 0x3f} //Char 55 U
- ,{0x1f, 0x20, 0x40, 0x20, 0x1f} //Char 56 V
- ,{0x3f, 0x40, 0x38, 0x40, 0x3f} //Char 57 W
- ,{0x63, 0x14, 0x08, 0x14, 0x63} //Char 58 X
- ,{0x07, 0x08, 0x70, 0x08, 0x07} //Char 59 Y
- ,{0x61, 0x51, 0x49, 0x45, 0x43} //Char 5a Z
- ,{0x00, 0x7f, 0x41, 0x41, 0x00} //Char 5b [
- ,{0x02, 0x04, 0x08, 0x10, 0x20} //Char 5c ¥
- ,{0x00, 0x41, 0x41, 0x7f, 0x00} //Char 5d ]
- ,{0x04, 0x02, 0x01, 0x02, 0x04} //Char 5e ^
- ,{0x40, 0x40, 0x40, 0x40, 0x40} //Char 5f _
- ,{0x00, 0x01, 0x02, 0x04, 0x00} //Char 60 `
- ,{0x20, 0x54, 0x54, 0x54, 0x78} //Char 61 a
- ,{0x7f, 0x48, 0x44, 0x44, 0x38} //Char 62 b
- ,{0x38, 0x44, 0x44, 0x44, 0x20} //Char 63 c
- ,{0x38, 0x44, 0x44, 0x48, 0x7f} //Char 64 d
- ,{0x38, 0x54, 0x54, 0x54, 0x18} //Char 65 e
- ,{0x08, 0x7e, 0x09, 0x01, 0x02} //Char 66 f
- ,{0x0c, 0x52, 0x52, 0x52, 0x3e} //Char 67 g
- ,{0x7f, 0x08, 0x04, 0x04, 0x78} //Char 68 h
- ,{0x00, 0x44, 0x7d, 0x40, 0x00} //Char 69 i
- ,{0x20, 0x40, 0x44, 0x3d, 0x00} //Char 6a j
- ,{0x7f, 0x10, 0x28, 0x44, 0x00} //Char 6b k
- ,{0x00, 0x41, 0x7f, 0x40, 0x00} //Char 6c l
- ,{0x7c, 0x04, 0x18, 0x04, 0x78} //Char 6d m
- ,{0x7c, 0x08, 0x04, 0x04, 0x78} //Char 6e n
- ,{0x38, 0x44, 0x44, 0x44, 0x38} //Char 6f o
- ,{0x7c, 0x14, 0x14, 0x14, 0x08} //Char 70 p
- ,{0x08, 0x14, 0x14, 0x18, 0x7c} //Char 71 q
- ,{0x7c, 0x08, 0x04, 0x04, 0x08} //Char 72 r
- ,{0x48, 0x54, 0x54, 0x54, 0x20} //Char 73 s
- ,{0x04, 0x3f, 0x44, 0x40, 0x20} //Char 74 t
- ,{0x3c, 0x40, 0x40, 0x20, 0x7c} //Char 75 u
- ,{0x1c, 0x20, 0x40, 0x20, 0x1c} //Char 76 v
- ,{0x3c, 0x40, 0x30, 0x40, 0x3c} //Char 77 w
- ,{0x44, 0x28, 0x10, 0x28, 0x44} //Char 78 x
- ,{0x0c, 0x50, 0x50, 0x50, 0x3c} //Char 79 y
- ,{0x44, 0x64, 0x54, 0x4c, 0x44} //Char 7a z
- ,{0x00, 0x08, 0x36, 0x41, 0x00} //Char 7b {
- ,{0x00, 0x00, 0x7f, 0x00, 0x00} //Char 7c |
- ,{0x00, 0x41, 0x36, 0x08, 0x00} //Char 7d }
- ,{0x10, 0x08, 0x08, 0x10, 0x08} //Char 7e ←
- ,{0x78, 0x46, 0x41, 0x46, 0x78} // Char 7f →
- };
- void LcdCharacter(char character)
- {
- LcdWrite(LCD_D, 0x00);
- for (int index = 0; index < 5; index++)
- {
- LcdWrite(LCD_D, ASCII[character - 0x20][index]);
- }
- LcdWrite(LCD_D, 0x00);
- }
- void LcdClear(void)
- {
- for (int index = 0; index < LCD_X * LCD_Y / 8; index++)
- {
- LcdWrite(LCD_D, 0x00);
- }
- }
- void LcdInitialise(void)
- {
- pinMode(PIN_SCE, OUTPUT);
- pinMode(PIN_RESET, OUTPUT);
- pinMode(PIN_DC, OUTPUT);
- pinMode(PIN_SDIN, OUTPUT);
- pinMode(PIN_SCLK, OUTPUT);
- digitalWrite(PIN_RESET, LOW);
- digitalWrite(PIN_RESET, HIGH);
- LcdWrite(LCD_C, 0x21 ); // LCD Extended Commands
- LcdWrite(LCD_C, 0xB1 ); // Set LCD Contrast
- LcdWrite(LCD_C, 0x04 ); // Set Temp coefficient 0x04
- LcdWrite(LCD_C, 0x14 ); // LCD bias mode 1:48 0x13
- LcdWrite(LCD_C, 0x0C ); // LCD in normal mode
- LcdWrite(LCD_C, 0x20 );
- LcdWrite(LCD_C, 0x0C );
- }
- void LcdString(char *characters)
- {
- while (*characters)
- {
- LcdCharacter(*characters++); // Populate the Characters
- }
- }
- void LcdWrite(byte dc, byte data)
- {
- digitalWrite(PIN_DC, dc); //Set Digital Pin 5
- digitalWrite(PIN_SCE, LOW); //Set Digital Pin 7 to Off
- shiftOut(PIN_SDIN, PIN_SCLK, MSBFIRST, data);
- digitalWrite(PIN_SCE, HIGH);
- }
- void gotoXY(int x, int y)
- {
- LcdWrite( 0, 0x80 | x); // LCD Column.
- LcdWrite( 0, 0x40 | y); // LCD ROW
- }
- bool is_authentified(){
- Serial.println("Enter is_authentified");
- if (server.hasHeader("Cookie")){
- Serial.print("Found cookie: ");
- String cookie = server.header("Cookie");
- Serial.println(cookie);
- if (cookie.indexOf("ESPSESSIONID=1") != -1) {
- Serial.println("Authentification Successful");
- return true;
- }
- }
- Serial.println("Authentification Failed");
- return false;
- }
- //login page, also called for disconnect
- void handleLogin(){
- String msg;
- if (server.hasHeader("Cookie")){
- Serial.print("Found cookie: ");
- String cookie = server.header("Cookie");
- Serial.println(cookie);
- }
- if (server.hasArg("DISCONNECT")){
- Serial.println("Disconnection");
- String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=0\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
- server.sendContent(header);
- return;
- }
- if (server.hasArg("USERNAME") && server.hasArg("PASSWORD")){
- if (server.arg("USERNAME") == "admin" && server.arg("PASSWORD") == "admin" ){
- String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=1\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
- server.sendContent(header);
- Serial.println("Log in Successful");
- return;
- }
- msg = "Wrong username/password! try again.";
- Serial.println("Log in Failed");
- }
- String content = "<html><body>";
- content += "<form action='/login' method='POST'>Aby przegladac ta stronÄ™ musisz byc zalogowany<br>";
- content += "User:<br><input type='text' name='USERNAME' placeholder='user name'><br>";
- content += "Password:<br><input type='password' name='PASSWORD' placeholder='password'><br>";
- content += "<input type='submit' name='SUBMIT' value='Submit'></form>" + msg + "<br>";
- server.send(200, "text/html", content);
- }
- void handleRoot(){
- Serial.println("Enter handleRoot");
- String header;
- if (!is_authentified()){
- String header = "HTTP/1.1 301 OK\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
- server.sendContent(header);
- return;
- }
- //poczÄ…tek tabeli
- String content = "<html><body><H2>Witaj</H2><br><br>";
- content += "<table style='width:30%; border-style: solid; border-color: red;'>";
- content += "<tr><th>Modul</th><th>Czujnik 1</th><th>Czujnik 2</th></tr>";
- content += "<tr>";
- content += "<td>";
- content += "<center>Temperatura</center>";
- content += "</td>";
- content += "<td>";
- content += "<center>";
- content += temp;
- content += "</center>";
- content += "</td>";
- content += "<td>";
- content += "<center>";
- content += temp;
- content += "</center>";
- content += "</td>";
- content += "</tr>";
- content += "<tr>";
- content += "<td>";
- content += "<center>Wilgoc</center>";
- content += "</td>";
- content += "<td>";
- content += "<center>";
- content += wilg;
- content += "</center>";
- content += "</td>";
- content += "<td>";
- content += "<center>";
- content += wilg;
- content += "</center>";
- content += "</td>";
- content += "</tr>";
- content += "</center>";
- content += "</table>";
- content += "<br><br>";
- //koniec tabeli
- content += "<a href=\"/przek1on\"\"><button>Blok1 on</button></a> ";
- content += "<a href=\"/przek1off\"\"><button>Blok1 off</button></a> ";
- if (digitalRead(przekaznik) == LOW){
- content += "<font color='red'>Wylaczone</font><br><br>";
- }else{
- content += "<font color='green'>Wlaczone</font><br><br>";
- }
- content += "<br><br>";
- content += "<a href=\"/praca1\"\"><button>Praca 1</button></a> ";
- content += "<a href=\"/praca2\"\"><button>Praca 2</button></a> ";
- content += "<a href=\"/login?DISCONNECT=YES\">Wyloguj</a></body></html>";
- server.send(200, "text/html", content);
- }
- //no need authentification
- void handleNotFound(){
- String message = "File Not Found\n\n";
- message += "URI: ";
- message += server.uri();
- message += "\nMethod: ";
- message += (server.method() == HTTP_GET)?"GET":"POST";
- message += "\nArguments: ";
- message += server.args();
- message += "\n";
- for (uint8_t i=0; i<server.args(); i++){
- message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
- }
- server.send(404, "text/plain", message);
- }
- void prz1on(){
- digitalWrite(przekaznik, HIGH);
- String header = "HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
- server.sendContent(header);
- }
- void prz1off(){
- digitalWrite(przekaznik, LOW);
- String header = "HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
- server.sendContent(header);
- }
- void praca1(){
- praca = 0;
- String header = "HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
- server.sendContent(header);
- }
- void praca2(){
- praca = 1;
- String header = "HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
- server.sendContent(header);
- }
- int woda_czekanie = 2000; //czas ile ma woda działać
- int wsekundach = woda_czekanie / 1000;
- void setup() {
- Serial.begin(115200);
- WiFi.begin(ssid, password);
- Serial.println("");
- pinMode(przekaznik, OUTPUT);
- digitalWrite(przekaznik, LOW);
- // Wait for connection
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- server.on("/", handleRoot);
- server.on("/login", handleLogin);
- server.on("/przek1on", prz1on);
- server.on("/przek1off", prz1off);
- server.on("/praca1", praca1);
- server.on("/praca2", praca2);
- server.onNotFound(handleNotFound);
- //here the list of headers to be recorded
- const char * headerkeys[] = {"User-Agent","Cookie"} ;
- size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*);
- //ask server to track these headers
- server.collectHeaders(headerkeys, headerkeyssize );
- server.begin();
- Serial.println("HTTP server started");
- LcdInitialise();
- sensors.begin();
- }
- char myIpString[24];
- char mytempnow[24];
- char nastawiona[24];
- char jakapraca[24];
- void loop() {
- server.handleClient();
- // start working...
- Serial.println("=================================");
- // read without samples.
- byte temperature = 0;
- sensors.requestTemperatures();
- temp = sensors.getTempCByIndex(0);
- IPAddress myIp = WiFi.localIP();
- sprintf(myIpString, "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]);
- sprintf(jakapraca, "%d", praca);
- dtostrf(temp, 4, 2, mytempnow);
- LcdClear();
- gotoXY(0, 0);
- LcdString("Temp: ");
- LcdString(mytempnow);
- gotoXY(0, 1);
- dtostrf(nastawionatemp, 4, 2, nastawiona);
- LcdString("Nast: ");
- LcdString(nastawiona);
- gotoXY(0, 2);
- LcdString(myIpString);
- gotoXY(0, 4);
- LcdString("Praca: ");
- LcdString(jakapraca);
- if (praca == 0){
- if (temp > nastawionatemp){
- prz1on();
- }else{
- prz1off();
- }
- }
- if (praca == 1){
- if (temp < nastawionatemp){
- prz1on();
- }else{
- prz1off();
- }
- }
- Serial.println(sensors.getTempCByIndex(0));
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement