Jack Hansdampf / ESP8266WebserverF103

Dependents:   ATCmdParserTest_V1_0_F103

Files at this revision

API Documentation at this revision

Comitter:
jack1930
Date:
Mon Jun 07 18:32:57 2021 +0000
Parent:
12:2bd6fc1bd375
Child:
14:d17cfd8131b1
Commit message:
Webserver V1_0 ESP01 ESP8266

Changed in this revision

ESP8266Webserver.cpp Show annotated file Show diff for this revision Revisions of this file
ESP8266Webserver.h Show annotated file Show diff for this revision Revisions of this file
MFS.cpp Show diff for this revision Revisions of this file
MFS.h Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP8266Webserver.cpp	Mon Jun 07 18:32:57 2021 +0000
@@ -0,0 +1,261 @@
+#include "ESP8266Webserver.h"
+
+ESP8266Webserver::ESP8266Webserver(int Port,PinName tx, PinName rx, bool pDebug)
+{
+    //HAL_Delay(1000);
+    hs_count=0;
+    clientIdx=0;
+    if (pDebug) printf("\nATCmdParser with ESP8266 example");
+    port=Port;
+    
+    dbg=pDebug;
+    _serial = new BufferedSerial(tx,rx, ESP8266_DEFAULT_BAUD_RATE);
+    _parser = new ATCmdParser(_serial, "\r\n");
+    _parser->debug_on(false);//pDebug);
+
+    _parser->set_timeout(1000);
+    
+//AT
+    if (pDebug) printf("\nATCmdParser: AT\r\n");
+    _parser->send("AT");
+
+    //HAL_Delay(1000);
+    
+    if (_parser->recv("OK")) {
+        if (pDebug) printf("\nAntw.: OK\r\n");
+    } else {
+        if (pDebug) printf("\nATCmdParser: OK failed\r\n");
+    }
+    
+    _parser->send("ATE0");
+    //HAL_Delay(300);
+//AT+CWMODE=1 
+    if (pDebug) printf("\nATCmdParser: //AT+CWMODE=2 \r\n");
+    _parser->send("AT+CWMODE=2");
+    
+    if (_parser->recv("OK")) {
+        if (pDebug) printf("\nAntw.: OK\r\n");
+    } else {
+        if (pDebug) printf("\nATCmdParser: OK failed\r\n");
+    }
+    
+//AT+CWMODE? 
+    if (pDebug) printf("\nATCmdParser: //AT+CWMODE? \r\n");
+    _parser->send("AT+CWMODE?");
+    
+    if (_parser->recv("OK")) {
+        if (pDebug) printf("\nAntw.: OK\r\n");
+    } else {
+        if (pDebug) printf("\nATCmdParser: OK failed\r\n");
+    }
+
+
+//AT+CIFSR
+    if (pDebug) printf("\nATCmdParser: //AT+CIFSR \r\n");
+    _parser->send("AT+CIFSR");
+    
+    if (_parser->recv("OK")) {
+       if (pDebug)  printf("\nAntw.: OK\r\n");
+    } else {
+       if (pDebug) printf("\nATCmdParser: OK failed\r\n");
+    }
+    
+/*    
+//AT+CWJAP= “Wi-FiNetwork”,“Password”
+    printf("\nATCmdParser: //AT+CWJAP= \"Wi-FiNetwork\",\"Password\" \r\n");
+    _parser->send("AT+CWJAP=\"x-Netz\",\"aCT3xuSbm9rt\"");
+    
+    if (_parser->recv("OK")) {
+        printf("\nAntw.: OK\r\n");
+    } else {
+        printf("\nATCmdParser: OK failed\r\n");
+    }
+*/   
+//AT+CIPAP="192.168.5.1","192.168.5.1","255.255.255.0"
+    if (pDebug) printf("\nATCmdParser: //AT+CIPAP=\"192.168.5.1\",\"192.168.5.1\",\"255.255.255.0\" \r\n");
+    _parser->send("AT+CIPAP=\"192.168.5.1\",\"192.168.5.1\",\"255.255.255.0\"");
+    
+    if (_parser->recv("OK")) {
+        if (pDebug) printf("\nAntw.: OK\r\n");
+    } else {
+        if (pDebug) printf("\nATCmdParser: OK failed\r\n");
+    }
+ 
+    
+//AT+CIPMUX=1
+    if (pDebug) printf("\nATCmdParser: //AT+CIPMUX=1");
+    _parser->send("AT+CIPMUX=1");
+    
+    if (_parser->recv("OK")) {
+        if (pDebug) printf("\nAntw.: OK\r\n");
+    } else {
+        if (pDebug) printf("\nATCmdParser: OK failed\r\n");
+    }
+
+}
+
+
+
+int ESP8266Webserver::on(const char* handlestring,Callback< void()> func)
+{
+  if (hs_count>9) return -1;
+  else
+  {
+    strcpy(hs[hs_count],handlestring); 
+    if (dbg) printf("\r\n\r\n%s, %s\r\n",handlestring,hs[hs_count]);
+    cbs[hs_count]=func;
+    hs_count++;
+    return 0;
+  }
+    
+}
+
+int ESP8266Webserver::begin(void)
+{
+//AT+CIPSERVER=1,80
+    if (dbg) printf("\nATCmdParser: //AT+CIPSERVER=1,80");
+    _parser->send("AT+CIPSERVER=1,80");
+    if (_parser->recv("OK")) {
+        if (dbg) printf("\nAntw.: OK\r\n");
+        return 0;
+    } else {
+        if (dbg) printf("\nATCmdParser: OK failed\r\n");
+        return -1;
+    }
+}
+
+//durchsucht recbuf nach suchstring
+bool ESP8266Webserver::beinhaltet(char* suchstring)
+{
+    char* referer;
+    char* fundort;
+    char* favicon;
+    favicon=strstr(recbuf,"favicon");
+    if (favicon!=NULL) return false;
+    referer=strstr(recbuf,"Referer");
+    fundort=strstr(recbuf, suchstring);
+    if (strstr(recbuf,"ERROR")==NULL)
+    if( fundort != NULL&&(fundort<referer||referer==NULL)) return true;
+    return false;
+}
+
+int ESP8266Webserver::handleClient(void)
+{
+    int value,value2;
+    if (_serial->readable())
+        {   
+          /*  HAL_Delay(1000);
+            _serial->read(buf,1000);
+            printf("\r\n ich %s\r\n",buf);
+            */
+            if (_parser->recv("+IPD,%d,%d:",&clientID[clientIdx],&value2)) //_parser->recv("+IPD,%d",value)
+            {
+                
+                //HAL_Delay(1000);
+
+                if (dbg) printf("\r\nich Client ID=%d, Anzahl=%d",clientID[clientIdx],value2);
+                _parser->read(recbuf, value2);
+                //HAL_Delay(1000);
+
+                //recbuf[value2]=0;
+                if (dbg) printf("\r\nBuf=\n%s",recbuf);
+
+                //(cbs[1])();
+                gefunden=false;
+                for (int i=0;i<hs_count&&!gefunden;i++)
+                {
+                    
+                    if (beinhaltet(hs[i]))
+                    {
+                        
+                        (cbs[i])();
+                        
+                        gefunden=true;
+                    }
+                }  
+
+                if (dbg) for (int i=0;i<hs_count;i++)
+                {
+                    printf("\r\n%s\r\n",hs[i]);
+                }
+                
+                if (dbg) printf("\r\nich Got\r\n");
+                
+                //_parser->flush();
+
+            }
+          
+        }
+
+    return 0;
+}
+
+
+int ESP8266Webserver::send(int HTTPStatus,const char* Mimetype, string webseite)
+{
+    return ESP8266Webserver::send(HTTPStatus,Mimetype, webseite.c_str());
+}
+int ESP8266Webserver::send(int HTTPStatus,const char* Mimetype, const char* webseite)
+{
+    char hilf[40];
+    
+    printf("\r\nHallo\r\n");
+    Aufrufe++;
+    sprintf(sendstring,"HTTP/1.1 %d OK\n",HTTPStatus);
+    strcat(sendstring,"Date: Wed, 23 Apr 2021 04:36:25 GMT\n");
+    strcat(sendstring,"Connection: close\n");
+    strcat(sendstring,"Content-Type: ");
+    strcat(sendstring,Mimetype);
+    strcat(sendstring,"\n");
+    sprintf(hilf,"%d",strlen(webseite));
+    strcat(sendstring,"Content-Length: ");
+    strcat(sendstring,hilf);
+    strcat(sendstring,"\n\n");
+    strcat(sendstring,webseite);
+    if (dbg) printf("len=%d, inhalt=\n%s",strlen(sendstring),sendstring);
+    //sprintf(hilf,"AT+CIPSEND=%d,%d\r\n",clientID,strlen(sendstring));
+    //printf("\n %s, %d\n",hilf,strlen(hilf));
+    //_serial->write(hilf,strlen(hilf));
+    _parser->debug_on(true);
+    _parser->send("AT+CIPSEND=%d,%d",clientID[clientIdx],strlen(sendstring));  
+    _parser->debug_on(false);          
+    _parser->write(sendstring,strlen(sendstring));  
+    HAL_Delay(200);                
+    _parser->send("AT+CIPCLOSE=%d",clientID[clientIdx]);
+    return 0;
+}
+
+void ESP8266Webserver::debugOn(bool pD)
+{
+    dbg=pD;
+    _parser->debug_on(pD);
+  
+}
+
+string ESP8266Webserver::gibWertString(string suchstring)
+{
+    return ESP8266Webserver::gibWert(suchstring.c_str());
+}
+
+const char* ESP8266Webserver::gibWert(const char* suchstring)
+{
+    static char hilf[20];
+    
+    char *fundort;
+    int i=0;
+    fundort=strstr(recbuf,suchstring);
+    if (fundort!=NULL)
+    {
+    fundort=fundort+strlen(suchstring)+1;
+    while(i<20 && fundort[i]!=38 && fundort[i]>32 ) //space, &
+    {
+        hilf[i]=fundort[i];
+        i++;
+    }
+    hilf[i]=0;
+    if (dbg) printf("\r\nsuchergebnis=%s\r\n",hilf);
+    return hilf;
+    }
+    else
+    return NULL;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP8266Webserver.h	Mon Jun 07 18:32:57 2021 +0000
@@ -0,0 +1,38 @@
+
+#include "mbed.h"
+#include "string"
+
+#define   ESP8266_DEFAULT_BAUD_RATE   115200
+
+class ESP8266Webserver
+{   
+    private:    
+    int hs_count=0;
+    Callback< void()> cbs[10];
+    char hs[10][20];
+    char recbuf[1000];
+    bool beinhaltet(char* suchstring);
+    int port=80;
+    bool dbg;
+    int clientID[10]={0,0,0,0,0,0,0,0,0,0};
+    int clientIdx=0;
+    char sendstring[1000];
+    BufferedSerial *_serial;
+    ATCmdParser *_parser;
+    int Aufrufe=0;
+    bool gefunden;
+    
+
+    public:
+    char suchergebnis[20];
+    
+    ESP8266Webserver(int Port=80,PinName tx=PB_10, PinName rx=PB_11, bool pDebug=false);
+    int on(const char* handlestring,Callback< void()> func);
+    int begin(void);
+    int handleClient(void);
+    int send(int HTTPStatus,const char* Mimetype, const char* webseite);
+    int send(int HTTPStatus,const char* Mimetype, string webseite);
+    const char* gibWert(const char* suchstring);
+    string gibWertString(string suchstring);
+    void debugOn(bool pD);
+};
\ No newline at end of file
--- a/MFS.cpp	Fri Mar 12 09:55:13 2021 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,122 +0,0 @@
-#include "MFS.h"
-
-MFS::MFS(void)
-    {
-    HAL_Init();
-    GPIO_InitTypeDef GPIO_InitStruct = {0};
-        /* GPIO Ports Clock Enable */
-      __HAL_RCC_GPIOC_CLK_ENABLE();
-      //__HAL_RCC_GPIOH_CLK_ENABLE(); //F103
-      __HAL_RCC_GPIOA_CLK_ENABLE();
-      __HAL_RCC_GPIOB_CLK_ENABLE();
-      __HAL_RCC_USART1_CLK_ENABLE();
-
-  /*Configure GPIO pin Output Level */
-  HAL_GPIO_WritePin(GPIOB, beep_Pin|latch_Pin, GPIO_PIN_SET);
-  
-/*Configure GPIO pin : beep_Pin */
-  GPIO_InitStruct.Pin = beep_Pin;
-  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
-  GPIO_InitStruct.Pull = GPIO_NOPULL;
-  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
-  HAL_GPIO_Init(beep_GPIO_Port, &GPIO_InitStruct);
-
-  /*Configure GPIO pin : latch_Pin */
-  GPIO_InitStruct.Pin = latch_Pin;
-  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
-  GPIO_InitStruct.Pull = GPIO_NOPULL;
-  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;  
-
-  
-  HAL_GPIO_Init(latch_GPIO_Port, &GPIO_InitStruct);  
- 
-     /**USART1 GPIO Configuration
-    PA8     ------> USART1_CK
-    PA9     ------> USART1_TX
-    PA10     ------> USART1_RX
-    */
-    GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
-    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
-    GPIO_InitStruct.Pull = GPIO_NOPULL;
-    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; //F103
-
-    #ifndef __STM32F1xx_HAL_H 
-        GPIO_InitStruct.Alternate = GPIO_AF7_USART1; //F103
-    #endif
-    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-    
-      husart1.Instance = USART1;
-      husart1.Init.BaudRate = 115000;
-      husart1.Init.WordLength = USART_WORDLENGTH_8B;
-      husart1.Init.StopBits = USART_STOPBITS_1;
-      husart1.Init.Parity = USART_PARITY_NONE;
-      husart1.Init.Mode = USART_MODE_TX_RX;
-      husart1.Init.CLKPolarity = USART_POLARITY_LOW;
-      husart1.Init.CLKPhase = USART_PHASE_1EDGE;
-      husart1.Init.CLKLastBit = USART_LASTBIT_ENABLE;
-      if (HAL_USART_Init(&husart1) != HAL_OK)
-      {
-        //Error_Handler();
-        HAL_GPIO_WritePin(GPIOB,beep_Pin,GPIO_PIN_RESET);
-      }
-   };
-
-    void MFS::send(void)
-    {
-            HAL_GPIO_WritePin(GPIOB, latch_Pin, GPIO_PIN_RESET);
-            HAL_USART_Transmit(&husart1,&dieSegmente,1,100);
-            HAL_USART_Transmit(&husart1,&dieAuswahl,1,100);
-            HAL_GPIO_WritePin(GPIOB, latch_Pin, GPIO_PIN_SET);
-    }
-    void MFS::siebensegment(uint8_t wert)
-    {
-        dieSegmente=~wert;
-        send();
-    }
-    void MFS::siebensegment(uint8_t awert,uint8_t wert)
-    {
-        dieSegmente=~wert;
-        dieAuswahl=awert;
-        send();
-    }
-    void MFS::bcd(int wert)
-    {
-        dieSegmente=~seg7[wert];
-        send();
-    }
-    
-    void MFS::bcd(uint8_t awert, int wert)
-    {
-        dieSegmente=~seg7[wert];
-        dieAuswahl=awert;
-        send();
-    }    
-    void MFS::auswahl(uint8_t wert)
-    {
-        dieAuswahl=wert;
-        send();
-    }
-    
-    void MFS::operator=(unsigned int wert)
-    {
-        char hilf=0, hilf2=wert;
-        for (int i=0;i<8;i++)
-        {
-         hilf=(hilf<<1)+hilf2%2; 
-         hilf2=hilf2/2;  
-        }
-        dieSegmente=(~hilf)&0xFF;
-        dieAuswahl=(wert/0x100);
-        send();
-    }
-    
-    MFS::operator int(void)
-    {
-        char hilf=0, hilf2=~dieSegmente;
-        for (int i=0;i<8;i++)
-        {
-         hilf=(hilf<<1)+hilf2%2; 
-         hilf2=hilf2/2;  
-        }        
-        return dieAuswahl*0x100+hilf;
-    }
\ No newline at end of file
--- a/MFS.h	Fri Mar 12 09:55:13 2021 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,39 +0,0 @@
-//#include "stm32f4xx_hal.h"
-#include "mbed.h"
-
-#define USART_TX_Pin GPIO_PIN_2
-#define USART_TX_GPIO_Port GPIOA
-#define USART_RX_Pin GPIO_PIN_3
-#define USART_RX_GPIO_Port GPIOA
-#define beep_Pin GPIO_PIN_3
-#define beep_GPIO_Port GPIOB
-#define latch_Pin GPIO_PIN_5
-#define latch_GPIO_Port GPIOB
-
-#ifndef GPIO_SPEED_FREQ_VERY_HIGH 
-#define GPIO_SPEED_FREQ_VERY_HIGH GPIO_SPEED_FREQ_HIGH
-#endif
-
-#ifndef GPIO_AF7_USART1
-#define GPIO_AF7_USART1 1
-#endif
-
-class MFS
-{   
-    private:
-    uint8_t dieAuswahl=0;
-    uint8_t dieSegmente=0xFF;
-    char seg7[10]={0xFC,0x60,0xDA, 0xF2, 0x66, 0xB6, 0xBE, 0xE0, 0xFE, 0xF6};
-    USART_HandleTypeDef husart1;
-    public:
-    void operator=(unsigned int wert);
-    operator int(void);
-    MFS(void);
-    void siebensegment(uint8_t wert);
-    void siebensegment(uint8_t awert,uint8_t wert);
-    void bcd(int wert);
-    void bcd(uint8_t awert, int wert);
-    void auswahl(uint8_t wert);
-    private:
-    void send(void);
-};
\ No newline at end of file