Guest User

Untitled

a guest
Nov 17th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.67 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <WebSocketsServer.h>
  5. #include <Hash.h>
  6. #include <FS.h>
  7. #include <DHT.h>
  8.  
  9. const char* ssid = "nombredered";
  10. const char* password = "password";
  11.  
  12. WebSocketsServer webSocket = WebSocketsServer(81);
  13. ESP8266WebServer server(80);
  14.  
  15. String datos= "0 0 0 0 0 0 0 0 0";
  16.  
  17. int SENSOR = 2;
  18.  
  19. DHT dht (SENSOR, DHT11);
  20.  
  21. int pines[]={16,5,4,0,2,12,14,15,13,3};
  22. double temp=temp,hum=hum;
  23. void prender(String dat)
  24. {
  25. String x="";
  26. int y;
  27. for(int i=0;i<dat.length();i++){
  28. //Serial.printf("Pin: %d - Datos-> %dn", pines[i],(uint8_t)dat.charAt(i)-48);
  29. y=(int)dat.charAt(i)-48;
  30. x+=y;
  31. x+=" ";
  32. digitalWrite(pines[i],(int)dat.charAt(i)-48);
  33. }
  34. //Leer dato de temperatura y humedad y guardar en variables, ejemplo temp y hum;
  35. // Se concatena los valores 9 valores de lamparas y dos datos ->>>>0 0 0 0 0 0 0 0 0 24.5 28.4<<<-
  36. x+=temp;
  37. x+=" ";
  38. x+=hum;
  39. webSocket.broadcastTXT(x);
  40. }
  41.  
  42. void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
  43. Serial.printf("webSocketEvent(%d, %d, ...)rn", num, type);
  44. switch(type) {
  45. case WStype_DISCONNECTED:
  46. {
  47. Serial.printf("Usuario #%u - Desconectado!n", num);
  48. break;
  49. }
  50. case WStype_CONNECTED:
  51. {
  52. IPAddress ip = webSocket.remoteIP(num);
  53. Serial.printf("Conexión establecida desde la IP: %d.%d.%d.%d Nombre: %s url: %un", ip[0], ip[1], ip[2], ip[3], payload, num);
  54. prender(datos);
  55. break;
  56. }
  57. case WStype_TEXT:
  58. {
  59. datos="";
  60. for (int i = 0; i < lenght; i++)
  61. {
  62. datos.concat((char)payload[i]);
  63. }
  64. prender(datos);
  65. break;
  66. }
  67. }
  68. }
  69.  
  70. void setup()
  71. {
  72. pinMode(pines[0], OUTPUT);
  73. pinMode(pines[1], OUTPUT);
  74. pinMode(pines[2], OUTPUT);
  75. pinMode(pines[3], OUTPUT);
  76. pinMode(pines[4], OUTPUT);
  77. pinMode(pines[5], OUTPUT);
  78. pinMode(pines[6], OUTPUT);
  79. pinMode(pines[7], OUTPUT);
  80. pinMode(pines[8], OUTPUT);
  81. pinMode(pines[9], INPUT);
  82.  
  83. Serial.begin(115200);
  84. dht.begin();
  85. Serial.println();
  86. // enviaIR.begin();
  87. WiFi.begin(ssid, password);
  88. Serial.println("Conectando con el modem infinitum");
  89. while (WiFi.status() != WL_CONNECTED) {
  90. delay(500);
  91. Serial.print(".");
  92. }
  93. server.onNotFound([](){
  94. if(!handleFileRead(server.uri()))
  95. server.send(404, "text/plain", "Archivo no encontradonn");
  96. });
  97.  
  98. IPAddress myIP = WiFi.localIP();
  99. Serial.println("");
  100. Serial.print("Direccion IP: ");
  101. Serial.println(myIP);
  102.  
  103. SPIFFS.begin();
  104.  
  105. webSocket.begin();
  106. webSocket.onEvent(webSocketEvent);
  107.  
  108. server.onNotFound([](){
  109. if(!handleFileRead(server.uri()))
  110. server.send(404, "text/plain", "FileNotFound");
  111. });
  112. server.begin();
  113. Serial.println("SERVIDOR WEB INICIADO");
  114. }
  115.  
  116. String getContentType(String filename)
  117. {
  118. if(server.hasArg("download")) return "application/octet-stream";
  119. else if(filename.endsWith(".htm")) return "text/html";
  120. else if(filename.endsWith(".html")) return "text/html";
  121. else if(filename.endsWith(".css")) return "text/css";
  122. else if(filename.endsWith(".js")) return "application/javascript";
  123. else if(filename.endsWith(".png")) return "image/png";
  124. else if(filename.endsWith(".gif")) return "image/gif";
  125. else if(filename.endsWith(".jpg")) return "image/jpeg";
  126. else if(filename.endsWith(".ico")) return "image/x-icon";
  127. else if(filename.endsWith(".xml")) return "text/xml";
  128. else if(filename.endsWith(".pdf")) return "application/x-pdf";
  129. else if(filename.endsWith(".zip")) return "application/x-zip";
  130. else if(filename.endsWith(".gz")) return "application/x-gzip";
  131. return "text/plain";
  132. }
  133.  
  134. bool handleFileRead(String path)
  135. {
  136. #ifdef DEBUG
  137. Serial.println("handleFileRead: " + path);
  138. #endif
  139. if(path.endsWith("/")) path += "index.html";
  140. if(SPIFFS.exists(path)){
  141. File file = SPIFFS.open(path, "r");
  142. size_t sent = server.streamFile(file, getContentType(path));
  143. file.close();
  144. return true;
  145. }
  146. return false;
  147. }
  148.  
  149. void loop() {
  150. webSocket.loop();
  151. server.handleClient();
  152. }
  153.  
  154. <input type="checkbox" onclick="enviar()" id="lampara">
  155. <label for="lampara" id="StatusLampara">LAMPARA</label>
  156. </div>
  157. <div class="Baño">
  158. <input type="checkbox" onclick="enviar()" id="baño">
  159. <label for="baño" id="StatusBaño">BAÑO</label>
  160.  
  161. <input type="checkbox" onclick="enviar()" id="tocador">
  162. <label for="tocador" id="StatusTocador">TOCADOR</label>
  163. </div>
  164. <div class="accesorios">
  165. <h3>CONTROL DE ACCESORIOS</h3>
  166. <div class="Calefactor">
  167. <input type="checkbox" onclick="enviar()" id="calefactor">
  168. <label for="calefactor" id="StatusCalefactor">CALEFACTOR</label>
  169. </div>
  170. <div class="Puerta">
  171. <input type="checkbox" onclick="enviar()" id="puerta">
  172. <label for="puerta" id="StatusPuerta">PUERTA</label>
  173. </div>
  174. <div class="Ventilador">
  175. <input type="checkbox" onclick="enviar()" id="ventilador">
  176. <label for="ventilador" id="StatusVentilador">VENTILADOR</label>
  177. </div>
  178. </div>
  179. <div class="temperatura">
  180. <div class="temp">
  181. <h3>TEMPERATURA</h3>
  182. <label id="temperatura">24°C</label>
  183. </div>
  184. <div class="humed">
  185. <h3>HUMEDAD</h3>
  186. <label id="humedad">28 %</label>
  187. </div>
  188. </div>
  189. </div>
  190. <hr>
  191. <h4>TECNOLOGIA Y HOGAR</h4>
  192. <P>Gracias a la tecnología cada uno de nuestros artefactos eléctricos y electrónicos, en la actualidad los podemos controlar de manera inalámbrica, ya sea por <strong>"Control remoto, Bluetooth, Wifi"</strong>, y hoy lo podemos hacer via internet; Gracias al <strong>"Internet de las cosas(IOT)"</strong>, y una tecnología muy indispensable, como lo es <strong>"WebSocket"</strong>.</P>
  193. <footer>Diseñado por Michel Bernales &copy 2018 ¡Todos los derechos son para uso personal.!</footer>
  194. </div>
  195. <script type="text/javascript" src="javascript/index.js"></script>
  196. </body>
  197. </html>
  198.  
  199.  
  200. `body {
  201. display: flex;
  202. background: rgba(186, 186, 199,.6);
  203. margin: auto;
  204. justify-content: center;
  205. text-align: center;
  206. max-width: 600px;
  207. font-family: sans-serif;
  208. }
  209. h1{
  210. margin: 5px;
  211. display: flex;
  212. flex-wrap: wrap;
  213. justify-content: center;
  214. color: #020972;
  215. }
  216. h3{
  217. color: #781123;
  218. font-weight: 600;
  219. margin-bottom: 2px;
  220. }
  221. .contenedor{
  222. width: 85%;
  223. background: #E4A5A0;
  224. padding: 3px;
  225. }
  226. hr{
  227. height: 5px;
  228. }
  229. .enlaces{
  230. width: 100%;
  231. margin-bottom: -14px;
  232. display: inline-block;
  233. flex-wrap: wrap;
  234. flex-flow:
  235. }
  236. a{
  237. transition: all .2s;
  238. float: right;
  239. width: 52px;
  240. margin-top: -4px;
  241. margin-right: 2px;
  242. height: auto;
  243. color: #050C5D;
  244. display: inline-block;
  245. text-decoration: none;
  246. text-align: center;
  247. font-size: 14px;
  248. font-weight: 600;
  249. flex-direction: row;
  250. flex-wrap: wrap;
  251. border-radius: 3px;
  252. padding: 8px;
  253. }
  254. a:hover{
  255. background: rgba(231, 58, 137,.7);
  256. }
  257. .wrap{
  258. display: flex;
  259. flex-direction: row;
  260. flex-wrap: wrap;
  261. justify-content: center;
  262. align-items: center;
  263. }
  264. .accesorios{
  265. display: flex;
  266. flex-direction: row;
  267. flex-wrap: wrap;
  268. justify-content: center;
  269. align-items: center;
  270. width: 90%;
  271. }
  272. .accesorios h3{
  273. width: 100%;
  274. margin-bottom: 5px;
  275. }
  276. .temperatura{
  277. width: 77%;
  278. height: 85px;
  279. margin: auto;
  280. display: flex;
  281. justify-content: space-around;
  282. }
  283. .temperatura label{
  284. width: 75px;
  285. height: 53px;
  286. font-size: 24px;
  287. background: #FFBAE3;
  288. color: darkgreen;
  289. margin-top: -12px;
  290. padding: 0 2px;
  291. border-top-left-radius: 55%;
  292. border-top-right-radius: 55%;
  293. }
  294. .temperatura h3{
  295. font-size: 13px;
  296. margin-bottom: 20px;
  297. }
  298. .temp label{
  299. margin-left: 7px;
  300. }
  301. input{
  302. display: none;
  303. }
  304. label{
  305. display: inline-block;
  306. background: red;
  307. color: #FDE4E4;
  308. text-align: center;
  309. border-radius: 5px;
  310. cursor: pointer;
  311. width: 80px;
  312. height: 60px;
  313. margin: 2px;
  314. font-size: 70%;
  315. display: flex;
  316. flex-direction: row;
  317. flex-wrap: wrap;
  318. justify-content: center;
  319. align-items: center;
  320. }
  321. input[type="checkbox"] + label{
  322. background: red;
  323. color: #FDE4E4;
  324. transition: all .1s;
  325. }
  326. input[type="checkbox"]:checked + label{
  327. background: rgba(48, 242, 0,.9);
  328. color: black;
  329. transition: all .1s;
  330. }
  331.  
  332. h4{
  333. color: #045508;
  334. text-decoration-line: underline;
  335. margin-top: 8px;
  336. margin-bottom: 20px;
  337. font-size: 14px;
  338. }
  339. p{
  340. font-size: 11px;
  341. color: rgba(0, 0, 0,.7);
  342. justify-content: center;
  343. margin-bottom: -5px;
  344. }
  345. footer{
  346. font-size: 12px;
  347. font-weight: 600;
  348. margin-top: 25px;
  349. }`
  350.  
  351. function start() {
  352.  
  353. Socket = new WebSocket('ws://' + window.location.hostname + ':81');
  354. Socket.onopen = () => {
  355. console.log('Conexión abierta al webSockets: ' + new Date());
  356. };
  357. Socket.onclose = function(evt) {
  358. console.log('Conexión cerrada con el webSockets');
  359. };
  360. Socket.onerror = evt => {
  361. console.log('Error al conectar con el webSockets', evt);
  362. };
  363. Socket.onmessage = evt => {
  364.  
  365. x=evt.data;
  366. var datos = x.split(' ');
  367. checks=new Array();
  368. checks[0]=document.getElementById('comedor');
  369. checks[1]=document.getElementById('alacena');
  370. checks[2]=document.getElementById('dormitorio');
  371. checks[3]=document.getElementById('lampara');
  372. checks[4]=document.getElementById('baño');
  373. checks[5]=document.getElementById('tocador');
  374. checks[6]=document.getElementById('calefactor');
  375. checks[7]=document.getElementById('puerta');
  376. checks[8]=document.getElementById('ventilador');
  377. for (var i = 0; i < 9; i++) {
  378. console.log("Dato "+datos[i]);
  379. console.log("Comparación 1:"+(datos[i]==="1"));
  380. console.log("Comparación 2:"+(datos[i]=="1"));
  381. console.log("Comparación 3:"+(datos[i]==="0"));
  382. console.log("Comparación 4:"+(datos[i]=="0"));
  383. if(datos[i]==="1"){
  384. checks[i].checked=true;
  385. }else if(datos[i]==="0"){
  386. checks[i].checked=false;
  387. }
  388. //console.log("Dato "+i+"=>"+datos[i]+"
  389.  
  390. Tipo=>"+typeof(datos[i])+" toString=>"+datos[i].toString());
  391. }
  392.  
  393. document.getElementById("temperatura").innerHTML=datos[9]+"C";
  394. document.getElementById("humedad").innerHTML=datos[10]+"%";
  395. };
  396. }
  397.  
  398. function enviar() {
  399. x="";
  400. d1=document.getElementById('comedor');
  401. d2=document.getElementById('alacena');
  402. d3=document.getElementById('dormitorio');
  403. d4=document.getElementById('lampara');
  404. d5=document.getElementById('baño');
  405. d6=document.getElementById('tocador');
  406. d7=document.getElementById('calefactor');
  407. d8=document.getElementById('puerta');
  408. d9=document.getElementById('ventilador');
  409. datos=[d1,d2,d3,d4,d5,d6,d7,d8,d9];
  410. for (var i = 0; i < datos.length; i++) {
  411. if(datos[i].checked==true){
  412. x+="1";
  413. }else{
  414. x+="0";
  415. }
  416. }
  417. //alert("Datos a enviar:"+x);
  418. Socket.send(x);
Add Comment
Please, Sign In to add comment