Simple usage of wifi module ESP826601.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
zvonimir
Date:
Thu Nov 19 11:42:27 2020 +0000
Commit message:
Upravljanje wifi modulom esp826601.

Changed in this revision

ESP826601/ES826601.cpp Show annotated file Show diff for this revision Revisions of this file
ESP826601/ESP826601.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP826601/ES826601.cpp	Thu Nov 19 11:42:27 2020 +0000
@@ -0,0 +1,96 @@
+#include "ESP826601.h"
+#include "mbed.h"
+
+Serial pc(USBTX, USBRX, 115200);
+Serial esp(p28, p27, 115200);
+DigitalOut reset(p26);
+
+bool flag = false;
+bool flagConnection = false;
+bool flagSend = false;
+
+char connection = '0';
+
+char command[500]; 
+int counter = 0;
+
+Esp::Esp(){}
+
+void Esp::init(void){
+    reset = 0;
+    wait(0.2);
+    reset = 1;
+    wait(5);
+    esp.attach(&ESP_interrupt, Serial::RxIrq);
+    pc.printf("------------\n");
+}
+
+void Esp::send(char* text){
+    pc.printf("PC: ");
+    pc.printf(text);
+    pc.putc('\r');
+    esp.printf(text);
+    esp.printf("\r\n");
+}
+
+void Esp::waitResponse(){
+    while(!flag){
+        wait_ms(100);    
+    }
+    flag = false;  
+}
+
+void Esp::waitConnection(){
+    while(!flagConnection){
+        wait_ms(100);   
+    }  
+    flagConnection = false;  
+    
+    esp.printf("AT+CIPSEND=");
+    esp.putc(connection);
+    esp.printf(",20\r\n");
+    
+    Esp::waitResponse();
+}
+
+void Esp::closeConnection(){
+    while(!flagSend){
+        wait_ms(100); 
+    }
+    flagSend = false;
+    
+    esp.printf("AT+CIPCLOSE=");
+    esp.putc(connection);
+    esp.printf("\r\n");
+    
+    Esp::waitResponse();
+}
+
+void Esp::printToPC(char* text){
+    pc.printf(text);
+}
+
+void Esp::printToPC(char c){
+    pc.putc(c);
+}
+
+void ESP_interrupt(){
+    char c = esp.getc();
+    if(c == '\n' || c == '\r'){
+        if(counter != 0){
+            pc.printf("ESP: ");
+            for(int i=0; i<counter; i++) pc.putc(command[i]);
+            pc.putc('\r');
+            counter = 0;
+            if(command[0] == 'O' && command[1] == 'K') flag = true;
+            if(command[2] == 'C' && command[3] == 'O' && command[4] == 'N' && command[5] == 'N') {
+                connection = command[0];
+                flagConnection = true;
+            }
+            if(command[0] == 'S' && command[1] == 'E' && command[2] == 'N' && command[3] == 'D') flagSend = true;
+        }
+    }else {
+        command[counter] = c;
+        counter++;  
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP826601/ESP826601.h	Thu Nov 19 11:42:27 2020 +0000
@@ -0,0 +1,20 @@
+#ifndef ESP826601_H
+#define ESP826601_H
+
+#include "mbed.h"
+
+void ESP_interrupt();
+
+class Esp{
+    public: 
+        Esp(void);
+        void init(void);
+        void waitResponse(void);
+        void waitConnection(void);
+        void closeConnection(void);
+        void send(char* text);
+        void printToPC(char* text);
+        void printToPC(char c);
+};
+
+#endif 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Nov 19 11:42:27 2020 +0000
@@ -0,0 +1,33 @@
+#include "mbed.h"
+#include "ESP826601.h"
+
+Esp ESP;
+DigitalOut myled(LED1);
+Ticker tick;
+
+void readData(){
+    myled = !myled;
+}
+
+int main() {
+    tick.attach(&readData, 0.5);
+    
+    ESP.init();
+    ESP.send("AT+CIFSR");
+    ESP.waitResponse();
+    ESP.send("AT+CIPMUX=1");
+    ESP.waitResponse();
+    ESP.send("AT+CIPSERVER=1,80");
+    ESP.waitResponse();
+    
+    tick.detach();
+    myled = 0;
+    
+    while(1){
+        ESP.waitConnection();
+        ESP.send("Pozdrav od Zvonimira");
+        ESP.closeConnection();
+    }
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Nov 19 11:42:27 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file