Ejemplo de domotica con ESP8266

Dependencies:   mbed

Revision:
5:31b41594c6ea
Parent:
4:40dd020463ea
--- a/main.cpp	Fri Aug 28 01:37:50 2015 +0000
+++ b/main.cpp	Fri Feb 14 00:43:04 2020 +0000
@@ -3,43 +3,43 @@
 #include "mbed.h"
 //#include "DS18B20.h"
 
-Serial pc(USBTX, USBRX);
-Serial esp(p28, p27); // tx, rx
+Serial pc(PA_2,PA_3);
+Serial esp(PB_6,PB_7); // tx, rx
 
 //DS18B20 thermom(A0, DS18B20::RES_12_BIT);
 
 // Standard Mbed LED definitions
-DigitalOut  led1(LED1);      // (PTB18)
-DigitalOut  led2(LED2);    // (PTB19)
-DigitalOut  led3(LED3);     // (PTD1)
+DigitalOut  led1(PD_12);      // (PTB18)
+DigitalOut  led2(PD_13);    // (PTB19)
+DigitalOut  led3(PD_14);     // (PTD1)
 
 // Digital Out and In pins, can be configured to any suitable pin depending on Platform
-DigitalOut  Out1(p6);
-DigitalOut  Out2(p7);
-DigitalOut  Out3(p8);
-DigitalOut  reset(p26);
+DigitalOut  Out1(PA_5);
+DigitalOut  Out2(PA_6);
+DigitalOut  Out3(PA_7);
+DigitalOut  reset(PA_8);
 
-DigitalIn  In1(p9);
-DigitalIn  In2(p10);
-DigitalIn  In3(p11);
+DigitalIn  In1(PB_2);
+DigitalIn  In2(PB_3);
+DigitalIn  In3(PB_4);
 
-PwmOut speaker(p21);
-AnalogIn   Ain1(p18);
-AnalogIn   Ain2(p19);
+PwmOut speaker(PB_1);
+AnalogIn   Ain1(PA_0);
+AnalogIn   Ain2(PB_0);
 
 Timer t1;
 Timer t2;
 
 struct tm t;
 
-int bufflen, DataRX, count, getcount, replycount, servreq, timeout;
-int bufl, ipdLen, linkID, weberror, webcounter;
+int bufflen, DataRX, contador, getcontador, replycontador, servreq, timeout;
+int bufl, ipdLen, linkID, weberror, webcontadorer;
 float temperature, AdcIn, Ht;
 float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage
 char Vcc[10];
 char Temp[10];
 char temp[10];
-char webcount[8];
+char webcontador[8];
 char lasthit[30];
 char timebuf[30];
 char type[16];
@@ -68,8 +68,8 @@
 {
     led3=1;
     while (esp.readable()) {
-        webbuff[count] = esp.getc();
-        count++;
+        webbuff[contador] = esp.getc();
+        contador++;
     }
     if(strlen(webbuff)>bufflen) {
         DataRX=1;
@@ -87,7 +87,7 @@
     reset=1;
     led1=1,led2=0,led3=0;
     timeout=6000;
-    getcount=500;
+    getcontador=500;
     getreply();
     esp.baud(115200);   // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F
     if (time(NULL) < 1420070400) {
@@ -125,8 +125,8 @@
     strcat(webbuff, "<html><head><title>ESP8266 Mbed LPC1768</title></head>");
     strcat(webbuff, "<body>");
     strcat(webbuff, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>ESP8266 Mbed IoT Web Controller</h1>");
-    strcat(webbuff, "Hit Count - ");
-    strcat(webbuff, webcount);
+    strcat(webbuff, "Hit contador - ");
+    strcat(webbuff, webcontador);
     strcat(webbuff, "<br>Last Hit - ");
     strcat(webbuff, lasthit);
     strcat(webbuff, "</div><br /><hr>");
@@ -200,7 +200,7 @@
     bufl = strlen(webbuff); // get total page buffer length
     sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
     timeout=200;
-    getcount=7;
+    getcontador=7;
     SendCMD();
     getreply();
     SendWEB();  // send web page
@@ -213,7 +213,7 @@
 {
     weberror=1;
     timeout=500;
-    getcount=24;
+    getcontador=24;
     t2.reset();
     t2.start();
     while(weberror==1 && t2.read() <5) {
@@ -225,13 +225,13 @@
     if(weberror==1) { // restart connection
         strcpy(cmdbuff, "AT+CIPMUX=1\r\n");
         timeout=500;
-        getcount=10;
+        getcontador=10;
         SendCMD();
         getreply();
         pc.printf(replybuff);
         sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
         timeout=500;
-        getcount=10;
+        getcontador=10;
         SendCMD();
         getreply();
         pc.printf(replybuff);
@@ -249,7 +249,7 @@
 {
     wait_ms(200);
     esp.attach(NULL);
-    count=0;
+    contador=0;
     DataRX=0;
     weberror=0;
     memset(webdata, '\0', sizeof(webdata));
@@ -289,8 +289,8 @@
         if (strstr(webdata, "POST") != NULL) {
             servreq=1;
         }
-        webcounter++;
-        sprintf(webcount, "%d",webcounter);
+        webcontadorer++;
+        sprintf(webcontador, "%d",webcontadorer);
     } else {
         memset(webbuff, '\0', sizeof(webbuff));
         esp.attach(&callback);
@@ -306,29 +306,29 @@
     pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
     strcpy(cmdbuff,"AT+RST\r\n");
     timeout=8000;
-    getcount=1000;
+    getcontador=1000;
     SendCMD();
     getreply();
     pc.printf(replybuff);
-    pc.printf("%d",count);
+    pc.printf("%d",contador);
     if (strstr(replybuff, "OK") != NULL) {
         pc.printf("\n++++++++++ Starting Server ++++++++++\r\n");
         strcpy(cmdbuff, "AT+CIPMUX=1\r\n");  // set multiple connections.
         timeout=500;
-        getcount=20;
+        getcontador=20;
         SendCMD();
         getreply();
         pc.printf(replybuff);
         sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
         timeout=500;
-        getcount=20;
+        getcontador=20;
         SendCMD();
         getreply();
         pc.printf(replybuff);
         wait(1);
         sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout);
         timeout=500;
-        getcount=50;
+        getcontador=50;
         SendCMD();
         getreply();
         pc.printf(replybuff);
@@ -336,7 +336,7 @@
         pc.printf("\n Getting Server IP \r\n");
         strcpy(cmdbuff, "AT+CIFSR\r\n");
         timeout=2500;
-        getcount=200;
+        getcontador=200;
         while(weberror==0) {
             SendCMD();
             getreply();
@@ -352,7 +352,7 @@
         pc.printf(webdata);
         led2=1;
         bufflen=200;
-        count=0;
+        contador=0;
         pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
         esp.attach(&callback);
     } else {
@@ -385,11 +385,11 @@
     memset(replybuff, '\0', sizeof(replybuff));
     t1.reset();
     t1.start();
-    replycount=0;
-    while(t1.read_ms()< timeout && replycount < getcount) {
+    replycontador=0;
+    while(t1.read_ms()< timeout && replycontador < getcontador) {
         if(esp.readable()) {
-            replybuff[replycount] = esp.getc();
-            replycount++;
+            replybuff[replycontador] = esp.getc();
+            replycontador++;
         }
     }
     t1.stop();