Advertisement
Guest User

Untitled

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