IISEC / Mbed 2 deprecated ESP8266-demo

Dependencies:   mbed

Revision:
0:bc0e1a75926f
Child:
2:eaf4e7fa577e
diff -r 000000000000 -r bc0e1a75926f WROOM2.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WROOM2.cpp	Sun Mar 24 11:32:07 2019 +0000
@@ -0,0 +1,226 @@
+/* Simple embedded web server on LPC1768 with the WROOM2 WiFi module.
+/* Copyright Toshihiro Matsui, IISEC, March, 2019
+/* Any form of use of this program is fully granted to anyone.
+*/
+
+#include "mbed.h"
+
+#define MAXRECVBUF 2048
+#define WEBFILE "webfile.htm"
+#define debug 0
+
+class KBline 
+{ char line[100];
+  Serial *term;
+
+  public:
+    int index, receiving;
+  private:
+    void serialirq() 
+      { char ch;
+        if (term->readable()) {
+          ch=term->getc();
+          switch(ch) { //simple line editing
+              case '\n':
+              case '\r':  //LF and CR terminate reading
+                line[index++]=ch; line[index++]=0;
+                term->printf("\r\n");
+                receiving=0;
+                break;
+              case 21:  /* control-U cancels entire line*/
+                term->printf("\n\r"); index=0; break;
+              case 0x08:
+              case 0x7f: /*BS and DEL delete the last character*/
+                if (index>0) { index--;
+                   term->putc(0x08); term->putc(0x20); term->putc(0x08);}
+                break;
+              default:
+                receiving=2; line[index++]=ch; term->putc(ch); break; 
+              } } }
+  public:
+    KBline(Serial *pc) { term=pc; }; //constructor
+    void start_getline() {
+       index=0; receiving=1;
+       term->attach(this, &KBline::serialirq); }
+    char *getline() {
+       while (receiving) wait(0.1);
+       return(line); }
+    } ;
+
+
+class Wroom  //tried to extend Serial but failed ...
+ {
+    char sendbuf[256];
+    char recvbuf[MAXRECVBUF];  /* 1024 */
+    Serial *port, *pc;
+    /* Timer tm; */
+    Timeout tmout;
+    int recvindex;
+ 
+    public:
+     int receiving;
+     Wroom(Serial *p) { port=p;} ;
+     void printf(char *s) { port->printf(s); }
+     void recvtimeout() {
+         tmout.detach();
+         recvbuf[recvindex++]=0; /*NULL at the end*/
+         receiving=0; } /* receiving finished*/
+     void prerecvtimeout() {
+         }
+     void recvirq() {
+        while (port->readable()) {
+            recvbuf[recvindex++]=port->getc();
+            if (recvindex>=MAXRECVBUF) recvindex=0; }
+        receiving=2;
+        tmout.detach();
+        tmout.attach(this, &Wroom::recvtimeout, 1.0); 
+        }  
+     void sendirq() {
+         }
+     void start_receiving() {
+         recvindex=0; receiving=1;
+         }
+     char *getreply() {
+         /* tmout.attach(this, &Wroom::recvtimeout, 7.0);*/
+         while (receiving) { wait(0.1); }
+         return(recvbuf); }  
+    char *getmessage() { //get http request
+         this->start_receiving();
+         return(this->getreply()); }    
+
+    char *atcom(char *com) {
+         this->start_receiving();
+         port->printf("%s\r\n", com); //pc->printf("atcom: %s\n", com);
+         return(this->getreply());}
+    char *atcom(char *com, int mode) {
+         sprintf(sendbuf, "%s=%d", com, mode);
+         return(this->atcom(sendbuf));  }
+    char *atcom(char *com, int param1, int param2) {
+         sprintf(sendbuf, "%s=%d,%d", com, param1, param2);
+         pc->printf("atcom2 -->%s\r\n", sendbuf);
+         return(this->atcom(sendbuf)); }
+    void configure(Serial *p) {  //first set up of WROOM2
+         pc=p; port->baud(115200); wait(2.0);
+         recvindex=0; port->attach(this, &Wroom::recvirq);
+         this->atcom("AT"); pc->printf("AT-->%s\n", this->getreply());
+         this->atcom("ATE0"); //turn off echo
+         this->getreply();  pc->printf("ATE0-->%s\n", recvbuf);
+         this->atcom("AT+CWMODE", 1); //1=station, 2=AP, 3=both
+         this->getreply(); pc->printf("CWMODE-->%s\n", recvbuf);
+         this->atcom("AT+CIPMUX", 1); //0=single, 1=multi
+         this->getreply(); pc->printf("CIPMUX-->%s\n", recvbuf);
+         this->atcom("AT+CIPSTO", 120);
+         pc->printf("CIPSTO-->%s\n", this->getreply());
+         this->atcom("AT+CWLAP"); //AP list
+         pc->printf("CWLAP-->%s\n", this->getreply());
+         // connection to AP giving SSID and PASS is not programmed.
+         }
+     };
+
+
+DigitalOut myled(LED4), led1(LED1), led2(LED2), led3(LED3);
+AnalogIn adc(p20);
+Serial pc(USBTX, USBRX), ser_wroom(p28, p27);
+KBline kbline(&pc);
+Wroom wroom(&ser_wroom);
+LocalFileSystem local("local");
+char webbuf[2000];
+
+void strsubst(char *current, char *pat, char *subst, char *news)
+ //substitute pat in current with subst and generate in news
+{ char *s;
+  int i;
+  s=strstr(current, pat);
+  i=s-current;
+  if (s) {
+    strncpy(news, current, i);
+    strncpy(news+i, subst, strlen(subst));
+    strncpy(news+i+strlen(subst), current+i+strlen(pat), strlen(current)-i-strlen(pat));
+    news[strlen(current)-strlen(pat)+strlen(subst)]=NULL;
+    }
+  }
+  
+char *readfile(char *fname, char *buf)
+{ int rdfd, filelen;
+  char fullfname[128];
+  sprintf(fullfname, "/local/%s", fname);
+  rdfd=open(fullfname,O_RDONLY);
+  if (rdfd<0) {
+    pc.printf("%s not found\r\n", fullfname);
+    return(NULL); }
+  else { 
+    filelen=read(rdfd, buf, 2000);
+    if (0<filelen && filelen<2000)  buf[filelen]=0;
+    pc.printf("%d bytes read from %s\r\n", filelen, fullfname); 
+    return(buf);}
+  }
+  
+char *makepage()
+{ char altwebbuf[2000], adval[16];
+  readfile(WEBFILE, webbuf);
+  if (led1) {
+      strsubst(webbuf, "$LED1ON$", "checked", altwebbuf);
+      strsubst(altwebbuf, "$LED1OFF$", " ", webbuf); }
+  else {
+      strsubst(webbuf, "$LED1OFF$", "checked", altwebbuf);
+      strsubst(altwebbuf, "$LED1ON$", " ", webbuf); }
+  if (led2) {
+      strsubst(webbuf, "$LED2ON$", "checked", altwebbuf);
+      strsubst(altwebbuf, "$LED2OFF$", " ", webbuf); }
+  else {
+      strsubst(webbuf, "$LED2OFF$", "checked", altwebbuf);
+      strsubst(altwebbuf, "$LED2ON$", " ", webbuf); }
+  sprintf(adval, "%6.3f", adc.read());
+  strsubst(webbuf, "$ADVAL$", adval, altwebbuf);
+  strcpy(webbuf, altwebbuf);
+  return(webbuf);   }
+
+void change_led(char *request)
+{ 
+  if (strstr(request, "led1=1")) led1=1;
+  if (strstr(request, "led1=0")) led1=0;
+  if (strstr(request, "led2=1")) led2=1;
+  if (strstr(request, "led2=0")) led2=0;
+   }
+
+
+  
+int main() {
+char *line, *page;
+  pc.baud(9600);
+  pc.printf("\r\nWROOM AT commander.\r\n");  
+  wroom.configure(&pc);
+  pc.printf("\r\nWROOM is configured.\r\n");  
+  pc.printf(">");
+  kbline.start_getline();
+  wroom.start_receiving();
+  while(1) {
+     myled=1; //prompt
+     if (kbline.receiving==0) { //kb input completed
+       line=kbline.getline();  /*pc.printf(line);*/ 
+       myled=0; 
+       if (line[0] == 'r') {  //receive http request
+         line=wroom.getmessage(); 
+         change_led(line); }
+       else if (*line == 's') { //send http message
+         page=makepage();
+         wroom.atcom("AT+CIPSEND",0,strlen(page));
+         pc.printf(wroom.getreply());
+         wroom.atcom(page); //send html document
+         pc.printf(wroom.getreply());
+         wroom.atcom("AT+CIPCLOSE", 0);
+         line=wroom.getreply();     }
+       else { //AT command
+         if(debug) pc.printf(line);
+         wroom.atcom(line); line=wroom.getreply(); } 
+       pc.printf(line); 
+       wroom.receiving=1;
+       pc.printf(">"); kbline.start_getline();  } 
+     if (wroom.receiving==0) {
+         line=pc.printf(wroom.getreply());
+         /*  */
+         wroom.start_receiving(); } 
+     wait(0.1);
+     }
+  } ;
+