View difference between Paste ID: CV3PLGK7 and Gz7epCYq
SHOW: | | - or go back to the newest paste.
1
#include <ESP8266WiFi.h>
2
#include <WiFiClient.h>
3
#include <ESP8266WebServer.h>
4
#include <ESP8266mDNS.h>
5
6
const char* ssid = "SuaSSID";
7
const char* password = "SuaSenha";
8
9-
const char* ssid = "Mauro";
9+
10-
const char* password = "mauro.......";
10+
#define VERSION "0.1"
11
12-
#define VERSION "1.1a"
12+
13-
int shiftDataPin = 12; // SER Pin 14 on 595 // Blue Wire
13+
14-
int shiftClockPin= 13; // SRCLK Pin 11 on 595 // Green Wire
14+
int pin [] = {D1, BUILTIN_LED}; // pinos utilizados Array
15-
int shiftLatchPin = 2; // RCLK Pin 12 on 595 // White Wire // connected together with pullup resistor
15+
int pinCount = 2; // declara numero de pinos utilizados
16-
int shiftEnablePin = 16; // OE Pin 13 on 595 // Black Wire
16+
byte output_status = 0; // status dos pinos ou deveria ser hehe
17-
int oneWireBusPin = 14; // Data pin for DS18B20 temperature sensor
17+
18
19
void shiftWrite(byte value) { // precisaria configurar as condições do array aqui
20
 for (int thisPin = 0; thisPin < pinCount; thisPin++) {
21
    // turn the pin on:
22-
byte output_status = B00000000;
22+
    digitalWrite(pin[thisPin], output_status);
23
    delay(timer);
24
    // turn the pin off:
25
    digitalWrite(pin[thisPin],output_status);
26
27
  }
28
  // loop from the highest pin to the lowest:
29-
void shiftWrite(byte value) {
29+
  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
30-
    digitalWrite(shiftLatchPin, LOW);
30+
    // turn the pin on:
31-
    shiftOut(shiftDataPin, shiftClockPin, MSBFIRST, value);
31+
    digitalWrite(pin[thisPin], HIGH);
32-
    digitalWrite(shiftLatchPin, HIGH);
32+
    delay(timer);
33
    // turn the pin off:
34
    digitalWrite(pin[thisPin], LOW);
35
  }
36
}
37
}
38
39
void handleTest() {
40
  server.send(200, "text/html", F(""));
41
}
42
43
void handleAbout() {
44
  String message = "";
45
  message += "Firmware Version: " VERSION "\n";
46
  message += "Compile Date: " __DATE__ " " __TIME__ "\n";
47
  message += "Source: " __FILE__ "\n";
48
  message += "Flash Chip ID: ";
49
  message += ESP.getFlashChipId();
50
  message += "\nFlash Chip Real Size: ";
51
  message += ESP.getFlashChipRealSize();
52
  message += "\nFlash Chip Size: ";
53
  message += ESP.getFlashChipSize();
54
  message += "\nFlash Chip Speed: ";
55
  message += ESP.getFlashChipSpeed();
56
  message += "\nFlash Chip Mode: ";
57
  message += ESP.getFlashChipMode();
58-
  server.send(200, "text/plain", message);
58+
59
  message += WiFi.localIP();
60
  message += "\n";
61-
void handleRoot() {
61+
62
  server.send(200, "text/plain", message);// concatena o texto da pagina HTML na string "message"
63-
"<!DOCTYPE html>"
63+
64-
"<html lang=\"en\">"
64+
65-
"  <head>"
65+
void handleRoot() {// monta a pagina HTML
66-
"    <meta charset=\"utf-8\">"
66+
67-
"    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">"
67+
                   "<!DOCTYPE html>"
68-
"    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
68+
                   "<html lang=\"en\">"
69-
"    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->"
69+
                   "  <head>"
70-
"    <title>Moises Sprinkler Controller</title>"
70+
                   "    <meta charset=\"utf-8\">"
71-
""
71+
                   "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">"
72-
"    <!-- Bootstrap -->"
72+
                   "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
73-
"    <link href=\"https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/darkly/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-kVo/Eh0sv7ZdiwZK32nRsp1FrDT3sLRLx3zVpSSTI9UdO5H02LJNLBg5F1gwvKg0\" crossorigin=\"anonymous\">"
73+
                   "    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->"
74-
""
74+
                   "    <title>WebServer ESP8266 Controller</title>"
75-
"    <!-- Custom styles for this template -->"
75+
                   ""
76-
"    <link href=\"bootstrap.css\" rel=\"stylesheet\">"
76+
                   "    <!-- Bootstrap -->"
77-
""
77+
                   "    <link href=\"https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/darkly/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-kVo/Eh0sv7ZdiwZK32nRsp1FrDT3sLRLx3zVpSSTI9UdO5H02LJNLBg5F1gwvKg0\" crossorigin=\"anonymous\">"
78-
"    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->"
78+
                   ""
79-
"    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->"
79+
                   "    <!-- Custom styles for this template -->"
80-
"    <!--[if lt IE 9]>"
80+
                   "    <link href=\"bootstrap.css\" rel=\"stylesheet\">"
81-
"      <script src=\"https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js\"></script>"
81+
                   ""
82-
"      <script src=\"https://oss.maxcdn.com/respond/1.4.2/respond.min.js\"></script>"
82+
                   "    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->"
83-
"    <![endif]-->"
83+
                   "    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->"
84-
"  </head>"
84+
                   "    <!--[if lt IE 9]>"
85-
"  <body>"
85+
                   "      <script src=\"https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js\"></script>"
86-
"    "
86+
                   "      <script src=\"https://oss.maxcdn.com/respond/1.4.2/respond.min.js\"></script>"
87-
"    <!-- Fixed navbar -->"
87+
                   "    <![endif]-->"
88-
"    <nav class=\"navbar navbar-default navbar-fixed-top\">"
88+
                     "  </head>"
89-
"      <div class=\"container\">"
89+
                   "  <body>"
90-
"        <div class=\"navbar-header\">"
90+
                   "    "
91-
"          <button type=\"button\" class=\"navbar-toggle collapsed\" data-toggle=\"collapse\" data-target=\"#navbar\" aria-expanded=\"false\" aria-controls=\"navbar\">"
91+
                   "<h1>WebServer ESP8266 Controller</h1>"
92-
"            <span class=\"sr-only\">Toggle navigation</span>"
92+
                   "    <!-- Fixed navbar -->"
93-
"            <span class=\"icon-bar\"></span>"
93+
                   "    <nav class=\"navbar navbar-default navbar-fixed-top\">"
94-
"            <span class=\"icon-bar\"></span>"
94+
                   "      <div class=\"container\">"
95-
"            <span class=\"icon-bar\"></span>"
95+
                   "        <div class=\"navbar-header\">"
96-
"          </button>"
96+
                   "          <button type=\"button\" class=\"navbar-toggle collapsed\" data-toggle=\"collapse\" data-target=\"#navbar\" aria-expanded=\"false\" aria-controls=\"navbar\">"
97
                   "            <span class=\"sr-only\">Toggle navigation</span>"
98-
"        </div>"
98+
                   "            <span class=\"icon-bar\"></span>"
99-
"        <div id=\"navbar\" class=\"collapse navbar-collapse\">"
99+
                   "            <span class=\"icon-bar\"></span>"
100-
"          <ul class=\"nav navbar-nav\">"
100+
                   "            <span class=\"icon-bar\"></span>"
101-
"          </ul>"
101+
                   "          </button>"
102-
"          <ul class=\"nav navbar-nav navbar-right\">"
102+
103
                   "        </div>"
104
                   "        <div id=\"navbar\" class=\"collapse navbar-collapse\">"
105
                   "          <ul class=\"nav navbar-nav\">"
106
                   "          </ul>"
107-
"          </ul>"
107+
                   "          <ul class=\"nav navbar-nav navbar-right\">"
108-
"        </div><!--/.nav-collapse -->"
108+
                   "          </ul>"
109-
"      </div>"
109+
                   "        </div><!--/.nav-collapse -->"
110-
"    </nav>"
110+
                   "      </div>"
111-
""
111+
                   "    </nav>"
112-
"    <!-- Begin page content -->"
112+
                   ""
113-
"    <div class=\"container\">"
113+
                   "    <!-- Begin page content -->"
114-
"      <div class=\"page-header\">"
114+
                   "    <div class=\"container\">"
115-
"        "
115+
                   "      <div class=\"page-header\">"
116-
"      </div>"
116+
                   "        "
117-
"    </div>"
117+
                   "      </div>"
118-
""
118+
                   "    </div>"
119-
"    <div class=\"container\">"
119+
                   ""
120-
"";
120+
                   "    <div class=\"container\">"
121
                   "";
122-
  for (int c=0; c<8; c++) {
122+
123-
    message += "      <p>";
123+
  for (int c = 0; c < 2; c++) { // cria o numero de botões desejados, neste caso 2
124
    message += "<p>";
125-
    message += "        Station ";
125+
126-
    message += c+1;
126+
    message += "botão ";
127
    message += c + 1;
128
    message += " &nbsp;\n";
129-
    // Print First Button (The OFF Button)
129+
130-
    if (output_status & (B00000001 << c)) {
130+
    // Print First Button (The OFF Button)// adiciona os botões on/off
131
    if (output_status & (pin << c)) {
132
      // Currently On
133
      message += "            <a href=\"switch?pin=";
134-
      message += "&state=0\" class=\"btn btn-default\">Off</a>";
134+
135
      message += "&state=0\" class=\"btn btn-default\">Off</a>";// state=0 condição OFF
136
    } else {
137
      // Currently Off
138
      message += "            <a href=\"switch?pin=";
139
      message += c;
140
      message += "&state=0\" class=\"btn btn-success\">Off</a>";
141
    }
142
143-
    if (output_status & (B00000001 << c)) {
143+
144
    if (output_status & (pin << c)) {
145
      // Currently On
146
      message += "            <a href=\"switch?pin=";
147-
      message += "&state=1\" class=\"btn btn-warning\">On</a>";
147+
148
      message += "&state=1\" class=\"btn btn-warning\">On</a>";// state=1 condição ON
149
    } else {
150
      // Currently Off
151
      message += "            <a href=\"switch?pin=";
152
      message += c;
153
      message += "&state=1\" class=\"btn btn-default\">On</a>";
154-
    
154+
155
    message += "</tr>\n";
156
  }
157
  message += ""
158
             "       "
159
             "    </div>"
160
             ""
161-
  "       "
161+
             ""
162-
"    </div>"
162+
             "    <!-- Bootstrap core JavaScript"
163-
""
163+
             "    ================================================== -->"
164-
""
164+
             "    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->"
165-
"    <!-- Bootstrap core JavaScript"
165+
             "    <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\"></script>"
166-
"    ================================================== -->"
166+
             "    <!-- Include all compiled plugins (below), or include individual files as needed -->"
167-
"    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->"
167+
             "    <script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js\" integrity=\"sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS\" crossorigin=\"anonymous\"></script>"
168-
"    <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\"></script>"
168+
             ""
169-
"    <!-- Include all compiled plugins (below), or include individual files as needed -->"
169+
             "  </body>"
170-
"    <script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js\" integrity=\"sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS\" crossorigin=\"anonymous\"></script>"
170+
             "</html>"
171-
""
171+
             "";
172-
"  </body>"
172+
173-
"</html>"
173+
174-
  "";
174+
175
}
176
177
void handleSwitch() {// deveria recuperar a condição dos pinos do esp aqui
178
  int pin = server.arg(0).toInt(); // identifica os pinos utilizados
179
  int state = server.arg(1).toInt(); // identifica o estado do botão no html
180-
void handleSwitch() {
180+
  bitWrite(output_status, pin, state);// configura o estado dos pinos usando   shiftWrite(output_status) lá de cima
181-
  int pin = server.arg(0).toInt();
181+
182-
  int state = server.arg(1).toInt();
182+
183-
  bitWrite(output_status, pin, state);
183+
184
185
void handleNotFound() {
186
  String message = "File Not Found\n\n";
187
  message += "URI: ";
188-
void handleNotFound(){
188+
189
  message += "\nMethod: ";
190
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
191
  message += "\nArguments: ";
192
  message += server.args();
193-
  message += (server.method() == HTTP_GET)?"GET":"POST";
193+
194
  for (uint8_t i = 0; i < server.args(); i++) {
195
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
196
  }
197-
  for (uint8_t i=0; i<server.args(); i++){
197+
198
}
199
200
void check_wifi(void) {
201
  // Wait for connection
202
203
204
  Serial.print("CONNECTING TO WIFI:");
205
  // Wait until we are reconnected
206-
  // If no connection, set all outputs to 0
206+
207-
  if (WiFi.status() != WL_CONNECTED) {
207+
208-
    shiftWrite(B00000000); // Set all outputs to 0
208+
209-
  } else {
209+
210-
    return;
210+
    delay(250);
211
  }
212
  Serial.println(":CONNECTED");
213
}
214
215
216
void setup(void) {
217-
    digitalWrite(shiftEnablePin, HIGH);
217+
  // Enable all outputs
218-
    delay(100);
218+
219-
    digitalWrite(shiftEnablePin, LOW);
219+
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
220-
    delay(100);
220+
    pinMode(pin[thisPin], OUTPUT);
221
  }
222
223
    Serial.begin(115200);
224
    Serial.println("Device Power Up");
225
226
    WiFi.begin(ssid, password);
227
    Serial.println("");
228
229
    check_wifi();
230-
    pinMode(shiftDataPin, OUTPUT);
230+
231-
    pinMode(shiftClockPin, OUTPUT);
231+
    Serial.println("");
232-
    pinMode(shiftLatchPin, OUTPUT);
232+
    Serial.print("Connected to ");
233-
    shiftWrite(B00000000); // Set all outputs to 0
233+
    Serial.println(ssid);
234-
    pinMode(shiftEnablePin, OUTPUT);
234+
    Serial.print("IP address: ");
235-
    digitalWrite(shiftEnablePin, LOW); // Enable all outputs
235+
    Serial.println(WiFi.localIP());
236
237
    if (MDNS.begin("sprinkler")) {
238-
    
238+
      Serial.println("MDNS responder started");
239
    }
240-
  Serial.println("");
240+
241
242-
  check_wifi();
242+
    server.on("/", handleRoot);
243
244-
  Serial.println("");
244+
    server.on("/switch", handleSwitch);
245-
  Serial.print("Connected to ");
245+
    server.on("/about", handleAbout);
246-
  Serial.println(ssid);
246+
247-
  Serial.print("IP address: ");
247+
    server.on("/inline", []() {
248-
  Serial.println(WiFi.localIP());
248+
      server.send(200, "text/plain", "this works as well");
249
    });
250-
  if (MDNS.begin("sprinkler")) {
250+
251-
    Serial.println("MDNS responder started");
251+
    server.onNotFound(handleNotFound);
252
253
    server.begin();
254
    Serial.println("HTTP server started");
255-
  server.on("/", handleRoot);
255+
256
    // Setup temperature sensor
257-
  server.on("/switch", handleSwitch);
257+
    // locate devices on the bus
258-
   server.on("/about", handleAbout);
258+
    Serial.print("Locating devices...");
259
260-
  server.on("/inline", [](){
260+
    Serial.print("Found ");
261-
    server.send(200, "text/plain", "this works as well");
261+
    Serial.println(" devices.");
262-
  });
262+
    Serial.println();
263
    Serial.println("DONE");
264-
  server.onNotFound(handleNotFound);
264+
265
    //
266-
  server.begin();
266+
267-
  Serial.println("HTTP server started");
267+
268
269-
  // Setup temperature sensor
269+
  void loop() {
270-
  // locate devices on the bus
270+
271-
  Serial.print("Locating devices...");
271+
    server.handleClient();
272
  }